diff --git a/test/integration/managementplane/config_apply_test.go b/test/integration/managementplane/config_apply_test.go index 6fcb48096..39297e716 100644 --- a/test/integration/managementplane/config_apply_test.go +++ b/test/integration/managementplane/config_apply_test.go @@ -15,8 +15,7 @@ import ( "github.com/nginx/agent/v3/test/integration/utils" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" ) const ( @@ -24,134 +23,156 @@ const ( "number of arguments in \"worker_processes\" directive in /etc/nginx/nginx.conf:1" ) -func TestGrpc_ConfigApply(t *testing.T) { - ctx := context.Background() - teardownTest := utils.SetupConnectionTest(t, false, false, false, +type ConfigApplyTestSuite struct { + suite.Suite + ctx context.Context + teardownTest func(testing.TB) + nginxInstanceID string +} + +type ConfigApplyChunkingTestSuite struct { + suite.Suite + ctx context.Context + teardownTest func(testing.TB) + nginxInstanceID string +} + +func (s *ConfigApplyTestSuite) SetupSuite() { + s.ctx = context.Background() + s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false, "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) + s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) +} - nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) +func (s *ConfigApplyTestSuite) TearDownSuite() { + s.teardownTest(s.T()) +} - responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) +func (s *ConfigApplyTestSuite) TearDownTest() { + utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress) +} - t.Run("Test 1: No config changes", func(t *testing.T) { - utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress) - utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress) - responses = utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) - t.Logf("Config apply responses: %v", responses) +func (s *ConfigApplyTestSuite) TestConfigApply_Test1_TestNoConfigChanges() { + utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress) + s.T().Logf("Config apply responses: %v", responses) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply successful, no files to change", responses[0].GetCommandResponse().GetMessage()) - }) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) + s.Equal("Config apply successful, no files to change", responses[1].GetCommandResponse().GetMessage()) +} - t.Run("Test 2: Valid config", func(t *testing.T) { - utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress) - newConfigFile := "../../config/nginx/nginx-with-test-location.conf" +func (s *ConfigApplyTestSuite) TestConfigApply_Test2_TestValidConfig() { + newConfigFile := "../../config/nginx/nginx-with-test-location.conf" - if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" { - newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf" - } + if os.Getenv("IMAGE_PATH") == "/nginx-plus/agent" { + newConfigFile = "../../config/nginx/nginx-plus-with-test-location.conf" + } + err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer( + s.ctx, + newConfigFile, + fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID), + 0o666, + ) + s.Require().NoError(err) - err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer( - ctx, - newConfigFile, - fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID), - 0o666, - ) - require.NoError(t, err) + utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress) + s.T().Logf("Config apply responses: %v", responses) - utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress) + sort.Slice(responses, func(i, j int) bool { + return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage() + }) - responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) - t.Logf("Config apply responses: %v", responses) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) +} - sort.Slice(responses, func(i, j int) bool { - return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage() - }) +func (s *ConfigApplyTestSuite) TestConfigApply_Test3_TestInvalidConfig() { + err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer( + s.ctx, + "../../config/nginx/invalid-nginx.conf", + fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID), + 0o666, + ) + s.Require().NoError(err) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply successful", responses[0].GetCommandResponse().GetMessage()) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) - }) + utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress) - t.Run("Test 3: Invalid config", func(t *testing.T) { - utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress) - err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer( - ctx, - "../../config/nginx/invalid-nginx.conf", - fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID), - 0o666, - ) - require.NoError(t, err) - - utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress) - - responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) - t.Logf("Config apply responses: %v", responses) - - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage()) - assert.Equal(t, configApplyErrorMessage, responses[0].GetCommandResponse().GetError()) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage()) - assert.Equal(t, configApplyErrorMessage, responses[1].GetCommandResponse().GetError()) - }) + responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress) + s.T().Logf("Config apply responses: %v", responses) - t.Run("Test 4: File not in allowed directory", func(t *testing.T) { - utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress) - utils.PerformInvalidConfigApply(t, nginxInstanceID) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_ERROR, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Config apply failed, rolling back config", responses[0].GetCommandResponse().GetMessage()) + s.Equal(configApplyErrorMessage, responses[0].GetCommandResponse().GetError()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[1].GetCommandResponse().GetStatus()) + s.Equal("Config apply failed, rollback successful", responses[1].GetCommandResponse().GetMessage()) + s.Equal(configApplyErrorMessage, responses[1].GetCommandResponse().GetError()) +} - responses = utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) - t.Logf("Config apply responses: %v", responses) +func (s *ConfigApplyTestSuite) TestConfigApply_Test4_TestFileNotInAllowedDirectory() { + utils.PerformInvalidConfigApply(s.T(), s.nginxInstanceID) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply failed", responses[0].GetCommandResponse().GetMessage()) - assert.Equal( - t, - "file not in allowed directories /unknown/nginx.conf", - responses[0].GetCommandResponse().GetError(), - ) - }) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.T().Logf("Config apply responses: %v", responses) + + s.Equal(mpi.CommandResponse_COMMAND_STATUS_FAILURE, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Config apply failed", responses[0].GetCommandResponse().GetMessage()) + s.Equal( + "file not in allowed directories /unknown/nginx.conf", + responses[0].GetCommandResponse().GetError(), + ) } -func TestGrpc_ConfigApply_Chunking(t *testing.T) { - ctx := context.Background() - teardownTest := utils.SetupConnectionTest(t, false, false, false, +func (s *ConfigApplyChunkingTestSuite) SetupSuite() { + s.ctx = context.Background() + s.teardownTest = utils.SetupConnectionTest(s.T(), false, false, false, "../../config/agent/nginx-config-with-max-file-size.conf") - defer teardownTest(t) - - nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) + s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.Require().Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Require().Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) +} - responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) +func (s *ConfigApplyChunkingTestSuite) TearDownSuite() { + s.teardownTest(s.T()) +} - utils.ClearManagementPlaneResponses(t, utils.MockManagementPlaneAPIAddress) +func (s *ConfigApplyChunkingTestSuite) TestConfigApplyChunking() { + utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress) newConfigFile := "../../config/nginx/nginx-1mb-file.conf" err := utils.MockManagementPlaneGrpcContainer.CopyFileToContainer( - ctx, + s.ctx, newConfigFile, - fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", nginxInstanceID), + fmt.Sprintf("/mock-management-plane-grpc/config/%s/etc/nginx/nginx.conf", s.nginxInstanceID), 0o666, ) - require.NoError(t, err) + s.Require().NoError(err) - utils.PerformConfigApply(t, nginxInstanceID, utils.MockManagementPlaneAPIAddress) + utils.PerformConfigApply(s.T(), s.nginxInstanceID, utils.MockManagementPlaneAPIAddress) - responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) - t.Logf("Config apply responses: %v", responses) + responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress) + s.T().Logf("Config apply responses: %v", responses) sort.Slice(responses, func(i, j int) bool { return responses[i].GetCommandResponse().GetMessage() < responses[j].GetCommandResponse().GetMessage() }) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Config apply successful", responses[0].GetCommandResponse().GetMessage()) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Config apply successful", responses[0].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) +} + +func TestConfigApplyTestSuite(t *testing.T) { + suite.Run(t, new(ConfigApplyTestSuite)) + suite.Run(t, new(ConfigApplyChunkingTestSuite)) } diff --git a/test/integration/managementplane/config_upload_test.go b/test/integration/managementplane/config_upload_test.go index 35d57a628..0f888d3c4 100644 --- a/test/integration/managementplane/config_upload_test.go +++ b/test/integration/managementplane/config_upload_test.go @@ -6,6 +6,7 @@ package managementplane import ( + "context" "fmt" "net/http" "testing" @@ -14,23 +15,37 @@ import ( "github.com/go-resty/resty/v2" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" ) -func TestGrpc_ConfigUpload(t *testing.T) { - teardownTest := utils.SetupConnectionTest(t, true, false, false, - "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) +type MPITestSuite struct { + suite.Suite + ctx context.Context + teardownTest func(testing.TB) + nginxInstanceID string +} - nginxInstanceID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - assert.False(t, t.Failed()) +func (s *MPITestSuite) TearDownSuite() { + s.teardownTest(s.T()) +} - responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) +func (s *MPITestSuite) TearDownTest() { + utils.ClearManagementPlaneResponses(s.T(), utils.MockManagementPlaneAPIAddress) +} - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) +func (s *MPITestSuite) SetupSuite() { + s.ctx = context.Background() + s.teardownTest = utils.SetupConnectionTest(s.T(), true, false, false, + "../../config/agent/nginx-config-with-grpc-client.conf") + s.nginxInstanceID = utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) + s.False(s.T().Failed()) +} + +func (s *MPITestSuite) TestConfigUpload() { request := fmt.Sprintf(`{ "message_meta": { "message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184", @@ -44,9 +59,9 @@ func TestGrpc_ConfigUpload(t *testing.T) { } } } -}`, nginxInstanceID) +}`, s.nginxInstanceID) - t.Logf("Sending config upload request: %s", request) + s.T().Logf("Sending config upload request: %s", request) client := resty.New() client.SetRetryCount(utils.RetryCount).SetRetryWaitTime(utils.RetryWaitTime).SetRetryMaxWaitTime( @@ -55,13 +70,17 @@ func TestGrpc_ConfigUpload(t *testing.T) { url := fmt.Sprintf("http://%s/api/v1/requests", utils.MockManagementPlaneAPIAddress) resp, err := client.R().EnableTrace().SetBody(request).Post(url) - require.NoError(t, err) - assert.Equal(t, http.StatusOK, resp.StatusCode()) + s.Require().NoError(err) + s.Equal(http.StatusOK, resp.StatusCode()) - responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 2, utils.MockManagementPlaneAPIAddress) + + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) +} - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) +func TestMPITestSuite(t *testing.T) { + suite.Run(t, new(MPITestSuite)) } diff --git a/test/integration/managementplane/file_watcher_test.go b/test/integration/managementplane/file_watcher_test.go index 5d6bb58f8..3e6824233 100644 --- a/test/integration/managementplane/file_watcher_test.go +++ b/test/integration/managementplane/file_watcher_test.go @@ -6,70 +6,54 @@ package managementplane import ( - "context" - "testing" - "github.com/nginx/agent/v3/test/integration/utils" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) -func TestGrpc_FileWatcher(t *testing.T) { - ctx := context.Background() - teardownTest := utils.SetupConnectionTest(t, true, false, false, - "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) - - utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - assert.False(t, t.Failed()) +func (s *MPITestSuite) TestFileWatcher_Test1_TestUpdateNGINXConfig() { + err := utils.Container.CopyFileToContainer( + s.ctx, + "../../config/nginx/nginx-with-server-block-access-log.conf", + "/etc/nginx/nginx.conf", + 0o666, + ) + s.Require().NoError(err) - t.Run("Test 1: update nginx config file on data plane", func(t *testing.T) { - err := utils.Container.CopyFileToContainer( - ctx, - "../../config/nginx/nginx-with-server-block-access-log.conf", - "/etc/nginx/nginx.conf", - 0o666, - ) - require.NoError(t, err) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) - responses := utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[1].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - utils.VerifyUpdateDataPlaneStatus(t, utils.MockManagementPlaneAPIAddress) - }) + utils.VerifyUpdateDataPlaneStatus(s.T(), utils.MockManagementPlaneAPIAddress) +} - t.Run("Test 2: create new nginx config file", func(t *testing.T) { - err := utils.Container.CopyFileToContainer( - ctx, - "../../config/nginx/empty-nginx.conf", - "/etc/nginx/test/test.conf", - 0o666, - ) - require.NoError(t, err) +func (s *MPITestSuite) TestFileWatcher_Test2_TestCreateNGINXConfig() { + err := utils.Container.CopyFileToContainer( + s.ctx, + "../../config/nginx/empty-nginx.conf", + "/etc/nginx/test/test.conf", + 0o666, + ) + s.Require().NoError(err) - responses := utils.ManagementPlaneResponses(t, 3, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[2].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[2].GetCommandResponse().GetMessage()) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - utils.VerifyUpdateDataPlaneStatus(t, utils.MockManagementPlaneAPIAddress) - }) + utils.VerifyUpdateDataPlaneStatus(s.T(), utils.MockManagementPlaneAPIAddress) +} - t.Run("Test 3: delete nginx config file", func(t *testing.T) { - _, _, err := utils.Container.Exec( - ctx, - []string{"rm", "-rf", "/etc/nginx/test"}, - ) - require.NoError(t, err) +func (s *MPITestSuite) TestFileWatcher_Test3_TestDeleteNGINXConfig() { + _, _, err := utils.Container.Exec( + s.ctx, + []string{"rm", "-rf", "/etc/nginx/test"}, + ) + s.Require().NoError(err) - responses := utils.ManagementPlaneResponses(t, 4, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[3].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[3].GetCommandResponse().GetMessage()) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - utils.VerifyUpdateDataPlaneStatus(t, utils.MockManagementPlaneAPIAddress) - }) + utils.VerifyUpdateDataPlaneStatus(s.T(), utils.MockManagementPlaneAPIAddress) } diff --git a/test/integration/managementplane/grpc_management_plane_api_test.go b/test/integration/managementplane/grpc_management_plane_api_test.go index 7bee15f28..7cfe9050d 100644 --- a/test/integration/managementplane/grpc_management_plane_api_test.go +++ b/test/integration/managementplane/grpc_management_plane_api_test.go @@ -6,72 +6,43 @@ package managementplane import ( - "context" "fmt" "net" "net/http" - "testing" "time" "github.com/nginx/agent/v3/test/integration/utils" "github.com/go-resty/resty/v2" mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) -func TestGrpc_Reconnection(t *testing.T) { - ctx := context.Background() - teardownTest := utils.SetupConnectionTest(t, false, false, false, - "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) - +func (s *ConfigApplyTestSuite) TestGrpc_Test1_Reconnection() { timeout := 15 * time.Second - originalID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - - stopErr := utils.MockManagementPlaneGrpcContainer.Stop(ctx, &timeout) + stopErr := utils.MockManagementPlaneGrpcContainer.Stop(s.ctx, &timeout) - require.NoError(t, stopErr) + s.Require().NoError(stopErr) - startErr := utils.MockManagementPlaneGrpcContainer.Start(ctx) - require.NoError(t, startErr) + startErr := utils.MockManagementPlaneGrpcContainer.Start(s.ctx) + s.Require().NoError(startErr) - ipAddress, err := utils.MockManagementPlaneGrpcContainer.Host(ctx) - require.NoError(t, err) - ports, err := utils.MockManagementPlaneGrpcContainer.Ports(ctx) - require.NoError(t, err) + ipAddress, err := utils.MockManagementPlaneGrpcContainer.Host(s.ctx) + s.Require().NoError(err) + ports, err := utils.MockManagementPlaneGrpcContainer.Ports(s.ctx) + s.Require().NoError(err) utils.MockManagementPlaneAPIAddress = net.JoinHostPort(ipAddress, ports["9093/tcp"][0].HostPort) - currentID := utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, originalID, currentID) + currentID := utils.VerifyConnection(s.T(), 2, utils.MockManagementPlaneAPIAddress) + s.Equal(s.nginxInstanceID, currentID) } // Verify that the agent sends a connection request and an update data plane status request -func TestGrpc_StartUp(t *testing.T) { - teardownTest := utils.SetupConnectionTest(t, true, false, false, - "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) - - utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - assert.False(t, t.Failed()) - utils.VerifyUpdateDataPlaneHealth(t, utils.MockManagementPlaneAPIAddress) +func (s *MPITestSuite) TestGrpc_Test2_StartUp() { + utils.VerifyUpdateDataPlaneHealth(s.T(), utils.MockManagementPlaneAPIAddress) } -func TestGrpc_DataplaneHealthRequest(t *testing.T) { - teardownTest := utils.SetupConnectionTest(t, true, false, false, - "../../config/agent/nginx-config-with-grpc-client.conf") - defer teardownTest(t) - - utils.VerifyConnection(t, 2, utils.MockManagementPlaneAPIAddress) - - responses := utils.ManagementPlaneResponses(t, 1, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully updated all files", responses[0].GetCommandResponse().GetMessage()) - - assert.False(t, t.Failed()) - +func (s *MPITestSuite) TestGrpc_Test3_DataplaneHealthRequest() { request := `{ "message_meta": { "message_id": "5d0fa83e-351c-4009-90cd-1f2acce2d184", @@ -88,11 +59,11 @@ func TestGrpc_DataplaneHealthRequest(t *testing.T) { url := fmt.Sprintf("http://%s/api/v1/requests", utils.MockManagementPlaneAPIAddress) resp, err := client.R().EnableTrace().SetBody(request).Post(url) - require.NoError(t, err) - assert.Equal(t, http.StatusOK, resp.StatusCode()) + s.Require().NoError(err) + s.Equal(http.StatusOK, resp.StatusCode()) - responses = utils.ManagementPlaneResponses(t, 2, utils.MockManagementPlaneAPIAddress) + responses := utils.ManagementPlaneResponses(s.T(), 1, utils.MockManagementPlaneAPIAddress) - assert.Equal(t, mpi.CommandResponse_COMMAND_STATUS_OK, responses[1].GetCommandResponse().GetStatus()) - assert.Equal(t, "Successfully sent health status update", responses[1].GetCommandResponse().GetMessage()) + s.Equal(mpi.CommandResponse_COMMAND_STATUS_OK, responses[0].GetCommandResponse().GetStatus()) + s.Equal("Successfully sent health status update", responses[0].GetCommandResponse().GetMessage()) }