Skip to content

Commit 40e44d0

Browse files
Spencer Ugbospencerugbo
authored andcommitted
Implement test suites to reuse containers for tests
1 parent a4918b3 commit 40e44d0

File tree

4 files changed

+221
-216
lines changed

4 files changed

+221
-216
lines changed

test/integration/managementplane/config_apply_test.go

Lines changed: 126 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -15,143 +15,173 @@ 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,
30-
"../../config/agent/nginx-config-with-grpc-client.conf")
31-
defer teardownTest(t)
26+
type ConfigApplyTestSuite struct {
27+
suite.Suite
28+
ctx context.Context
29+
teardownTest func(testing.TB)
30+
nginxInstanceID string
31+
}
3232

33-
nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress)
33+
type ConfigApplyChunkingTestSuite struct {
34+
suite.Suite
35+
ctx context.Context
36+
teardownTest func(testing.TB)
37+
nginxInstanceID string
38+
}
3439

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())
40+
func (s *ConfigApplyTestSuite) SetupSuite() {
41+
s.ctx = context.Background()
42+
s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false,
43+
"../../config/agent/nginx-config-with-grpc-client.conf")
44+
s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress)
45+
}
3846

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)
47+
func (s *ConfigApplyTestSuite) TearDownSuite() {
48+
s.teardownTest(s.T())
49+
}
4450

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-
})
51+
func (s *ConfigApplyTestSuite) TearDownTest() {
52+
utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress)
53+
}
4854

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"
55+
func (s *ConfigApplyTestSuite) TestConfigApply_Test1_TestSetup() {
56+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
57+
s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
58+
s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
59+
}
5260

53-
if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" {
54-
newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf"
55-
}
61+
func (s *ConfigApplyTestSuite) TestConfigApply_Test2_TestNoConfigChanges() {
62+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
63+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
64+
s.T().Logf("Config apply responses: %v", responses)
5665

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)
66+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
67+
s.Equal("Config apply successful, no files to change", responses[0].GetCommandResponse().GetMessage())
68+
}
6469

65-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
70+
func (s *ConfigApplyTestSuite) TestConfigApply_Test3_TestValidConfig() {
71+
newConfigFile := "../../config/nginx/nginx-with-test-location.conf"
6672

67-
responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress)
68-
t.Logf("Config apply responses: %v", responses)
73+
if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" {
74+
newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf"
75+
}
76+
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
77+
s.ctx,
78+
newConfigFile,
79+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
80+
0o666,
81+
)
82+
s.Require().NoError(err)
6983

70-
sort.Slice(responses, func(i, j int) bool {
71-
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
72-
})
84+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
85+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
86+
s.T().Logf("Config apply responses: %v", responses)
7387

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())
88+
sort.Slice(responses, func(i, j int) bool {
89+
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
7890
})
7991

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-
})
92+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
93+
s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage())
94+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
95+
s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
96+
}
10297

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)
98+
func (s *ConfigApplyTestSuite) TestConfigApply_Test4_TestInvalidConfig() {
99+
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
100+
s.ctx,
101+
"../../config/nginx/invalid-nginx.conf",
102+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
103+
0o666,
104+
)
105+
s.Require().NoError(err)
106106

107-
responses = utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
108-
t.Logf("Config apply responses: %v", responses)
107+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
109108

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-
})
109+
responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress)
110+
s.T().Logf("Config apply responses: %v", responses)
111+
112+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus())
113+
s.Equal("Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage())
114+
s.Equal(configApplyErrorMessage, responses[0].GetCommandResponse().GetError())
115+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus())
116+
s.Equal("Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage())
117+
s.Equal(configApplyErrorMessage, responses[1].GetCommandResponse().GetError())
118+
}
119+
120+
func (s *ConfigApplyTestSuite) TestConfigApply_Test5_TestFileNotInAllowedDirectory() {
121+
utils.PerformInvalidConfigApply(s.T(), s.nginxInstanceID)
122+
123+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
124+
s.T().Logf("Config apply responses: %v", responses)
125+
126+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus())
127+
s.Equal("Config apply failed", responses[0].GetCommandResponse().GetMessage())
128+
s.Equal(
129+
"file not in allowed directories /unknown/nginx.conf",
130+
responses[0].GetCommandResponse().GetError(),
131+
)
132+
}
133+
134+
func TestConfigApplyTestSuite(t *testing.T) {
135+
suite.Run(t, new(ConfigApplyTestSuite))
118136
}
119137

