Skip to content

Commit 84fd588

Browse files
committed
adds tests for updateServiceTags
1 parent dae2ae2 commit 84fd588

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

pkg/tagit/tagit_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,64 @@ func TestCmdExecutor_Execute(t *testing.T) {
562562
})
563563
}
564564
}
565+
566+
func TestUpdateServiceTags(t *testing.T) {
567+
tests := []struct {
568+
name string
569+
mockScriptOutput string
570+
mockScriptError error
571+
existingTags []string
572+
newTags []string
573+
mockRegisterErr error
574+
expectError bool
575+
}{
576+
{
577+
name: "Successful Update",
578+
mockScriptOutput: "new-tag1 new-tag2",
579+
existingTags: []string{"old-tag"},
580+
newTags: []string{"tag-new-tag1", "tag-new-tag2"},
581+
expectError: false,
582+
},
583+
{
584+
name: "Script Error",
585+
mockScriptError: fmt.Errorf("script error"),
586+
expectError: true,
587+
},
588+
{
589+
name: "Consul Register Error",
590+
mockScriptOutput: "new-tag1 new-tag2",
591+
mockRegisterErr: fmt.Errorf("consul error"),
592+
expectError: true,
593+
},
594+
}
595+
596+
for _, tt := range tests {
597+
t.Run(tt.name, func(t *testing.T) {
598+
mockExecutor := &MockCommandExecutor{
599+
MockOutput: []byte(tt.mockScriptOutput),
600+
MockError: tt.mockScriptError,
601+
}
602+
mockConsulClient := &MockConsulClient{
603+
MockAgent: &MockAgent{
604+
ServicesFunc: func() (map[string]*api.AgentService, error) {
605+
return map[string]*api.AgentService{
606+
"test-service": {
607+
ID: "test-service",
608+
Tags: tt.existingTags,
609+
},
610+
}, nil
611+
},
612+
ServiceRegisterFunc: func(reg *api.AgentServiceRegistration) error {
613+
return tt.mockRegisterErr
614+
},
615+
},
616+
}
617+
tagit := New(mockConsulClient, mockExecutor, "test-service", "echo test", 30*time.Second, "tag")
618+
619+
err := tagit.updateServiceTags()
620+
if (err != nil) != tt.expectError {
621+
t.Errorf("updateServiceTags() error = %v, wantErr %v", err, tt.expectError)
622+
}
623+
})
624+
}
625+
}

0 commit comments

Comments
 (0)