forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 11
/
plugins_test.go
383 lines (349 loc) · 9.7 KB
/
plugins_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugins
import (
"errors"
"reflect"
"testing"
)
func TestGetPlugins(t *testing.T) {
var testcases = []struct {
name string
pluginMap map[string][]string // this is read from the plugins.yaml file typically.
owner string
repo string
expectedPlugins []string
}{
{
name: "All plugins enabled for org should be returned for any org/repo query",
pluginMap: map[string][]string{
"org1": {"plugin1", "plugin2"},
},
owner: "org1",
repo: "repo",
expectedPlugins: []string{"plugin1", "plugin2"},
},
{
name: "All plugins enabled for org/repo should be returned for a org/repo query",
pluginMap: map[string][]string{
"org1": {"plugin1", "plugin2"},
"org1/repo": {"plugin3"},
},
owner: "org1",
repo: "repo",
expectedPlugins: []string{"plugin1", "plugin2", "plugin3"},
},
{
name: "Plugins for org1/repo should not be returned for org2/repo query",
pluginMap: map[string][]string{
"org1": {"plugin1", "plugin2"},
"org1/repo": {"plugin3"},
},
owner: "org2",
repo: "repo",
expectedPlugins: nil,
},
{
name: "Plugins for org1 should not be returned for org2/repo query",
pluginMap: map[string][]string{
"org1": {"plugin1", "plugin2"},
"org2/repo": {"plugin3"},
},
owner: "org2",
repo: "repo",
expectedPlugins: []string{"plugin3"},
},
}
for _, tc := range testcases {
pa := ConfigAgent{configuration: &Configuration{Plugins: tc.pluginMap}}
plugins := pa.getPlugins(tc.owner, tc.repo)
if len(plugins) != len(tc.expectedPlugins) {
t.Errorf("Different number of plugins for case \"%s\". Got %v, expected %v", tc.name, plugins, tc.expectedPlugins)
} else {
for i := range plugins {
if plugins[i] != tc.expectedPlugins[i] {
t.Errorf("Different plugin for case \"%s\": Got %v expected %v", tc.name, plugins, tc.expectedPlugins)
}
}
}
}
}
func TestValidateExternalPlugins(t *testing.T) {
tests := []struct {
name string
plugins map[string][]ExternalPlugin
expectedErr error
}{
{
name: "valid config",
plugins: map[string][]ExternalPlugin{
"kubernetes/test-infra": {
{
Name: "cherrypick",
},
{
Name: "configupdater",
},
{
Name: "tetris",
},
},
"kubernetes": {
{
Name: "coffeemachine",
},
{
Name: "blender",
},
},
},
expectedErr: nil,
},
{
name: "invalid config",
plugins: map[string][]ExternalPlugin{
"kubernetes/test-infra": {
{
Name: "cherrypick",
},
{
Name: "configupdater",
},
{
Name: "tetris",
},
},
"kubernetes": {
{
Name: "coffeemachine",
},
{
Name: "tetris",
},
},
},
expectedErr: errors.New("invalid plugin configuration:\n\texternal plugins [tetris] are duplicated for kubernetes/test-infra and kubernetes"),
},
}
for _, test := range tests {
t.Logf("Running scenario %q", test.name)
err := validateExternalPlugins(test.plugins)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("unexpected error: %v, expected: %v", err, test.expectedErr)
}
}
}
func TestSetDefault_Maps(t *testing.T) {
cases := []struct {
name string
config ConfigUpdater
expected map[string]ConfigMapSpec
}{
{
name: "nothing",
expected: map[string]ConfigMapSpec{
"prow/config.yaml": {Name: "config"},
"prow/plugins.yaml": {Name: "plugins"},
},
},
{
name: "basic",
config: ConfigUpdater{
Maps: map[string]ConfigMapSpec{
"hello.yaml": {Name: "my-cm"},
"world.yaml": {Name: "you-cm"},
},
},
expected: map[string]ConfigMapSpec{
"hello.yaml": {Name: "my-cm"},
"world.yaml": {Name: "you-cm"},
},
},
{
name: "deprecated config",
config: ConfigUpdater{
ConfigFile: "foo.yaml",
},
expected: map[string]ConfigMapSpec{
"foo.yaml": {Name: "config"},
"prow/plugins.yaml": {Name: "plugins"},
},
},
{
name: "deprecated plugins",
config: ConfigUpdater{
PluginFile: "bar.yaml",
},
expected: map[string]ConfigMapSpec{
"bar.yaml": {Name: "plugins"},
"prow/config.yaml": {Name: "config"},
},
},
{
name: "deprecated both",
config: ConfigUpdater{
ConfigFile: "foo.yaml",
PluginFile: "bar.yaml",
},
expected: map[string]ConfigMapSpec{
"foo.yaml": {Name: "config"},
"bar.yaml": {Name: "plugins"},
},
},
{
name: "both current and deprecated",
config: ConfigUpdater{
Maps: map[string]ConfigMapSpec{
"config.yaml": {Name: "overwrite-config"},
"plugins.yaml": {Name: "overwrite-plugins"},
"unconflicting.yaml": {Name: "ignored"},
},
ConfigFile: "config.yaml",
PluginFile: "plugins.yaml",
},
expected: map[string]ConfigMapSpec{
"config.yaml": {Name: "overwrite-config"},
"plugins.yaml": {Name: "overwrite-plugins"},
"unconflicting.yaml": {Name: "ignored"},
},
},
}
for _, tc := range cases {
cfg := Configuration{
ConfigUpdater: tc.config,
}
cfg.setDefaults()
actual := cfg.ConfigUpdater.Maps
if len(actual) != len(tc.expected) {
t.Errorf("%s: actual and expected have different keys: %v %v", tc.name, actual, tc.expected)
continue
}
for k, n := range tc.expected {
if an := actual[k]; an != n {
t.Errorf("%s - %s: expected %s != actual %s", tc.name, k, n, an)
}
}
}
}
func TestSetTriggerDefaults(t *testing.T) {
tests := []struct {
name string
trustedOrg string
joinOrgURL string
expectedTrustedOrg string
expectedJoinOrgURL string
}{
{
name: "url defaults to org",
trustedOrg: "kubernetes",
joinOrgURL: "",
expectedTrustedOrg: "kubernetes",
expectedJoinOrgURL: "https://github.com/orgs/kubernetes/people",
},
{
name: "both org and url are set",
trustedOrg: "kubernetes",
joinOrgURL: "https://git.k8s.io/community/community-membership.md#member",
expectedTrustedOrg: "kubernetes",
expectedJoinOrgURL: "https://git.k8s.io/community/community-membership.md#member",
},
{
name: "only url is set",
trustedOrg: "",
joinOrgURL: "https://git.k8s.io/community/community-membership.md#member",
expectedTrustedOrg: "",
expectedJoinOrgURL: "https://git.k8s.io/community/community-membership.md#member",
},
{
name: "nothing is set",
trustedOrg: "",
joinOrgURL: "",
expectedTrustedOrg: "",
expectedJoinOrgURL: "",
},
}
for _, test := range tests {
c := &Configuration{
Triggers: []Trigger{
{
TrustedOrg: test.trustedOrg,
JoinOrgURL: test.joinOrgURL,
},
},
}
c.setDefaults()
if c.Triggers[0].TrustedOrg != test.expectedTrustedOrg {
t.Errorf("unexpected trusted_org: %s, expected: %s", c.Triggers[0].TrustedOrg, test.expectedTrustedOrg)
}
if c.Triggers[0].JoinOrgURL != test.expectedJoinOrgURL {
t.Errorf("unexpected join_org_url: %s, expected: %s", c.Triggers[0].JoinOrgURL, test.expectedJoinOrgURL)
}
}
}
func TestSetCherryPickUnapprovedDefaults(t *testing.T) {
defaultBranchRegexp := `^release-.*$`
defaultComment := `This PR is not for the master branch but does not have the ` + "`cherry-pick-approved`" + ` label. Adding the ` + "`do-not-merge/cherry-pick-not-approved`" + ` label.
To approve the cherry-pick, please assign the patch release manager for the release branch by writing ` + "`/assign @username`" + ` in a comment when ready.
The list of patch release managers for each release can be found [here](https://git.k8s.io/sig-release/release-managers.md).`
testcases := []struct {
name string
branchRegexp string
comment string
expectedBranchRegexp string
expectedComment string
}{
{
name: "none of branchRegexp and comment are set",
branchRegexp: "",
comment: "",
expectedBranchRegexp: defaultBranchRegexp,
expectedComment: defaultComment,
},
{
name: "only branchRegexp is set",
branchRegexp: `release-1.1.*$`,
comment: "",
expectedBranchRegexp: `release-1.1.*$`,
expectedComment: defaultComment,
},
{
name: "only comment is set",
branchRegexp: "",
comment: "custom comment",
expectedBranchRegexp: defaultBranchRegexp,
expectedComment: "custom comment",
},
{
name: "both branchRegexp and comment are set",
branchRegexp: `release-1.1.*$`,
comment: "custom comment",
expectedBranchRegexp: `release-1.1.*$`,
expectedComment: "custom comment",
},
}
for _, tc := range testcases {
c := &Configuration{
CherryPickUnapproved: CherryPickUnapproved{
BranchRegexp: tc.branchRegexp,
Comment: tc.comment,
},
}
c.setDefaults()
if c.CherryPickUnapproved.BranchRegexp != tc.expectedBranchRegexp {
t.Errorf("unexpected branchRegexp: %s, expected: %s", c.CherryPickUnapproved.BranchRegexp, tc.expectedBranchRegexp)
}
if c.CherryPickUnapproved.Comment != tc.expectedComment {
t.Errorf("unexpected comment: %s, expected: %s", c.CherryPickUnapproved.Comment, tc.expectedComment)
}
}
}