Skip to content

Commit

Permalink
Add Stop to Landscape test's mock config
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Feb 9, 2024
1 parent 8490a6f commit 61f1cc7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
7 changes: 3 additions & 4 deletions windows-agent/internal/proservices/landscape/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,9 @@ func testReceiveCommand(t *testing.T, distrosettings distroSettings, testSetup f

// Set up agent components (config, database, etc.)
if tb.conf == nil {
tb.conf = &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}
tb.conf = newMockConfig(ctx)
tb.conf.proToken = "TOKEN"
tb.conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr())
}

db, err := database.New(ctx, t.TempDir(), tb.conf)
Expand Down
86 changes: 62 additions & 24 deletions windows-agent/internal/proservices/landscape/landscape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,22 @@ func TestConnect(t *testing.T) {
lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", p)
defer lis.Close()

conf := &mockConfig{
proToken: "TOKEN",
conf := newMockConfig(ctx)
defer conf.Stop()

// We trigger an error on first-contact SendUpdatedInfo by erroring out in conf.ProToken()
proTokenErr: tc.tokenErr,
conf.proToken = "TOKEN"

// We trigger errors trying to read or write to/from the registry
landscapeUIDErr: tc.landscapeUIDReadErr,
setLandscapeUIDErr: tc.landscapeUIDWriteErr,
// We trigger an error on first-contact SendUpdatedInfo by erroring out in conf.ProToken()
conf.proTokenErr = tc.tokenErr

// We trigger an error when deciding to use a certificate or not
landscapeConfigErr: tc.breakLandscapeClientConfig,
// We trigger errors trying to read or write to/from the registry
conf.landscapeUIDErr = tc.landscapeUIDReadErr
conf.setLandscapeUIDErr = tc.landscapeUIDWriteErr

landscapeAgentUID: tc.uid,
}
// We trigger an error when deciding to use a certificate or not
conf.landscapeConfigErr = tc.breakLandscapeClientConfig

conf.landscapeAgentUID = tc.uid

if tc.emptyToken {
conf.proToken = ""
Expand Down Expand Up @@ -244,10 +245,11 @@ func TestSendUpdatedInfo(t *testing.T) {

lis, server, mockService := setUpLandscapeMock(t, ctx, "localhost:", "")

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}
conf := newMockConfig(ctx)
defer conf.Stop()

conf.proToken = "TOKEN"
conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr())

//nolint:errcheck // We don't care about these errors
go server.Serve(lis)
Expand Down Expand Up @@ -439,10 +441,11 @@ func TestAutoReconnection(t *testing.T) {
defer lis.Close()
defer server.Stop()

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr()),
}
conf := newMockConfig(ctx)
defer conf.Stop()

conf.proToken = "TOKEN"
conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, defaultLandscapeConfig, "", lis.Addr())

db, err := database.New(ctx, t.TempDir(), conf)
require.NoError(t, err, "Setup: database New should not return an error")
Expand Down Expand Up @@ -610,10 +613,11 @@ func TestReconnect(t *testing.T) {

lis, server, _ := setUpLandscapeMock(t, ctx, "localhost:", certPath)

conf := &mockConfig{
proToken: "TOKEN",
landscapeClientConfig: executeLandscapeConfigTemplate(t, lcapeConfig, certPath, lis.Addr()),
}
conf := newMockConfig(ctx)
defer conf.Stop()

conf.proToken = "TOKEN"
conf.landscapeClientConfig = executeLandscapeConfigTemplate(t, lcapeConfig, certPath, lis.Addr())

//nolint:errcheck // We don't care about these errors
go server.Serve(lis)
Expand Down Expand Up @@ -737,6 +741,9 @@ func setUpLandscapeMock(t *testing.T, ctx context.Context, addr string, certPath
}

type mockConfig struct {
ctx context.Context
cancel func()

proToken string
landscapeClientConfig string
landscapeAgentUID string
Expand All @@ -747,10 +754,29 @@ type mockConfig struct {
setLandscapeUIDErr bool

callbacks []func()
wg sync.WaitGroup

mu sync.Mutex
}

func newMockConfig(ctx context.Context) *mockConfig {
ctx, cancel := context.WithCancel(ctx)

return &mockConfig{
ctx: ctx,
cancel: cancel,
}
}

func (m *mockConfig) Stop() {
m.cancel()

m.mu.Lock()
defer m.mu.Unlock()

m.wg.Wait()
}

func (m *mockConfig) LandscapeClientConfig(ctx context.Context) (string, config.Source, error) {
m.mu.Lock()
defer m.mu.Unlock()
Expand Down Expand Up @@ -811,6 +837,18 @@ func (m *mockConfig) Notify(f func()) {

func (m *mockConfig) triggerNotifications() {
for _, f := range m.callbacks {
go f()
m.wg.Add(1)
f := f
go func() {
defer m.wg.Done()

select {
case <-m.ctx.Done():
return
default:
}

f()
}()
}
}

0 comments on commit 61f1cc7

Please sign in to comment.