Skip to content

Commit 1cc62f9

Browse files
authored
Reduce integration tests execution time (#1171)
1 parent f89e3a2 commit 1cc62f9

File tree

4 files changed

+212
-217
lines changed

4 files changed

+212
-217
lines changed

test/integration/managementplane/config_apply_test.go

Lines changed: 118 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,143 +15,164 @@ import (
1515
"github.com/nginx/agent/v3/test/integration/utils"
1616

1717
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
18-
"github.com/stretchr/testify/assert"
19-
"github.com/stretchr/testify/require"
18+
"github.com/stretchr/testify/suite"
2019
)
2120

2221
const (
2322
configApplyErrorMessage = "failed to parse config invalid " +
2423
"number of arguments in \"worker_processes\" directive in /etc/nginx/nginx.conf:1"
2524
)
2625

27-
func TestGrpc_ConfigApply(t *testing.T) {
28-
ctx := context.Background()
29-
teardownTest := utils.SetupConnectionTest(t, false, false, false,
26+
type ConfigApplyTestSuite struct {
27+
suite.Suite
28+
ctx context.Context
29+
teardownTest func(testing.TB)
30+
nginxInstanceID string
31+
}
32+
33+
type ConfigApplyChunkingTestSuite struct {
34+
suite.Suite
35+
ctx context.Context
36+
teardownTest func(testing.TB)
37+
nginxInstanceID string
38+
}
39+
40+
func (s *ConfigApplyTestSuite) SetupSuite() {
41+
s.ctx = context.Background()
42+
s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false,
3043
"../../config/agent/nginx-config-with-grpc-client.conf")
31-
defer teardownTest(t)
44+
s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress)
45+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
46+
s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
47+
s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
48+
}
3249

33-
nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress)
50+
func (s *ConfigApplyTestSuite) TearDownSuite() {
51+
s.teardownTest(s.T())
52+
}
3453

35-
responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
36-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
37-
assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
54+
func (s *ConfigApplyTestSuite) TearDownTest() {
55+
utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress)
56+
}
3857

39-
t.Run("Test 1: No config changes", func(t *testing.T) {
40-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
41-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
42-
responses = utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
43-
t.Logf("Config apply responses: %v", responses)
58+
func (s *ConfigApplyTestSuite) TestConfigApply_Test1_TestNoConfigChanges() {
59+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
60+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
61+
s.T().Logf("Config apply responses: %v", responses)
4462

45-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
46-
assert.Equal(t, "Config apply successful, no files to change", responses[0].GetCommandResponse().GetMessage())
47-
})
63+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
64+
s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
65+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
66+
s.Equal("Config apply successful, no files to change", responses[1].GetCommandResponse().GetMessage())
67+
}
4868

49-
t.Run("Test 2: Valid config", func(t *testing.T) {
50-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
51-
newConfigFile := "../../config/nginx/nginx-with-test-location.conf"
69+
func (s *ConfigApplyTestSuite) TestConfigApply_Test2_TestValidConfig() {
70+
newConfigFile := "../../config/nginx/nginx-with-test-location.conf"
5271

53-
if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" {
54-
newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf"
55-
}
72+
if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" {
73+
newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf"
74+
}
75+
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
76+
s.ctx,
77+
newConfigFile,
78+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
79+
0o666,
80+
)
81+
s.Require().NoError(err)
5682

57-
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
58-
ctx,
59-
newConfigFile,
60-
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
61-
0o666,
62-
)
63-
require.NoError(t, err)
83+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
84+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
85+
s.T().Logf("Config apply responses: %v", responses)
6486

65-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
87+
sort.Slice(responses, func(i, j int) bool {
88+
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
89+
})
6690

67-
responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress)
68-
t.Logf("Config apply responses: %v", responses)
91+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
92+
s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage())
93+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
94+
s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
95+
}
6996

