-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathvfkit_suite_test.go
253 lines (217 loc) · 6.75 KB
/
vfkit_suite_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
241
242
243
244
245
246
247
248
249
250
251
252
253
//go:build darwin
package e2evfkit
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
e2e_utils "github.com/containers/gvisor-tap-vsock/test-utils"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
"golang.org/x/mod/semver"
)
func TestSuite(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "gvisor-tap-vsock suite")
}
const (
sock = "/tmp/gvproxy-api-vfkit.sock"
vfkitSock = "/tmp/vfkit.sock"
sshPort = 2223
ignitionUser = "test"
// #nosec "test" (for manual usage)
ignitionPasswordHash = "$y$j9T$TqJWt3/mKJbH0sYi6B/LD1$QjVRuUgntjTHjAdAkqhkr4F73m.Be4jBXdAaKw98sPC" // notsecret
efiStore = "efi-variable-store"
vfkitVersionNeeded = 0.6
)
var (
tmpDir string
binDir string
host *exec.Cmd
client *exec.Cmd
privateKeyFile string
publicKeyFile string
ignFile string
cmdDir string
)
var debugEnabled = flag.Bool("debug", false, "enable debugger")
func init() {
flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory")
flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries")
privateKeyFile = filepath.Join(tmpDir, "id_test")
publicKeyFile = privateKeyFile + ".pub"
ignFile = filepath.Join(tmpDir, "test.ign")
cmdDir = "../cmd"
}
var _ = ginkgo.BeforeSuite(func() {
// clear the environment before running the tests. It may happen the tests were abruptly stopped earlier leaving a dirty env
clear()
// check if vfkit version is greater than v0.5 (ignition support is available starting from v0.6)
version, err := vfkitVersion()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(version >= vfkitVersionNeeded).Should(gomega.BeTrue())
// check if ssh port is free
gomega.Expect(e2e_utils.IsPortAvailable(sshPort)).Should(gomega.BeTrue())
gomega.Expect(os.MkdirAll(filepath.Join(tmpDir, "disks"), os.ModePerm)).Should(gomega.Succeed())
downloader, err := e2e_utils.NewFcosDownloader(filepath.Join(tmpDir, "disks"))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
fcosImage, err := downloader.DownloadImage("applehv", "raw.gz")
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
publicKey, err := e2e_utils.CreateSSHKeys(publicKeyFile, privateKeyFile)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = e2e_utils.CreateIgnition(ignFile, publicKey, ignitionUser, ignitionPasswordHash)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
errors := make(chan error)
outer:
for panics := 0; ; panics++ {
_ = os.Remove(sock)
_ = os.Remove(vfkitSock)
gvproxyArgs := []string{fmt.Sprintf("--ssh-port=%d", sshPort), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-vfkit=unixgram://%s", vfkitSock)}
if *debugEnabled {
dlvArgs := []string{"debug", "--headless", "--listen=:2345", "--api-version=2", "--accept-multiclient", filepath.Join(cmdDir, "gvproxy"), "--"}
dlvArgs = append(dlvArgs, gvproxyArgs...)
host = exec.Command("dlv", dlvArgs...)
} else {
// #nosec
host = exec.Command(filepath.Join(binDir, "gvproxy"), gvproxyArgs...)
}
host.Stderr = os.Stderr
host.Stdout = os.Stdout
gomega.Expect(host.Start()).Should(gomega.Succeed())
go func() {
if err := host.Wait(); err != nil {
log.Error(err)
errors <- err
}
}()
for {
_, err := os.Stat(sock)
if os.IsNotExist(err) {
log.Info("waiting for vfkit-api socket")
time.Sleep(100 * time.Millisecond)
continue
}
_, err = os.Stat(vfkitSock)
if os.IsNotExist(err) {
log.Info("waiting for vfkit socket")
time.Sleep(100 * time.Millisecond)
continue
}
break
}
vfkitArgs := `--cpus 2 --memory 2048 --bootloader efi,variable-store=%s,create --device virtio-blk,path=%s --ignition %s --device virtio-net,unixSocketPath=%s,mac=5a:94:ef:e4:0c:ee`
// #nosec
client = exec.Command(vfkitExecutable(), strings.Split(fmt.Sprintf(vfkitArgs, efiStore, fcosImage, ignFile, vfkitSock), " ")...)
client.Stderr = os.Stderr
client.Stdout = os.Stdout
gomega.Expect(client.Start()).Should(gomega.Succeed())
go func() {
if err := client.Wait(); err != nil {
log.Error(err)
errors <- err
}
}()
for {
_, err := sshExec("whoami")
if err == nil {
break outer
}
select {
case err := <-errors:
log.Errorf("Error %v", err)
// this expect will always fail so the tests stop
gomega.Expect(err).To(gomega.Equal(nil))
break outer
case <-time.After(1 * time.Second):
log.Infof("waiting for client to connect: %v", err)
}
}
}
time.Sleep(5 * time.Second)
})
func vfkitVersion() (float64, error) {
executable := vfkitExecutable()
if executable == "" {
return 0, fmt.Errorf("vfkit executable not found")
}
out, err := exec.Command(executable, "-v").Output()
if err != nil {
return 0, err
}
version := strings.TrimPrefix(string(out), "vfkit version:")
majorMinor := strings.TrimPrefix(semver.MajorMinor(strings.TrimSpace(version)), "v")
versionF, err := strconv.ParseFloat(majorMinor, 64)
if err != nil {
return 0, err
}
return versionF, nil
}
func vfkitExecutable() string {
vfkitBinaries := []string{"vfkit"}
for _, binary := range vfkitBinaries {
path, err := exec.LookPath(binary)
if err == nil && path != "" {
return path
}
}
return ""
}
func sshExec(cmd ...string) ([]byte, error) {
return sshCommand(cmd...).Output()
}
func sshCommand(cmd ...string) *exec.Cmd {
sshCmd := exec.Command("ssh",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "IdentitiesOnly=yes",
"-i", privateKeyFile,
"-p", strconv.Itoa(sshPort),
fmt.Sprintf("%[email protected]", ignitionUser), "--", strings.Join(cmd, " ")) // #nosec G204
return sshCmd
}
func clear() {
_ = os.Remove(efiStore)
_ = os.Remove(sock)
_ = os.Remove(vfkitSock)
// this should be handled by vfkit once https://github.com/crc-org/vfkit/pull/230 gets merged
// it removes the ignition.sock file
socketPath := filepath.Join(os.TempDir(), "ignition.sock")
_ = os.Remove(socketPath)
}
func scp(src, dst string) error {
sshCmd := exec.Command("/usr/bin/scp",
"-o", "UserKnownHostsFile=/dev/null",
"-o", "StrictHostKeyChecking=no",
"-o", "IdentitiesOnly=yes",
"-i", privateKeyFile,
"-P", strconv.Itoa(sshPort),
src, dst) // #nosec G204
sshCmd.Stderr = os.Stderr
sshCmd.Stdout = os.Stdout
return sshCmd.Run()
}
func scpToVM(src, dst string) error {
return scp(src, fmt.Sprintf("%[email protected]:%s", ignitionUser, dst))
}
func scpFromVM(src, dst string) error {
return scp(fmt.Sprintf("%[email protected]:%s", ignitionUser, src), dst)
}
var _ = ginkgo.AfterSuite(func() {
if host != nil {
if err := host.Process.Kill(); err != nil {
log.Error(err)
}
}
if client != nil {
if err := client.Process.Kill(); err != nil {
log.Error(err)
}
}
clear()
})