@@ -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
2221const (
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}
0 commit comments