forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugins_test.go
190 lines (176 loc) · 5.12 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
/*
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 (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"sigs.k8s.io/yaml"
"k8s.io/test-infra/pkg/genyaml"
)
func TestHasSelfApproval(t *testing.T) {
cases := []struct {
name string
cfg string
expected bool
}{
{
name: "self approval by default",
expected: true,
},
{
name: "reject approval when require_self_approval set",
cfg: `{"require_self_approval": true}`,
expected: false,
},
{
name: "has approval when require_self_approval set to false",
cfg: `{"require_self_approval": false}`,
expected: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var a Approve
if err := yaml.Unmarshal([]byte(tc.cfg), &a); err != nil {
t.Fatalf("failed to unmarshal cfg: %v", err)
}
if actual := a.HasSelfApproval(); actual != tc.expected {
t.Errorf("%t != expected %t", actual, tc.expected)
}
})
}
}
func TestConsiderReviewState(t *testing.T) {
cases := []struct {
name string
cfg string
expected bool
}{
{
name: "consider by default",
expected: true,
},
{
name: "do not consider when irs = true",
cfg: `{"ignore_review_state": true}`,
},
{
name: "consider when irs = false",
cfg: `{"ignore_review_state": false}`,
expected: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var a Approve
if err := yaml.Unmarshal([]byte(tc.cfg), &a); err != nil {
t.Fatalf("failed to unmarshal cfg: %v", err)
}
if actual := a.ConsiderReviewState(); actual != tc.expected {
t.Errorf("%t != expected %t", actual, tc.expected)
}
})
}
}
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 TestGenYamlDocs(t *testing.T) {
const fixtureName = "./plugin-config-documented.yaml"
inputFiles, err := filepath.Glob("*.go")
if err != nil {
t.Fatalf("filepath.Glob: %v", err)
}
commentMap, err := genyaml.NewCommentMap(inputFiles...)
if err != nil {
t.Fatalf("failed to construct commentMap: %v", err)
}
actualYaml, err := commentMap.GenYaml(genyaml.PopulateStruct(&Configuration{}))
if err != nil {
t.Fatalf("genyaml errored: %v", err)
}
if os.Getenv("UPDATE") != "" {
if err := ioutil.WriteFile(fixtureName, []byte(actualYaml), 0644); err != nil {
t.Fatalf("failed to write fixture: %v", err)
}
}
expectedYaml, err := ioutil.ReadFile(fixtureName)
if err != nil {
t.Fatalf("failed to read fixture: %v", err)
}
if diff := cmp.Diff(actualYaml, string(expectedYaml)); diff != "" {
t.Errorf("Actual result differs from expected: %s. If this is expected, re-run the tests with the UPDATE env var set to update the fixture: UPDATE=true go test ./...", diff)
}
}