@@ -11,9 +11,11 @@ import (
11
11
"strconv"
12
12
"testing"
13
13
"time"
14
+
15
+ "github.com/prashantv/gostub"
14
16
)
15
17
16
- var nodeDB = `
18
+ const nodeDB = `
17
19
# BEGIN RECORD 6.2.0.874
18
20
node.name = iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced
19
21
node.tpgt = -1
@@ -80,35 +82,30 @@ node.conn[0].iscsi.OFMarker = No
80
82
# END RECORD
81
83
`
82
84
83
- var emptyTransportName = "iface.transport_name = \n "
84
- var emptyDbRecord = "\n \n \n "
85
- var testCmdOutput = ""
86
- var testCmdTimeout = false
87
- var testCmdError error
88
- var testExecWithTimeoutError error
89
- var mockedExitStatus = 0
90
- var mockedStdout string
91
-
85
+ const emptyTransportName = "iface.transport_name = \n "
86
+ const emptyDbRecord = "\n \n \n "
92
87
const testRootFS = "/tmp/iscsi-tests"
93
88
94
- type testCmdRunner struct {}
95
-
96
- func fakeExecCommand ( command string , args ... string ) * exec. Cmd {
97
- cs := [] string { "-test.run=TestExecCommandHelper" , "--" , command }
98
- cs = append ( cs , args ... )
99
- cmd := exec . Command ( os . Args [ 0 ], cs ... )
100
- es := strconv . Itoa ( mockedExitStatus )
101
- cmd . Env = [] string { "GO_WANT_HELPER_PROCESS=1" ,
102
- "STDOUT =" + mockedStdout ,
103
- "EXIT_STATUS=" + es }
104
- return cmd
89
+ func makeFakeExecCommand ( exitStatus int , stdout string ) func ( string , ... string ) * exec. Cmd {
90
+ return func ( command string , args ... string ) * exec. Cmd {
91
+ cs := [] string { "-test.run=TestExecCommandHelper" , "--" , command }
92
+ cs = append ( cs , args ... )
93
+ cmd := exec . Command ( os . Args [ 0 ], cs ... )
94
+ es := strconv . Itoa ( exitStatus )
95
+ cmd . Env = [] string { "GO_WANT_HELPER_PROCESS=1" ,
96
+ "STDOUT=" + stdout ,
97
+ "EXIT_STATUS =" + es }
98
+ return cmd
99
+ }
105
100
}
106
101
107
- func fakeExecWithTimeout (command string , args []string , timeout time.Duration ) ([]byte , error ) {
108
- if testCmdTimeout {
109
- return nil , context .DeadlineExceeded
102
+ func makeFakeExecWithTimeout (testCmdTimeout bool , testExecWithTimeoutError error ) func (string , []string , time.Duration ) ([]byte , error ) {
103
+ return func (command string , args []string , timeout time.Duration ) ([]byte , error ) {
104
+ if testCmdTimeout {
105
+ return nil , context .DeadlineExceeded
106
+ }
107
+ return []byte ("" ), testExecWithTimeoutError
110
108
}
111
- return []byte (testCmdOutput ), testExecWithTimeoutError
112
109
}
113
110
114
111
func TestExecCommandHelper (t * testing.T ) {
@@ -121,11 +118,6 @@ func TestExecCommandHelper(t *testing.T) {
121
118
os .Exit (i )
122
119
}
123
120
124
- func (tr testCmdRunner ) execCmd (cmd string , args ... string ) (string , error ) {
125
- return testCmdOutput , testCmdError
126
-
127
- }
128
-
129
121
func getDevicePath (device * Device ) string {
130
122
sysDevicePath := "/tmp/iscsi-tests/sys/class/scsi_device/"
131
123
return filepath .Join (sysDevicePath , device .Hctl , "device" )
@@ -149,6 +141,16 @@ func preparePaths(devices []Device) error {
149
141
return nil
150
142
}
151
143
144
+ func checkFileContents (t * testing.T , path string , contents string ) {
145
+ if out , err := ioutil .ReadFile (path ); err != nil {
146
+ t .Errorf ("could not read file: %v" , err )
147
+ return
148
+ } else if string (out ) != contents {
149
+ t .Errorf ("file content mismatch, got = %q, want = %q" , string (out ), contents )
150
+ return
151
+ }
152
+ }
153
+
152
154
func Test_parseSessions (t * testing.T ) {
153
155
var sessions []iscsiSession
154
156
output := "tcp: [2] 192.168.1.107:3260,1 iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced (non-flash)\n " +
@@ -226,9 +228,9 @@ func Test_extractTransportName(t *testing.T) {
226
228
}
227
229
228
230
func Test_sessionExists (t * testing.T ) {
229
- mockedExitStatus = 0
230
- mockedStdout = "tcp: [4] 192.168.1.107:3260,1 iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced (non-flash) \n "
231
- execCommand = fakeExecCommand
231
+ fakeOutput := "tcp: [4] 192.168.1.107:3260,1 iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced (non-flash) \n "
232
+ defer gostub . Stub ( & execCommand , makeFakeExecCommand ( 0 , fakeOutput )). Reset ()
233
+
232
234
type args struct {
233
235
tgtPortal string
234
236
tgtIQN string
@@ -267,10 +269,9 @@ func Test_sessionExists(t *testing.T) {
267
269
268
270
func Test_DisconnectNormalVolume (t * testing.T ) {
269
271
deleteDeviceFile := "/tmp/deleteDevice"
270
- osOpenFile = func (name string , flag int , perm os.FileMode ) (* os.File , error ) {
271
- fmt .Println (deleteDeviceFile )
272
+ defer gostub .Stub (& osOpenFile , func (name string , flag int , perm os.FileMode ) (* os.File , error ) {
272
273
return os .OpenFile (deleteDeviceFile , flag , perm )
273
- }
274
+ }). Reset ()
274
275
275
276
tests := []struct {
276
277
name string
@@ -313,13 +314,10 @@ func Test_DisconnectNormalVolume(t *testing.T) {
313
314
}
314
315
315
316
func Test_DisconnectMultipathVolume (t * testing.T ) {
316
- execWithTimeout = fakeExecWithTimeout
317
- mockedExitStatus = 0
318
- mockedStdout = ""
319
- execCommand = fakeExecCommand
320
- osStat = func (name string ) (os.FileInfo , error ) {
317
+ defer gostub .Stub (& execCommand , makeFakeExecCommand (0 , "" )).Reset ()
318
+ defer gostub .Stub (& osStat , func (name string ) (os.FileInfo , error ) {
321
319
return nil , nil
322
- }
320
+ }). Reset ()
323
321
324
322
tests := []struct {
325
323
name string
@@ -335,16 +333,15 @@ func Test_DisconnectMultipathVolume(t *testing.T) {
335
333
336
334
for _ , tt := range tests {
337
335
t .Run (tt .name , func (t * testing.T ) {
338
- testExecWithTimeoutError = tt .cmdError
339
- testCmdTimeout = tt .timeout
336
+ defer gostub .Stub (& execWithTimeout , makeFakeExecWithTimeout (tt .timeout , tt .cmdError )).Reset ()
340
337
c := Connector {
341
338
Devices : []Device {{Hctl : "hctl1" }, {Hctl : "hctl2" }},
342
339
MountTargetDevice : & Device {Type : "mpath" },
343
340
}
344
341
345
- osOpenFile = func (name string , flag int , perm os.FileMode ) (* os.File , error ) {
342
+ defer gostub . Stub ( & osOpenFile , func (name string , flag int , perm os.FileMode ) (* os.File , error ) {
346
343
return os .OpenFile (testRootFS + name , flag , perm )
347
- }
344
+ }). Reset ()
348
345
349
346
if tt .withDeviceFile {
350
347
if err := preparePaths (c .Devices ); err != nil {
@@ -373,18 +370,7 @@ func Test_DisconnectMultipathVolume(t *testing.T) {
373
370
checkFileContents (t , getDevicePath (& device )+ "/delete" , "1" )
374
371
checkFileContents (t , getDevicePath (& device )+ "/state" , "offline\n " )
375
372
}
376
-
377
373
}
378
374
})
379
375
}
380
376
}
381
-
382
- func checkFileContents (t * testing.T , path string , contents string ) {
383
- if out , err := ioutil .ReadFile (path ); err != nil {
384
- t .Errorf ("could not read file: %v" , err )
385
- return
386
- } else if string (out ) != contents {
387
- t .Errorf ("file content mismatch, got = %q, want = %q" , string (out ), contents )
388
- return
389
- }
390
- }
0 commit comments