From 8abf4b37ab0c85f18b6e357025333529065a671f Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Thu, 2 May 2024 15:21:53 +0100 Subject: [PATCH 1/2] add unit tests to plugins --- internal/plugin/grpc_client_plugin_test.go | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/internal/plugin/grpc_client_plugin_test.go b/internal/plugin/grpc_client_plugin_test.go index 441604c633..cbe9d40feb 100644 --- a/internal/plugin/grpc_client_plugin_test.go +++ b/internal/plugin/grpc_client_plugin_test.go @@ -12,6 +12,8 @@ import ( "testing" "time" + v1 "github.com/nginx/agent/v3/api/grpc/mpi/v1" + "github.com/nginx/agent/v3/test/helpers" "github.com/nginx/agent/v3/test/protos" @@ -155,6 +157,55 @@ func TestGrpcClient_Process_ResourceTopic(t *testing.T) { assert.True(t, client.isConnected.Load()) } +func TestGrpcClient_ProcessRequest(t *testing.T) { + ctx := context.Background() + + agentConfig := types.GetAgentConfig() + client := NewGrpcClient(agentConfig) + pipe := &bus.FakeMessagePipe{} + client.messagePipe = pipe + assert.NotNil(t, client) + + tests := []struct { + name string + request *v1.ManagementPlaneRequest + expected *bus.Message + }{ + { + name: "Test 1: Config Apply Request", + request: &v1.ManagementPlaneRequest{ + Request: &v1.ManagementPlaneRequest_ConfigApplyRequest{ + ConfigApplyRequest: &v1.ConfigApplyRequest{ + ConfigVersion: protos.CreateConfigVersion(), + }, + }, + }, + expected: &bus.Message{ + Topic: bus.InstanceConfigUpdateRequestTopic, + Data: &v1.ManagementPlaneRequest_ConfigApplyRequest{ + ConfigApplyRequest: &v1.ConfigApplyRequest{ + ConfigVersion: protos.CreateConfigVersion(), + }, + }, + }, + }, + { + name: "Test 2: Invalid Request", + request: &v1.ManagementPlaneRequest{ + Request: &v1.ManagementPlaneRequest_ActionRequest{}, + }, + expected: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + client.ProcessRequest(ctx, test.request) + pipe.GetMessages() + }) + } +} + func TestGrpcClient_Close(t *testing.T) { ctx := context.Background() serverMockLock := sync.Mutex{} From 53dcd67f89b94c96d658377c32a7f665bf098d78 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Thu, 2 May 2024 15:22:52 +0100 Subject: [PATCH 2/2] add unit tests to plugins --- internal/plugin/grpc_client_plugin_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/plugin/grpc_client_plugin_test.go b/internal/plugin/grpc_client_plugin_test.go index cbe9d40feb..f3e3353014 100644 --- a/internal/plugin/grpc_client_plugin_test.go +++ b/internal/plugin/grpc_client_plugin_test.go @@ -12,7 +12,7 @@ import ( "testing" "time" - v1 "github.com/nginx/agent/v3/api/grpc/mpi/v1" + "github.com/nginx/agent/v3/api/grpc/mpi/v1" "github.com/nginx/agent/v3/test/helpers" "github.com/nginx/agent/v3/test/protos"