Skip to content

Commit 129c3ed

Browse files
authored
Merge pull request #80 from Madhan-SWE/unit_tests
Unit tests
2 parents 510fd82 + 3d1f0f1 commit 129c3ed

16 files changed

+3211
-5
lines changed

cmd/options/node_options_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package options
15+
16+
import (
17+
"flag"
18+
"testing"
19+
)
20+
21+
func TestNodeOptions(t *testing.T) {
22+
testCases := []struct {
23+
name string
24+
flag string
25+
found bool
26+
}{
27+
{
28+
name: "lookup desired flag",
29+
flag: "volume-attach-limit",
30+
found: true,
31+
},
32+
{
33+
name: "fail for non-desired flag",
34+
flag: "some-flag",
35+
found: false,
36+
},
37+
}
38+
39+
for _, tc := range testCases {
40+
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
41+
nodeOptions := &NodeOptions{}
42+
43+
t.Run(tc.name, func(t *testing.T) {
44+
nodeOptions.AddFlags(flagSet)
45+
flag := flagSet.Lookup(tc.flag)
46+
found := flag != nil
47+
if found != tc.found {
48+
t.Fatalf("result not equal\ngot:\n%v\nexpected:\n%v", found, tc.found)
49+
}
50+
})
51+
}
52+
}

cmd/options/server_options_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package options
15+
16+
import (
17+
"flag"
18+
"testing"
19+
)
20+
21+
func TestServerOptions(t *testing.T) {
22+
testCases := []struct {
23+
name string
24+
flag string
25+
found bool
26+
}{
27+
{
28+
name: "lookup desired flag",
29+
flag: "endpoint",
30+
found: true,
31+
},
32+
{
33+
name: "fail for non-desired flag",
34+
flag: "some-other-flag",
35+
found: false,
36+
},
37+
}
38+
39+
for _, tc := range testCases {
40+
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
41+
serverOptions := &ServerOptions{}
42+
43+
t.Run(tc.name, func(t *testing.T) {
44+
serverOptions.AddFlags(flagSet)
45+
46+
flag := flagSet.Lookup(tc.flag)
47+
found := flag != nil
48+
if found != tc.found {
49+
t.Fatalf("result not equal\ngot:\n%v\nexpected:\n%v", found, tc.found)
50+
}
51+
})
52+
}
53+
}

