Skip to content

Commit

Permalink
chore: added config apply test
Browse files Browse the repository at this point in the history
  • Loading branch information
RRashmit committed Oct 25, 2024
1 parent afb3ca0 commit 108e062
Showing 1 changed file with 72 additions and 59 deletions.
131 changes: 72 additions & 59 deletions internal/command/command_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 108e062

Please sign in to comment.