forked from kubernetes/org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
273 lines (231 loc) · 8.07 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
/*
Copyright 2018 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 config
import (
"fmt"
"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"
)
var cfg org.FullConfig
func TestMain(m *testing.M) {
configPath := os.Getenv("MERGED_CONFIG")
if configPath == "" {
fmt.Println("MERGED_CONFIG env variable must be set")
os.Exit(1)
}
raw, err := os.ReadFile(configPath)
if err != nil {
fmt.Printf("cannot read generated config.yaml from %s: %v\n", configPath, err)
os.Exit(1)
}
if err := yaml.Unmarshal(raw, &cfg); err != nil {
fmt.Printf("cannot unmarshal generated config.yaml from %s: %v\n", configPath, err)
os.Exit(1)
}
os.Exit(m.Run())
}
type owners struct {
Reviewers []string `json:"reviewers,omitempty"`
Approvers []string `json:"approvers"`
}
func readInto(path string, i interface{}) error {
buf, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("read: %v", err)
}
if err := yaml.Unmarshal(buf, i); err != nil {
return fmt.Errorf("unmarshal: %v", err)
}
return nil
}
func loadOwners(dir string) (*owners, error) {
var own owners
if err := readInto(dir+"/OWNERS", &own); err != nil {
return nil, err
}
return &own, nil
}
func loadOrg(dir string) (*org.Config, error) {
var cfg org.Config
if err := readInto(dir+"/org.yaml", &cfg); err != nil {
return nil, err
}
return &cfg, nil
}
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)
// ensure all teams have privacy as closed
if team.Privacy == nil || (team.Privacy != nil && *team.Privacy != org.Closed) {
errs = append(errs, fmt.Errorf("The team %s in org %s doesn't have the `privacy: closed` field", teamName, orgName))
}
// 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 testOrg(targetDir string, t *testing.T) {
cfg, err := loadOrg(targetDir)
if err != nil {
t.Fatalf("failed to load org.yaml: %v", err)
}
own, err := loadOwners(targetDir)
if err != nil {
t.Fatalf("failed to load OWNERS: %v", err)
}
members := normalize(sets.NewString(cfg.Members...))
admins := normalize(sets.NewString(cfg.Admins...))
allOrgMembers := members.Union(admins)
reviewers := normalize(sets.NewString(own.Reviewers...))
approvers := normalize(sets.NewString(own.Approvers...))
if n := len(approvers); n < 5 {
t.Errorf("Require at least 5 approvers, found %d: %s", n, strings.Join(approvers.List(), ", "))
}
if missing := reviewers.Difference(allOrgMembers); len(missing) > 0 {
t.Errorf("The following reviewers must be members: %s", strings.Join(missing.List(), ", "))
}
if missing := approvers.Difference(allOrgMembers); len(missing) > 0 {
t.Errorf("The following approvers must be members: %s", strings.Join(missing.List(), ", "))
}
if err := testDuplicates(reviewers); err != nil {
t.Errorf("duplicate reviewers: %v", err)
}
if err := testDuplicates(approvers); err != nil {
t.Errorf("duplicate approvers: %v", err)
}
}
func TestAllOrgs(t *testing.T) {
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)
}
testOrg(n, t)
})
}
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("k8s-ci-robot") {
t.Errorf("k8s-ci-robot 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)
}
}
}
}