forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
580 lines (513 loc) · 18 KB
/
config_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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
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 testgrids
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/mail"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/GoogleCloudPlatform/testgrid/config"
config_pb "github.com/GoogleCloudPlatform/testgrid/pb/config"
prow_config "k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/flagutil"
"k8s.io/test-infra/testgrid/pkg/configurator/configurator"
"k8s.io/test-infra/testgrid/pkg/configurator/options"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
)
type SQConfig struct {
Data map[string]string `yaml:"data,omitempty"`
}
var (
companies = []string{
"canonical",
"cos",
"cri-o",
"istio",
"googleoss",
"google",
"kopeio",
"redhat",
"ibm",
"vmware",
"gardener",
"jetstack",
"kyma",
"kubevirt",
}
orgs = []string{
"conformance",
"kops",
"presubmits",
"sig",
"wg",
"provider",
"kubernetes-clients",
"kcp",
}
dashboardPrefixes = [][]string{orgs, companies}
// gcs prefixes populated by the kubernetes prow instance
prowGcsPrefixes = []string{
"kubernetes-jenkins/logs/",
"kubernetes-jenkins/pr-logs/directory/",
}
)
var defaultInputs options.MultiString = []string{"../../testgrids"}
var prowPath = flag.String("prow-config", "../../../config/prow/config.yaml", "Path to prow config")
var jobPath = flag.String("job-config", "../../jobs", "Path to prow job config")
var defaultYAML = flag.String("default", "../../testgrids/default.yaml", "Default yaml for testgrid")
var inputs options.MultiString
var protoPath = flag.String("config", "", "Path to TestGrid config proto")
// Shared testgrid config, loaded at TestMain.
var cfg *config_pb.Configuration
// Shared prow config, loaded at Test Main
var prowConfig *prow_config.Config
func TestMain(m *testing.M) {
flag.Var(&inputs, "yaml", "comma-separated list of input YAML files or directories")
flag.Parse()
if *protoPath == "" {
if len(inputs) == 0 {
inputs = defaultInputs
}
// Generate proto from testgrid config
tmpDir, err := ioutil.TempDir("", "testgrid-config-test")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer os.RemoveAll(tmpDir)
tmpFile := path.Join(tmpDir, "test-proto")
opt := options.Options{
Inputs: inputs,
ProwConfig: configflagutil.ConfigOptions{
ConfigPath: *prowPath,
JobConfigPath: *jobPath,
},
DefaultYAML: *defaultYAML,
Output: flagutil.NewStringsBeenSet(tmpFile),
Oneshot: true,
StrictUnmarshal: true,
}
if err := configurator.RealMain(&opt); err != nil {
fmt.Println(err)
os.Exit(1)
}
protoPath = &tmpFile
}
var err error
cfg, err = config.Read(context.Background(), *protoPath, nil)
if err != nil {
fmt.Printf("Could not load config: %v\n", err)
os.Exit(1)
}
prowConfig, err = prow_config.Load(*prowPath, *jobPath, nil, "")
if err != nil {
fmt.Printf("Could not load prow configs: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestConfig(t *testing.T) {
// testgroup - occurrence map, validate testgroups
testgroupMap := make(map[string]int32)
for testgroupidx, testgroup := range cfg.TestGroups {
// All testgroup must have a name and a query
if testgroup.Name == "" || testgroup.GcsPrefix == "" {
t.Errorf("Testgroup #%v (Name: '%v', GcsPrefix: '%v'): - Must have a name and gcs_prefix",
testgroupidx, testgroup.Name, testgroup.GcsPrefix)
}
// All testgroup must not have duplicated names
if testgroupMap[testgroup.Name] > 0 {
t.Errorf("Duplicated Testgroup: %v", testgroup.Name)
} else {
testgroupMap[testgroup.Name] = 1
}
t.Run("Testgroup "+testgroup.Name, func(t *testing.T) {
if !testgroup.IsExternal {
t.Error("IsExternal must be true")
}
for hIdx, header := range testgroup.ColumnHeader {
if header.ConfigurationValue == "" {
t.Errorf("Column Header %d is empty", hIdx)
}
}
for _, prowGcsPrefix := range prowGcsPrefixes {
if strings.Contains(testgroup.GcsPrefix, prowGcsPrefix) {
// The expectation is that testgroup.Name is the name of a Prow job and the GCSPrefix
// follows the convention kubernetes-jenkins/logs/.../jobName
// The final part of the prefix should be the job name.
expected := filepath.Join(filepath.Dir(testgroup.GcsPrefix), testgroup.Name)
if expected != testgroup.GcsPrefix {
t.Errorf("GcsPrefix: Got %s; Want %s", testgroup.GcsPrefix, expected)
}
break // out of prowGcsPrefix for loop
}
}
if testgroup.TestNameConfig != nil {
if testgroup.TestNameConfig.NameFormat == "" {
t.Error("Empty NameFormat")
}
if got, want := len(testgroup.TestNameConfig.NameElements), strings.Count(testgroup.TestNameConfig.NameFormat, "%"); got != want {
t.Errorf("TestNameConfig has %d elements, format %s wants %d", got, testgroup.TestNameConfig.NameFormat, want)
}
}
// All PR testgroup has num_columns_recent equals 20
if strings.HasPrefix(testgroup.GcsPrefix, "kubernetes-jenkins/pr-logs/directory/") {
if testgroup.NumColumnsRecent < 20 {
t.Errorf("presubmit num_columns_recent want >=20, got %d", testgroup.NumColumnsRecent)
}
}
})
}
// dashboard name set
dashboardSet := sets.NewString()
for dashboardidx, dashboard := range cfg.Dashboards {
// All dashboard must have a name
if dashboard.Name == "" {
t.Errorf("Dashboard %v: - Must have a name", dashboardidx)
}
found := false
for _, kind := range dashboardPrefixes {
for _, prefix := range kind {
if strings.HasPrefix(dashboard.Name, prefix) || dashboard.Name == prefix {
found = true
break
}
}
if found {
break
}
}
if !found {
t.Errorf("Dashboard %v: must prefix with one of: %v", dashboard.Name, dashboardPrefixes)
}
// All dashboard must not have duplicated names
if dashboardSet.Has(dashboard.Name) {
t.Errorf("Duplicated dashboard: %v", dashboard.Name)
} else {
dashboardSet.Insert(dashboard.Name)
}
// All dashboard must have at least one tab
if len(dashboard.DashboardTab) == 0 {
t.Errorf("Dashboard %v: - Must have more than one dashboardtab", dashboard.Name)
}
// dashboardtabSet is a set that checks duplicate tab name within each dashboard
dashboardtabSet := sets.NewString()
// dashboardtestgroupSet is a set that checks duplicate testgroups within each dashboard
dashboardtestgroupSet := sets.NewString()
// All notifications in dashboard must have a summary
if len(dashboard.Notifications) != 0 {
for notificationindex, notification := range dashboard.Notifications {
if notification.Summary == "" {
t.Errorf("Notification %v in dashboard %v: - Must have a summary", notificationindex, dashboard.Name)
}
}
}
for tabindex, dashboardtab := range dashboard.DashboardTab {
// All dashboardtab must have a name and a testgroup
if dashboardtab.Name == "" || dashboardtab.TestGroupName == "" {
t.Errorf("Dashboard %v, tab %v: - Must have a name and a testgroup name", dashboard.Name, tabindex)
}
// All dashboardtab within a dashboard must not have duplicated names
if dashboardtabSet.Has(dashboardtab.Name) {
t.Errorf("Duplicated name in dashboard %s: %v", dashboard.Name, dashboardtab.Name)
} else {
dashboardtabSet.Insert(dashboardtab.Name)
}
// All dashboardtab within a dashboard must not have duplicated testgroupnames
if dashboardtestgroupSet.Has(dashboardtab.TestGroupName) {
t.Errorf("Duplicated testgroupnames in dashboard %s: %v", dashboard.Name, dashboardtab.TestGroupName)
} else {
dashboardtestgroupSet.Insert(dashboardtab.TestGroupName)
}
// All testgroup in dashboard must be defined in testgroups
if testgroupMap[dashboardtab.TestGroupName] == 0 {
t.Errorf("Dashboard %v, tab %v: - Testgroup %v must be defined first",
dashboard.Name, dashboardtab.Name, dashboardtab.TestGroupName)
} else {
testgroupMap[dashboardtab.TestGroupName]++
}
}
}
// No dup of dashboard groups, and no dup dashboard in a dashboard group
groupSet := sets.NewString()
dashboardToGroupMap := make(map[string]string)
for idx, dashboardGroup := range cfg.DashboardGroups {
// All dashboard must have a name
if dashboardGroup.Name == "" {
t.Errorf("DashboardGroup %v: - DashboardGroup must have a name", idx)
}
found := false
for _, kind := range dashboardPrefixes {
for _, prefix := range kind {
if strings.HasPrefix(dashboardGroup.Name, prefix) || prefix == dashboardGroup.Name {
found = true
break
}
}
if found {
break
}
}
if !found {
t.Errorf("Dashboard group %v: must prefix with one of: %v", dashboardGroup.Name, dashboardPrefixes)
}
// All dashboardgroup must not have duplicated names
if groupSet.Has(dashboardGroup.Name) {
t.Errorf("Duplicated dashboard: %v", dashboardGroup.Name)
} else {
groupSet.Insert(dashboardGroup.Name)
}
if dashboardSet.Has(dashboardGroup.Name) {
t.Errorf("%v is both a dashboard and dashboard group name.", dashboardGroup.Name)
}
for _, dashboard := range dashboardGroup.DashboardNames {
// All dashboard must not have duplicated names
if assignedGroup, ok := dashboardToGroupMap[dashboard]; ok {
t.Errorf("Duplicated dashboard %v in dashboard group %v and %v", dashboard, assignedGroup, dashboardGroup.Name)
} else {
dashboardToGroupMap[dashboard] = dashboardGroup.Name
}
if !dashboardSet.Has(dashboard) {
t.Errorf("Dashboard %v needs to be defined before adding to a dashboard group!", dashboard)
}
if !strings.HasPrefix(dashboard, dashboardGroup.Name) {
t.Errorf("Dashboard %v in group %v must have the group name as a prefix", dashboard, dashboardGroup.Name)
}
}
}
// Dashboards that match this dashboard group's prefix should be a part of it, unless this group is the prefix of the assigned group
// (e.g. knative and knative-sandbox).
for thisGroup := range groupSet {
for dashboard := range dashboardSet {
if strings.HasPrefix(dashboard, thisGroup+"-") {
assignedGroup, ok := dashboardToGroupMap[dashboard]
if !ok {
t.Errorf("Dashboard %v should be in dashboard_group %v", dashboard, thisGroup)
} else if assignedGroup != thisGroup && !strings.HasPrefix(assignedGroup, thisGroup) {
t.Errorf("Dashboard %v should be in dashboard_group %v instead of dashboard_group %v", dashboard, thisGroup, assignedGroup)
}
}
}
}
// All Testgroup should be mapped to one or more tabs
missedTestgroups := false
for testgroupname, occurrence := range testgroupMap {
if occurrence == 1 {
t.Errorf("Testgroup %v - defined but not used in any dashboards.", testgroupname)
missedTestgroups = true
}
}
if missedTestgroups {
t.Logf("Note: Testgroups are automatically defined for postsubmits and periodics.")
t.Logf("Testgroups can be added to a dashboard either by using the `testgrid-dashboards` annotation on the prowjob, or by adding them to testgrid/config.yaml")
}
}
// TODO: These are all repos that don't have their presubmits in testgrid.
// Convince sig leads or subproject owners this is a bad idea and whittle this down
// to just kubernetes-security/
// Tracking issue: https://github.com/kubernetes/test-infra/issues/18159
var noPresubmitsInTestgridPrefixes = []string{
"containerd/cri",
"GoogleCloudPlatform/k8s-multicluster-ingress",
"kubernetes-sigs/cluster-capacity",
"kubernetes-sigs/gcp-filestore-csi-driver",
"kubernetes-sigs/kind",
"kubernetes-sigs/kubetest2",
"kubernetes-sigs/oci-proxy",
"kubernetes-sigs/kubebuilder-declarative-pattern",
"kubernetes-sigs/scheduler-plugins",
"kubernetes-sigs/service-catalog",
"kubernetes-sigs/sig-storage-local-static-provisioner",
"kubernetes-sigs/slack-infra",
"kubernetes-sigs/testing_frameworks",
"kubernetes/client-go",
"kubernetes/cloud-provider-openstack",
"kubernetes/dns",
"kubernetes/enhancements",
"kubernetes/ingress-gce",
"kubernetes/kubeadm",
"kubernetes/minikube",
// This is the one entry that should be here
"kubernetes-security/",
}
func hasAnyPrefix(s string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
// A job is merge-blocking if it:
// - is not optional
// - reports (aka does not skip reporting)
// - always runs OR runs if some path changed
func isMergeBlocking(job prow_config.Presubmit) bool {
return !job.Optional && !job.SkipReport && (job.AlwaysRun || job.RunIfChanged != "" || job.SkipIfOnlyChanged != "")
}
// All jobs in presubmits-kuberentes-blocking must be merge-blocking for kubernetes/kubernetes
// All jobs that are merge-blocking for kubernetes/kubernetes must be in presubmits-kubernetes-blocking
func TestPresubmitsKubernetesDashboards(t *testing.T) {
var dashboard *config_pb.Dashboard
repo := "kubernetes/kubernetes"
dash := "presubmits-kubernetes-blocking"
for _, d := range cfg.Dashboards {
if d.Name == dash {
dashboard = d
}
}
if dashboard == nil {
t.Fatalf("Missing dashboard: %s", dash)
}
testgroups := make(map[string]bool)
for _, tab := range dashboard.DashboardTab {
testgroups[tab.TestGroupName] = false
}
jobs := make(map[string]bool)
for _, job := range prowConfig.AllStaticPresubmits([]string{repo}) {
if isMergeBlocking(job) {
jobs[job.Name] = false
}
}
for job, seen := range jobs {
if _, ok := testgroups[job]; !seen && !ok {
t.Errorf("%s: job is merge-blocking for %s but missing from %s", job, repo, dash)
}
jobs[job] = true
}
for tg, seen := range testgroups {
if _, ok := jobs[tg]; !seen && !ok {
t.Errorf("%s: should not be in %s because not actually merge-blocking for %s", tg, dash, repo)
}
testgroups[tg] = true
}
}
func TestKubernetesProwInstanceJobsMustHaveMatchingTestgridEntries(t *testing.T) {
jobs := make(map[string]bool)
for repo, presubmits := range prowConfig.PresubmitsStatic {
// Assume that all jobs in the exceptionList are valid
if hasAnyPrefix(repo, noPresubmitsInTestgridPrefixes) {
for _, job := range presubmits {
jobs[job.Name] = true
}
continue
}
for _, job := range presubmits {
jobs[job.Name] = false
}
}
for _, job := range prowConfig.AllStaticPostsubmits([]string{}) {
jobs[job.Name] = false
}
for _, job := range prowConfig.AllPeriodics() {
jobs[job.Name] = false
}
// Ignore any test groups that get their results from a gcs prefix
// that is not populated by the kubernetes prow instance
testgroups := make(map[string]bool)
for _, testgroup := range cfg.TestGroups {
for _, prowGcsPrefix := range prowGcsPrefixes {
if strings.Contains(testgroup.GcsPrefix, prowGcsPrefix) {
// The convention is that the job name is the final part of the GcsPrefix
job := filepath.Base(testgroup.GcsPrefix)
testgroups[job] = false
break // to next testgroup
}
}
}
// Each job running in the kubernetes prow instance must have an
// identically named test_groups entry in the kubernetes testgrid config
for job := range jobs {
if _, ok := testgroups[job]; ok {
testgroups[job] = true
jobs[job] = true
}
}
// Conclusion
for job, valid := range jobs {
if !valid {
t.Errorf("Job %v does not have a matching testgrid testgroup", job)
}
}
for testgroup, valid := range testgroups {
if !valid {
t.Errorf("Testgrid group %v is supposed to be moved to have their presubmits in testgrid. See this issue: https://github.com/kubernetes/test-infra/issues/18159", testgroup)
}
}
}
func TestReleaseBlockingJobsMustHaveTestgridDescriptions(t *testing.T) {
// TODO(spiffxp): start with master, enforce for all release branches
re := regexp.MustCompile("^sig-release-master-(blocking|informing)$")
for _, dashboard := range cfg.Dashboards {
if !re.MatchString(dashboard.Name) {
continue
}
suffix := re.FindStringSubmatch(dashboard.Name)[1]
for _, dashboardtab := range dashboard.DashboardTab {
intro := fmt.Sprintf("dashboard_tab %v/%v is release-%v", dashboard.Name, dashboardtab.Name, suffix)
if dashboardtab.Name == "" {
t.Errorf("%v: - Must have a name", intro)
}
if dashboardtab.TestGroupName == "" {
t.Errorf("%v: - Must have a test_group_name", intro)
}
if dashboardtab.Description == "" {
t.Errorf("%v: - Must have a description", intro)
}
// TODO(spiffxp): enforce for informing as well
if suffix == "informing" {
if !strings.HasPrefix(dashboardtab.Description, "OWNER: ") {
t.Logf("NOTICE: %v: - Must have a description that starts with OWNER: ", intro)
}
if dashboardtab.AlertOptions == nil {
t.Logf("NOTICE: %v: - Must have alert_options (ensure informing dashboard is listed first in testgrid-dashboards)", intro)
} else if dashboardtab.AlertOptions.AlertMailToAddresses == "" {
t.Logf("NOTICE: %v: - Must have alert_options.alert_mail_to_addresses", intro)
}
} else {
if dashboardtab.AlertOptions == nil {
t.Errorf("%v: - Must have alert_options (ensure blocking dashboard is listed first in testgrid-dashboards)", intro)
} else if dashboardtab.AlertOptions.AlertMailToAddresses == "" {
t.Errorf("%v: - Must have alert_options.alert_mail_to_addresses", intro)
}
}
}
}
}
func TestNoEmpyMailToAddresses(t *testing.T) {
for _, dashboard := range cfg.Dashboards {
for _, dashboardtab := range dashboard.DashboardTab {
intro := fmt.Sprintf("dashboard_tab %v/%v", dashboard.Name, dashboardtab.Name)
if dashboardtab.AlertOptions != nil {
if dashboardtab.AlertOptions.AlertMailToAddresses == "" {
continue
}
mails := strings.Split(dashboardtab.AlertOptions.AlertMailToAddresses, ",")
for _, m := range mails {
_, err := mail.ParseAddress(m)
if err != nil {
t.Errorf("%v: - invalid alert_mail_to_address '%v': %v", intro, m, err)
}
}
}
}
}
}