-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtagger_test.go
111 lines (103 loc) · 2.82 KB
/
tagger_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
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGetTagDiff(t *testing.T) {
tests := map[string]struct {
diff Diff
have, want []string
}{
"equal": {
have: []string{"apple", "banana", "orange"},
want: []string{"apple", "banana", "orange"},
diff: Diff{
Added: nil,
Removed: nil,
},
},
"missing": {
have: []string{"apple", "orange"},
want: []string{"apple", "banana", "orange"},
diff: Diff{
Added: []string{"banana"},
Removed: nil,
},
},
"extra": {
have: []string{"apple", "banana", "orange", "pickle", "onion"},
want: []string{"apple", "banana", "orange"},
diff: Diff{
Added: nil,
Removed: []string{"onion", "pickle"},
},
},
"empty": {
have: nil,
want: []string{"apple", "banana", "orange"},
diff: Diff{
Added: []string{"apple", "banana", "orange"},
Removed: nil,
},
},
"mixed": {
have: []string{"aplpe", "pickle", "BaNaNa", "foobar", "orange", "asdf", "testing123"},
want: []string{"apple", "banana", "orange"},
diff: Diff{
Added: []string{"apple", "banana"},
Removed: []string{"BaNaNa", "aplpe", "asdf", "foobar", "pickle", "testing123"},
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got := getTagDiff(tc.have, tc.want)
require.Equal(t, tc.diff, got)
})
}
}
func TestGetNewTags(t *testing.T) {
tests := map[string]struct {
label string
tags []string
rules []TagRule
desiredTags []string
found bool
}{
"no_match": {
label: "foo",
tags: []string{"apple", "banana"},
rules: []TagRule{{Regex: "blah", Tags: TagSet{Present: []string{"apple"}, Absent: []string{"asdf"}}}},
desiredTags: nil,
found: false,
},
"no_rules": {
label: "foo",
tags: []string{"apple", "banana"},
rules: []TagRule{},
desiredTags: []string{"apple", "banana"},
found: false,
},
"updated_rules": {
label: "foo",
tags: []string{"orange", "asdf", "banana"},
rules: []TagRule{{Regex: "foo", Tags: TagSet{Present: []string{"apple"}, Absent: []string{"asdf"}}}},
desiredTags: []string{"apple", "banana", "orange"},
found: true,
},
"regex_capture_groups": {
label: "foo01_us-southeast_testing",
tags: []string{"orange", "asdf", "banana"},
rules: []TagRule{{Regex: "^foo\\d{2}_(.+)_(.+)$", Tags: TagSet{Present: []string{"region=$1", "environment=$2"}, Absent: nil}}},
desiredTags: []string{"asdf", "banana", "environment=testing", "orange", "region=us-southeast"},
found: true,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, found := getNewTags(tc.label, tc.tags, tc.rules)
require.Equal(t, tc.found, found)
require.Equal(t, tc.desiredTags, got)
})
}
}