120-
func TestGrpc_ConfigApply_Chunking(t *testing.T) {
121-
ctx := context.Background()
122-
teardownTest := utils.SetupConnectionTest(t, false, false, false,
138+
func (s *ConfigApplyChunkingTestSuite) SetupSuite() {
139+
s.ctx = context.Background()
140+
s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false,
123141
"../../config/agent/nginx-config-with-max-file-size.conf")
124-
defer teardownTest(t)
142+
s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress)
143+
}
125144

126-
nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress)
145+
func (s *ConfigApplyChunkingTestSuite) TearDownSuite() {
146+
s.teardownTest(s.T())
147+
}
127148

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())
149+
func (s *ConfigApplyChunkingTestSuite) TearDownTest() {
150+
utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress)
151+
}
131152

132-
utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress)
153+
func (s *ConfigApplyChunkingTestSuite) TestConfigApplyChunking_Test1_TestSetup() {
154+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
155+
s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
156+
s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
157+
}
133158

159+
func (s *ConfigApplyChunkingTestSuite) TestConfigApplyChunking_Test2_TestChunkedConfigApply() {
134160
newConfigFile := "../../config/nginx/nginx-1mb-file.conf"
135161

136162
err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer(
137-
ctx,
163+
s.ctx,
138164
newConfigFile,
139-
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID),
165+
fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID),
140166
0o666,
141167
)
142-
require.NoError(t, err)
168+
s.Require().NoError(err)
143169

144-
utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress)
170+
utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress)
145171

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

149175
sort.Slice(responses, func(i, j int) bool {
150176
return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage()
151177
})
152178

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())
179+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
180+
s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage())
181+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus())
182+
s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage())
183+
}
184+
185+
func TestConfigApplyChunkingTestSuite(t *testing.T) {
186+
suite.Run(t, new(ConfigApplyChunkingTestSuite))
157187
}

test/integration/managementplane/config_upload_test.go

Lines changed: 40 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,40 @@ 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 ConfigUploadMPIFileWatcherTestSuite 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 *ConfigUploadMPIFileWatcherTestSuite) TearDownSuite() {
29+
s.teardownTest(s.T())
30+
}
2831

29-
responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress)
32+
func (s *ConfigUploadMPIFileWatcherTestSuite) 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 *ConfigUploadMPIFileWatcherTestSuite) 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+
}
3342

43+
func (s *ConfigUploadMPIFileWatcherTestSuite) TestConfigUploadMPTFileWatcher_1_TestSetup() {
44+
responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress)
45+
s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus())
46+
s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage())
47+
48+
s.False(s.T().Failed())
49+
}
50+
51+
func (s *ConfigUploadMPIFileWatcherTestSuite) TestConfigUploadMPTFileWatcher_TestConfigUpload() {
3452
request := fmt.Sprintf(`{
3553
"message_meta": {
3654
"message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184",
@@ -44,9 +62,9 @@ func TestGrpc_ConfigUpload(t *testing.T) {
4462
}
4563
}
4664
}
47-
}`, nginxInstanceID)
65+
}`, s.nginxInstanceID)
4866

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

5169
client := resty.New()
5270
client.SetRetryCount(utils.RetryCount).SetRetryWaitTime(utils.RetryWaitTime).SetRetryMaxWaitTime(
@@ -55,13 +73,15 @@ func TestGrpc_ConfigUpload(t *testing.T) {
5573
url := fmt.Sprintf("http://%s/api/v1/requests", utils.MockManagementPlaneAPIAddress)
5674
resp, err := client.R().EnableTrace().SetBody(request).Post(url)
5775

58-
require.NoError(t, err)
59-
assert.Equal(t, http.StatusOK, resp.StatusCode())
76+
s.Require().NoError(err)
77+
s.Equal(http.StatusOK, resp.StatusCode())
6078

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

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())
85+
func TestConfigUploadMPIFileWatcherTestSuite(t *testing.T) {
86+
suite.Run(t, new(ConfigUploadMPIFileWatcherTestSuite))
6787
}

0 commit comments

Comments
 (0)