Skip to content

Commit 736e81c

Browse files
committed
replace manual reassignation with stubs in tests
1 parent df4b0a3 commit 736e81c

File tree

4 files changed

+54
-64
lines changed

4 files changed

+54
-64
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/kubernetes-csi/csi-lib-iscsi
22

33
go 1.15
4+
5+
require github.com/prashantv/gostub v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/prashantv/gostub v1.0.0 h1:wTzvgO04xSS3gHuz6Vhuo0/kvWelyJxwNS0IRBPAwGY=
2+
github.com/prashantv/gostub v1.0.0/go.mod h1:dP1v6T1QzyGJJKFocwAU0lSZKpfjstjH8TlhkEU0on0=

iscsi/iscsi_test.go

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
"strconv"
1212
"testing"
1313
"time"
14+
15+
"github.com/prashantv/gostub"
1416
)
1517

16-
var nodeDB = `
18+
const nodeDB = `
1719
# BEGIN RECORD 6.2.0.874
1820
node.name = iqn.2010-10.org.openstack:volume-eb393993-73d0-4e39-9ef4-b5841e244ced
1921
node.tpgt = -1
@@ -80,35 +82,30 @@ node.conn[0].iscsi.OFMarker = No
8082
# END RECORD
8183
`
8284

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"
9287
const testRootFS = "/tmp/iscsi-tests"
9388

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+
}
105100
}
106101

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
110108
}
111-
return []byte(testCmdOutput), testExecWithTimeoutError
112109
}
113110

114111
func TestExecCommandHelper(t *testing.T) {
@@ -121,11 +118,6 @@ func TestExecCommandHelper(t *testing.T) {
121118
os.Exit(i)
122119
}
123120

124-
func (tr testCmdRunner) execCmd(cmd string, args ...string) (string, error) {
125-
return testCmdOutput, testCmdError
126-
127-
}
128-
129121
func getDevicePath(device *Device) string {
130122
sysDevicePath := "/tmp/iscsi-tests/sys/class/scsi_device/"
131123
return filepath.Join(sysDevicePath, device.Hctl, "device")
@@ -149,6 +141,16 @@ func preparePaths(devices []Device) error {
149141
return nil
150142
}
151143

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+
152154
func Test_parseSessions(t *testing.T) {
153155
var sessions []iscsiSession
154156
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) {
226228
}
227229

228230
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+
232234
type args struct {
233235
tgtPortal string
234236
tgtIQN string
@@ -267,10 +269,9 @@ func Test_sessionExists(t *testing.T) {
267269

268270
func Test_DisconnectNormalVolume(t *testing.T) {
269271
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) {
272273
return os.OpenFile(deleteDeviceFile, flag, perm)
273-
}
274+
}).Reset()
274275

275276
tests := []struct {
276277
name string
@@ -313,13 +314,10 @@ func Test_DisconnectNormalVolume(t *testing.T) {
313314
}
314315

315316
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) {
321319
return nil, nil
322-
}
320+
}).Reset()
323321

324322
tests := []struct {
325323
name string
@@ -335,16 +333,15 @@ func Test_DisconnectMultipathVolume(t *testing.T) {
335333

336334
for _, tt := range tests {
337335
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()
340337
c := Connector{
341338
Devices: []Device{{Hctl: "hctl1"}, {Hctl: "hctl2"}},
342339
MountTargetDevice: &Device{Type: "mpath"},
343340
}
344341

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) {
346343
return os.OpenFile(testRootFS+name, flag, perm)
347-
}
344+
}).Reset()
348345

349346
if tt.withDeviceFile {
350347
if err := preparePaths(c.Devices); err != nil {
@@ -373,18 +370,7 @@ func Test_DisconnectMultipathVolume(t *testing.T) {
373370
checkFileContents(t, getDevicePath(&device)+"/delete", "1")
374371
checkFileContents(t, getDevicePath(&device)+"/state", "offline\n")
375372
}
376-
377373
}
378374
})
379375
}
380376
}
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-
}

iscsi/iscsiadm_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package iscsi
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/prashantv/gostub"
7+
)
48

59
func TestDiscovery(t *testing.T) {
6-
execCommand = fakeExecCommand
710
tests := map[string]struct {
811
tgtPortal string
912
iface string
@@ -63,8 +66,7 @@ iscsiadm: Could not perform SendTargets discovery.\n`,
6366

6467
for name, tt := range tests {
6568
t.Run(name, func(t *testing.T) {
66-
mockedExitStatus = tt.mockedExitStatus
67-
mockedStdout = tt.mockedStdout
69+
defer gostub.Stub(&execCommand, makeFakeExecCommand(tt.mockedExitStatus, tt.mockedStdout)).Reset()
6870
err := Discoverydb(tt.tgtPortal, tt.iface, tt.discoverySecret, tt.chapDiscovery)
6971
if (err != nil) != tt.wantErr {
7072
t.Errorf("Discoverydb() error = %v, wantErr %v", err, tt.wantErr)
@@ -75,7 +77,6 @@ iscsiadm: Could not perform SendTargets discovery.\n`,
7577
}
7678

7779
func TestCreateDBEntry(t *testing.T) {
78-
execCommand = fakeExecCommand
7980
tests := map[string]struct {
8081
tgtPortal string
8182
tgtIQN string
@@ -115,8 +116,7 @@ func TestCreateDBEntry(t *testing.T) {
115116

116117
for name, tt := range tests {
117118
t.Run(name, func(t *testing.T) {
118-
mockedExitStatus = tt.mockedExitStatus
119-
mockedStdout = tt.mockedStdout
119+
defer gostub.Stub(&execCommand, makeFakeExecCommand(tt.mockedExitStatus, tt.mockedStdout)).Reset()
120120
err := CreateDBEntry(tt.tgtIQN, tt.tgtPortal, tt.iface, tt.discoverySecret, tt.sessionSecret)
121121
if (err != nil) != tt.wantErr {
122122
t.Errorf("CreateDBEntry() error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)