| 1 | package e2eqemu |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "runtime" |
| 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 | |
| 23 | func TestSuite(t *testing.T) { |
| 24 | gomega.RegisterFailHandler(ginkgo.Fail) |
| 25 | ginkgo.RunSpecs(t, "gvisor-tap-vsock suite") |
| 26 | } |
| 27 | |
| 28 | const ( |
| 29 | sock = "/tmp/gvproxy-api.sock" |
| 30 | qemuPort = 5555 |
| 31 | sshPort = 2222 |
| 32 | ignitionUser = "test" |
| 33 | qconLog = "qcon.log" |
| 34 | podmanSock = "/run/user/1001/podman/podman.sock" |
| 35 | podmanRootSock = "/run/podman/podman.sock" |
| 36 | |
| 37 | |
| 38 | ignitionPasswordHash = "$y$j9T$TqJWt3/mKJbH0sYi6B/LD1$QjVRuUgntjTHjAdAkqhkr4F73m.Be4jBXdAaKw98sPC" |
| 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 | forwardSock string |
| 50 | forwardRootSock string |
| 51 | ) |
| 52 | |
| 53 | func init() { |
| 54 | flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory") |
| 55 | flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries") |
| 56 | privateKeyFile = filepath.Join(tmpDir, "id_test_qemu") |
| 57 | publicKeyFile = privateKeyFile + ".pub" |
| 58 | ignFile = filepath.Join(tmpDir, "test.ign") |
| 59 | forwardSock = filepath.Join(tmpDir, "podman-remote.sock") |
| 60 | forwardRootSock = filepath.Join(tmpDir, "podman-root-remote.sock") |
| 61 | |
| 62 | } |
| 63 | |
| 64 | var _ = ginkgo.BeforeSuite(func() { |
| 65 | gomega.Expect(os.MkdirAll(filepath.Join(tmpDir, "disks"), os.ModePerm)).Should(gomega.Succeed()) |
| 66 | |
| 67 | downloader, err := e2e_utils.NewFcosDownloader(filepath.Join(tmpDir, "disks")) |
| 68 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 69 | qemuImage, err := downloader.DownloadImage("qemu", "qcow2.xz") |
| 70 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 71 | |
| 72 | publicKey, err := e2e_utils.CreateSSHKeys(publicKeyFile, privateKeyFile) |
| 73 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 74 | |
| 75 | err = e2e_utils.CreateIgnition(ignFile, publicKey, ignitionUser, ignitionPasswordHash) |
| 76 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 77 | |
| 78 | outer: |
| 79 | for panics := 0; ; panics++ { |
| 80 | _ = os.Remove(sock) |
| 81 | |
| 82 | |
| 83 | host = exec.Command(filepath.Join(binDir, "gvproxy"), fmt.Sprintf("--listen=unix://%s", sock), fmt.Sprintf("--listen-qemu=tcp://127.0.0.1:%d", qemuPort), |
| 84 | fmt.Sprintf("--forward-sock=%s", forwardSock), fmt.Sprintf("--forward-dest=%s", podmanSock), fmt.Sprintf("--forward-user=%s", ignitionUser), |
| 85 | fmt.Sprintf("--forward-identity=%s", privateKeyFile), |
| 86 | fmt.Sprintf("--forward-sock=%s", forwardRootSock), fmt.Sprintf("--forward-dest=%s", podmanRootSock), fmt.Sprintf("--forward-user=%s", "root"), |
| 87 | fmt.Sprintf("--forward-identity=%s", privateKeyFile)) |
| 88 | |
| 89 | host.Stderr = os.Stderr |
| 90 | host.Stdout = os.Stdout |
| 91 | gomega.Expect(host.Start()).Should(gomega.Succeed()) |
| 92 | go func() { |
| 93 | if err := host.Wait(); err != nil { |
| 94 | log.Error(err) |
| 95 | } |
| 96 | }() |
| 97 | |
| 98 | for { |
| 99 | _, err := os.Stat(sock) |
| 100 | if os.IsNotExist(err) { |
| 101 | log.Info("waiting for socket") |
| 102 | time.Sleep(100 * time.Millisecond) |
| 103 | continue |
| 104 | } |
| 105 | break |
| 106 | } |
| 107 | |
| 108 | efiArgs, err := efiArgs() |
| 109 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 110 | |
| 111 | template := `%s%s-m 2048 -nographic -serial file:%s -drive if=virtio,file=%s,snapshot=on -fw_cfg name=opt/com.coreos/config,file=%s -netdev socket,id=vlan,connect=127.0.0.1:%d -device virtio-net-pci,netdev=vlan,mac=5a:94:ef:e4:0c:ee` |
| 112 | |
| 113 | |
| 114 | client = exec.Command(qemuExecutable(), strings.Split(fmt.Sprintf(template, qemuArgs(), efiArgs, qconLog, qemuImage, ignFile, qemuPort), " ")...) |
| 115 | client.Stderr = os.Stderr |
| 116 | client.Stdout = os.Stdout |
| 117 | gomega.Expect(client.Start()).Should(gomega.Succeed()) |
| 118 | go func() { |
| 119 | if err := client.Wait(); err != nil { |
| 120 | log.Error(err) |
| 121 | } |
| 122 | }() |
| 123 | |
| 124 | for { |
| 125 | _, err := sshExec("whoami") |
| 126 | if err == nil { |
| 127 | break outer |
| 128 | } |
| 129 | |
| 130 | |
| 131 | didPanic, err := panicCheck(qconLog) |
| 132 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 133 | |
| 134 | if didPanic { |
| 135 | gomega.Expect(panics).ToNot(gomega.BeNumerically(">", 15), "No more than 15 panics allowed") |
| 136 | log.Info("Detected Kernel panic, retrying...") |
| 137 | _ = client.Process.Kill() |
| 138 | _ = host.Process.Kill() |
| 139 | _ = os.Remove(qconLog) |
| 140 | continue outer |
| 141 | } |
| 142 | |
| 143 | log.Infof("waiting for client to connect: %v", err) |
| 144 | time.Sleep(time.Second) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | err = scp(filepath.Join(binDir, "test-companion"), "/tmp/test-companion") |
| 149 | gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) |
| 150 | |
| 151 | |
| 152 | cmd := sshCommand("sudo /tmp/test-companion") |
| 153 | gomega.Expect(cmd.Start()).ShouldNot(gomega.HaveOccurred()) |
| 154 | time.Sleep(5 * time.Second) |
| 155 | }) |
| 156 | |
| 157 | func qemuExecutable() string { |
| 158 | qemuBinaries := []string{"qemu-kvm", fmt.Sprintf("qemu-system-%s", e2e_utils.CoreosArch())} |
| 159 | for _, binary := range qemuBinaries { |
| 160 | path, err := exec.LookPath(binary) |
| 161 | if err == nil && path != "" { |
| 162 | return path |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return "" |
| 167 | } |
| 168 | |
| 169 | func qemuArgs() string { |
| 170 | accel := "kvm" |
| 171 | if runtime.GOOS == "darwin" { |
| 172 | accel = "hvf" |
| 173 | } |
| 174 | machine := "q35" |
| 175 | switch runtime.GOARCH { |
| 176 | case "amd64": |
| 177 | machine = "q35" |
| 178 | case "arm64": |
| 179 | machine = "virt" |
| 180 | default: |
| 181 | panic(fmt.Sprintf("unsupported arch: %s", runtime.GOARCH)) |
| 182 | } |
| 183 | return fmt.Sprintf("-machine %s,accel=%s:tcg -smp 4 -cpu host ", machine, accel) |
| 184 | } |
| 185 | |
| 186 | func scp(src, dst string) error { |
| 187 | sshCmd := exec.Command("scp", |
| 188 | "-o", "UserKnownHostsFile=/dev/null", |
| 189 | "-o", "StrictHostKeyChecking=no", |
| 190 | "-o", "IdentitiesOnly=yes", |
| 191 | "-i", privateKeyFile, |
| 192 | "-P", strconv.Itoa(sshPort), |
| 193 | src, |
| 194 | fmt.Sprintf("%[email protected]:%s", ignitionUser, dst)) |
| 195 | sshCmd.Stderr = os.Stderr |
| 196 | sshCmd.Stdout = os.Stdout |
| 197 | return sshCmd.Run() |
| 198 | } |
| 199 | |
| 200 | func sshExec(cmd ...string) ([]byte, error) { |
| 201 | return sshCommand(cmd...).Output() |
| 202 | } |
| 203 | |
| 204 | func sshCommand(cmd ...string) *exec.Cmd { |
| 205 | sshCmd := exec.Command("ssh", |
| 206 | "-o", "UserKnownHostsFile=/dev/null", |
| 207 | "-o", "StrictHostKeyChecking=no", |
| 208 | "-o", "IdentitiesOnly=yes", |
| 209 | "-i", privateKeyFile, |
| 210 | "-p", strconv.Itoa(sshPort), |
| 211 | fmt.Sprintf("%[email protected]", ignitionUser), "--", strings.Join(cmd, " ")) |
| 212 | return sshCmd |
| 213 | } |
| 214 | |
| 215 | func panicCheck(con string) (bool, error) { |
| 216 | file, err := os.Open(con) |
| 217 | if err != nil { |
| 218 | return false, err |
| 219 | } |
| 220 | |
| 221 | _, _ = file.Seek(-500, io.SeekEnd) |
| 222 | |
| 223 | |
| 224 | contents := make([]byte, 500) |
| 225 | _, err = io.ReadAtLeast(file, contents, len(contents)) |
| 226 | if err != nil && err != io.ErrUnexpectedEOF { |
| 227 | return false, err |
| 228 | } |
| 229 | |
| 230 | return strings.Contains(string(contents), "end Kernel panic"), nil |
| 231 | } |
| 232 | |
| 233 | var _ = ginkgo.AfterSuite(func() { |
| 234 | if host != nil { |
| 235 | if err := host.Process.Kill(); err != nil { |
| 236 | log.Error(err) |
| 237 | } |
| 238 | } |
| 239 | if client != nil { |
| 240 | if err := client.Process.Kill(); err != nil { |
| 241 | log.Error(err) |
| 242 | } |
| 243 | } |
| 244 | }) |