-
Notifications
You must be signed in to change notification settings - Fork 2
/
component_test.go
225 lines (202 loc) · 4.65 KB
/
component_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package versionbundle
import (
"testing"
)
func Test_Component_Validate(t *testing.T) {
testCases := []struct {
Component Component
ErrorMatcher func(err error) bool
}{
// Test 0 ensures an empty component is not valid.
{
Component: Component{},
ErrorMatcher: IsInvalidComponent,
},
// Test 1 is the same as 0 but with initialized properties.
{
Component: Component{
Name: "",
Version: "",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 2 ensures a component missing a version is not valid.
{
Component: Component{
Name: "kubernetes",
Version: "",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 3 ensures a non-semver version is invalid.
{
Component: Component{
Name: "kubernetes",
Version: "foo",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 4 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 5 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 6 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.x",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 7 is the same as 11 but with a different version.
//
// NOTE using the wildcard for minor does not allow a wildcard for patch.
{
Component: Component{
Name: "kubernetes",
Version: "1.x.2",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 8 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.x.x.x",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 9 ensures using the wildcard for the major version is not valid.
{
Component: Component{
Name: "kubernetes",
Version: "x.x.x",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 10 is the same as 9 but with different input.
{
Component: Component{
Name: "kubernetes",
Version: "x.3.9",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 11 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.3.9.4",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 12 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.3.foo",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 13 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "1.bar.3",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 14 is the same as 11 but with a different version.
{
Component: Component{
Name: "kubernetes",
Version: "kubernetes.14.3",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 15 is the same as 11 but with a different version.
//
// NOTE negative version numbers are not allowed.
{
Component: Component{
Name: "kubernetes",
Version: "1.-3.7",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 16 is the same as 13 but with a different version.
//
// NOTE negative version numbers are not allowed.
{
Component: Component{
Name: "kubernetes",
Version: "-1.3.7",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 17 is the same as 13 but with a different version.
//
// NOTE negative version numbers are not allowed.
{
Component: Component{
Name: "kubernetes",
Version: "1.3.-7",
},
ErrorMatcher: IsInvalidComponent,
},
// Test 18 ensures a valid component does not throw an error.
{
Component: Component{
Name: "kubernetes",
Version: "1.3.9",
},
ErrorMatcher: nil,
},
// Test 19 ensures a valid component does not throw an error.
{
Component: Component{
Name: "kubernetes",
Version: "0.0.0",
},
ErrorMatcher: nil,
},
// Test 20 ensures a valid component does not throw an error.
{
Component: Component{
Name: "kubernetes",
Version: "11.3.785",
},
ErrorMatcher: nil,
},
// Test 21 ensures a component with pre-release version is valid.
{
Component: Component{
Name: "kubernetes",
Version: "11.3.785-pre-release",
},
ErrorMatcher: nil,
},
}
for i, tc := range testCases {
err := tc.Component.Validate()
if tc.ErrorMatcher != nil {
if !tc.ErrorMatcher(err) {
t.Fatalf("test %d expected %#v got %#v", i, true, false)
}
} else if err != nil {
t.Fatalf("test %d expected %#v got %#v", i, nil, err)
}
}
}