From 5d65f3bbd4dac7d2c0756ab3c653116e32a3062f Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Wed, 21 Aug 2024 23:48:37 +0200 Subject: [PATCH] protocol version test implemented between agent and server (and found that it did not work) --- pkg/comms/agentserver.go | 22 +++++++++++---------- pkg/comms/agentserver_test.go | 36 ++++++++++++++++++++++++++++++++--- pkg/comms/events.go | 7 ++++++- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/pkg/comms/agentserver.go b/pkg/comms/agentserver.go index 0437e82..076fb4d 100644 --- a/pkg/comms/agentserver.go +++ b/pkg/comms/agentserver.go @@ -146,7 +146,9 @@ func ListenForServerEvents(channel CommChannel) { func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerInfo, error) { channel := NewGOBChannel(conn) err := CheckProtocolVersion(Agent, channel) - + if err != nil { + return ServerInfo{}, err + } err = SendWithTimeout(channel, agentInto) if err != nil { return ServerInfo{}, nil @@ -155,16 +157,16 @@ func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerI if err != nil { return ServerInfo{}, nil } - // TODO remove logging log.Println("Agent configuration received from server") - return serverInfo, err } func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (EnvironmentInfo, error) { channel := NewGOBChannel(conn) err := CheckProtocolVersion(ConvergeServer, channel) - + if err != nil { + return EnvironmentInfo{}, err + } agentInfo, err := ReceiveWithTimeout[EnvironmentInfo](channel) if err != nil { return EnvironmentInfo{}, err @@ -187,7 +189,7 @@ func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (Environmen func CheckProtocolVersion(role Role, channel GOBChannel) error { switch role { case Agent: - err := SendWithTimeout(channel, ProtocolVersion{Version: PROTOCOL_VERSION}) + err := SendWithTimeout(channel, ProtocolVersion{Version: agentProtocolVersion}) if err != nil { return err } @@ -195,9 +197,9 @@ func CheckProtocolVersion(role Role, channel GOBChannel) error { if err != nil { return err } - if version.Version != PROTOCOL_VERSION { + if version.Version != agentProtocolVersion { return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d", - PROTOCOL_VERSION, version.Version) + agentProtocolVersion, version.Version) } return nil case ConvergeServer: @@ -205,13 +207,13 @@ func CheckProtocolVersion(role Role, channel GOBChannel) error { if err != nil { return err } - err = SendWithTimeout(channel, ProtocolVersion{Version: PROTOCOL_VERSION}) + err = SendWithTimeout(channel, ProtocolVersion{Version: serverProtocolVersion}) if err != nil { return err } - if version.Version != PROTOCOL_VERSION { + if version.Version != serverProtocolVersion { return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d", - PROTOCOL_VERSION, version.Version) + serverProtocolVersion, version.Version) } return nil default: diff --git a/pkg/comms/agentserver_test.go b/pkg/comms/agentserver_test.go index d37622c..4214f2a 100644 --- a/pkg/comms/agentserver_test.go +++ b/pkg/comms/agentserver_test.go @@ -8,6 +8,7 @@ import ( "log" "math/rand" "net/http" + "strings" "testing" "time" ) @@ -48,6 +49,8 @@ func (s *AgentServerTestSuite) SetupTest() { } func (suite *AgentServerTestSuite) TearDownTest() { + agentProtocolVersion = PROTOCOL_VERSION + serverProtocolVersion = PROTOCOL_VERSION } func TestAgentServerTestSuite(t *testing.T) { @@ -163,6 +166,32 @@ func (s *AgentServerTestSuite) Test_Initialization() { }) } +func (s *AgentServerTestSuite) Test_InitializationProtocolVersionMismatch() { + serverProtocolVersion++ + testsupport.RunAndWait( + &s.Suite, + func() any { + serverInfo, err := AgentInitialization(s.agentConnection, + NewEnvironmentInfo("myshell")) + s.NotNil(err) + s.True(strings.Contains(strings.ToLower(err.Error()), "protocol")) + s.Equal(ServerInfo{}, serverInfo) + return nil + }, + func() any { + serverInfo := ServerInfo{TestId: 1000} + environmentInfo, err := ServerInitialization(s.serverConnection, serverInfo) + s.NotNil(err) + s.True(strings.Contains(strings.ToLower(err.Error()), "protocol")) + s.Equal(EnvironmentInfo{}, environmentInfo) + return nil + }) +} + +// TODO: +// Test for protocolversion mismatch: verify error is returned. Use sentinel error +// Tests when connection is close from agent and from server: verify error is returned + func (s *AgentServerTestSuite) Test_ListenForAgentEvents() { agentEvents := []any{ @@ -217,6 +246,7 @@ func (s *AgentServerTestSuite) Test_ListenForAgentEvents() { }) } -func (s *AgentServerTestSuite) Test_LIstenForServerEvents() { - -} +// This is currently a Noop. No need to test it. +//func (s *AgentServerTestSuite) Test_LIstenForServerEvents() { +// +//} diff --git a/pkg/comms/events.go b/pkg/comms/events.go index 41e5beb..0a12365 100644 --- a/pkg/comms/events.go +++ b/pkg/comms/events.go @@ -8,7 +8,12 @@ import ( "time" ) -const PROTOCOL_VERSION = 4 +const PROTOCOL_VERSION = 5 + +// for testing protocol version mismatch we define a separate protocol version +// for the agent and server +var agentProtocolVersion = PROTOCOL_VERSION +var serverProtocolVersion = PROTOCOL_VERSION func init() { RegisterEventsWithGob()