Skip to content
This repository was archived by the owner on Jun 23, 2020. It is now read-only.

Commit 1a3280e

Browse files
author
Harvey Lowndes
committed
Remove ExecWithResult to enable testing
1 parent 28094a3 commit 1a3280e

File tree

2 files changed

+82
-96
lines changed

2 files changed

+82
-96
lines changed

pkg/flexvolume/flexvolume.go

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,6 @@ type Driver interface {
8686
Unmount(mountDir string) DriverStatus
8787
}
8888

89-
// ExitWithResult outputs the given Result and exits with the appropriate exit
90-
// code.
91-
func ExitWithResult(result DriverStatus) {
92-
code := 1
93-
if result.Status == StatusSuccess || result.Status == StatusNotSupported {
94-
code = 0
95-
}
96-
97-
res, err := json.Marshal(result)
98-
if err != nil {
99-
log.Printf("Error marshaling result: %v", err)
100-
fmt.Fprintln(out, `{"status":"Failure","message":"Error marshaling result to JSON"}`)
101-
} else {
102-
s := string(res)
103-
log.Printf("Command result: %s", s)
104-
fmt.Fprintln(out, s)
105-
}
106-
exit(code)
107-
}
108-
10989
// Fail creates a StatusFailure Result with a given message.
11090
func Fail(a ...interface{}) DriverStatus {
11191
msg := fmt.Sprint(a...)
@@ -115,6 +95,15 @@ func Fail(a ...interface{}) DriverStatus {
11595
}
11696
}
11797

