protocol version test implemented between agent and server (and found

that it did not work)
This commit is contained in:
Erik Brakkee 2024-08-21 23:48:37 +02:00
parent 53534af27c
commit 5d65f3bbd4
3 changed files with 51 additions and 14 deletions

View File

@ -146,7 +146,9 @@ func ListenForServerEvents(channel CommChannel) {
func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerInfo, error) { func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerInfo, error) {
channel := NewGOBChannel(conn) channel := NewGOBChannel(conn)
err := CheckProtocolVersion(Agent, channel) err := CheckProtocolVersion(Agent, channel)
if err != nil {
return ServerInfo{}, err
}
err = SendWithTimeout(channel, agentInto) err = SendWithTimeout(channel, agentInto)
if err != nil { if err != nil {
return ServerInfo{}, nil return ServerInfo{}, nil
@ -155,16 +157,16 @@ func AgentInitialization(conn io.ReadWriter, agentInto EnvironmentInfo) (ServerI
if err != nil { if err != nil {
return ServerInfo{}, nil return ServerInfo{}, nil
} }
// TODO remove logging
log.Println("Agent configuration received from server") log.Println("Agent configuration received from server")
return serverInfo, err return serverInfo, err
} }
func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (EnvironmentInfo, error) { func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (EnvironmentInfo, error) {
channel := NewGOBChannel(conn) channel := NewGOBChannel(conn)
err := CheckProtocolVersion(ConvergeServer, channel) err := CheckProtocolVersion(ConvergeServer, channel)
if err != nil {
return EnvironmentInfo{}, err
}
agentInfo, err := ReceiveWithTimeout[EnvironmentInfo](channel) agentInfo, err := ReceiveWithTimeout[EnvironmentInfo](channel)
if err != nil { if err != nil {
return EnvironmentInfo{}, err return EnvironmentInfo{}, err
@ -187,7 +189,7 @@ func ServerInitialization(conn io.ReadWriter, serverInfo ServerInfo) (Environmen
func CheckProtocolVersion(role Role, channel GOBChannel) error { func CheckProtocolVersion(role Role, channel GOBChannel) error {
switch role { switch role {
case Agent: case Agent:
err := SendWithTimeout(channel, ProtocolVersion{Version: PROTOCOL_VERSION}) err := SendWithTimeout(channel, ProtocolVersion{Version: agentProtocolVersion})
if err != nil { if err != nil {
return err return err
} }
@ -195,9 +197,9 @@ func CheckProtocolVersion(role Role, channel GOBChannel) error {
if err != nil { if err != nil {
return err return err
} }
if version.Version != PROTOCOL_VERSION { if version.Version != agentProtocolVersion {
return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d", return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d",
PROTOCOL_VERSION, version.Version) agentProtocolVersion, version.Version)
} }
return nil return nil
case ConvergeServer: case ConvergeServer:
@ -205,13 +207,13 @@ func CheckProtocolVersion(role Role, channel GOBChannel) error {
if err != nil { if err != nil {
return err return err
} }
err = SendWithTimeout(channel, ProtocolVersion{Version: PROTOCOL_VERSION}) err = SendWithTimeout(channel, ProtocolVersion{Version: serverProtocolVersion})
if err != nil { if err != nil {
return err return err
} }
if version.Version != PROTOCOL_VERSION { if version.Version != serverProtocolVersion {
return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d", return fmt.Errorf("Protocol version mismatch: agent %d, converge server %d",
PROTOCOL_VERSION, version.Version) serverProtocolVersion, version.Version)
} }
return nil return nil
default: default:

View File

@ -8,6 +8,7 @@ import (
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"strings"
"testing" "testing"
"time" "time"
) )
@ -48,6 +49,8 @@ func (s *AgentServerTestSuite) SetupTest() {
} }
func (suite *AgentServerTestSuite) TearDownTest() { func (suite *AgentServerTestSuite) TearDownTest() {
agentProtocolVersion = PROTOCOL_VERSION
serverProtocolVersion = PROTOCOL_VERSION
} }
func TestAgentServerTestSuite(t *testing.T) { 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() { func (s *AgentServerTestSuite) Test_ListenForAgentEvents() {
agentEvents := []any{ 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() {
} //
//}

View File

@ -8,7 +8,12 @@ import (
"time" "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() { func init() {
RegisterEventsWithGob() RegisterEventsWithGob()