@@ -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
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 ,
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}
0 commit comments