98+
// Failf creates a StatusFailure Result with a given message.
99+
func Failf(s string, a ...interface{}) DriverStatus {
100+
msg := fmt.Sprintf(s, a...)
101+
return DriverStatus{
102+
Status: StatusFailure,
103+
Message: msg,
104+
}
105+
}
106+
118107
// Succeed creates a StatusSuccess Result with a given message.
119108
func Succeed(a ...interface{}) DriverStatus {
120109
return DriverStatus{
@@ -123,6 +112,14 @@ func Succeed(a ...interface{}) DriverStatus {
123112
}
124113
}
125114

115+
// NotSupportedf creates a StatusNotSupported Result with a given message.
116+
func NotSupportedf(s string, a ...interface{}) DriverStatus {
117+
return DriverStatus{
118+
Status: StatusNotSupported,
119+
Message: fmt.Sprintf(s, a...),
120+
}
121+
}
122+
126123
// NotSupported creates a StatusNotSupported Result with a given message.
127124
func NotSupported(a ...interface{}) DriverStatus {
128125
return DriverStatus{
@@ -147,9 +144,9 @@ func processOpts(optsStr string) (Options, error) {
147144

148145
// ExecDriver executes the appropriate FlexvolumeDriver command based on
149146
// recieved call-out.
150-
func ExecDriver(drivers map[string]Driver, args []string) {
147+
func ExecDriver(drivers map[string]Driver, args []string) DriverStatus {
151148
if len(args) < 2 {
152-
ExitWithResult(Fail("Expected at least one argument"))
149+
return Failf("Expected at least one argument")
153150
}
154151

155152
log.Printf("'%s %s' called with %s", args[0], args[1], args[2:])
@@ -161,127 +158,130 @@ func ExecDriver(drivers map[string]Driver, args []string) {
161158

162159
if dir != "oci" && dir != DefaultSymlinkDirectory {
163160
driver = drivers[dir]
164-
if driver == nil {
165-
ExitWithResult(Fail("No driver found for ", dir))
166-
}
161+
}
162+
163+
// Moved outside the above if to catch errors in code.
164+
if driver == nil {
165+
Failf("No driver found for ", dir)
167166
}
168167

169168
log.Printf("Using %s driver", dir)
170169

171170
switch args[1] {
172171
// <driver executable> init
173172
case "init":
174-
ExitWithResult(driver.Init())
173+
return driver.Init()
175174

176175
// <driver executable> getvolumename <json options>
177176
// Currently broken as of lates kube release (1.6.4). Work around hardcodes
178177
// exiting with StatusNotSupported.
179178
// TODO(apryde): Investigate current situation and version support
180179
// requirements.
181180
case "getvolumename":
182-
ExitWithResult(NotSupported("getvolumename is broken as of kube 1.6.4"))
181+
return NotSupportedf("getvolumename is broken as of kube 1.6.4")
183182

184183
// <driver executable> attach <json options> <node name>
185184
case "attach":
186185
if len(args) != 4 {
187-
ExitWithResult(Fail("attach expected exactly 4 arguments; got ", args))
186+
Failf("attach expected exactly 4 arguments; got ", args)
188187
}
189188

190189
opts, err := processOpts(args[2])
191190
if err != nil {
192-
ExitWithResult(Fail(err))
191+
return Failf(err.Error())
193192
}
194193

195194
nodeName := args[3]
196-
ExitWithResult(driver.Attach(opts, nodeName))
195+
return driver.Attach(opts, nodeName)
197196

198197
// <driver executable> detach <mount device> <node name>
199198
case "detach":
200199
if len(args) != 4 {
201-
ExitWithResult(Fail("detach expected exactly 4 arguments; got ", args))
200+
return Failf("detach expected exactly 4 arguments; got ", args)
202201
}
203202

204203
mountDevice := args[2]
205204
nodeName := args[3]
206-
ExitWithResult(driver.Detach(mountDevice, nodeName))
205+
return driver.Detach(mountDevice, nodeName)
207206

208207
// <driver executable> waitforattach <mount device> <json options>
209208
case "waitforattach":
210209
if len(args) != 4 {
211-
ExitWithResult(Fail("waitforattach expected exactly 4 arguments; got ", args))
210+
return Failf("waitforattach expected exactly 4 arguments; got ", args)
212211
}
213212

214213
mountDevice := args[2]
215214
opts, err := processOpts(args[3])
216215
if err != nil {
217-
ExitWithResult(Fail(err))
216+
return Failf(err.Error())
218217
}
219218

220-
ExitWithResult(driver.WaitForAttach(mountDevice, opts))
219+
return driver.WaitForAttach(mountDevice, opts)
221220

222221
// <driver executable> isattached <json options> <node name>
223222
case "isattached":
224223
if len(args) != 4 {
225-
ExitWithResult(Fail("isattached expected exactly 4 arguments; got ", args))
224+
return Failf("isattached expected exactly 4 arguments; got ", args)
226225
}
227226

228227
opts, err := processOpts(args[2])
229228
if err != nil {
230-
ExitWithResult(Fail(err))
229+
return Failf(err.Error())
231230
}
232231
nodeName := args[3]
233-
ExitWithResult(driver.IsAttached(opts, nodeName))
232+
return driver.IsAttached(opts, nodeName)
234233

235234
// <driver executable> mountdevice <mount dir> <mount device> <json options>
236235
case "mountdevice":
237236
if len(args) != 5 {
238-
ExitWithResult(Fail("mountdevice expected exactly 5 arguments; got ", args))
237+
return Failf("mountdevice expected exactly 5 arguments; got ", args)
239238
}
240239

241240
mountDir := args[2]
242241
mountDevice := args[3]
243242

244243
opts, err := processOpts(args[4])
245244
if err != nil {
246-
ExitWithResult(Fail(err))
245+
return Failf(err.Error())
247246
}
248247

249-
ExitWithResult(driver.MountDevice(mountDir, mountDevice, opts))
248+
return driver.MountDevice(mountDir, mountDevice, opts)
250249

251250
// <driver executable> unmountdevice <mount dir>
252251
case "unmountdevice":
253252
if len(args) != 3 {
254-
ExitWithResult(Fail("unmountdevice expected exactly 3 arguments; got ", args))
253+
return Failf("unmountdevice expected exactly 3 arguments; got ", args)
255254
}
256255

257256
mountDir := args[2]
258-
ExitWithResult(driver.UnmountDevice(mountDir))
257+
return driver.UnmountDevice(mountDir)
259258

260259
// <driver executable> mount <mount dir> <json options>
261260
case "mount":
262261
if len(args) != 4 {
263-
ExitWithResult(Fail("mount expected exactly 4 arguments; got ", args))
262+
return Failf("mount expected exactly 4 arguments; got ", args)
264263
}
265264

266265
mountDir := args[2]
267266

268267
opts, err := processOpts(args[3])
269268
if err != nil {
270-
ExitWithResult(Fail(err))
269+
return Failf(err.Error())
271270
}
272271

273-
ExitWithResult(driver.Mount(mountDir, opts))
272+
return driver.Mount(mountDir, opts)
274273

275274
// <driver executable> unmount <mount dir>
276275
case "unmount":
277276
if len(args) != 3 {
278-
ExitWithResult(Fail("mount expected exactly 3 arguments; got ", args))
277+
return Failf("mount expected exactly 3 arguments; got ", args)
279278
}
280279

281280
mountDir := args[2]
282-
ExitWithResult(driver.Unmount(mountDir))
281+
return driver.Unmount(mountDir)
283282

284283
default:
285-
ExitWithResult(Fail("Invalid command; got ", args))
284+
return Failf("invalid command; got ", args)
286285
}
286+
return Failf("Unexpected condition")
287287
}

pkg/flexvolume/flexvolume_test.go

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,60 @@
1515
package flexvolume
1616

1717
import (
18-
"bytes"
1918
"testing"
2019
)
2120

22-
const defaultTestOps = `{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw"}`
21+
const defaultTestOps = `{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw","kubernetes.io/pvOrVolumeName":"mockvolumeid"}`
22+
const noVolIDTestOps = `{"kubernetes.io/fsType":"ext4","kubernetes.io/readwrite":"rw"}`
2323

24-
func TestInit(t *testing.T) {
25-
bak := out
26-
out = new(bytes.Buffer)
27-
defer func() { out = bak }()
24+
func assertSuccess(t *testing.T, expected DriverStatus, status DriverStatus) {
25+
if status != expected {
26+
t.Fatalf(`Expected '%#v' got '%#v'`, expected, status)
27+
}
28+
}
2829

29-
code := 0
30-
osexit := exit
31-
exit = func(c int) { code = c }
32-
defer func() { exit = osexit }()
30+
func assertFailure(t *testing.T, expected DriverStatus, status DriverStatus) {
31+
if status != expected {
32+
t.Fatalf(`Expected '%#v' got '%#v'`, expected, status)
33+
}
34+
}
3335

34-
ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}}, []string{"oci", "init"})
36+
func TestInit(t *testing.T) {
37+
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
38+
[]string{"oci", "init"})
3539

36-
if out.(*bytes.Buffer).String() != `{"status":"Success"}`+"\n" {
37-
t.Fatalf(`Expected '{"status":"Success"}'; got %s`, out.(*bytes.Buffer).String())
38-
}
40+
expected := DriverStatus{Status: "Success"}
3941

40-
if code != 0 {
41-
t.Fatalf("Expected 'exit 0'; got 'exit %d'", code)
42-
}
42+
assertSuccess(t, expected, status)
4343
}
4444

4545
// TestVolumeName tests that the getvolumename call-out results in
4646
// StatusNotSupported as the call-out is broken as of the latest stable Kube
4747
// release (1.6.4).
4848
func TestGetVolumeName(t *testing.T) {
49-
bak := out
50-
out = new(bytes.Buffer)
51-
defer func() { out = bak }()
49+
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
50+
[]string{"oci", "getvolumename", defaultTestOps})
5251

53-
code := 0
54-
osexit := exit
55-
exit = func(c int) { code = c }
56-
defer func() { exit = osexit }()
52+
expected := DriverStatus{Status: "Not supported", Message: "getvolumename is broken as of kube 1.6.4"}
5753

58-
ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}}, []string{"oci", "getvolumename", defaultTestOps})
54+
assertFailure(t, expected, status)
55+
}
5956

