some refactoring inpraparation for multiple agents and clients.

This commit is contained in:
Erik Brakkee 2024-08-22 17:13:34 +02:00
parent 6e97162923
commit 5191b9cd49

View File

@ -17,18 +17,7 @@ import (
"time" "time"
) )
// test cases // test case
//
// Agent only: verify state, verify agentregistration message
// - Connect single agent
// - Connect agent, connect second agent with duplicate id
// -> new id taken out
// - Connect more than 100 agents with the same id
// -> 101th agents gets error
//
// Client connected to agent: Verify clientConnection and agentCOnnection, verify state, verify clientInfo message.
// - Connect agent + connect client with mmtching id
// - Connect agent + connect client with wrong id
// //
// Overall: // Overall:
// - Connect agent, connect 2 clients // - Connect agent, connect 2 clients
@ -171,34 +160,29 @@ func (s *AdminTestSuite) Test_connectClient() {
res := testsupport.RunAndWait( res := testsupport.RunAndWait(
&s.Suite, &s.Suite,
func() any { func() any {
// server return s.connectClient(publicId, serverToClientRW, data)
clientConn, err := s.admin.AddClient(models.RendezVousId(publicId),
iowrappers.NewSimpleReadWriteAddrCloser(serverToClientRW, testsupport.DummyRemoteAddr("remoteaddr")))
s.Nil(err)
// Connection to agent over yamux
serverToAgentYamux := clientConn.agentConnection
// test by sending a message to the agent.
testsupport.AssertWriteData(&s.Suite, data, serverToAgentYamux)
return clientConn
}, },
func() any { func() any {
// agent return s.clientConnection(agentRes, data)
listener := comms.NewAgentListener(agentRes.commChannel.Session)
//.Connection from server over yamux
agentToServerYamux, err := listener.Accept()
s.Nil(err)
// Test by receiving a message from the server
testsupport.AssertReadData(&s.Suite, data, agentToServerYamux)
return agentToServerYamux
}) })
// Now we need to verify bi-directional communication between client and agent through the wserv // bidirectional communciataion check
clientConn := res[0].(*clientConnection) clientConn := res[0].(*clientConnection)
go clientConn.Synchronize()
agentToServerYamux := res[1].(io.ReadWriter) agentToServerYamux := res[1].(io.ReadWriter)
go clientConn.Synchronize()
s.bidirectionalConnectionCheck(clientToServerRW, agentToServerYamux)
// verify state
state := s.admin.CreateNotifification()
s.Equal(1, len(state.Agents))
s.Equal(1, len(state.Clients))
s.Equal(clientConn.Info, state.Clients[clientConn.Info.Guid])
// will close the connections and as a result also th synchronize goroutine.
s.cancelFunc()
}
func (s *AdminTestSuite) bidirectionalConnectionCheck(clientToServerRW io.ReadWriteCloser, agentToServerYamux io.ReadWriter) {
data1 := "mytestdata" data1 := "mytestdata"
data2 := "mytestdata-2" data2 := "mytestdata-2"
testsupport.RunAndWait( testsupport.RunAndWait(
@ -213,9 +197,6 @@ func (s *AdminTestSuite) Test_connectClient() {
testsupport.AssertWriteData(&s.Suite, data2, clientToServerRW) testsupport.AssertWriteData(&s.Suite, data2, clientToServerRW)
return nil return nil
}) })
// will close the connections and as a result also th synchronize goroutine.
s.cancelFunc()
} }
func (s *AdminTestSuite) Test_connectClientUnknownRendezVousId() { func (s *AdminTestSuite) Test_connectClientUnknownRendezVousId() {
@ -229,8 +210,27 @@ func (s *AdminTestSuite) Test_connectClientUnknownRendezVousId() {
_, err := s.admin.AddClient(models.RendezVousId(publicId+"sothatisunknown"), _, err := s.admin.AddClient(models.RendezVousId(publicId+"sothatisunknown"),
iowrappers.NewSimpleReadWriteAddrCloser(serverToClientRW, testsupport.DummyRemoteAddr("remoteaddr"))) iowrappers.NewSimpleReadWriteAddrCloser(serverToClientRW, testsupport.DummyRemoteAddr("remoteaddr")))
s.NotNil(err) s.NotNil(err)
// verify state
state := s.admin.CreateNotifification()
s.Equal(1, len(state.Agents))
s.Equal(0, len(state.Clients))
} }
// Registering an agent on the server
func (s *AdminTestSuite) addAgent(publicId string, assignedPublicId string, serverRW io.ReadWriteCloser) (*agentConnection, error) {
agentConn, err := s.admin.AddAgent(
s.hostKey, models.RendezVousId(publicId), comms.EnvironmentInfo{},
serverRW)
if err != nil {
return nil, err
}
s.Equal(assignedPublicId, string(agentConn.Info.PublicId))
return agentConn, nil
}
// Agent activities registring on the server
func (s *AdminTestSuite) agentRegistration(agentRW io.ReadWriteCloser) AgentRegisterResult { func (s *AdminTestSuite) agentRegistration(agentRW io.ReadWriteCloser) AgentRegisterResult {
// verify registration message received // verify registration message received
agentRegistration, err := comms.ReceiveRegistrationMessage(agentRW) agentRegistration, err := comms.ReceiveRegistrationMessage(agentRW)
@ -245,13 +245,25 @@ func (s *AdminTestSuite) agentRegistration(agentRW io.ReadWriteCloser) AgentRegi
return AgentRegisterResult{registration: agentRegistration, commChannel: commChannel, err: nil} return AgentRegisterResult{registration: agentRegistration, commChannel: commChannel, err: nil}
} }
func (s *AdminTestSuite) addAgent(publicId string, assignedPublicId string, serverRW io.ReadWriteCloser) (*agentConnection, error) { func (s *AdminTestSuite) connectClient(publicId string, serverToClientRW io.ReadWriteCloser, data string) any {
agentConn, err := s.admin.AddAgent( // server
s.hostKey, models.RendezVousId(publicId), comms.EnvironmentInfo{}, clientConn, err := s.admin.AddClient(models.RendezVousId(publicId),
serverRW) iowrappers.NewSimpleReadWriteAddrCloser(serverToClientRW, testsupport.DummyRemoteAddr("remoteaddr")))
if err != nil { s.Nil(err)
return nil, err // Connection to agent over yamux
serverToAgentYamux := clientConn.agentConnection
// test by sending a message to the agent.
testsupport.AssertWriteData(&s.Suite, data, serverToAgentYamux)
return clientConn
} }
s.Equal(assignedPublicId, string(agentConn.Info.PublicId))
return agentConn, nil func (s *AdminTestSuite) clientConnection(agentRes AgentRegisterResult, data string) any {
// agent
listener := comms.NewAgentListener(agentRes.commChannel.Session)
//.Connection from server over yamux
agentToServerYamux, err := listener.Accept()
s.Nil(err)
// Test by receiving a message from the server
testsupport.AssertReadData(&s.Suite, data, agentToServerYamux)
return agentToServerYamux
} }