70-
sort.Slice(responses, func(i, j int) bool {
71-
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
72-
})
97+
func (s *ConfigApplyTestSuite) TestConfigApply_Test3_TestInvalidConfig() {
98+
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
99+
s.ctx,
100+
"../../config/nginx/invalid-nginx.conf",
101+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
102+
0o666,
103+
)
104+
s.Require().NoError(err)
73105

74-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
75-
assert.Equal(t, "Config apply successful", responses[0].GetCommandResponse().GetMessage())
76-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
77-
assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
78-
})
106+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
79107

80-
t.Run("Test 3: Invalid config", func(t *testing.T) {
81-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
82-
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
83-
ctx,
84-
"../../config/nginx/invalid-nginx.conf",
85-
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
86-
0o666,
87-
)
88-
require.NoError(t, err)
89-
90-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
91-
92-
responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress)
93-
t.Logf("Config apply responses: %v", responses)
94-
95-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus())
96-
assert.Equal(t, "Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage())
97-
assert.Equal(t, configApplyErrorMessage, responses[0].GetCommandResponse().GetError())
98-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus())
99-
assert.Equal(t, "Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage())
100-
assert.Equal(t, configApplyErrorMessage, responses[1].GetCommandResponse().GetError())
101-
})
108+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
109+
s.T().Logf("Config apply responses: %v", responses)
102110

103-
t.Run("Test 4: File not in allowed directory", func(t *testing.T) {
104-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
105-
utils.PerformInvalidConfigApply(t, nginxInstanceID)
111+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus())
112+
s.Equal("Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage())
113+
s.Equal(configApplyErrorMessage, responses[0].GetCommandResponse().GetError())
114+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus())
115+
s.Equal("Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage())
116+
s.Equal(configApplyErrorMessage, responses[1].GetCommandResponse().GetError())
117+
}
106118

107-
responses = utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
108-
t.Logf("Config apply responses: %v", responses)
119+
func (s *ConfigApplyTestSuite) TestConfigApply_Test4_TestFileNotInAllowedDirectory() {
120+
utils.PerformInvalidConfigApply(s.T(), s.nginxInstanceID)
109121

110-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus())
111-
assert.Equal(t, "Config apply failed", responses[0].GetCommandResponse().GetMessage())
112-
assert.Equal(
113-
t,
114-
"file not in allowed directories /unknown/nginx.conf",
115-
responses[0].GetCommandResponse().GetError(),
116-
)
117-
})
122+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
123+
s.T().Logf("Config apply responses: %v", responses)
124+
125+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus())
126+
s.Equal("Config apply failed", responses[0].GetCommandResponse().GetMessage())
127+
s.Equal(
128+
"file not in allowed directories /unknown/nginx.conf",
129+
responses[0].GetCommandResponse().GetError(),
130+
)
118131
}
119132

120-
func TestGrpc_ConfigApply_Chunking(t *testing.T) {
121-
ctx := context.Background()
122-
teardownTest := utils.SetupConnectionTest(t, false, false, false,
133+
func (s *ConfigApplyChunkingTestSuite) SetupSuite() {
134+
s.ctx = context.Background()
135+
s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false,
123136
"../../config/agent/nginx-config-with-max-file-size.conf")
124-
defer teardownTest(t)
125-
126-
nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress)
137+
s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress)
138+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
139+
s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
140+
s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
141+
}
127142

128-
responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
129-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
130-
assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
143+
func (s *ConfigApplyChunkingTestSuite) TearDownSuite() {
144+
s.teardownTest(s.T())
145+
}
131146

132-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
147+
func (s *ConfigApplyChunkingTestSuite) TestConfigApplyChunking() {
148+
utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress)
133149

134150
newConfigFile := "../../config/nginx/nginx-1mb-file.conf"
135151

136152
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
137-
ctx,
153+
s.ctx,
138154
newConfigFile,
139-
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
155+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
140156
0o666,
141157
)
142-
require.NoError(t, err)
158+
s.Require().NoError(err)
143159

