-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnetboot_test.go
164 lines (145 loc) · 4.87 KB
/
netboot_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 mtest
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// testNetboot tests iPXE boot
func testNetboot() {
It("is achieved", func() {
By("Set-up kernel params")
sabactlSafe("kernel-params", "set", "\"coreos.autologin=ttyS0 console=ttyS0\"")
By("Uploading an image")
kernel := filepath.Join("/var/tmp", filepath.Base(coreosKernel))
initrd := filepath.Join("/var/tmp", filepath.Base(coreosInitrd))
sabactlSafe("images", "upload", coreosVersion, kernel, initrd)
By("Waiting images to be distributed")
Eventually(func() error {
var index []struct {
ID string `json:"id"`
URLs []string `json:"urls"`
}
stdout, stderr, err := sabactl("images", "index")
if err != nil {
return fmt.Errorf("%v: stderr=%s", err, stderr)
}
err = json.Unmarshal([]byte(stdout), &index)
if err != nil {
return err
}
for _, img := range index {
if img.ID != coreosVersion {
continue
}
if len(img.URLs) == 3 {
return errors.New("uploaded image does not have 3 urls")
}
}
return nil
}).Should(Succeed())
By("Waiting worker to boot")
Expect(prepareSSHClients(worker1, worker2)).NotTo(HaveOccurred())
for _, worker := range []string{worker1, worker2} {
By("Checking kernel boot parameter")
stdout, stderr, err := execAt(worker, "cat", "/proc/cmdline")
Expect(err).NotTo(HaveOccurred(), "stderr=%s", stderr)
Expect(string(stdout)).To(ContainSubstring("coreos.autologin=ttyS0"))
By("Checking encrypted disks")
Eventually(func() error {
_, stderr, err := execAt(worker, "ls", "/dev/mapper/crypt-*")
if err != nil {
return fmt.Errorf("%v: stderr=%s", err, stderr)
}
stdout, stderr, err = execAt(worker, "sudo", "dmsetup", "table", "/dev/mapper/crypt-*")
if err != nil {
return fmt.Errorf("%v: stderr=%s", err, stderr)
}
scanner := bufio.NewScanner(bytes.NewReader(stdout))
for scanner.Scan() {
hasNoReadWorkQueue := false
hasNoWriteWorkQueue := false
for _, field := range strings.Fields(scanner.Text()) {
if field == "no_read_workqueue" {
hasNoReadWorkQueue = true
}
if field == "no_write_workqueue" {
hasNoWriteWorkQueue = true
}
}
if !hasNoReadWorkQueue || !hasNoWriteWorkQueue {
return fmt.Errorf("no_read_workqueue or no_write_workqueue is not set: %s", scanner.Text())
}
}
if err = scanner.Err(); err != nil {
return fmt.Errorf("reading stdout: %w", err)
}
return nil
}, 6*time.Minute).Should(Succeed())
By("Checking status of sabakan-cryptsetup")
Eventually(func() error {
stdout, _, err := execAt(worker, "systemctl", "is-active", "sabakan-cryptsetup")
if err != nil {
return err
}
if strings.TrimSpace(string(stdout)) != "active" {
return fmt.Errorf("sabakan-cryptsetup is not active:%s", stdout)
}
return nil
}).Should(Succeed())
}
By("Copying readnvram binary")
remoteFilename := filepath.Join("/var/tmp", filepath.Base(readNVRAM))
copyReadNVRAM(worker2, remoteFilename)
By("Reading encryption key from NVRAM")
ekHexBefore, stderr, err := execAt(worker2, "sudo", remoteFilename)
Expect(err).NotTo(HaveOccurred(), "stdout=%s, stderr=%s", ekHexBefore, stderr)
By("Checking encryption key is kept after reboot")
// Exit code is 255 when ssh is disconnected
execAt(worker2, "sudo", "reboot")
Expect(prepareSSHClients(worker2)).NotTo(HaveOccurred())
copyReadNVRAM(worker2, remoteFilename)
ekHexAfter, stderr, err := execAt(worker2, "sudo", remoteFilename)
Expect(err).NotTo(HaveOccurred(), "stdout=%s, stderr=%s", ekHexAfter, stderr)
Expect(ekHexAfter).To(Equal(ekHexBefore))
By("Checking encrypted disks")
Eventually(func() error {
_, stderr, err := execAt(worker2, "ls", "/dev/mapper/crypt-*")
if err != nil {
return fmt.Errorf("%v: stderr=%s", err, stderr)
}
return nil
}, 6*time.Minute).Should(Succeed())
By("Removing the image from the index")
sabactlSafe("images", "delete", coreosVersion)
By("Checking all servers remove the image")
Eventually(func() error {
for _, h := range []string{host1, host2, host3} {
stdout, _, err := execAt(h, "ls", "/var/lib/sabakan/images/coreos")
if err != nil || len(stdout) > 0 {
return err
}
}
return nil
}).Should(Succeed())
})
}
func copyReadNVRAM(worker, remoteFilename string) {
f, err := os.Open(readNVRAM)
Expect(err).NotTo(HaveOccurred())
defer f.Close()
_, err = f.Seek(0, io.SeekStart)
Expect(err).NotTo(HaveOccurred())
stdout, stderr, err := execAtWithStream(worker, f, "dd", "of="+remoteFilename)
Expect(err).NotTo(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
stdout, stderr, err = execAt(worker, "chmod", "755", remoteFilename)
Expect(err).NotTo(HaveOccurred(), "stdout=%s, stderr=%s", stdout, stderr)
}