test for duplicate id and new id gene4ration done

This commit is contained in:
Erik Brakkee 2024-08-22 12:00:22 +02:00
parent 37ff504b88
commit d144a4d230
2 changed files with 59 additions and 22 deletions

View File

@ -96,7 +96,7 @@ func (admin *Admin) getFreeId(publicId models.RendezVousId) (models.RendezVousId
if admin.agents[publicId] == nil { if admin.agents[publicId] == nil {
return publicId, nil return publicId, nil
} }
for i := 0; i < 100; i++ { for i := range 100 {
candidate := models.RendezVousId(string(publicId) + "-" + strconv.Itoa(i)) candidate := models.RendezVousId(string(publicId) + "-" + strconv.Itoa(i))
if admin.agents[candidate] == nil { if admin.agents[candidate] == nil {
return candidate, nil return candidate, nil
@ -130,6 +130,8 @@ func (admin *Admin) AddAgent(hostKey []byte, publicId models.RendezVousId, agent
Ok: false, Ok: false,
Message: err.Error(), Message: err.Error(),
}) })
return nil, fmt.Errorf(
"Agent requested id '%s' which is already taken anc could not allocate a new unique id", publicId)
} }
agentCheck := admin.agents[publicId] agentCheck := admin.agents[publicId]
if agentCheck != nil { if agentCheck != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"io" "io"
"net/http" "net/http"
"strings"
"testing" "testing"
"time" "time"
) )
@ -62,7 +63,7 @@ func (s *AdminTestSuite) SetupTest() {
s.admin = NewAdmin() s.admin = NewAdmin()
s.hostKey = make([]byte, 100) s.hostKey = make([]byte, 100)
rand.Read(s.hostKey) s.NotNil(rand.Read(s.hostKey))
} }
func (suite *AdminTestSuite) TearDownTest() { func (suite *AdminTestSuite) TearDownTest() {
@ -72,21 +73,43 @@ func TestAdminTestSuite(t *testing.T) {
suite.Run(t, &AdminTestSuite{}) suite.Run(t, &AdminTestSuite{})
} }
func (s *AdminTestSuite) agentRegisters(requestedPublicId, assignedPublicId string) *agentConnection { type AddAgentResult struct {
agentConn *agentConnection
err error
}
type AgentRegisterResult struct {
registration comms.AgentRegistration
err error
}
func (s *AdminTestSuite) agentRegisters(requestedPublicId, assignedPublicId string) (AddAgentResult, AgentRegisterResult) {
agentRW, serverRW := s.createPipe() agentRW, serverRW := s.createPipe()
res := testsupport.RunAndWait( res := testsupport.RunAndWait(
&s.Suite, &s.Suite,
func() any { func() any {
return s.addAgent(requestedPublicId, assignedPublicId, serverRW) agentConn, err := s.addAgent(requestedPublicId, assignedPublicId, serverRW)
return AddAgentResult{
agentConn: agentConn,
err: err,
}
}, },
func() any { func() any {
return s.agentRegistration(assignedPublicId, agentRW) res := s.agentRegistration(assignedPublicId, agentRW)
if assignedPublicId != "" {
s.Nil(res.err)
s.True(res.registration.Ok)
s.Equal(s.hostKey, res.registration.HostPrivateKey)
}
return res
}) })
return res[0].(*agentConnection) return res[0].(AddAgentResult), res[1].(AgentRegisterResult)
} }
func (s *AdminTestSuite) Test_AgentRegisters() { func (s *AdminTestSuite) Test_AgentRegisters() {
agentConn := s.agentRegisters("abc", "abc") res, _ := s.agentRegisters("abc", "abc")
s.Nil(res.err)
agentConn := res.agentConn
state := s.admin.CreateNotifification() state := s.admin.CreateNotifification()
s.Equal(1, len(state.Agents)) s.Equal(1, len(state.Agents))
s.Equal(0, len(state.Clients)) s.Equal(0, len(state.Clients))
@ -99,7 +122,9 @@ func (s *AdminTestSuite) Test_ManyAgentsRegister() {
for i := range N { for i := range N {
publicId := fmt.Sprintf("abc%d", i) publicId := fmt.Sprintf("abc%d", i)
agentRegistrations[i] = func() any { agentRegistrations[i] = func() any {
return s.agentRegisters(publicId, publicId) res, _ := s.agentRegisters(publicId, publicId)
s.Nil(res.err)
return res.agentConn
} }
} }
res := testsupport.RunAndWait( res := testsupport.RunAndWait(
@ -115,30 +140,40 @@ func (s *AdminTestSuite) Test_ManyAgentsRegister() {
} }
func (s *AdminTestSuite) Test_agentDuplicateId() { func (s *AdminTestSuite) Test_agentDuplicateId() {
s.agentRegisters("abc", "abc") res, _ := s.agentRegisters("abc", "abc")
s.agentRegisters("abc", "abc-0") s.Nil(res.err)
for i := range 100 {
res, _ = s.agentRegisters("abc", fmt.Sprintf("abc-%d", i))
s.Nil(res.err)
}
res, agentSideResult := s.agentRegisters("abc", "")
s.NotNil(res.err)
// verify it is the correct error an dnot an id mismatch.
s.True(strings.Contains(res.err.Error(), "could not allocate a new unique id"))
s.False(agentSideResult.registration.Ok)
} }
func (s *AdminTestSuite) agentRegistration(expectedPublicId string, agentRW io.ReadWriteCloser) any { func (s *AdminTestSuite) agentRegistration(expectedPublicId string, agentRW io.ReadWriteCloser) AgentRegisterResult {
// verify registration message received // verify registration message received
agentRegistration, err := comms.ReceiveRegistrationMessage(agentRW) agentRegistration, err := comms.ReceiveRegistrationMessage(agentRW)
s.Nil(err) if err != nil {
s.True(agentRegistration.Ok) return AgentRegisterResult{err: err}
s.Equal(expectedPublicId, agentRegistration.Id) }
s.Equal(s.hostKey, agentRegistration.HostPrivateKey)
commChannel, err := comms.NewCommChannel(comms.Agent, agentRW) commChannel, err := comms.NewCommChannel(comms.Agent, agentRW)
s.Nil(err) if err != nil {
return AgentRegisterResult{registration: agentRegistration, err: err}
}
s.NotNil(commChannel) s.NotNil(commChannel)
return nil return AgentRegisterResult{registration: agentRegistration, err: nil}
} }
func (s *AdminTestSuite) addAgent(publicId string, assignedPublicId string, serverRW io.ReadWriteCloser) any { func (s *AdminTestSuite) addAgent(publicId string, assignedPublicId string, serverRW io.ReadWriteCloser) (*agentConnection, error) {
agentConn, err := s.admin.AddAgent( agentConn, err := s.admin.AddAgent(
s.hostKey, models.RendezVousId(publicId), comms.EnvironmentInfo{}, s.hostKey, models.RendezVousId(publicId), comms.EnvironmentInfo{},
serverRW) serverRW)
s.Nil(err) if err != nil {
return nil, err
}
s.Equal(assignedPublicId, string(agentConn.Info.PublicId)) s.Equal(assignedPublicId, string(agentConn.Info.PublicId))
return agentConn, nil
return agentConn
} }