Skip to content

Commit ecefa11

Browse files
authored
Merge pull request #10 from ncode/juliano/moar_tests
tests for updateServiceTags
2 parents cf9768f + 6342b05 commit ecefa11

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,4 @@ sequenceDiagram
5353

5454
## Todo
5555

56-
- [ ] Adds support for multiple services (currently only supports one service)
5756
- [ ] Adds a systemd unit file generator

cmd/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19+
"context"
1920
"fmt"
2021
"os"
2122
"time"
@@ -39,6 +40,7 @@ example: tagit run -s my-super-service -x '/tmp/tag-role.sh'
3940
script := cmd.PersistentFlags().Lookup("script").Value.String()
4041
tagPrefix := cmd.PersistentFlags().Lookup("tag-prefix").Value.String()
4142
interval := cmd.PersistentFlags().Lookup("interval").Value.String()
43+
ctx := context.Background()
4244

4345
if serviceID == "" {
4446
fmt.Println("service-id is required")
@@ -71,7 +73,7 @@ example: tagit run -s my-super-service -x '/tmp/tag-role.sh'
7173
}
7274

7375
t := tagit.New(tagit.NewConsulAPIWrapper(consulClient), &tagit.CmdExecutor{}, serviceID, script, validInterval, tagPrefix)
74-
t.Run()
76+
t.Run(ctx)
7577
},
7678
}
7779

pkg/tagit/tagit.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tagit
22

33
import (
4+
"context"
45
"fmt"
56
"os/exec"
67
"strings"
@@ -77,16 +78,22 @@ func New(consulClient ConsulClient, commandExecutor CommandExecutor, serviceID s
7778
}
7879

7980
// Run will run the tagit flow and tag consul services based on the script output
80-
func (t *TagIt) Run() {
81+
func (t *TagIt) Run(ctx context.Context) {
82+
ticker := time.NewTicker(t.Interval)
8183
for {
82-
err := t.updateServiceTags()
83-
if err != nil {
84-
log.WithFields(log.Fields{
85-
"service": t.ServiceID,
86-
"error": err,
87-
}).Error("error updating service tags")
84+
select {
85+
case <-ctx.Done():
86+
ticker.Stop()
87+
return
88+
case <-ticker.C:
89+
err := t.updateServiceTags()
90+
if err != nil {
91+
log.WithFields(log.Fields{
92+
"service": t.ServiceID,
93+
"error": err,
94+
}).Error("error updating service tags")
95+
}
8896
}
89-
time.Sleep(t.Interval)
9097
}
9198
}
9299

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)