Skip to content

Commit 16278d5

Browse files
committed
break updateServiceTags in smaller pieces and start testing it
1 parent f62e790 commit 16278d5

File tree

2 files changed

+195
-24
lines changed

2 files changed

+195
-24
lines changed

pkg/tagit/tagit.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,48 @@ func (t *TagIt) runScript() ([]byte, error) {
8888
func (t *TagIt) updateServiceTags() error {
8989
service, err := t.getService()
9090
if err != nil {
91-
return err
91+
return fmt.Errorf("error getting service: %w", err)
9292
}
93-
registration := t.copyServiceToRegistration(service)
94-
log.WithFields(log.Fields{
95-
"service": t.ServiceID,
96-
"tags": registration.Tags,
97-
}).Debug("current service tags")
98-
out, err := t.runScript()
93+
94+
newTags, err := t.generateNewTags()
9995
if err != nil {
100-
return err
96+
return fmt.Errorf("error generating new tags: %w", err)
10197
}
10298

103-
var tags []string
104-
for _, tag := range strings.Fields(string(out)) {
105-
tags = append(tags, fmt.Sprintf("%s-%s", t.TagPrefix, tag))
99+
if err := t.updateConsulService(service, newTags); err != nil {
100+
return fmt.Errorf("error updating service in Consul: %w", err)
106101
}
107102

108-
updatedTags, shouldTag := t.needsTag(registration.Tags, tags)
103+
return nil
104+
}
105+
106+
// generateNewTags runs the script and generates new tags.
107+
func (t *TagIt) generateNewTags() ([]string, error) {
108+
out, err := t.runScript()
109+
if err != nil {
110+
return nil, err
111+
}
112+
return t.parseScriptOutput(out), nil
113+
}
114+
115+
// updateConsulService updates the service in Consul with the new tags.
116+
func (t *TagIt) updateConsulService(service *api.AgentService, newTags []string) error {
117+
registration := t.copyServiceToRegistration(service)
118+
updatedTags, shouldTag := t.needsTag(registration.Tags, newTags)
109119
if shouldTag {
110120
registration.Tags = updatedTags
111-
log.WithFields(log.Fields{
112-
"service": t.ServiceID,
113-
"tags": registration.Tags,
114-
}).Info("updating service tags")
115121
return t.client.Agent().ServiceRegister(registration)
116122
}
123+
return nil
124+
}
117125

118-
log.WithFields(log.Fields{
119-
"service": t.ServiceID,
120-
"tags": registration.Tags,
121-
}).Debug("no changes to service tags")
122-
123-
return err
126+
// parseScriptOutput parses the script output and generates tags.
127+
func (t *TagIt) parseScriptOutput(output []byte) []string {
128+
var tags []string
129+
for _, tag := range strings.Fields(string(output)) {
130+
tags = append(tags, fmt.Sprintf("%s-%s", t.TagPrefix, tag))
131+
}
132+
return tags
124133
}
125134

126135
// CleanupServiceTags cleans up the service tags added by tagit.

pkg/tagit/tagit_test.go

Lines changed: 164 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,16 @@ func (m *MockConsulClient) Agent() ConsulAgent {
301301

302302
// MockAgent simulates the Agent part of the Consul client.
303303
type MockAgent struct {
304-
ServicesFunc func() (map[string]*api.AgentService, error)
304+
ServicesFunc func() (map[string]*api.AgentService, error)
305+
ServiceRegisterFunc func(reg *api.AgentServiceRegistration) error
305306
}
306307

307308
func (m *MockAgent) Services() (map[string]*api.AgentService, error) {
308309
return m.ServicesFunc()
309310
}
310311

311312
func (m *MockAgent) ServiceRegister(reg *api.AgentServiceRegistration) error {
312-
return nil
313+
return m.ServiceRegisterFunc(reg)
313314
}
314315

315316
func TestNew(t *testing.T) {
@@ -334,3 +335,164 @@ func TestNew(t *testing.T) {
334335
t.Errorf("Expected ServiceID to be 'test-service', got '%s'", tagit.ServiceID)
335336
}
336337
}
338+
339+
func TestGetService(t *testing.T) {
340+
tests := []struct {
341+
name string
342+
serviceID string
343+
mockServicesData map[string]*api.AgentService
344+
mockServicesErr error
345+
expectErr bool
346+
expectService *api.AgentService
347+
}{
348+
{
349+
name: "Service Found",
350+
serviceID: "test-service",
351+
mockServicesData: map[string]*api.AgentService{
352+
"test-service": {
353+
ID: "test-service",
354+
Service: "test",
355+
Tags: []string{"tag1", "tag2"},
356+
},
357+
},
358+
expectErr: false,
359+
expectService: &api.AgentService{ID: "test-service", Service: "test", Tags: []string{"tag1", "tag2"}},
360+
},
361+
{
362+
name: "Service Not Found",
363+
serviceID: "nonexistent-service",
364+
mockServicesData: map[string]*api.AgentService{},
365+
expectErr: true,
366+
expectService: nil,
367+
},
368+
{
369+
name: "Consul Client Error",
370+
serviceID: "test-service",
371+
mockServicesErr: fmt.Errorf("consul client error"),
372+
expectErr: true,
373+
expectService: nil,
374+
},
375+
}
376+
377+
for _, tt := range tests {
378+
t.Run(tt.name, func(t *testing.T) {
379+
mockConsulClient := &MockConsulClient{
380+
MockAgent: &MockAgent{
381+
ServicesFunc: func() (map[string]*api.AgentService, error) {
382+
return tt.mockServicesData, tt.mockServicesErr
383+
},
384+
},
385+
}
386+
tagit := New(mockConsulClient, nil, tt.serviceID, "", 0, "")
387+
388+
service, err := tagit.getService()
389+
390+
if tt.expectErr && err == nil {
391+
t.Errorf("Expected an error but got none")
392+
} else if !tt.expectErr && err != nil {
393+
t.Errorf("Did not expect an error but got: %v", err)
394+
}
395+
396+
if !reflect.DeepEqual(service, tt.expectService) {
397+
t.Errorf("Expected service: %v, got: %v", tt.expectService, service)
398+
}
399+
})
400+
}
401+
}
402+
403+
func TestGenerateNewTags(t *testing.T) {
404+
tests := []struct {
405+
name string
406+
script string
407+
mockOutput string
408+
mockError error
409+
want []string
410+
wantErr bool
411+
}{
412+
{
413+
name: "Valid Script Output",
414+
script: "echo tag1 tag2",
415+
mockOutput: "tag1 tag2",
416+
want: []string{"tag-tag1", "tag-tag2"},
417+
wantErr: false,
418+
},
419+
{
420+
name: "Script Execution Error",
421+
script: "someinvalidcommand",
422+
mockError: fmt.Errorf("command failed"),
423+
want: nil,
424+
wantErr: true,
425+
},
426+
}
427+
428+
for _, tt := range tests {
429+
t.Run(tt.name, func(t *testing.T) {
430+
mockExecutor := &MockCommandExecutor{
431+
MockOutput: []byte(tt.mockOutput),
432+
MockError: tt.mockError,
433+
}
434+
tagit := TagIt{Script: tt.script, commandExecutor: mockExecutor, TagPrefix: "tag"}
435+
436+
got, err := tagit.generateNewTags()
437+
if (err != nil) != tt.wantErr {
438+
t.Fatalf("generateNewTags() error = %v, wantErr %v", err, tt.wantErr)
439+
}
440+
if !reflect.DeepEqual(got, tt.want) {
441+
t.Errorf("generateNewTags() got = %v, want %v", got, tt.want)
442+
}
443+
})
444+
}
445+
}
446+
447+
func TestUpdateConsulService(t *testing.T) {
448+
tests := []struct {
449+
name string
450+
existingTags []string
451+
newTags []string
452+
mockRegisterErr error
453+
expectUpdate bool
454+
expectErr bool
455+
}{
456+
{
457+
name: "Update Needed",
458+
existingTags: []string{"tag1", "tag2"},
459+
newTags: []string{"tag1", "tag3"},
460+
expectUpdate: true,
461+
expectErr: false,
462+
},
463+
{
464+
name: "No Update Needed",
465+
existingTags: []string{"tag1", "tag2"},
466+
newTags: []string{"tag1", "tag2"},
467+
expectUpdate: false,
468+
expectErr: false,
469+
},
470+
{
471+
name: "Consul Register Error",
472+
existingTags: []string{"tag1", "tag2"},
473+
newTags: []string{"tag1", "tag3"},
474+
mockRegisterErr: fmt.Errorf("consul error"),
475+
expectUpdate: true,
476+
expectErr: true,
477+
},
478+
}
479+
480+
for _, tt := range tests {
481+
t.Run(tt.name, func(t *testing.T) {
482+
service := &api.AgentService{Tags: tt.existingTags}
483+
mockConsulClient := &MockConsulClient{
484+
MockAgent: &MockAgent{
485+
ServiceRegisterFunc: func(reg *api.AgentServiceRegistration) error {
486+
return tt.mockRegisterErr
487+
},
488+
},
489+
}
490+
tagit := TagIt{client: mockConsulClient}
491+
492+
err := tagit.updateConsulService(service, tt.newTags)
493+
if (err != nil) != tt.expectErr {
494+
t.Fatalf("updateConsulService() error = %v, wantErr %v", err, tt.expectErr)
495+
}
496+
})
497+
}
498+
}

0 commit comments

Comments
 (0)