forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_environment.go
565 lines (474 loc) · 13.4 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package integration
import (
"encoding/json"
"fmt"
"io/ioutil"
"sort"
"strconv"
"strings"
"time"
"github.com/cloudfoundry/bosh-agent/agentclient"
"github.com/cloudfoundry/bosh-agent/integration/integrationagentclient"
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"
"github.com/kevinburke/ssh_config"
"golang.org/x/crypto/ssh"
)
type TestEnvironment struct {
cmdRunner boshsys.CmdRunner
currentDeviceNum int
sshTunnelProc boshsys.Process
logger logger.Logger
agentClient agentclient.AgentClient
deviceMap map[int]string
sshClient *ssh.Client
}
func NewTestEnvironment(
cmdRunner boshsys.CmdRunner,
) (*TestEnvironment, error) {
client, err := dialSSHClient(cmdRunner)
if err != nil {
return nil, err
}
return &TestEnvironment{
cmdRunner: cmdRunner,
currentDeviceNum: 2,
logger: logger.NewLogger(logger.LevelDebug),
deviceMap: make(map[int]string),
sshClient: client,
}, nil
}
func (t *TestEnvironment) SetupConfigDrive() error {
deviceNum, err := t.AttachLoopDevice(10)
if err != nil {
return err
}
setupConfigDriveTemplate := `
sudo mkfs -t ext3 -m 1 -v %s
sudo e2label %s 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, t.deviceMap[deviceNum], t.deviceMap[deviceNum], t.assetsDir(), t.assetsDir())
_, err = t.RunCommand(setupConfigDriveScript)
return err
}
type byLen []string
func (a byLen) Len() int { return len(a) }
func (a byLen) Less(i, j int) bool { return len(a[i]) > len(a[j]) }
func (a byLen) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (t *TestEnvironment) DetachDevice(dir string) error {
mountPoints, err := t.RunCommand(fmt.Sprintf(`sudo mount | grep "on %s" | cut -d ' ' -f 3`, dir))
if err != nil {
return err
}
mountPointsSlice := strings.Split(mountPoints, "\n")
sort.Sort(byLen(mountPointsSlice))
for _, mountPoint := range mountPointsSlice {
if mountPoint != "" {
t.RunCommand(fmt.Sprintf("sudo fuser -km %s", mountPoint))
t.RunCommand(fmt.Sprintf("sudo umount %s", mountPoint))
}
}
_, err = t.RunCommand(fmt.Sprintf("sudo rm -rf %s", dir))
return err
}
func (t *TestEnvironment) CleanupDataDir() error {
t.RunCommand(`sudo /var/vcap/bosh/bin/monit stop all`)
_, err := t.RunCommand("! mount | grep -q ' on /tmp ' || sudo umount /tmp")
if err != nil {
return err
}
err = t.DetachDevice("/var/tmp")
if err != nil {
return err
}
err = t.DetachDevice("/var/log")
if err != nil {
return err
}
err = t.DetachDevice("/var/vcap/data")
if err != nil {
return err
}
_, err = t.RunCommand("sudo mkdir -p /var/tmp")
if err != nil {
return err
}
_, err = t.RunCommand("sudo chmod 700 /var/tmp")
if err != nil {
return err
}
_, err = t.RunCommand("sudo chmod 1777 /tmp")
if err != nil {
return err
}
_, err = t.RunCommand("sudo mkdir -p /var/log")
if err != nil {
return err
}
_, err = t.RunCommand("sudo chmod 775 /var/log")
if err != nil {
return err
}
_, err = t.RunCommand("sudo chown root:syslog /var/log")
if err != nil {
return err
}
return nil
}
// 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) CleanupSSH() error {
_, err := t.RunCommand("sudo rm -rf /var/vcap/bosh_ssh")
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)
}
deviceNum, err := t.AttachLoopDevice(partitionSize)
if err != nil {
return err
}
output, err := t.RunCommand(fmt.Sprintf("ls -al %s | cut -d' ' -f 6", t.deviceMap[deviceNum]))
minorNum := strings.TrimSpace(output)
if err != nil {
return err
}
err = t.RemoveDevice(partitionPath)
if err != nil {
return err
}
_, err = t.RunCommand(fmt.Sprintf("sudo mknod %s b 7 %s", partitionPath, minorNum))
if err != nil {
return err
}
}
return nil
}
func (t *TestEnvironment) AttachPartitionedRootDevice(devicePath string, sizeInMB, rootPartitionSizeInMB int) (string, error) {
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
}
_, err = t.RunCommand(fmt.Sprintf("sudo mv %s %s-temp", strings.TrimSpace(oldRootDevice), strings.TrimSpace(oldRootDevice)))
if err != nil {
return "", err
}
// Agent reads the symlink to get root device
// Create a symlink to our fake device
_, err = t.RunCommand(fmt.Sprintf("sudo ln -sf %s1 %s", devicePath, strings.TrimSpace(rootLink)))
if err != nil {
return strings.TrimSpace(oldRootDevice), err
}
return strings.TrimSpace(oldRootDevice), nil
}
func (t *TestEnvironment) DetachPartitionedRootDevice(rootLink string, devicePath string) error {
_, err := t.RunCommand(fmt.Sprintf("sudo rm -f %s", rootLink))
if err != nil {
return err
}
partitionPath := devicePath
for i := 3; i >= 0; i-- {
if i > 0 {
partitionPath = fmt.Sprintf("%s%d", devicePath, i)
}
if _, err := t.RunCommand(fmt.Sprintf("losetup %s", partitionPath)); err == nil {
if output, _ := t.RunCommand(fmt.Sprintf("sudo mount | grep '%s ' | awk '{print $3}'", partitionPath)); output != "" {
t.RunCommand(fmt.Sprintf("sudo umount -l %s", output))
}
if i > 0 {
_, _ = t.RunCommand(fmt.Sprintf("sudo parted %s rm %d", devicePath, i))
}
err = t.DetachLoopDevice(partitionPath)
if err != nil {
return err
}
err = t.RemoveDevice(partitionPath)
if err != nil {
return err
}
}
}
_, err = t.RunCommand(fmt.Sprintf("sudo mv %s-temp %s", rootLink, rootLink))
if err != nil {
return err
}
return nil
}
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) {
deviceNum := t.currentDeviceNum
output, err := t.RunCommand("sudo losetup -f")
devicePath := strings.TrimSpace(output)
if err != nil {
return 0, err
}
if oldDevicePath, ok := t.deviceMap[deviceNum]; ok {
t.DetachLoopDevice(oldDevicePath)
}
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, deviceNum, deviceNum, size, devicePath, deviceNum)
_, err = t.RunCommand(attachDeviceScript)
if err != nil {
return 0, err
}
t.deviceMap[deviceNum] = devicePath
t.currentDeviceNum++
return deviceNum, 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) (*integrationagentclient.IntegrationAgentClient, 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 := integrationagentclient.NewIntegrationAgentClient(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) {
s, err := t.sshClient.NewSession()
if err != nil {
return "", err
}
defer s.Close()
out, err := s.Output(command)
if err != nil {
return "", err
}
return string(out), nil
}
func (t *TestEnvironment) RunCommand3(command string) (string, string, int, error) {
return t.cmdRunner.RunCommand("vagrant", "ssh", "-c", command)
}
func (t *TestEnvironment) CreateBlobFromAsset(assetPath, blobID string) error {
_, err := t.RunCommand("sudo mkdir -p /var/vcap/data/blobs")
if err != nil {
return err
}
_, _, _, err = t.cmdRunner.RunCommand(
"vagrant",
"ssh",
"-c",
fmt.Sprintf("sudo cp %s/%s /var/vcap/data/blobs/%s", t.assetsDir(), assetPath, blobID),
)
return 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())
}
func dialSSHClient(cmdRunner boshsys.CmdRunner) (*ssh.Client, error) {
stdout, _, _, err := cmdRunner.RunCommand("vagrant", "ssh-config")
if err != nil {
return nil, err
}
config, err := ssh_config.Decode(strings.NewReader(stdout))
if err != nil {
return nil, err
}
user, err := config.Get("default", "User")
if err != nil {
return nil, err
}
addr, err := config.Get("default", "HostName")
if err != nil {
return nil, err
}
port, err := config.Get("default", "Port")
if err != nil {
return nil, err
}
keyPath, err := config.Get("default", "IdentityFile")
if err != nil {
return nil, err
}
key, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return ssh.Dial("tcp", fmt.Sprintf("%s:%s", addr, port), &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
}