converge/pkg/server/admin/admin_test.go

109 lines
3.0 KiB
Go

package admin
import (
"context"
"crypto/rand"
"git.wamblee.org/converge/pkg/comms"
"git.wamblee.org/converge/pkg/models"
"git.wamblee.org/converge/pkg/testsupport"
"github.com/stretchr/testify/suite"
"io"
"net/http"
"testing"
"time"
)
// test cases
//
// 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:
// - Connect agent, connect 2 clients
// - Connect multiple agents and clients
type AdminTestSuite struct {
suite.Suite
ctx context.Context
cancelFunc context.CancelFunc
pprofServer *http.Server
agentReadWriter io.ReadWriteCloser
serverReadWriter io.ReadWriteCloser
admin *Admin
hostKey []byte
}
func (s *AdminTestSuite) SetupSuite() {
s.pprofServer = testsupport.StartPprof("")
}
func (s *AdminTestSuite) TearDownSuite() {
testsupport.StopPprof(s.ctx, s.pprofServer)
}
func (s *AdminTestSuite) SetupTest() {
ctx, cancelFunc := testsupport.CreateTestContext(context.Background(), 10*time.Second)
s.ctx = ctx
s.cancelFunc = cancelFunc
// Could have also used net.Pipe but net.Pipe uses synchronous communication
// by default and the bitpipe implementation can become asynchronous when
// a channels ize > 0 is passed in. Also the test utility respects the context
// so also deals with cancellation much better than net.Pipe.
bitpipe := testsupport.NewInmemoryConnection(s.ctx, "inmemory", 10)
agentReadWriter := bitpipe.Front()
serverReadWriter := bitpipe.Back()
s.agentReadWriter = agentReadWriter
s.serverReadWriter = serverReadWriter
s.admin = NewAdmin()
s.hostKey = make([]byte, 100)
rand.Read(s.hostKey)
}
func (suite *AdminTestSuite) TearDownTest() {
}
func TestAdminTestSuite(t *testing.T) {
suite.Run(t, &AdminTestSuite{})
}
func (s *AdminTestSuite) Test_AgentRegisters() {
publicId := "abc"
testsupport.RunAndWait(
&s.Suite,
func() any {
agentConn, err := s.admin.AddAgent(s.hostKey, models.RendezVousId(publicId), comms.EnvironmentInfo{}, s.serverReadWriter)
s.Nil(err)
s.Equal(publicId, string(agentConn.Info.PublicId))
state := s.admin.CreateNotifification()
s.Equal(1, len(state.Agents))
s.Equal(0, len(state.Clients))
s.Equal(agentConn.Info, state.Agents[agentConn.Info.Guid])
return nil
},
func() any {
// verify registration message received
agentRegistration, err := comms.ReceiveRegistrationMessage(s.agentReadWriter)
s.Nil(err)
s.True(agentRegistration.Ok)
s.Equal(s.hostKey, agentRegistration.HostPrivateKey)
commChannel, err := comms.NewCommChannel(comms.Agent, s.agentReadWriter)
s.Nil(err)
s.NotNil(commChannel)
return nil
})
}