-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
184 lines (157 loc) · 5.72 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
/*
Copyright 2018 The Kubernetes Authors.
Copyright 2020 Thales Group
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 config
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config/org"
"k8s.io/test-infra/prow/github"
"github.com/ghodss/yaml"
)
func testDuplicates(list sets.String) error {
found := sets.String{}
dups := sets.String{}
all := list.List()
for _, i := range all {
if found.Has(i) {
dups.Insert(i)
}
found.Insert(i)
}
if n := len(dups); n > 0 {
return fmt.Errorf("%d duplicate names: %s", n, strings.Join(dups.List(), ", "))
}
return nil
}
func isSorted(list []string) bool {
items := make([]string, len(list))
for _, l := range list {
items = append(items, strings.ToLower(l))
}
return sort.StringsAreSorted(items)
}
func normalize(s sets.String) sets.String {
out := sets.String{}
for i := range s {
out.Insert(github.NormLogin(i))
}
return out
}
// testTeamMembers ensures that a user is not a maintainer and member at the same time,
// there are no duplicate names in the list and all users are org members.
func testTeamMembers(teams map[string]org.Team, admins sets.String, orgMembers sets.String, orgName string) []error {
var errs []error
for teamName, team := range teams {
teamMaintainers := sets.NewString(team.Maintainers...)
teamMembers := sets.NewString(team.Members...)
teamMaintainers = normalize(teamMaintainers)
teamMembers = normalize(teamMembers)
// check for non-admins in maintainers list
if nonAdminMaintainers := teamMaintainers.Difference(admins); len(nonAdminMaintainers) > 0 {
errs = append(errs, fmt.Errorf("The team %s in org %s has non-admins listed as maintainers; these users should be in the members list instead: %s", teamName, orgName, strings.Join(nonAdminMaintainers.List(), ",")))
}
// check for users in both maintainers and members
if both := teamMaintainers.Intersection(teamMembers); len(both) > 0 {
errs = append(errs, fmt.Errorf("The team %s in org %s has users in both maintainer admin and member roles: %s", teamName, orgName, strings.Join(both.List(), ", ")))
}
// check for duplicates
if err := testDuplicates(teamMaintainers); err != nil {
errs = append(errs, fmt.Errorf("The team %s in org %s has duplicate maintainers: %v", teamName, orgName, err))
}
if err := testDuplicates(teamMembers); err != nil {
errs = append(errs, fmt.Errorf("The team %s in org %s has duplicate members: %v", teamMembers, orgName, err))
}
// check if all are org members
if missing := teamMembers.Difference(orgMembers); len(missing) > 0 {
errs = append(errs, fmt.Errorf("The following members of team %s are not %s org members: %s", teamName, orgName, strings.Join(missing.List(), ", ")))
}
// check if admins are a regular member of team
if adminTeamMembers := teamMembers.Intersection(admins); len(adminTeamMembers) > 0 {
errs = append(errs, fmt.Errorf("The team %s in org %s has org admins listed as members; these users should be in the maintainers list instead, and cannot be on the members list: %s", teamName, orgName, strings.Join(adminTeamMembers.List(), ", ")))
}
// check if lists are sorted
if !isSorted(team.Maintainers) {
errs = append(errs, fmt.Errorf("The team %s in org %s has an unsorted list of maintainers", teamName, orgName))
}
if !isSorted(team.Members) {
errs = append(errs, fmt.Errorf("The team %s in org %s has an unsorted list of members", teamName, orgName))
}
if team.Children != nil {
errs = append(errs, testTeamMembers(team.Children, admins, orgMembers, orgName)...)
}
}
return errs
}
func TestAllOrgs(t *testing.T) {
raw, err := ioutil.ReadFile("config.yaml")
var cfg org.FullConfig
if err := yaml.Unmarshal(raw, &cfg); err != nil {
t.Fatalf("cannot read config.yaml from //config:gen-config.yaml: %v", err)
}
f, err := os.Open(".")
if err != nil {
t.Fatalf("cannot read config: %v", err)
}
infos, err := f.Readdir(0)
if err != nil {
t.Fatalf("cannot read subdirs: %v", err)
}
for _, i := range infos {
if !i.IsDir() {
continue
}
n := i.Name()
if strings.HasPrefix(n, "linux_") || strings.HasPrefix(n, "darwin_") {
continue
}
t.Run(n, func(t *testing.T) {
if _, ok := cfg.Orgs[n]; !ok {
t.Errorf("%s missing from generated config.yaml", n)
}
})
}
for _, org := range cfg.Orgs {
members := normalize(sets.NewString(org.Members...))
admins := normalize(sets.NewString(org.Admins...))
allOrgMembers := members.Union(admins)
if both := admins.Intersection(members); len(both) > 0 {
t.Errorf("users in both org admin and member roles for org '%s': %s", *org.Name, strings.Join(both.List(), ", "))
}
if !admins.Has("rlenferink-bot") {
t.Errorf("rlenferink-bot must be an admin")
}
if err := testDuplicates(admins); err != nil {
t.Errorf("duplicate admins: %v", err)
}
if err := testDuplicates(allOrgMembers); err != nil {
t.Errorf("duplicate members: %v", err)
}
if !isSorted(org.Admins) {
t.Errorf("admins for %s org are unsorted", *org.Name)
}
if !isSorted(org.Members) {
t.Errorf("members for %s org are unsorted", *org.Name)
}
if errs := testTeamMembers(org.Teams, admins, allOrgMembers, *org.Name); errs != nil {
for _, err := range errs {
t.Error(err)
}
}
}
}