-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups_test.go
267 lines (234 loc) · 7.38 KB
/
groups_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
/*
Copyright 2019 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 main
import (
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
"unicode/utf8"
"k8s.io/apimachinery/pkg/util/sets"
)
var cfg GroupsConfig
var rConfig RestrictionsConfig
var groupsPath = flag.String("groups-path", "", "Directory containing groups.yaml files")
var restrictionsPath = flag.String("restrictions-path", "", "Path to the configuration file containing restrictions")
func TestMain(m *testing.M) {
flag.Parse()
var err error
if *restrictionsPath != "" && !filepath.IsAbs(*restrictionsPath) {
fmt.Printf("restrictions-path \"%s\" must be an absolute path\n", *restrictionsPath)
os.Exit(1)
}
if *restrictionsPath == "" {
baseDir, err := os.Getwd()
if err != nil {
fmt.Printf("Cannot get current working directory: %v\n", err)
os.Exit(1)
}
rPath := filepath.Join(baseDir, defaultRestrictionsFile)
restrictionsPath = &rPath
}
if err := rConfig.Load(*restrictionsPath); err != nil {
fmt.Printf("Could not load restrictions config: %v\n", err)
os.Exit(1)
}
if *groupsPath != "" && !filepath.IsAbs(*groupsPath) {
fmt.Printf("groups-path \"%s\" must be an absolute path\n", *groupsPath)
os.Exit(1)
}
if *groupsPath == "" {
*groupsPath, err = os.Getwd()
if err != nil {
fmt.Printf("Cannot get current working directory: %v\n", err)
os.Exit(1)
}
}
if err := cfg.Load(*groupsPath, &rConfig); err != nil {
fmt.Printf("Could not load groups config: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
// TestMergedGroupsConfig tests that readGroupsConfig reads all
// groups.yaml files and the merged config does not contain any duplicates.
//
// It tests that the config is merged by checking that the final
// GroupsConfig contains at least one group that isn't in the
// root groups.yaml file.
func TestMergedGroupsConfig(t *testing.T) {
var containsMergedConfig bool
found := sets.String{}
dups := sets.String{}
for _, g := range cfg.Groups {
name := g.Name
if name == "ws-oss" {
containsMergedConfig = true
}
if found.Has(name) {
dups.Insert(name)
}
found.Insert(name)
}
if !containsMergedConfig {
t.Errorf("Final GroupsConfig does not have merged configs from all groups.yaml files")
}
if n := len(dups); n > 0 {
t.Errorf("%d duplicate groups: %s", n, strings.Join(dups.List(), ", "))
}
}
// TestDescriptionLength tests that the number of characters in the
// google groups description does not exceed 300.
//
// This validation is needed because gcloud allows apps:description
// with length no greater than 300
func TestDescriptionLength(t *testing.T) {
var errs []error
for _, g := range cfg.Groups {
description := g.Description
len := utf8.RuneCountInString(description)
//Ref: https://developers.google.com/admin-sdk/groups-settings/v1/reference/groups
if len > 300 {
errs = append(errs,
fmt.Errorf("Number of characters in description \"%s\" for group name \"%s\" "+
"should not exceed 300; is: %d", description, g.Name, len))
}
}
if len(errs) > 0 {
for _, err := range errs {
t.Error(err)
}
}
}
// Enforce conventions for all groups
func TestGroupConventions(t *testing.T) {
for _, g := range cfg.Groups {
// groups are easier to reason about if email and name match
expectedEmailId := g.Name + "@inclusivenaming.org"
if g.EmailId != expectedEmailId {
t.Errorf("group '%s': expected email '%s', got '%s'", g.Name, expectedEmailId, g.EmailId)
}
}
}
// TODO: Consider adding RBAC groups and accompanying tests
// TODO: Consider adding security response groups and accompanying tests
// An e-mail address can only show up once within a given group, whether that
// be as a member, manager, or owner
func TestNoDuplicateMembers(t *testing.T) {
for _, g := range cfg.Groups {
members := map[string]bool{}
for _, m := range g.Members {
if _, ok := members[m]; ok {
t.Errorf("group '%s' cannot have duplicate member '%s'", g.EmailId, m)
}
members[m] = true
}
managers := map[string]bool{}
for _, m := range g.Managers {
if _, ok := members[m]; ok {
t.Errorf("group '%s' manager '%s' cannot also be listed as a member", g.EmailId, m)
}
if _, ok := managers[m]; ok {
t.Errorf("group '%s' cannot have duplicate manager '%s'", g.EmailId, m)
}
managers[m] = true
}
owners := map[string]bool{}
for _, m := range g.Owners {
if _, ok := members[m]; ok {
t.Errorf("group '%s' owner '%s' cannot also be listed as a member", g.EmailId, m)
}
if _, ok := managers[m]; ok {
t.Errorf("group '%s' owner '%s' cannot also be listed as a manager", g.EmailId, m)
}
if _, ok := owners[m]; ok {
t.Errorf("group '%s' cannot have duplicate owner '%s'", g.EmailId, m)
}
owners[m] = true
}
}
}
// NOTE: make very certain you know what you are doing if you change one
// of these groups, we don't want to accidentally lock ourselves out
func TestHardcodedGroupsForParanoia(t *testing.T) {
groups := map[string][]string{
"[email protected]": {
},
"[email protected]": {
},
}
found := make(map[string]bool)
for _, g := range cfg.Groups {
if expected, ok := groups[g.EmailId]; ok {
found[g.EmailId] = true
sort.Strings(expected)
actual := make([]string, len(g.Managers))
copy(actual, g.Managers)
sort.Strings(actual)
if !reflect.DeepEqual(expected, actual) {
t.Errorf("group '%s': expected managers '%v', got '%v'", g.Name, expected, actual)
}
}
}
for email := range groups {
if _, ok := found[email]; !ok {
t.Errorf("group '%s' is missing, should be present", email)
}
}
}
// Setting AllowWebPosting should be set for every group which should support
// access to the group not only via gmail but also via web (you can see the list
// and history of threads and also use web interface to operate the group)
// More info:
// https://developers.google.com/admin-sdk/groups-settings/v1/reference/groups#allowWebPosting
func TestGroupsWhichShouldSupportHistory(t *testing.T) {
groups := map[string]struct{}{
"[email protected]": {},
"[email protected]": {},
"[email protected]": {},
}
found := make(map[string]struct{})
for _, group := range cfg.Groups {
emailId := group.EmailId
found[emailId] = struct{}{}
if _, ok := groups[emailId]; ok {
allowedWebPosting, ok := group.Settings["AllowWebPosting"]
if !ok {
t.Errorf(
"group '%s': must have 'settings.allowedWebPosting = true'",
group.Name,
)
} else if allowedWebPosting != "true" {
t.Errorf(
"group '%s': must have 'settings.allowedWebPosting = true'"+
" but have 'settings.allowedWebPosting = %s' instead",
group.Name,
allowedWebPosting,
)
}
}
}
for email := range groups {
if _, ok := found[email]; !ok {
t.Errorf("group '%s' is missing, should be present", email)
}
}
}