-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmultipath_test.go
79 lines (71 loc) · 1.68 KB
/
multipath_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
package iscsi
import (
"context"
"os/exec"
"testing"
"time"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/assert"
)
func TestExecWithTimeout(t *testing.T) {
tests := map[string]struct {
mockedStdout string
mockedExitStatus int
wantTimeout bool
}{
"Success": {
mockedStdout: "some output",
mockedExitStatus: 0,
wantTimeout: false,
},
"WithError": {
mockedStdout: "some\noutput",
mockedExitStatus: 1,
wantTimeout: false,
},
"WithTimeout": {
mockedStdout: "",
mockedExitStatus: 0,
wantTimeout: true,
},
"WithTimeoutAndOutput": {
mockedStdout: "should not be returned",
mockedExitStatus: 0,
wantTimeout: true,
},
"WithTimeoutAndError": {
mockedStdout: "",
mockedExitStatus: 1,
wantTimeout: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
timeout := time.Second
if tt.wantTimeout {
timeout = time.Millisecond * 50
}
defer gostub.Stub(&execCommandContext, func(ctx context.Context, command string, args ...string) *exec.Cmd {
if tt.wantTimeout {
time.Sleep(timeout + time.Millisecond*10)
}
return makeFakeExecCommandContext(tt.mockedExitStatus, tt.mockedStdout)(ctx, command, args...)
}).Reset()
out, err := ExecWithTimeout("dummy", []string{}, timeout)
if tt.wantTimeout || tt.mockedExitStatus != 0 {
assert.NotNil(err)
if tt.wantTimeout {
assert.Equal(context.DeadlineExceeded, err)
}
} else {
assert.Nil(err)
}
if tt.wantTimeout {
assert.Equal("", string(out))
} else {
assert.Equal(tt.mockedStdout, string(out))
}
})
}
}