| 1 | |
| 2 | |
| 3 | package e2evfkit |
| 4 | |
| 5 | import ( |
| 6 | "flag" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | e2e_utils "github.com/containers/gvisor-tap-vsock/test-utils" |
| 17 | |
| 18 | "github.com/onsi/ginkgo" |
| 19 | "github.com/onsi/gomega" |
| 20 | log "github.com/sirupsen/logrus" |
| 21 | |
| 22 | "golang.org/x/mod/semver" |
| 23 | ) |
| 24 | |
| 25 | func TestSuite(t *testing.T) { |
| 26 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 27 | ginkgo.RunSpecs(t, "gvisor-tap-vsock suite") |
| 28 | } |
| 29 | |
| 30 | const ( |
| 31 | sock = "/tmp/gvproxy-api-vfkit.sock" |
| 32 | vfkitSock = "/tmp/vfkit.sock" |
| 33 | sshPort = 2223 |
| 34 | ignitionUser = "test" |
| 35 | |
| 36 | ignitionPasswordHash = "$y$j9T$TqJWt3/mKJbH0sYi6B/LD1$QjVRuUgntjTHjAdAkqhkr4F73m.Be4jBXdAaKw98sPC" |
| 37 | efiStore = "efi-variable-store" |
| 38 | vfkitVersionNeeded = 0.6 |
| 39 | ) |
| 40 | |
| 41 | var ( |
| 42 | tmpDir string |
| 43 | binDir string |
| 44 | host *exec.Cmd |
| 45 | client *exec.Cmd |
| 46 | privateKeyFile string |
| 47 | publicKeyFile string |
| 48 | ignFile string |
| 49 | cmdDir string |
| 50 | ) |
| 51 | |
| 52 | var debugEnabled = flag.Bool("debug", false, "enable debugger") |
| 53 | |
| 54 | func init() { |
| 55 | flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory") |
| 56 | flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries") |
| 57 | privateKeyFile = filepath.Join(tmpDir, "id_test_vfkit") |
| 58 | publicKeyFile = privateKeyFile + ".pub" |
| 59 | ignFile = filepath.Join(tmpDir, "test.ign") |
| 60 | cmdDir = "../cmd" |
| 61 | } |
| 62 | |
| 63 | var _ = ginkgo.BeforeSuite(func() { |
| 64 | |
| 65 | cleanup() |
| 66 | |
| 67 | |
| 68 | version, err := vfkitVersion() |
| 69 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 70 | gomega.Expect(version >= vfkitVersionNeeded).Should(gomega.BeTrue()) |
| 71 | |
| 72 | |
| 73 | gomega.Expect(e2e_utils.IsPortAvailable(sshPort)).Should(gomega.BeTrue()) |
| 74 | |
| 75 | gomega.Expect(os.MkdirAll(filepath.Join(tmpDir, "disks"), os.ModePerm)).Should(gomega.Succeed()) |
| 76 | |
| 77 | downloader, err := e2e_utils.NewFcosDownloader(filepath.Join(tmpDir, "disks")) |
| 78 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 79 | fcosImage, err := downloader.DownloadImage("applehv", "raw.gz") |
| 80 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 81 | |
| 82 | publicKey, err := e2e_utils.CreateSSHKeys(publicKeyFile, privateKeyFile) |
| 83 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 84 | |
| 85 | err = e2e_utils.CreateIgnition(ignFile, publicKey, ignitionUser, ignitionPasswordHash) |
| 86 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 87 | |
| 88 | errors := make(chan error) |
| 89 | |
| 90 | outer: |
| 91 | for panics := 0; ; panics++ { |
| 92 | _ = os.Remove(sock) |
| 93 | _ = os.Remove(vfkitSock) |
| 94 | |
| 95 | gvproxyArgs := []string{fmt.Sprintf("--ssh-port=%d", sshPort), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-vfkit=unixgram://%s", vfkitSock)} |
| 96 | if *debugEnabled { |
| 97 | dlvArgs := []string{"debug", "--headless", "--listen=:2345", "--api-version=2", "--accept-multiclient", filepath.Join(cmdDir, "gvproxy"), "--"} |
| 98 | dlvArgs = append(dlvArgs, gvproxyArgs...) |
| 99 | host = exec.Command("dlv", dlvArgs...) |
| 100 | } else { |
| 101 | |
| 102 | host = exec.Command(filepath.Join(binDir, "gvproxy"), gvproxyArgs...) |
| 103 | } |
| 104 | |
| 105 | host.Stderr = os.Stderr |
| 106 | host.Stdout = os.Stdout |
| 107 | gomega.Expect(host.Start()).Should(gomega.Succeed()) |
| 108 | go func() { |
| 109 | if err := host.Wait(); err != nil { |
| 110 | log.Error(err) |
| 111 | errors <- err |
| 112 | } |
| 113 | }() |
| 114 | |
| 115 | for { |
| 116 | _, err := os.Stat(sock) |
| 117 | if os.IsNotExist(err) { |
| 118 | log.Info("waiting for vfkit-api socket") |
| 119 | time.Sleep(100 * time.Millisecond) |
| 120 | continue |
| 121 | } |
| 122 | _, err = os.Stat(vfkitSock) |
| 123 | if os.IsNotExist(err) { |
| 124 | log.Info("waiting for vfkit socket") |
| 125 | time.Sleep(100 * time.Millisecond) |
| 126 | continue |
| 127 | } |
| 128 | break |
| 129 | } |
| 130 | |
| 131 | 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` |
| 132 | |
| 133 | client = exec.Command(vfkitExecutable(), strings.Split(fmt.Sprintf(vfkitArgs, efiStore, fcosImage, ignFile, vfkitSock), " ")...) |
| 134 | client.Stderr = os.Stderr |
| 135 | client.Stdout = os.Stdout |
| 136 | gomega.Expect(client.Start()).Should(gomega.Succeed()) |
| 137 | go func() { |
| 138 | if err := client.Wait(); err != nil { |
| 139 | log.Error(err) |
| 140 | errors <- err |
| 141 | } |
| 142 | }() |
| 143 | |
| 144 | for { |
| 145 | _, err := sshExec("whoami") |
| 146 | if err == nil { |
| 147 | break outer |
| 148 | } |
| 149 | |
| 150 | select { |
| 151 | case err := <-errors: |
| 152 | log.Errorf("Error %v", err) |
| 153 | |
| 154 | gomega.Expect(err).To(gomega.Equal(nil)) |
| 155 | break outer |
| 156 | case <-time.After(1 * time.Second): |
| 157 | log.Infof("waiting for client to connect: %v", err) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | time.Sleep(5 * time.Second) |
| 163 | }) |
| 164 | |
| 165 | func vfkitVersion() (float64, error) { |
| 166 | executable := vfkitExecutable() |
| 167 | if executable == "" { |
| 168 | return 0, fmt.Errorf("vfkit executable not found") |
| 169 | } |
| 170 | out, err := exec.Command(executable, "-v").Output() |
| 171 | if err != nil { |
| 172 | return 0, err |
| 173 | } |
| 174 | version := strings.TrimPrefix(string(out), "vfkit version:") |
| 175 | majorMinor := strings.TrimPrefix(semver.MajorMinor(strings.TrimSpace(version)), "v") |
| 176 | versionF, err := strconv.ParseFloat(majorMinor, 64) |
| 177 | if err != nil { |
| 178 | return 0, err |
| 179 | } |
| 180 | return versionF, nil |
| 181 | } |
| 182 | |
| 183 | func vfkitExecutable() string { |
| 184 | vfkitBinaries := []string{"vfkit"} |
| 185 | for _, binary := range vfkitBinaries { |
| 186 | path, err := exec.LookPath(binary) |
| 187 | if err == nil && path != "" { |
| 188 | return path |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return "" |
| 193 | } |
| 194 | |
| 195 | func sshExec(cmd ...string) ([]byte, error) { |
| 196 | return sshCommand(cmd...).Output() |
| 197 | } |
| 198 | |
| 199 | func sshCommand(cmd ...string) *exec.Cmd { |
| 200 | sshCmd := exec.Command("ssh", |
| 201 | "-o", "UserKnownHostsFile=/dev/null", |
| 202 | "-o", "StrictHostKeyChecking=no", |
| 203 | "-o", "IdentitiesOnly=yes", |
| 204 | "-i", privateKeyFile, |
| 205 | "-p", strconv.Itoa(sshPort), |
| 206 | fmt.Sprintf("%[email protected]", ignitionUser), "--", strings.Join(cmd, " ")) |
| 207 | return sshCmd |
| 208 | } |
| 209 | |
| 210 | func cleanup() { |
| 211 | _ = os.Remove(efiStore) |
| 212 | _ = os.Remove(sock) |
| 213 | _ = os.Remove(vfkitSock) |
| 214 | |
| 215 | |
| 216 | |
| 217 | socketPath := filepath.Join(os.TempDir(), "ignition.sock") |
| 218 | _ = os.Remove(socketPath) |
| 219 | } |
| 220 | |
| 221 | func scp(src, dst string) error { |
| 222 | sshCmd := exec.Command("/usr/bin/scp", |
| 223 | "-o", "UserKnownHostsFile=/dev/null", |
| 224 | "-o", "StrictHostKeyChecking=no", |
| 225 | "-o", "IdentitiesOnly=yes", |
| 226 | "-i", privateKeyFile, |
| 227 | "-P", strconv.Itoa(sshPort), |
| 228 | src, dst) |
| 229 | sshCmd.Stderr = os.Stderr |
| 230 | sshCmd.Stdout = os.Stdout |
| 231 | return sshCmd.Run() |
| 232 | } |
| 233 | func scpToVM(src, dst string) error { |
| 234 | return scp(src, fmt.Sprintf("%[email protected]:%s", ignitionUser, dst)) |
| 235 | } |
| 236 | |
| 237 | func scpFromVM(src, dst string) error { |
| 238 | return scp(fmt.Sprintf("%[email protected]:%s", ignitionUser, src), dst) |
| 239 | } |
| 240 | |
| 241 | var _ = ginkgo.AfterSuite(func() { |
| 242 | if host != nil { |
| 243 | if err := host.Process.Kill(); err != nil { |
| 244 | log.Error(err) |
| 245 | } |
| 246 | } |
| 247 | if client != nil { |
| 248 | if err := client.Process.Kill(); err != nil { |
| 249 | log.Error(err) |
| 250 | } |
| 251 | } |
| 252 | cleanup() |
| 253 | }) |