-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ServerToAgent_RequestRestart flag to request agent restart
Refactor to use a new field ServerToAgentCommand for Restart and Shutdown Removed reference to shutdown command for now
- Loading branch information
Showing
8 changed files
with
859 additions
and
565 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,115 @@ | ||
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 | ||
shutdown | ||
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: protobufs.ServerToAgentCommand_Shutdown, | ||
}, | ||
action: shutdown, | ||
message: "A Shutdown command should result in a shutdown", | ||
}, | ||
{ | ||
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 | ||
case protobufs.ServerToAgentCommand_Shutdown: | ||
action = shutdown | ||
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.