Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(coverage): Add a test case to cover that command is ignored when AcceptsCommand capability is not set #195

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions client/internal/wsreceiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,60 @@ func TestServerToAgentCommand(t *testing.T) {
}

func TestServerToAgentCommandExclusive(t *testing.T) {
calledCommand := false
calledOnMessageConfig := false

callbacks := types.CallbacksStruct{
OnCommandFunc: func(command *protobufs.ServerToAgentCommand) error {
calledCommand = true
return nil
cases := []struct {
capabilities protobufs.AgentCapabilities
command *protobufs.ServerToAgentCommand
calledCommand bool
calledCommandMsg string
calledOnMessageConfig bool
calledOnMessageConfigMsg string
}{
{
capabilities: protobufs.AgentCapabilities_AgentCapabilities_AcceptsRestartCommand,
command: &protobufs.ServerToAgentCommand{
Type: protobufs.CommandType_CommandType_Restart,
},
calledCommand: true,
calledCommandMsg: "OnCommand should be called when a Command is specified and capabilities are set",
calledOnMessageConfig: false,
calledOnMessageConfigMsg: "OnMessage should not be called when a Command is specified and capabilities are set",
},
OnMessageFunc: func(ctx context.Context, msg *types.MessageData) {
calledOnMessageConfig = true
{
capabilities: protobufs.AgentCapabilities_AgentCapabilities_Unspecified,
command: &protobufs.ServerToAgentCommand{
Type: protobufs.CommandType_CommandType_Restart,
},
calledCommand: false,
calledCommandMsg: "OnCommand should not be called when a Command is specified and capabilities are not set",
calledOnMessageConfig: true,
calledOnMessageConfigMsg: "OnMessage should be called when a Command is specified and capabilities are not set",
},
}
clientSyncedState := ClientSyncedState{}
capabilities := protobufs.AgentCapabilities_AgentCapabilities_AcceptsRestartCommand
receiver := NewWSReceiver(TestLogger{t}, callbacks, nil, nil, &clientSyncedState, nil, capabilities)
receiver.processor.ProcessReceivedMessage(context.Background(), &protobufs.ServerToAgent{
Command: &protobufs.ServerToAgentCommand{
Type: protobufs.CommandType_CommandType_Restart,
},
RemoteConfig: &protobufs.AgentRemoteConfig{},
})
assert.Equal(t, true, calledCommand, "OnCommand should be called when a Command is specified")
assert.Equal(t, false, calledOnMessageConfig, "OnMessage should not be called when a Command is specified")

for _, test := range cases {
calledCommand := false
calledOnMessageConfig := false

callbacks := types.CallbacksStruct{
OnCommandFunc: func(command *protobufs.ServerToAgentCommand) error {
calledCommand = true
return nil
},
OnMessageFunc: func(ctx context.Context, msg *types.MessageData) {
calledOnMessageConfig = true
},
}
clientSyncedState := ClientSyncedState{}
receiver := NewWSReceiver(TestLogger{t}, callbacks, nil, nil, &clientSyncedState, nil, test.capabilities)
receiver.processor.ProcessReceivedMessage(context.Background(), &protobufs.ServerToAgent{
Command: &protobufs.ServerToAgentCommand{
Type: protobufs.CommandType_CommandType_Restart,
},
RemoteConfig: &protobufs.AgentRemoteConfig{},
})
assert.Equal(t, test.calledCommand, calledCommand, test.calledCommandMsg)
assert.Equal(t, test.calledOnMessageConfig, calledOnMessageConfig, test.calledOnMessageConfigMsg)
}
}

func TestDecodeMessage(t *testing.T) {
Expand Down
Loading