forked from go-debos/fakemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_test.go
164 lines (127 loc) · 3.46 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
package fakemachine
import (
"bufio"
"flag"
"github.com/stretchr/testify/assert"
"io"
"os"
"strings"
"testing"
)
func TestSuccessfullCommand(t *testing.T) {
m := NewMachine()
exitcode, _ := m.Run("ls /")
if exitcode != 0 {
t.Fatalf("Expected 0 but got %d", exitcode)
}
}
func TestCommandNotFound(t *testing.T) {
m := NewMachine()
exitcode, _ := m.Run("/a/b/c /")
if exitcode != 127 {
t.Fatalf("Expected 127 but got %d", exitcode)
}
}
func TestImage(t *testing.T) {
m := NewMachine()
m.CreateImage("test.img", 1024*1024)
exitcode, _ := m.Run("test -b /dev/vda")
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")
assert.Nil(t, err)
mtab := bufio.NewReader(m)
for {
line, err := mtab.ReadString('\n')
if err == io.EOF {
assert.Fail(t, "mountpoint not found")
break
}
assert.Nil(t, err)
fields := strings.Fields(line)
if fields[1] == mountpoint {
assert.Equal(t, fields[2], fstype)
return
}
}
}
func TestScratchTmp(t *testing.T) {
if InMachine() {
AssertMount(t, "/scratch", "tmpfs")
return
}
m := NewMachine()
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) {
if InMachine() {
AssertMount(t, "/scratch", "ext4")
return
}
m := NewMachine()
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) {
m := NewMachine()
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 tmpfs mount on scratch failed with %d", exitcode)
}
}
func TestSpawnMachine(t *testing.T) {
if InMachine() {
t.Log("Running in the machine")
return
}
m := NewMachine()
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) {
if InMachine() {
t.Log("Running in the machine")
devices := flag.Args()
assert.Equal(t, len(devices), 2, "Only expected two devices")
autolabel := devices[0]
labeled := devices[1]
info, err := os.Stat(autolabel)
assert.Nil(t, err)
assert.Equal(t, info.Mode()&os.ModeType, os.ModeDevice, "Expected a device")
info, err = os.Stat(labeled)
assert.Nil(t, err)
assert.Equal(t, info.Mode()&os.ModeType, os.ModeDevice, "Expected a device")
return
}
m := NewMachine()
autolabel, err := m.CreateImage("test-autolabel.img", 1024*1024)
assert.Nil(t, err)
labeled, err := m.CreateImageWithLabel("test-labeled.img", 1024*1024, "test-labeled")
assert.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)
}
}