60-
if out.(*bytes.Buffer).String() != `{"status":"Not supported","message":"getvolumename is broken as of kube 1.6.4"}`+"\n" {
61-
t.Fatalf(`Expected '{"status":"Not supported","message":"getvolumename is broken as of kube 1.6.4"}}'; got %s`, out.(*bytes.Buffer).String())
62-
}
57+
func TestNoVolumeIDDispatch(t *testing.T) {
58+
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
59+
[]string{"oci", "attach", noVolIDTestOps, "nodeName"})
6360

64-
if code != 0 {
65-
t.Fatalf("Expected 'exit 0'; got 'exit %d'", code)
61+
expected := DriverStatus{
62+
Status: "Failure",
63+
Message: "key 'kubernetes.io/pvOrVolumeName' not found in attach options",
6664
}
65+
assertFailure(t, expected, status)
6766
}
6867

6968
func TestAttachUnsuported(t *testing.T) {
70-
bak := out
71-
out = new(bytes.Buffer)
72-
defer func() { out = bak }()
73-
74-
code := 0
75-
osexit := exit
76-
exit = func(c int) { code = c }
77-
defer func() { exit = osexit }()
78-
79-
ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}}, []string{"oci", "attach", defaultTestOps, "nodeName"})
69+
status := ExecDriver(map[string]Driver{"oci-bvs": mockFlexvolumeDriver{}},
70+
[]string{"oci", "attach", defaultTestOps, "nodeName"})
8071

81-
if out.(*bytes.Buffer).String() != `{"status":"Not supported"}`+"\n" {
82-
t.Fatalf(`Expected '{"status":"Not supported""}'; got %s`, out.(*bytes.Buffer).String())
83-
}
84-
85-
if code != 0 {
86-
t.Fatalf("Expected 'exit 0'; got 'exit %d'", code)
87-
}
72+
expected := DriverStatus{Status: "Not supported"}
73+
assertFailure(t, expected, status)
8874
}

0 commit comments

Comments
 (0)