forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvbm_test.go
More file actions
142 lines (111 loc) · 3.96 KB
/
vbm_test.go
File metadata and controls
142 lines (111 loc) · 3.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package virtualbox
import (
"testing"
"os/exec"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
)
func TestValidCheckVBoxManageVersion(t *testing.T) {
var tests = []struct {
version string
}{
{"5.1"},
{"5.0.8r103449"},
{"5.0"},
{"4.10"},
{"4.3.1"},
}
for _, test := range tests {
err := checkVBoxManageVersion(test.version)
assert.NoError(t, err)
}
}
func TestInvalidCheckVBoxManageVersion(t *testing.T) {
var tests = []struct {
version string
expectedError string
}{
{"3.9", `We support Virtualbox starting with version 5. Your VirtualBox install is "3.9". Please upgrade at https://www.virtualbox.org`},
{"4.0", `We support Virtualbox starting with version 5. Your VirtualBox install is "4.0". Please upgrade at https://www.virtualbox.org`},
{"4.1.1", `We support Virtualbox starting with version 5. Your VirtualBox install is "4.1.1". Please upgrade at https://www.virtualbox.org`},
{"4.2.36-104064", `We support Virtualbox starting with version 5. Your VirtualBox install is "4.2.36-104064". Please upgrade at https://www.virtualbox.org`},
{"X.Y", `We support Virtualbox starting with version 5. Your VirtualBox install is "X.Y". Please upgrade at https://www.virtualbox.org`},
{"", `We support Virtualbox starting with version 5. Your VirtualBox install is "". Please upgrade at https://www.virtualbox.org`},
}
for _, test := range tests {
err := checkVBoxManageVersion(test.version)
assert.EqualError(t, err, test.expectedError)
}
}
func TestVbmOutErr(t *testing.T) {
var cmdRun *exec.Cmd
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error {
cmdRun = cmd
fmt.Fprint(cmd.Stdout, "Printed to StdOut")
fmt.Fprint(cmd.Stderr, "Printed to StdErr")
return nil
}
stdOut, stdErr, err := vBoxManager.vbmOutErr("arg1", "arg2")
assert.Equal(t, []string{vboxManageCmd, "arg1", "arg2"}, cmdRun.Args)
assert.Equal(t, "Printed to StdOut", stdOut)
assert.Equal(t, "Printed to StdErr", stdErr)
assert.NoError(t, err)
}
func TestVbmOutErrError(t *testing.T) {
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error { return errors.New("BUG") }
_, _, err := vBoxManager.vbmOutErr("arg1", "arg2")
assert.EqualError(t, err, "BUG")
}
func TestVbmOutErrNotFound(t *testing.T) {
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error { return &exec.Error{Err: exec.ErrNotFound} }
_, _, err := vBoxManager.vbmOutErr("arg1", "arg2")
assert.Equal(t, ErrVBMNotFound, err)
}
func TestVbmOutErrFailingWithExitStatus(t *testing.T) {
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error {
fmt.Fprint(cmd.Stderr, "error: Unable to run vbox")
return errors.New("exit status BUG")
}
_, _, err := vBoxManager.vbmOutErr("arg1", "arg2", "arg3")
assert.EqualError(t, err, vboxManageCmd+" arg1 arg2 arg3 failed:\nerror: Unable to run vbox")
}
func TestVbmOutErrRetryOnce(t *testing.T) {
var cmdRun *exec.Cmd
var runCount int
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error {
cmdRun = cmd
runCount++
if runCount == 1 {
fmt.Fprint(cmd.Stderr, "error: The object is not ready")
return errors.New("Fail the first time it's called")
}
fmt.Fprint(cmd.Stdout, "Printed to StdOut")
return nil
}
stdOut, stdErr, err := vBoxManager.vbmOutErr("command", "arg")
assert.Equal(t, 2, runCount)
assert.Equal(t, []string{vboxManageCmd, "command", "arg"}, cmdRun.Args)
assert.Equal(t, "Printed to StdOut", stdOut)
assert.Empty(t, stdErr)
assert.NoError(t, err)
}
func TestVbmOutErrRetryMax(t *testing.T) {
var runCount int
vBoxManager := NewVBoxManager()
vBoxManager.runCmd = func(cmd *exec.Cmd) error {
runCount++
fmt.Fprint(cmd.Stderr, "error: The object is not ready")
return errors.New("Always fail")
}
stdOut, stdErr, err := vBoxManager.vbmOutErr("command", "arg")
assert.Equal(t, 5, runCount)
assert.Empty(t, stdOut)
assert.Equal(t, "error: The object is not ready", stdErr)
assert.Error(t, err)
}