forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
154 lines (142 loc) · 4.13 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
/*
Copyright 2017 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/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/flagutil"
configflagutil "k8s.io/test-infra/prow/flagutil/config"
pluginsflagutil "k8s.io/test-infra/prow/flagutil/plugins"
)
func newSetStringsFlagForTest(vals ...string) flagutil.Strings {
ss := flagutil.NewStrings()
for _, v := range vals {
ss.Set(v)
}
return ss
}
func TestGatherOptions(t *testing.T) {
cases := []struct {
name string
args []string
expected func(*options)
expectedErr error
}{
{
name: "minimal flags work",
},
{
name: "gcs-credentials-file sets the GCS credentials on the storage client",
args: []string{
"-gcs-credentials-file=/creds",
},
expected: func(o *options) {
o.storage = flagutil.StorageClientOptions{
GCSCredentialsFile: "/creds",
}
},
},
{
name: "s3-credentials-file sets the S3 credentials on the storage client",
args: []string{
"-s3-credentials-file=/creds",
},
expected: func(o *options) {
o.storage = flagutil.StorageClientOptions{
S3CredentialsFile: "/creds",
}
},
},
{
name: "support denylist",
args: []string{
"-denylist=a",
"-denylist=b",
},
expected: func(o *options) {
o.addedPresubmitDenylist = newSetStringsFlagForTest("a", "b")
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
expected := &options{
dryRun: true,
config: configflagutil.ConfigOptions{
ConfigPath: "yo",
ConfigPathFlagName: "config-path",
JobConfigPathFlagName: "job-config-path",
SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
},
pluginsConfig: pluginsflagutil.PluginOptions{
PluginConfigPath: "/etc/plugins/plugins.yaml",
PluginConfigPathDefault: "/etc/plugins/plugins.yaml",
SupplementalPluginsConfigsFileNameSuffix: "_pluginconfig.yaml",
},
instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
}
expectedfs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
expected.github.AddCustomizedFlags(expectedfs, flagutil.ThrottlerDefaults(300, 100))
if tc.expected != nil {
tc.expected(expected)
}
args := append(tc.args,
"--config-path=yo")
fs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
actual := gatherOptions(fs, args...)
switch err := actual.Validate(); {
case err == nil && tc.expectedErr != nil:
t.Errorf("Expect err, got nil")
case err != nil && tc.expectedErr == nil:
t.Errorf("Expect no error, got: %v", err)
case err != nil && err.Error() != tc.expectedErr.Error():
t.Errorf("Expect error: %v\ngot:\n%v", err, tc.expectedErr)
case !reflect.DeepEqual(*expected, actual):
t.Errorf("actual differs from expected: %s", cmp.Diff(actual, *expected, cmp.Exporter(func(_ reflect.Type) bool { return true })))
}
})
}
}
func TestGetDenyList(t *testing.T) {
tests := []struct {
name string
o options
want sets.String
}{
{
name: "black list only",
o: options{
addedPresubmitDenylist: newSetStringsFlagForTest("a", "b"),
},
want: sets.NewString("a", "b"),
},
{
name: "deny list only",
o: options{
addedPresubmitDenylist: newSetStringsFlagForTest("c", "d"),
},
want: sets.NewString("c", "d"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.o.getDenyList()
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Fatalf("Want(-), got(+):\n%s", diff)
}
})
}
}