/
check_tags_test.go
141 lines (135 loc) · 3.04 KB
/
check_tags_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package trapcheck
import (
"context"
"fmt"
"io"
"log"
"reflect"
"testing"
"github.com/circonus-labs/go-apiclient"
)
func TestTrapCheck_UpdateCheckTags(t *testing.T) {
tc := &TrapCheck{}
tc.Log = &LogWrapper{
Log: log.New(io.Discard, "", log.LstdFlags),
Debug: false,
}
tests := []struct {
client API
bundle *apiclient.CheckBundle
want *apiclient.CheckBundle
name string
newTags []string
wantErr bool
}{
{
name: "new tag",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"bar"},
want: &apiclient.CheckBundle{
Tags: []string{"foo", "bar"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "new tag, ignore blank",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"bar", ""},
want: &apiclient.CheckBundle{
Tags: []string{"foo", "bar"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "modify tag",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo:bar"},
},
newTags: []string{"foo:baz"},
want: &apiclient.CheckBundle{
Tags: []string{"foo:baz"},
},
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "no change",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo"},
},
newTags: []string{"foo"},
want: nil,
wantErr: false,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return cfg, nil
},
},
},
{
name: "invalid (nil check bundle)",
bundle: nil,
wantErr: true,
},
{
name: "no tags",
bundle: &apiclient.CheckBundle{},
want: nil,
wantErr: false,
},
{
name: "api error",
bundle: &apiclient.CheckBundle{Tags: []string{"foo"}},
newTags: []string{"bar"},
want: nil,
wantErr: true,
client: &APIMock{
UpdateCheckBundleFunc: func(cfg *apiclient.CheckBundle) (*apiclient.CheckBundle, error) {
return nil, fmt.Errorf("api error 500")
},
},
},
{
name: "no update, multiple tag formats",
bundle: &apiclient.CheckBundle{
Tags: []string{"foo:bar", "baz:", ":bar", "qux"},
},
newTags: []string{"foo:bar", "baz:", ":bar", "qux"},
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tc.client = tt.client
tc.checkBundle = tt.bundle
got, err := tc.UpdateCheckTags(context.Background(), tt.newTags)
if (err != nil) != tt.wantErr {
t.Errorf("TrapCheck.UpdateCheckTags() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrapCheck.UpdateCheckTags() = %v, want %v", got, tt.want)
}
})
}
}