cmd/options_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package main
15+
16+
import (
17+
"flag"
18+
"os"
19+
"strconv"
20+
"testing"
21+
22+
"github.com/ppc64le-cloud/powervs-csi-driver/pkg/driver"
23+
)
24+
25+
func TestGetOptions(t *testing.T) {
26+
testFunc := func(
27+
t *testing.T,
28+
additionalArgs []string,
29+
withServerOptions bool,
30+
withControllerOptions bool,
31+
withNodeOptions bool,
32+
) *Options {
33+
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
34+
35+
endpointFlagName := "endpoint"
36+
endpoint := "foo"
37+
VolumeAttachLimitFlagName := "volume-attach-limit"
38+
var VolumeAttachLimit int64 = 42
39+
40+
args := append([]string{
41+
"powervs-csi-driver",
42+
}, additionalArgs...)
43+
44+
if withServerOptions {
45+
args = append(args, "-"+endpointFlagName+"="+endpoint)
46+
}
47+
48+
if withNodeOptions {
49+
args = append(args, "-"+VolumeAttachLimitFlagName+"="+strconv.FormatInt(VolumeAttachLimit, 10))
50+
}
51+
52+
oldArgs := os.Args
53+
defer func() { os.Args = oldArgs }()
54+
os.Args = args
55+
56+
options := GetOptions(flagSet)
57+
58+
if withServerOptions {
59+
endpointFlag := flagSet.Lookup(endpointFlagName)
60+
if endpointFlag == nil {
61+
t.Fatalf("expected %q flag to be added but it is not", endpointFlagName)
62+
}
63+
if options.ServerOptions.Endpoint != endpoint {
64+
t.Fatalf("expected endpoint to be %q but it is %q", endpoint, options.ServerOptions.Endpoint)
65+
}
66+
}
67+
68+
if withNodeOptions {
69+
VolumeAttachLimitFlag := flagSet.Lookup(VolumeAttachLimitFlagName)
70+
if VolumeAttachLimitFlag == nil {
71+
t.Fatalf("expected %q flag to be added but it is not", VolumeAttachLimitFlagName)
72+
}
73+
if options.NodeOptions.VolumeAttachLimit != VolumeAttachLimit {
74+
t.Fatalf("expected VolumeAttachLimit to be %d but it is %d", VolumeAttachLimit, options.NodeOptions.VolumeAttachLimit)
75+
}
76+
}
77+
78+
return options
79+
}
80+
81+
testCases := []struct {
82+
name string
83+
testFunc func(t *testing.T)
84+
}{
85+
{
86+
name: "no controller mode given - expect all mode",
87+
testFunc: func(t *testing.T) {
88+
options := testFunc(t, nil, true, true, true)
89+
90+
if options.DriverMode != driver.AllMode {
91+
t.Fatalf("expected driver mode to be %q but it is %q", driver.AllMode, options.DriverMode)
92+
}
93+
},
94+
},
95+
96+
{
97+
name: "all mode given - expect all mode",
98+
testFunc: func(t *testing.T) {
99+
options := testFunc(t, []string{"all"}, true, true, true)
100+
101+
if options.DriverMode != driver.AllMode {
102+
t.Fatalf("expected driver mode to be %q but it is %q", driver.AllMode, options.DriverMode)
103+
}
104+
},
105+
},
106+
{
107+
name: "controller mode given - expect controller mode",
108+
testFunc: func(t *testing.T) {
109+
options := testFunc(t, []string{"controller"}, true, true, false)
110+
111+
if options.DriverMode != driver.ControllerMode {
112+
t.Fatalf("expected driver mode to be %q but it is %q", driver.ControllerMode, options.DriverMode)
113+
}
114+
},
115+
},
116+
{
117+
name: "node mode given - expect node mode",
118+
testFunc: func(t *testing.T) {
119+
options := testFunc(t, []string{"node"}, true, false, true)
120+
121+
if options.DriverMode != driver.NodeMode {
122+
t.Fatalf("expected driver mode to be %q but it is %q", driver.NodeMode, options.DriverMode)
123+
}
124+
},
125+
},
126+
{
127+
name: "version flag specified",
128+
testFunc: func(t *testing.T) {
129+
oldOSExit := osExit
130+
defer func() { osExit = oldOSExit }()
131+
132+
var exitCode int
133+
testExit := func(code int) {
134+
exitCode = code
135+
}
136+
osExit = testExit
137+
138+
oldArgs := os.Args
139+
defer func() { os.Args = oldArgs }()
140+
os.Args = []string{
141+
"powervs-csi-driver",
142+
"-version",
143+
}
144+
145+
flagSet := flag.NewFlagSet("test-flagset", flag.ContinueOnError)
146+
_ = GetOptions(flagSet)
147+
148+
if exitCode != 0 {
149+
t.Fatalf("expected exit code 0 but got %d", exitCode)
150+
}
151+
},
152+
},
153+
}
154+
155+
for _, tc := range testCases {
156+
t.Run(tc.name, tc.testFunc)
157+
}
158+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ require (
5151
github.com/go-stack/stack v1.8.0 // indirect
5252
github.com/gogo/protobuf v1.3.2 // indirect
5353
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
54+
github.com/golang/mock v1.6.0 // indirect
5455
github.com/golang/protobuf v1.5.2 // indirect
5556
github.com/google/go-cmp v0.5.5 // indirect
5657
github.com/google/gofuzz v1.1.0 // indirect
@@ -64,6 +65,7 @@ require (
6465
github.com/jmespath/go-jmespath v0.4.0 // indirect
6566
github.com/josharian/intern v1.0.0 // indirect
6667
github.com/json-iterator/go v1.1.11 // indirect
68+
github.com/kubernetes-csi/csi-test v2.2.0+incompatible // indirect
6769
github.com/leodido/go-urn v1.2.0 // indirect
6870
github.com/mailru/easyjson v0.7.6 // indirect
6971
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
@@ -95,12 +97,15 @@ require (
9597
go.opentelemetry.io/otel/trace v0.20.0 // indirect
9698
go.opentelemetry.io/proto/otlp v0.7.0 // indirect
9799
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
100+
golang.org/x/mod v0.4.2 // indirect
98101
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
99102
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
100103
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
101104
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
102105
golang.org/x/text v0.3.7 // indirect
103106
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
107+
golang.org/x/tools v0.1.6-0.20210820212750-d4cc65f0b2ff // indirect
108+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
104109
google.golang.org/appengine v1.6.7 // indirect
105110
google.golang.org/genproto v0.0.0-20210831024726-fe130286e0e2 // indirect
106111
google.golang.org/protobuf v1.27.1 // indirect

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
398398
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
399399
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
400400
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
401+
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
402+
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
401403
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
402404
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
403405
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -554,6 +556,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
554556
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
555557
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
556558
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
559+
github.com/kubernetes-csi/csi-test v2.2.0+incompatible h1:ksIV60Q+4mY0Fg8LKvBssjEcvbyxo7nz0eAD6ZLMux0=
560+
github.com/kubernetes-csi/csi-test v2.2.0+incompatible/go.mod h1:YxJ4UiuPWIhMBkxUKY5c267DyA0uDZ/MtAimhx/2TA0=
557561
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
558562
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
559563
github.com/libopenstorage/openstorage v1.0.0/go.mod h1:Sp1sIObHjat1BeXhfMqLZ14wnOzEhNx2YQedreMcUyc=
@@ -907,6 +911,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
907911
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
908912
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
909913
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
914+
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
910915
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
911916
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
912917
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -1163,8 +1168,10 @@ golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4f
11631168
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
11641169
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
11651170
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
1171+
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
11661172
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
11671173
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
1174+
golang.org/x/tools v0.1.6-0.20210820212750-d4cc65f0b2ff h1:VX/uD7MK0AHXGiScH3fsieUQUcpmRERPDYtqZdJnA+Q=
11681175
golang.org/x/tools v0.1.6-0.20210820212750-d4cc65f0b2ff/go.mod h1:YD9qOF0M9xpSpdWTBbzEl5e/RnCefISl8E5Noe10jFM=
11691176
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11701177
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)