converge/pkg/comms/agentserver_test.go

133 lines
3.2 KiB
Go

package comms
import (
"context"
"git.wamblee.org/converge/pkg/testsupport"
"github.com/stretchr/testify/suite"
"log"
"net"
"net/http"
"testing"
"time"
)
type AgentServerTestSuite struct {
suite.Suite
ctx context.Context
cancelFunc context.CancelFunc
pprofServer *http.Server
agentConnection net.Conn
serverConnection net.Conn
}
func (s *AgentServerTestSuite) SetupSuite() {
s.pprofServer = testsupport.StartPprof("")
}
func (s *AgentServerTestSuite) TearDownSuite() {
testsupport.StopPprof(s.ctx, s.pprofServer)
}
func (s *AgentServerTestSuite) SetupTest() {
ctx, cancelFunc := testsupport.CreateTestContext(context.Background(), 10*time.Second)
s.ctx = ctx
s.cancelFunc = cancelFunc
serverConnection, agentConnection := net.Pipe()
deadline := time.Now().Add(10 * time.Second)
serverConnection.SetDeadline(deadline)
agentConnection.SetDeadline(deadline)
s.serverConnection = serverConnection
s.agentConnection = agentConnection
}
func (suite *AgentServerTestSuite) TearDownTest() {
}
func TestAgentServerTestSuite(t *testing.T) {
suite.Run(t, &AgentServerTestSuite{})
}
func (s *AgentServerTestSuite) createCommChannel() (CommChannel, CommChannel) {
commChannels := testsupport.RunAndWait(
&s.Suite,
func() any {
log.Println("Agent initializing")
commChannel, err := NewCommChannel(Agent, s.agentConnection)
s.Nil(err)
return commChannel
},
func() any {
log.Println("Server initializing")
commChannel, err := NewCommChannel(ConvergeServer, s.serverConnection)
s.Nil(err)
return commChannel
},
)
s.Equal(2, len(commChannels))
agentCommChannel := commChannels[0].(CommChannel)
serverCommChannel := commChannels[1].(CommChannel)
return agentCommChannel, serverCommChannel
}
func (s *AgentServerTestSuite) TestNewCommChannel() {
// Setup Comm channel
agentCommChannel, serverCommChannel := s.createCommChannel()
// verify the side channel is working by sending an object
testsupport.RunAndWait(
&s.Suite,
func() any {
protocolVersion := ProtocolVersion{Version: 10}
err := SendWithTimeout[ProtocolVersion](agentCommChannel.SideChannel, protocolVersion)
s.Nil(err)
log.Printf("Sent one message %v", protocolVersion)
return nil
},
func() any {
protocolVersion, err := ReceiveWithTimeout[ProtocolVersion](serverCommChannel.SideChannel)
s.Nil(err)
log.Printf("Received one message %v", protocolVersion)
return nil
},
)
log.Printf("%v %v", agentCommChannel, serverCommChannel)
}
func (s *AgentServerTestSuite) Test_ConnectThroughYamux() {
agentCommChannel, serverCommChannel := s.createCommChannel()
data := "hello"
testsupport.RunAndWait(
&s.Suite,
func() any {
conn, err := agentCommChannel.Session.OpenStream()
s.Nil(err)
n, err := conn.Write([]byte(data))
s.Nil(err)
s.Equal(len(data), n)
return nil
},
func() any {
conn, err := serverCommChannel.Session.Accept()
s.Nil(err)
buf := make([]byte, len(data))
n, err := conn.Read(buf)
s.Nil(err)
s.Equal(len(data), n)
s.Equal([]byte(data), buf[:n])
return nil
})
}
func (s *AgentServerTestSuite) Test_ListenForAgentEvents() {
}
func (s *AgentServerTestSuite) Test_LIstenForServerEvents() {
}