-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow OpAMP server to request an agent restart or shutdown (#53)
* Adds ServerToAgentCommand to allow the server to request a restart or shutdown. * Adds AcceptsRestartRequests to AgentCapabilities so that agents can indicate if they support restart. * Adds OnCommand to the client Callbacks which is called when the server requests shutdown via the ServerToAgentCommand * Moved client.CallbacksStruct to types.CallbacksStruct so that it could be used it in receiver_test.go without an import cycle.
- Loading branch information
Showing
8 changed files
with
838 additions
and
561 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package internal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opamp-go/client/types" | ||
"github.com/open-telemetry/opamp-go/protobufs" | ||
) | ||
|
||
type TestLogger struct { | ||
*testing.T | ||
} | ||
|
||
func (logger TestLogger) Debugf(format string, v ...interface{}) { | ||
logger.Logf(format, v...) | ||
} | ||
|
||
type commandAction int | ||
|
||
const ( | ||
none commandAction = iota | ||
restart | ||
unknown | ||
) | ||
|
||
func TestServerToAgentCommand(t *testing.T) { | ||
|
||
tests := []struct { | ||
command *protobufs.ServerToAgentCommand | ||
action commandAction | ||
message string | ||
}{ | ||
{ | ||
command: nil, | ||
action: none, | ||
message: "No command should result in no action", | ||
}, | ||
{ | ||
command: &protobufs.ServerToAgentCommand{ | ||
Type: protobufs.ServerToAgentCommand_Restart, | ||
}, | ||
action: restart, | ||
message: "A Restart command should result in a restart", | ||
}, | ||
{ | ||
command: &protobufs.ServerToAgentCommand{ | ||
Type: -1, | ||
}, | ||
action: unknown, | ||
message: "An unknown command is still passed to the OnCommand callback", | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
t.Run(fmt.Sprint(i), func(t *testing.T) { | ||
action := none | ||
|
||
callbacks := types.CallbacksStruct{ | ||
OnCommandFunc: func(command *protobufs.ServerToAgentCommand) error { | ||
switch command.Type { | ||
case protobufs.ServerToAgentCommand_Restart: | ||
action = restart | ||
default: | ||
action = unknown | ||
} | ||
return nil | ||
}, | ||
} | ||
receiver := NewReceiver(TestLogger{t}, callbacks, nil, nil) | ||
receiver.processReceivedMessage(context.Background(), &protobufs.ServerToAgent{ | ||
Command: test.command, | ||
}) | ||
assert.Equal(t, test.action, action, test.message) | ||
}) | ||
} | ||
} | ||
|
||
func TestServerToAgentCommandExclusive(t *testing.T) { | ||
calledCommand := false | ||
calledRemoteConfig := false | ||
|
||
callbacks := types.CallbacksStruct{ | ||
OnCommandFunc: func(command *protobufs.ServerToAgentCommand) error { | ||
calledCommand = true | ||
return nil | ||
}, | ||
OnRemoteConfigFunc: func(ctx context.Context, remoteConfig *protobufs.AgentRemoteConfig) (effectiveConfig *protobufs.EffectiveConfig, configChanged bool, err error) { | ||
calledRemoteConfig = true | ||
return nil, false, nil | ||
}, | ||
} | ||
receiver := NewReceiver(TestLogger{t}, callbacks, nil, nil) | ||
receiver.processReceivedMessage(context.Background(), &protobufs.ServerToAgent{ | ||
Command: &protobufs.ServerToAgentCommand{ | ||
Type: protobufs.ServerToAgentCommand_Restart, | ||
}, | ||
RemoteConfig: &protobufs.AgentRemoteConfig{}, | ||
}) | ||
assert.Equal(t, true, calledCommand, "OnCommand should be called when a Command is specified") | ||
assert.Equal(t, false, calledRemoteConfig, "OnRemoteConfig should not be called when a Command is specified") | ||
} |
Oops, something went wrong.