-
Notifications
You must be signed in to change notification settings - Fork 38
/
machine_test.go
240 lines (189 loc) · 5.27 KB
/
machine_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package fakemachine
import (
"bufio"
"flag"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
var backendName string
var testArg string
func init() {
flag.StringVar(&backendName, "backend", "auto", "Fakemachine backend to use")
flag.StringVar(&testArg, "testarg", "", "Test specific argument")
}
func CreateMachine(t *testing.T) *Machine {
machine, err := NewMachineWithBackend(backendName)
require.Nil(t, err)
machine.SetNumCPUs(2)
return machine
}
func TestSuccessfulCommand(t *testing.T) {
t.Parallel()
m := CreateMachine(t)
exitcode, _ := m.Run("ls /")
if exitcode != 0 {
t.Fatalf("Expected 0 but got %d", exitcode)
}
}
func TestCommandNotFound(t *testing.T) {
t.Parallel()
m := CreateMachine(t)
exitcode, _ := m.Run("/a/b/c /")
if exitcode != 127 {
t.Fatalf("Expected 127 but got %d", exitcode)
}
}
func TestImage(t *testing.T) {
t.Parallel()
m := CreateMachine(t)
_, err := m.CreateImage("test.img", 1024*1024)
require.Nil(t, err)
exitcode, _ := m.Run("test -b /dev/disk/by-fakemachine-label/fakedisk-0")
if exitcode != 0 {
t.Fatalf("Test for the virtual image device failed with %d", exitcode)
}
}
func AssertMount(t *testing.T, mountpoint, fstype string) {
m, err := os.Open("/proc/self/mounts")
require.Nil(t, err)
mtab := bufio.NewReader(m)
for {
line, err := mtab.ReadString('\n')
if err == io.EOF {
require.Fail(t, "mountpoint not found")
break
}
require.Nil(t, err)
fields := strings.Fields(line)
if fields[1] == mountpoint {
require.Equal(t, fields[2], fstype)
return
}
}
}
func TestScratchTmp(t *testing.T) {
t.Parallel()
if InMachine() {
AssertMount(t, "/scratch", "tmpfs")
return
}
m := CreateMachine(t)
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestScratchTmp"})
if exitcode != 0 {
t.Fatalf("Test for tmpfs mount on scratch failed with %d", exitcode)
}
}
func TestScratchDisk(t *testing.T) {
t.Parallel()
if InMachine() {
AssertMount(t, "/scratch", "ext4")
return
}
m := CreateMachine(t)
m.SetScratch(1024*1024*1024, "")
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestScratchDisk"})
if exitcode != 0 {
t.Fatalf("Test for device mount on scratch failed with %d", exitcode)
}
}
func TestMemory(t *testing.T) {
t.Parallel()
m := CreateMachine(t)
m.SetMemory(1024)
// Nasty hack, this gets a chunk of shell script inserted in the wrapper script
// which is not really what fakemachine expects but seems good enough for
// testing
command := `
MEM=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ' )
# MemTotal is usable ram, not physical ram so accept a range
if [ ${MEM} -lt 900000 -o ${MEM} -gt 1024000 ] ; then
exit 1
fi
`
exitcode, _ := m.Run(command)
if exitcode != 0 {
t.Fatalf("Test for set memory failed with %d", exitcode)
}
}
func TestSpawnMachine(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
return
}
m := CreateMachine(t)
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestSpawnMachine"})
if exitcode != 0 {
t.Fatalf("Test for respawning in the machine failed failed with %d", exitcode)
}
}
func TestImageLabel(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
devices := flag.Args()
require.Equal(t, len(devices), 2, "Only expected two devices")
autolabel := devices[0]
labeled := devices[1]
info, err := os.Stat(autolabel)
require.Nil(t, err)
require.Equal(t, info.Mode()&os.ModeType, os.ModeDevice, "Expected a device")
info, err = os.Stat(labeled)
require.Nil(t, err)
require.Equal(t, info.Mode()&os.ModeType, os.ModeDevice, "Expected a device")
return
}
m := CreateMachine(t)
autolabel, err := m.CreateImage("test-autolabel.img", 1024*1024)
require.Nil(t, err)
labeled, err := m.CreateImageWithLabel("test-labeled.img", 1024*1024, "test-labeled")
require.Nil(t, err)
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestImageLabel", autolabel, labeled})
if exitcode != 0 {
t.Fatalf("Test for images in the machine failed failed with %d", exitcode)
}
}
func TestVolumes(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
return
}
/* Try to mount a non-existent file into the machine */
m := CreateMachine(t)
m.AddVolume("random_directory_never_exists")
exitcode, err := m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)
/* Try to mount a device file into the machine */
m = CreateMachine(t)
m.AddVolume("/dev/zero")
exitcode, err = m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)
/* Try to mount a volume with whitespace into the machine */
m = CreateMachine(t)
m.AddVolumeAt("/dev", "/dev ices")
exitcode, err = m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)
}
func TestCommandEscaping(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
require.Equal(t, testArg, "$s'n\\akes")
t.Log(testArg)
return
}
m := CreateMachine(t)
exitcode, _ := m.RunInMachineWithArgs([]string{
"-test.v", "-test.run",
"TestCommandEscaping", "-testarg", "$s'n\\akes"})
if exitcode != 0 {
t.Fatalf("Expected 0 but got %d", exitcode)
}
}