144-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
160+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
145161

146-
responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress)
147-
t.Logf("Config apply responses: %v", responses)
162+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
163+
s.T().Logf("Config apply responses: %v", responses)
148164

149165
sort.Slice(responses, func(i, j int) bool {
150166
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
151167
})
152168

153-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
154-
assert.Equal(t, "Config apply successful", responses[0].GetCommandResponse().GetMessage())
155-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
156-
assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
169+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
170+
s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage())
171+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
172+
s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
173+
}
174+
175+
func TestConfigApplyTestSuite(t *testing.T) {
176+
suite.Run(t, new(ConfigApplyTestSuite))
177+
suite.Run(t, new(ConfigApplyChunkingTestSuite))
157178
}

test/integration/managementplane/config_upload_test.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package managementplane
77

88
import (
9+
"context"
910
"fmt"
1011
"net/http"
1112
"testing"
@@ -14,23 +15,37 @@ import (
1415

1516
"github.com/go-resty/resty/v2"
1617
mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
17-
"github.com/stretchr/testify/assert"
18-
"github.com/stretchr/testify/require"
18+
"github.com/stretchr/testify/suite"
1919
)
2020

21-
func TestGrpc_ConfigUpload(t *testing.T) {
22-
teardownTest := utils.SetupConnectionTest(t, true, false, false,
23-
"../../config/agent/nginx-config-with-grpc-client.conf")
24-
defer teardownTest(t)
21+
type MPITestSuite struct {
22+
suite.Suite
23+
ctx context.Context
24+
teardownTest func(testing.TB)
25+
nginxInstanceID string
26+
}
2527

26-
nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress)
27-
assert.False(t, t.Failed())
28+
func (s *MPITestSuite) TearDownSuite() {
29+
s.teardownTest(s.T())
30+
}
2831

29-
responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
32+
func (s *MPITestSuite) TearDownTest() {
33+
utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress)
34+
}
3035

31-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
32-
assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
36+
func (s *MPITestSuite) SetupSuite() {
37+
s.ctx = context.Background()
38+
s.teardownTest = utils.SetupConnectionTest(s.T(), true, false, false,
39+
"../../config/agent/nginx-config-with-grpc-client.conf")
40+
s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress)
41+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
42+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
43+
s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
3344

45+
s.False(s.T().Failed())
46+
}
47+
48+
func (s *MPITestSuite) TestConfigUpload() {
3449
request := fmt.Sprintf(`{
3550
"message_meta": {
3651
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
@@ -44,9 +59,9 @@ func TestGrpc_ConfigUpload(t *testing.T) {
4459
}
4560
}
4661
}
47-
}`, nginxInstanceID)
62+
}`, s.nginxInstanceID)
4863

49-
t.Logf("Sending config upload request: %s", request)
64+
s.T().Logf("Sending config upload request: %s", request)
5065

5166
client := resty.New()
5267
client.SetRetryCount(utils.RetryCount).SetRetryWaitTime(utils.RetryWaitTime).SetRetryMaxWaitTime(
@@ -55,13 +70,17 @@ func TestGrpc_ConfigUpload(t *testing.T) {
5570
url := fmt.Sprintf("http://%s/api/v1/requests", utils.MockManagementPlaneAPIAddress)
5671
resp, err := client.R().EnableTrace().SetBody(request).Post(url)
5772

58-
require.NoError(t, err)
59-
assert.Equal(t, http.StatusOK, resp.StatusCode())
73+
s.Require().NoError(err)
74+
s.Equal(http.StatusOK, resp.StatusCode())
6075

61-
responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress)
76+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
77+
78+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
79+
s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
80+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
81+
s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
82+
}
6283

63-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
64-
assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
65-
assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
66-
assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
84+
func TestMPITestSuite(t *testing.T) {
85+
suite.Run(t, new(MPITestSuite))
6786
}

0 commit comments

Comments
 (0)