| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 4 | package test |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| Tim Windelschmidt | 4bd25e8 | 2025-07-11 19:36:28 +0200 | [diff] [blame] | 8 | "os" |
| 9 | "path/filepath" |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/bazelbuild/rules_go/go/runfiles" |
| 14 | |
| Jan Schär | 341cd42 | 2025-09-04 10:33:21 +0200 | [diff] [blame^] | 15 | "source.monogon.dev/osbase/test/cmd" |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | // These are filled by bazel at linking time with the canonical path of |
| 20 | // their corresponding file. Inside the init function we resolve it |
| 21 | // with the rules_go runfiles package to the real path. |
| 22 | xOvmfCodePath string |
| 23 | xOvmfVarsPath string |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 24 | xSucceedKernelPath string |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 25 | xPanicKernelPath string |
| 26 | xErrorKernelPath string |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 27 | xQEMUPath string |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func init() { |
| 31 | var err error |
| 32 | for _, path := range []*string{ |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 33 | &xOvmfCodePath, &xOvmfVarsPath, &xQEMUPath, |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 34 | &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath, |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 35 | } { |
| 36 | *path, err = runfiles.Rlocation(*path) |
| 37 | if err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | } |
| Tim Windelschmidt | 4bd25e8 | 2025-07-11 19:36:28 +0200 | [diff] [blame] | 41 | // When running QEMU with snapshot=on set, QEMU creates a copy of the |
| 42 | // provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always |
| 43 | // be overridden to /var/tmp. This creates an issue for us, as the |
| 44 | // bazel tests only wire up /tmp, with /var/tmp being unaccessible |
| 45 | // because of permissions. Bazel provides $TEST_TMPDIR for this |
| 46 | // usecase, which we resolve to an absolute path and then override |
| 47 | // $TMPDIR. |
| 48 | tmpDir, err := filepath.Abs(os.Getenv("TEST_TMPDIR")) |
| 49 | if err != nil { |
| 50 | panic(err) |
| 51 | } |
| 52 | os.Setenv("TMPDIR", tmpDir) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // runQemu starts a new QEMU process, expecting the given output to appear |
| 56 | // in any line printed. It returns true, if the expected string was found, |
| 57 | // and false otherwise. |
| 58 | // |
| 59 | // QEMU is killed shortly after the string is found, or when the context is |
| 60 | // cancelled. |
| 61 | func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
| 62 | defaultArgs := []string{ |
| 63 | "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", |
| 64 | "-m", "512", |
| 65 | "-smp", "2", |
| 66 | "-cpu", "host", |
| 67 | "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath, |
| 68 | "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath, |
| 69 | "-serial", "stdio", |
| 70 | "-no-reboot", |
| 71 | } |
| 72 | qemuArgs := append(defaultArgs, args...) |
| 73 | pf := cmd.TerminateIfFound(expectedOutput, nil) |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 74 | return cmd.RunCommand(ctx, xQEMUPath, qemuArgs, pf) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | func TestBringupSuccess(t *testing.T) { |
| 78 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 79 | defer ctxC() |
| 80 | |
| 81 | extraArgs := append([]string(nil), "-kernel", xSucceedKernelPath) |
| 82 | |
| 83 | // Run QEMU. Expect the installer to succeed with a predefined error string. |
| 84 | expectedOutput := "_BRINGUP_LAUNCH_SUCCESS_" |
| 85 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 86 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 87 | t.Error(err) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 88 | } |
| 89 | if !result { |
| 90 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 91 | } |
| 92 | } |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 93 | func TestBringupPanic(t *testing.T) { |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 94 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 95 | defer ctxC() |
| 96 | |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 97 | extraArgs := append([]string(nil), "-kernel", xPanicKernelPath) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 98 | |
| 99 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| 100 | expectedOutput := "root runnable paniced" |
| 101 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 102 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 103 | t.Error(err) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 104 | } |
| 105 | if !result { |
| 106 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 107 | } |
| 108 | } |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 109 | |
| 110 | func TestBringupError(t *testing.T) { |
| 111 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 112 | defer ctxC() |
| 113 | |
| 114 | extraArgs := append([]string(nil), "-kernel", xErrorKernelPath) |
| 115 | |
| 116 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| 117 | expectedOutput := "this is an error" |
| 118 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 119 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 120 | t.Error(err) |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 121 | } |
| 122 | if !result { |
| 123 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 124 | } |
| 125 | } |