From 108e062062b0679bc76562cb2681d682261eeb55 Mon Sep 17 00:00:00 2001 From: RRashmit Date: Fri, 25 Oct 2024 11:05:08 +0100 Subject: [PATCH] chore: added config apply test --- internal/command/command_plugin_test.go | 131 +++++++++++++----------- 1 file changed, 72 insertions(+), 59 deletions(-) diff --git a/internal/command/command_plugin_test.go b/internal/command/command_plugin_test.go index f32ace4841..4c4f401457 100644 --- a/internal/command/command_plugin_test.go +++ b/internal/command/command_plugin_test.go @@ -105,69 +105,82 @@ func TestCommandPlugin_Process(t *testing.T) { } func TestCommandPlugin_monitorSubscribeChannel(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - messagePipe := bus.NewFakeMessagePipe() - - commandPlugin := NewCommandPlugin(types.AgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{}) - err := commandPlugin.Init(ctx, messagePipe) - require.NoError(t, err) - defer commandPlugin.Close(ctx) - - go commandPlugin.monitorSubscribeChannel(ctx) - - commandPlugin.subscribeChannel <- &mpi.ManagementPlaneRequest{ - Request: &mpi.ManagementPlaneRequest_ConfigUploadRequest{ - ConfigUploadRequest: &mpi.ConfigUploadRequest{}, + tests := []struct { + mpiRequest *mpi.ManagementPlaneRequest + expectedTopic *bus.Message + name string + isUploadRequest bool + isApplyRequest bool + }{ + { + name: "Test 1: Config Upload Request", + mpiRequest: &mpi.ManagementPlaneRequest{ + Request: &mpi.ManagementPlaneRequest_ConfigUploadRequest{ + ConfigUploadRequest: &mpi.ConfigUploadRequest{}, + }, + }, + expectedTopic: &bus.Message{Topic: bus.ConfigUploadRequestTopic}, + isUploadRequest: true, }, - } - - assert.Eventually( - t, - func() bool { return len(messagePipe.GetMessages()) == 1 }, - 2*time.Second, - 10*time.Millisecond, - ) - - messages := messagePipe.GetMessages() - assert.Len(t, messages, 1) - assert.Equal(t, bus.ConfigUploadRequestTopic, messages[0].Topic) - - request, ok := messages[0].Data.(*mpi.ManagementPlaneRequest) - assert.True(t, ok) - require.NotNil(t, request.GetConfigUploadRequest()) -} - -func TestCommandPlugin_monitorHealthRequest(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - messagePipe := bus.NewFakeMessagePipe() - - commandPlugin := NewCommandPlugin(types.AgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{}) - err := commandPlugin.Init(ctx, messagePipe) - require.NoError(t, err) - defer commandPlugin.Close(ctx) - - go commandPlugin.monitorSubscribeChannel(ctx) - - commandPlugin.subscribeChannel <- &mpi.ManagementPlaneRequest{ - Request: &mpi.ManagementPlaneRequest_HealthRequest{ - HealthRequest: &mpi.HealthRequest{}, + { + name: "Test 2: Config Apply Request", + mpiRequest: &mpi.ManagementPlaneRequest{ + Request: &mpi.ManagementPlaneRequest_ConfigApplyRequest{ + ConfigApplyRequest: &mpi.ConfigApplyRequest{}, + }, + }, + expectedTopic: &bus.Message{Topic: bus.ConfigApplyRequestTopic}, + isApplyRequest: true, + }, + { + name: "Test 3: Config Health Request", + mpiRequest: &mpi.ManagementPlaneRequest{ + Request: &mpi.ManagementPlaneRequest_HealthRequest{ + HealthRequest: &mpi.HealthRequest{}, + }, + }, + expectedTopic: &bus.Message{Topic: bus.DataplaneHealthTopic}, }, } - assert.Eventually( - t, - func() bool { return len(messagePipe.GetMessages()) == 1 }, - 2*time.Second, - 10*time.Millisecond, - ) - - messages := messagePipe.GetMessages() - assert.Len(t, messages, 1) - assert.Equal(t, bus.DataplaneHealthTopic, messages[0].Topic) + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + messagePipe := bus.NewFakeMessagePipe() + + commandPlugin := NewCommandPlugin(types.AgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{}) + err := commandPlugin.Init(ctx, messagePipe) + require.NoError(t, err) + defer commandPlugin.Close(ctx) + + go commandPlugin.monitorSubscribeChannel(ctx) + + commandPlugin.subscribeChannel <- test.mpiRequest + + assert.Eventually( + t, + func() bool { return len(messagePipe.GetMessages()) == 1 }, + 2*time.Second, + 10*time.Millisecond, + ) + + messages := messagePipe.GetMessages() + assert.Len(t, messages, 1) + assert.Equal(t, test.expectedTopic.Topic, messages[0].Topic) + + _, ok := messages[0].Data.(*mpi.ManagementPlaneRequest) + + if test.isUploadRequest { + assert.True(t, ok) + require.NotNil(t, test.mpiRequest.GetConfigUploadRequest()) + } + if test.isApplyRequest { + assert.True(t, ok) + require.NotNil(t, test.mpiRequest.GetConfigApplyRequest()) + } + }) + } } func TestMonitorSubscribeChannel(t *testing.T) {