forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_environment.go
400 lines (336 loc) · 9.81 KB
/
test_environment.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package integration
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/bosh-agent/agentclient"
"github.com/cloudfoundry/bosh-agent/agentclient/http"
boshsettings "github.com/cloudfoundry/bosh-agent/settings"
"github.com/cloudfoundry/bosh-utils/httpclient"
"github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type TestEnvironment struct {
cmdRunner boshsys.CmdRunner
currentDeviceNum int
sshTunnelProc boshsys.Process
logger logger.Logger
agentClient agentclient.AgentClient
}
func NewTestEnvironment(
cmdRunner boshsys.CmdRunner,
) *TestEnvironment {
return &TestEnvironment{
cmdRunner: cmdRunner,
currentDeviceNum: 2,
logger: logger.NewLogger(logger.LevelDebug),
}
}
func (t *TestEnvironment) SetupConfigDrive() error {
loopDeviceNum, err := t.AttachLoopDevice(10)
if err != nil {
return err
}
setupConfigDriveTemplate := `
sudo mkfs -t ext3 -m 1 -v /dev/loop%d
sudo e2label /dev/loop%d config-2
sudo rm -rf /tmp/config-drive
sudo mkdir /tmp/config-drive
sudo mount /dev/disk/by-label/config-2 /tmp/config-drive
sudo chown vagrant:vagrant /tmp/config-drive
sudo mkdir -p /tmp/config-drive/ec2/latest
sudo cp %s/meta-data.json /tmp/config-drive/ec2/latest/meta-data.json
sudo cp %s/user-data.json /tmp/config-drive/ec2/latest/user-data.json
sudo umount /tmp/config-drive
`
setupConfigDriveScript := fmt.Sprintf(setupConfigDriveTemplate, loopDeviceNum, loopDeviceNum, t.assetsDir(), t.assetsDir())
_, err = t.RunCommand(setupConfigDriveScript)
return err
}
func (t *TestEnvironment) CleanupDataDir() error {
t.RunCommand(`sudo /var/vcap/bosh/bin/monit stop all`)
mountPoints, err := t.RunCommand(`sudo mount | grep "on /var/vcap/data" | cut -d ' ' -f 3`)
if err != nil {
return err
}
for _, mountPoint := range strings.Split(mountPoints, "\n") {
if mountPoint != "" {
t.RunCommand(fmt.Sprintf("sudo umount -l %s", mountPoint))
}
}
_, err = t.RunCommand("sudo rm -rf /var/vcap/data")
return err
}
// ConfigureAgentForGenericInfrastructure executes the agent_runit.sh asset.
// Required for reverse-compatibility with older bosh-lite
// (remove once a new warden stemcell is built).
func (t *TestEnvironment) ConfigureAgentForGenericInfrastructure() error {
_, err := t.RunCommand(
fmt.Sprintf(
"sudo cp %s/agent_runit.sh /etc/service/agent/run",
t.assetsDir(),
),
)
return err
}
func (t *TestEnvironment) CleanupLogFile() error {
_, err := t.RunCommand("sudo truncate -s 0 /var/vcap/bosh/log/current")
return err
}
func (t *TestEnvironment) LogFileContains(content string) bool {
_, err := t.RunCommand(fmt.Sprintf(`sudo grep "%s" /var/vcap/bosh/log/current`, content))
return err == nil
}
func (t *TestEnvironment) AttachDevice(devicePath string, partitionSize, numPartitions int) error {
partitionPath := devicePath
for i := 0; i <= numPartitions; i++ {
if i > 0 {
partitionPath = fmt.Sprintf("%s%d", devicePath, i)
}
loopDeviceNum, err := t.AttachLoopDevice(partitionSize)
if err != nil {
return err
}
t.RemoveDevice(partitionPath)
_, err = t.RunCommand(fmt.Sprintf("sudo mknod %s b 7 %d", partitionPath, loopDeviceNum))
if err != nil {
return err
}
}
return nil
}
func (t *TestEnvironment) AttachPartitionedRootDevice(devicePath string, sizeInMB, rootPartitionSizeInMB int) (string, string, error) {
// Partitioner requires fs backed device
_, err := t.RunCommand(fmt.Sprintf("sudo mknod %s b 7 99", devicePath))
if err != nil {
return "", "", err
}
attachDeviceTemplate := `
sudo rm -rf /virtual-root-fs
sudo dd if=/dev/zero of=/virtual-root-fs bs=1M count=%d
sudo losetup %s /virtual-root-fs
`
attachDeviceScript := fmt.Sprintf(attachDeviceTemplate, sizeInMB, devicePath)
_, err = t.RunCommand(attachDeviceScript)
if err != nil {
return "", "", err
}
err = t.AttachDevice(devicePath, sizeInMB, 3)
if err != nil {
return "", "", err
}
// Create only first partition, agent will partition the rest for ephemeral disk
partitionTemplate := `
echo ',%d,L,' | sudo sfdisk -uM %s
`
partitionScript := fmt.Sprintf(partitionTemplate, rootPartitionSizeInMB, devicePath)
_, err = t.RunCommand(partitionScript)
if err != nil {
return "", "", err
}
rootLink, err := t.RunCommand("df / | grep /dev/ | cut -d' ' -f1")
if err != nil {
return "", "", err
}
oldRootDevice, err := t.RunCommand(fmt.Sprintf("readlink -f %s", rootLink))
if err != nil {
return "", "", err
}
// Agent reads the symlink to get root device
// Replace the symlink with our fake device
err = t.SwitchRootDevice(devicePath, rootLink)
if err != nil {
return "", "", err
}
return strings.TrimSpace(oldRootDevice), strings.TrimSpace(rootLink), nil
}
func (t *TestEnvironment) SwitchRootDevice(devicePath, rootLink string) error {
_, err := t.RunCommand(fmt.Sprintf("sudo rm -f %s", rootLink))
if err != nil {
return err
}
_, err = t.RunCommand(fmt.Sprintf("sudo ln -s %s1 %s", devicePath, rootLink))
if err != nil {
return err
}
return nil
}
func (t *TestEnvironment) DetachDevice(devicePath string) error {
mountPoint, err := t.RunCommand(fmt.Sprintf("sudo mount | grep %s | cut -d ' ' -f 3", devicePath))
if err != nil {
return err
}
if mountPoint != "" {
_, err = t.RunCommand(fmt.Sprintf("sudo umount -l %s", mountPoint))
if err != nil {
return err
}
}
_, err = t.RunCommand(fmt.Sprintf("sudo rm -f %s*", devicePath))
return err
}
func (t *TestEnvironment) RemoveDevice(devicePath string) error {
_, err := t.RunCommand(fmt.Sprintf("sudo rm -f %s", devicePath))
return err
}
func (t *TestEnvironment) AttachLoopDevice(size int) (int, error) {
loopDeviceNum := t.currentDeviceNum
devicePath := fmt.Sprintf("/dev/loop%d", t.currentDeviceNum)
t.DetachLoopDevice(devicePath)
attachDeviceTemplate := `
sudo rm -rf /virtualfs-%d
sudo dd if=/dev/zero of=/virtualfs-%d bs=1M count=%d
sudo losetup %s /virtualfs-%d
`
attachDeviceScript := fmt.Sprintf(attachDeviceTemplate, loopDeviceNum, loopDeviceNum, size, devicePath, loopDeviceNum)
_, err := t.RunCommand(attachDeviceScript)
if err != nil {
return 0, err
}
t.currentDeviceNum++
return loopDeviceNum, nil
}
func (t *TestEnvironment) DetachLoopDevice(devicePath string) error {
_, err := t.RunCommand(fmt.Sprintf("sudo losetup -d %s", devicePath))
return err
}
func (t *TestEnvironment) UpdateAgentConfig(configFile string) error {
_, err := t.RunCommand(
fmt.Sprintf(
"sudo cp %s/%s /var/vcap/bosh/agent.json",
t.assetsDir(),
configFile,
),
)
return err
}
func (t *TestEnvironment) RestartAgent() error {
err := t.StopAgent()
if err != nil {
return err
}
return t.StartAgent()
}
func (t *TestEnvironment) StopAgent() error {
_, err := t.RunCommand("nohup sudo sv stop agent &")
return err
}
func (t *TestEnvironment) StartAgent() error {
_, err := t.RunCommand("nohup sudo sv start agent &")
return err
}
type emptyReader struct{}
func (er emptyReader) Read(p []byte) (int, error) {
time.Sleep(1 * time.Second)
return 0, nil
}
func (t *TestEnvironment) StartAgentTunnel(mbusUser, mbusPass string, mbusPort int) (agentclient.AgentClient, error) {
if t.sshTunnelProc != nil {
return nil, fmt.Errorf("Already running")
}
sshCmd := boshsys.Command{
Name: "vagrant",
Args: []string{
"ssh",
"--",
fmt.Sprintf("-L16868:127.0.0.1:%d", mbusPort),
},
Stdin: emptyReader{},
}
newTunnelProc, err := t.cmdRunner.RunComplexCommandAsync(sshCmd)
if err != nil {
return nil, err
}
t.sshTunnelProc = newTunnelProc
httpClient := httpclient.NewHTTPClient(httpclient.DefaultClient, t.logger)
mbusURL := fmt.Sprintf("https://%s:%s@localhost:16868", mbusUser, mbusPass)
client := http.NewAgentClient(mbusURL, "fake-director-uuid", 1*time.Second, 10, httpClient, t.logger)
for i := 1; i < 1000000; i++ {
t.logger.Debug("test environment", "Trying to contact agent via ssh tunnel...")
time.Sleep(1 * time.Second)
_, err := client.Ping()
if err == nil {
break
}
t.logger.Debug("test environment", err.Error())
}
return client, nil
}
func (t *TestEnvironment) StopAgentTunnel() error {
if t.sshTunnelProc == nil {
return fmt.Errorf("Not running")
}
t.sshTunnelProc.Wait()
t.sshTunnelProc = nil
return nil
}
func (t *TestEnvironment) StartRegistry(settings boshsettings.Settings) error {
settingsJSON, err := json.Marshal(settings)
if err != nil {
return err
}
_, err = t.RunCommand("sudo rm -f /var/vcap/bosh/settings.json")
if err != nil {
return err
}
t.RunCommand("sudo killall -9 fake-registry")
_, err = t.RunCommand(
fmt.Sprintf(
`nohup %s/tmp/fake-registry -user user -password pass -host 127.0.0.1 -port 9090 -instance instance-id -settings %s &> /dev/null &`,
t.agentDir(),
strconv.Quote(string(settingsJSON)),
),
)
return err
}
func (t *TestEnvironment) GetVMNetworks() (boshsettings.Networks, error) {
stdout, _, _, err := t.cmdRunner.RunCommand("vagrant", "status")
if err != nil {
return boshsettings.Networks{}, err
}
if strings.Contains(stdout, "virtualbox") {
return boshsettings.Networks{
"eth0": {
Type: "dynamic",
},
"eth1": {
Type: "manual",
IP: "192.168.50.4",
Netmask: "255.255.255.0",
},
}, nil
}
if strings.Contains(stdout, "aws") {
return boshsettings.Networks{
"eth0": {
Type: "dynamic",
},
}, nil
}
return boshsettings.Networks{}, nil
}
func (t *TestEnvironment) GetFileContents(filePath string) (string, error) {
return t.RunCommand(
fmt.Sprintf(
`cat %s`,
filePath,
),
)
}
func (t *TestEnvironment) RunCommand(command string) (string, error) {
stdout, _, _, err := t.cmdRunner.RunCommand(
"vagrant",
"ssh",
"-c",
command,
)
return stdout, err
}
func (t *TestEnvironment) agentDir() string {
return "/home/vagrant/go/src/github.com/cloudfoundry/bosh-agent"
}
func (t *TestEnvironment) assetsDir() string {
return fmt.Sprintf("%s/integration/assets", t.agentDir())
}