forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
259 lines (242 loc) · 8.53 KB
/
main_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
/*
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 main
import (
"flag"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"k8s.io/test-infra/prow/flagutil"
prowflagutil "k8s.io/test-infra/prow/flagutil"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
)
func TestOptions(t *testing.T) {
var defaultGitHubOptions flagutil.GitHubOptions
defaultGitHubOptions.AddFlags(flag.NewFlagSet("", flag.ContinueOnError))
defaultGerritProjects := make(map[string][]string)
cases := []struct {
name string
args []string
expected *options
}{
//General
{
name: "no args, reject",
args: []string{},
},
{
name: "config-path is empty string, reject",
args: []string{"--pubsub-workers=1", "--config-path="},
},
//Gerrit Reporter
{
name: "gerrit supports multiple workers",
args: []string{"--gerrit-workers=99", "--gerrit-projects=foo=bar", "--cookiefile=foobar", "--config-path=foo"},
expected: &options{
gerritWorkers: 99,
cookiefilePath: "foobar",
gerritProjects: map[string][]string{
"foo": {"bar"},
},
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
github: defaultGitHubOptions,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "gerrit missing --cookiefile",
args: []string{"--gerrit-workers=5", "--gerrit-projects=foo=bar", "--config-path=foo"},
expected: &options{
gerritWorkers: 5,
gerritProjects: map[string][]string{
"foo": {"bar"},
},
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
github: defaultGitHubOptions,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
//PubSub Reporter
{
name: "pubsub workers, sets workers",
args: []string{"--pubsub-workers=7", "--config-path=baz"},
expected: &options{
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "baz",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
pubsubWorkers: 7,
github: defaultGitHubOptions,
gerritProjects: defaultGerritProjects,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "pubsub workers set to negative, rejects",
args: []string{"--pubsub-workers=-3", "--config-path=foo"},
},
//Slack Reporter
{
name: "slack workers, sets workers",
args: []string{"--slack-workers=13", "--slack-token-file=/bar/baz", "--config-path=foo"},
expected: &options{
slackWorkers: 13,
slackTokenFile: "/bar/baz",
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
github: defaultGitHubOptions,
gerritProjects: defaultGerritProjects,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "slack missing --slack-token, rejects",
args: []string{"--slack-workers=1", "--config-path=foo"},
},
{
name: "slack with --dry-run, sets",
args: []string{"--slack-workers=13", "--slack-token-file=/bar/baz", "--config-path=foo", "--dry-run"},
expected: &options{
slackWorkers: 13,
slackTokenFile: "/bar/baz",
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
dryrun: true,
github: defaultGitHubOptions,
gerritProjects: defaultGerritProjects,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "k8s-gcs enables k8s-gcs",
args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo"},
expected: &options{
k8sBlobStorageWorkers: 3,
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
github: defaultGitHubOptions,
gerritProjects: defaultGerritProjects,
k8sReportFraction: 1.0,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "k8s-gcs with report fraction sets report fraction",
args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=0.5"},
expected: &options{
k8sBlobStorageWorkers: 3,
config: configflagutil.ConfigOptions{
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
ConfigPath: "foo",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
github: defaultGitHubOptions,
gerritProjects: defaultGerritProjects,
k8sReportFraction: 0.5,
instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
},
},
{
name: "k8s-gcs with too large report fraction rejects",
args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=1.5"},
},
{
name: "k8s-gcs with negative report fraction rejects",
args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=-1.2"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
flags := flag.NewFlagSet(tc.name, flag.ContinueOnError)
var actual options
err := actual.parseArgs(flags, tc.args)
switch {
case err == nil && tc.expected == nil:
t.Fatalf("%s: failed to return an error", tc.name)
case err != nil && tc.expected != nil:
t.Fatalf("%s: unexpected error: %v", tc.name, err)
}
if tc.expected == nil {
return
}
if diff := cmp.Diff(actual, *tc.expected, cmp.Exporter(func(_ reflect.Type) bool { return true })); diff != "" {
t.Errorf("Result differs from expected: %s", diff)
}
})
}
}
/*
The GitHubOptions object has several private fields and objects
This unit testing covers only the public portions
*/
func TestGitHubOptions(t *testing.T) {
cases := []struct {
name string
args []string
expectedWorkers int
expectedTokenPath string
}{
{
name: "github workers, only support single worker",
args: []string{"--github-workers=5", "--github-token-path=tkpath", "--config-path=foo"},
expectedWorkers: 5,
expectedTokenPath: "tkpath",
},
}
for _, tc := range cases {
flags := flag.NewFlagSet(tc.name, flag.ContinueOnError)
actual := options{}
err := actual.parseArgs(flags, tc.args)
if err != nil {
t.Errorf("%s: unexpected error: %v", tc.name, err)
}
if actual.githubWorkers != tc.expectedWorkers {
t.Errorf("%s: worker mismatch: actual %d != expected %d",
tc.name, actual.githubWorkers, tc.expectedWorkers)
}
if actual.github.TokenPath != tc.expectedTokenPath {
t.Errorf("%s: path mismatch: actual %s != expected %s",
tc.name, actual.github.TokenPath, tc.expectedTokenPath)
}
}
}