/
options_test.go
181 lines (153 loc) · 4.82 KB
/
options_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
/*
Copyright 2020 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"
"os"
"reflect"
"strconv"
"testing"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
)
func TestGetOptions(t *testing.T) {
testFunc := func(
t *testing.T,
additionalArgs []string,
withServerOptions bool,
withControllerOptions bool,
withNodeOptions bool,
) *Options {
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
endpointFlagName := "endpoint"
endpoint := "foo"
extraTagsFlagName := "extra-tags"
extraTagKey := "bar"
extraTagValue := "baz"
extraTags := map[string]string{
extraTagKey: extraTagValue,
}
VolumeAttachLimitFlagName := "volume-attach-limit"
var VolumeAttachLimit int64 = 42
args := append([]string{
"aws-ebs-csi-driver",
}, additionalArgs...)
if withServerOptions {
args = append(args, "-"+endpointFlagName+"="+endpoint)
}
if withControllerOptions {
args = append(args, "-"+extraTagsFlagName+"="+extraTagKey+"="+extraTagValue)
}
if withNodeOptions {
args = append(args, "-"+VolumeAttachLimitFlagName+"="+strconv.FormatInt(VolumeAttachLimit, 10))
}
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = args
options := GetOptions(flagSet)
if withServerOptions {
endpointFlag := flagSet.Lookup(endpointFlagName)
if endpointFlag == nil {
t.Fatalf("expected %q flag to be added but it is not", endpointFlagName)
}
if options.ServerOptions.Endpoint != endpoint {
t.Fatalf("expected endpoint to be %q but it is %q", endpoint, options.ServerOptions.Endpoint)
}
}
if withControllerOptions {
extraTagsFlag := flagSet.Lookup(extraTagsFlagName)
if extraTagsFlag == nil {
t.Fatalf("expected %q flag to be added but it is not", extraTagsFlagName)
}
if !reflect.DeepEqual(options.ControllerOptions.ExtraTags, extraTags) {
t.Fatalf("expected extra tags to be %q but it is %q", extraTags, options.ControllerOptions.ExtraTags)
}
}
if withNodeOptions {
VolumeAttachLimitFlag := flagSet.Lookup(VolumeAttachLimitFlagName)
if VolumeAttachLimitFlag == nil {
t.Fatalf("expected %q flag to be added but it is not", VolumeAttachLimitFlagName)
}
if options.NodeOptions.VolumeAttachLimit != VolumeAttachLimit {
t.Fatalf("expected VolumeAttachLimit to be %d but it is %d", VolumeAttachLimit, options.NodeOptions.VolumeAttachLimit)
}
}
return options
}
testCases := []struct {
name string
testFunc func(t *testing.T)
}{
{
name: "no controller mode given - expect all mode",
testFunc: func(t *testing.T) {
options := testFunc(t, nil, true, true, true)
if options.DriverMode != driver.AllMode {
t.Fatalf("expected driver mode to be %q but it is %q", driver.AllMode, options.DriverMode)
}
},
},
{
name: "all mode given - expect all mode",
testFunc: func(t *testing.T) {
options := testFunc(t, []string{"all"}, true, true, true)
if options.DriverMode != driver.AllMode {
t.Fatalf("expected driver mode to be %q but it is %q", driver.AllMode, options.DriverMode)
}
},
},
{
name: "controller mode given - expect controller mode",
testFunc: func(t *testing.T) {
options := testFunc(t, []string{"controller"}, true, true, false)
if options.DriverMode != driver.ControllerMode {
t.Fatalf("expected driver mode to be %q but it is %q", driver.ControllerMode, options.DriverMode)
}
},
},
{
name: "node mode given - expect node mode",
testFunc: func(t *testing.T) {
options := testFunc(t, []string{"node"}, true, false, true)
if options.DriverMode != driver.NodeMode {
t.Fatalf("expected driver mode to be %q but it is %q", driver.NodeMode, options.DriverMode)
}
},
},
{
name: "version flag specified",
testFunc: func(t *testing.T) {
oldOSExit := osExit
defer func() { osExit = oldOSExit }()
var exitCode int
testExit := func(code int) {
exitCode = code
}
osExit = testExit
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{
"aws-ebs-csi-driver",
"-version",
}
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
_ = GetOptions(flagSet)
if exitCode != 0 {
t.Fatalf("expected exit code 0 but got %d", exitCode)
}
},
},
}
for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
}
}