-
Notifications
You must be signed in to change notification settings - Fork 1
/
box_api_test.go
138 lines (114 loc) · 3.21 KB
/
box_api_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
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
package vagrant_go
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestBoxApiList(t *testing.T) {
t.Run(
"with command execution failing and returning an error, it returns an error",
func(t *testing.T) {
t.Parallel()
client := emptyTestClient(t)
isCommandRunCalled := false
client.commandRunFunc = func(cmd string, args ...string) (bytes []byte, e error) {
assert.Equal(t, len(args), 3)
assert.Equal(t, args[0], "--machine-readable")
assert.Equal(t, args[1], "box")
assert.Equal(t, args[2], "list")
isCommandRunCalled = true
return []byte{}, errors.New("fake error")
}
boxAPI := &boxAPI{
client: client,
}
boxes, err := boxAPI.List()
require.Nil(t, boxes)
assert.Contains(t, err.Error(), "command execution failed")
assert.True(t, isCommandRunCalled)
},
)
t.Run(
"with no vagrant boxes available, it returns empty slice",
func(t *testing.T) {
t.Parallel()
boxAPI := &boxAPI{
client: emptyTestClient(t),
}
boxes, err := boxAPI.List()
require.NoError(t, err)
assert.Empty(t, boxes)
},
)
t.Run(
"with 1 vagrant box available, it returns slice of 1 box",
func(t *testing.T) {
t.Parallel()
boxCommandRunFunc := func(cmd string, args ...string) ([]byte, error) {
output := `
1546015529,,ui,info,my-debian (libvirt%!(VAGRANT_COMMA) 0)
1546015529,,box-name,my-debian
1546015529,,box-provider,libvirt
1546015529,,box-version,1.2.3
`
return []byte(output), nil
}
boxAPI := &boxAPI{
client: testClient(
t,
boxCommandRunFunc,
emptyLookPathFunc,
),
}
boxes, err := boxAPI.List()
require.NoError(t, err)
assert.Len(t, boxes, 1)
assert.Equal(t, "my-debian", boxes[0].Name)
assert.Equal(t, "libvirt", boxes[0].Provider)
assert.Equal(t, "1.2.3", boxes[0].Version)
},
)
t.Run(
"with 3 vagrant boxes available, it returns slice of 3 boxes",
func(t *testing.T) {
t.Parallel()
boxCommandRunFunc := func(cmd string, args ...string) ([]byte, error) {
output := `
1546015529,,ui,info,my-debian (libvirt%!(VAGRANT_COMMA) 0)
1546015529,,box-name,my-debian
1546015529,,box-provider,libvirt
1546015529,,box-version,1.2.3
1546015529,,ui,info,my-vbox-debian (virtualbox%!(VAGRANT_COMMA) 0)
1546015529,,box-name,my-vbox-debian
1546015529,,box-provider,virtualbox
1546015529,,box-version,1.2.4
1546015529,,ui,info,my-vmware-debian (vmware%!(VAGRANT_COMMA) 0)
1546015529,,box-name,my-vmware-debian
1546015529,,box-provider,vmware
1546015529,,box-version,1.2.5
`
return []byte(output), nil
}
boxAPI := &boxAPI{
client: testClient(
t,
boxCommandRunFunc,
emptyLookPathFunc,
),
}
boxes, err := boxAPI.List()
require.NoError(t, err)
assert.Len(t, boxes, 3)
assert.Equal(t, "my-debian", boxes[0].Name)
assert.Equal(t, "libvirt", boxes[0].Provider)
assert.Equal(t, "1.2.3", boxes[0].Version)
assert.Equal(t, "my-vbox-debian", boxes[1].Name)
assert.Equal(t, "virtualbox", boxes[1].Provider)
assert.Equal(t, "1.2.4", boxes[1].Version)
assert.Equal(t, "my-vmware-debian", boxes[2].Name)
assert.Equal(t, "vmware", boxes[2].Provider)
assert.Equal(t, "1.2.5", boxes[2].Version)
},
)
}