Skip to content

Commit 0d0324d

Browse files
committed
remode old function and adds another test
1 parent d922a37 commit 0d0324d

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

pkg/tagit/tagit.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -147,34 +147,6 @@ func (t *TagIt) parseScriptOutput(output []byte) []string {
147147
return tags
148148
}
149149

150-
// CleanupServiceTags cleans up the service tags added by tagit.
151-
func (t *TagIt) CleanupServiceTags() error {
152-
service, err := t.getService()
153-
if err != nil {
154-
return err
155-
}
156-
registration := t.copyServiceToRegistration(service)
157-
log.WithFields(log.Fields{
158-
"service": t.ServiceID,
159-
"tags": registration.Tags,
160-
}).Info("current service tags")
161-
162-
filteredTags, tagged := t.excludeTagged(registration.Tags)
163-
if tagged {
164-
log.WithFields(log.Fields{
165-
"service": t.ServiceID,
166-
"tags": filteredTags,
167-
}).Info("updating service tags")
168-
registration.Tags = filteredTags
169-
err = t.client.Agent().ServiceRegister(registration)
170-
if err != nil {
171-
return err
172-
}
173-
}
174-
175-
return err
176-
}
177-
178150
// Copy *api.AgentService to *api.AgentServiceRegistration
179151
func (t *TagIt) copyServiceToRegistration(service *api.AgentService) *api.AgentServiceRegistration {
180152
return &api.AgentServiceRegistration{

pkg/tagit/tagit_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"reflect"
66
"slices"
7+
"strings"
78
"testing"
89
"time"
910

@@ -519,3 +520,45 @@ func TestNewConsulAPIWrapper(t *testing.T) {
519520
_, isConsulAgent := wrapper.Agent().(ConsulAgent)
520521
assert.True(t, isConsulAgent, "Wrapper's Agent method does not return a ConsulAgent")
521522
}
523+
524+
func TestCmdExecutor_Execute(t *testing.T) {
525+
tests := []struct {
526+
name string
527+
command string
528+
wantOutput string
529+
expectError bool
530+
}{
531+
{
532+
name: "Echo Command",
533+
command: "echo test",
534+
wantOutput: "test\n",
535+
expectError: false,
536+
},
537+
{
538+
name: "Invalid Command",
539+
command: "invalidcommand",
540+
expectError: true,
541+
},
542+
}
543+
544+
executor := &CmdExecutor{}
545+
546+
for _, tt := range tests {
547+
t.Run(tt.name, func(t *testing.T) {
548+
output, err := executor.Execute(tt.command)
549+
550+
if tt.expectError {
551+
if err == nil {
552+
t.Errorf("Expected an error for command: %s, but got none", tt.command)
553+
}
554+
} else {
555+
if err != nil {
556+
t.Errorf("Did not expect an error for command: %s, but got: %v", tt.command, err)
557+
}
558+
if strings.TrimSpace(string(output)) != strings.TrimSpace(tt.wantOutput) {
559+
t.Errorf("Unexpected output for command: %s. Expected: %s, got: %s", tt.command, tt.wantOutput, string(output))
560+
}
561+
}
562+
})
563+
}
564+
}

0 commit comments

Comments
 (0)