| 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" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/bazelbuild/rules_go/go/runfiles" |
| 12 | |
| 13 | "source.monogon.dev/osbase/cmd" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | // These are filled by bazel at linking time with the canonical path of |
| 18 | // their corresponding file. Inside the init function we resolve it |
| 19 | // with the rules_go runfiles package to the real path. |
| 20 | xOvmfCodePath string |
| 21 | xOvmfVarsPath string |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 22 | xSucceedKernelPath string |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 23 | xPanicKernelPath string |
| 24 | xErrorKernelPath string |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | var err error |
| 29 | for _, path := range []*string{ |
| Tim Windelschmidt | 492434a | 2024-10-22 14:29:55 +0200 | [diff] [blame] | 30 | &xOvmfCodePath, &xOvmfVarsPath, |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 31 | &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath, |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 32 | } { |
| 33 | *path, err = runfiles.Rlocation(*path) |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // runQemu starts a new QEMU process, expecting the given output to appear |
| 41 | // in any line printed. It returns true, if the expected string was found, |
| 42 | // and false otherwise. |
| 43 | // |
| 44 | // QEMU is killed shortly after the string is found, or when the context is |
| 45 | // cancelled. |
| 46 | func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
| 47 | defaultArgs := []string{ |
| 48 | "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", |
| 49 | "-m", "512", |
| 50 | "-smp", "2", |
| 51 | "-cpu", "host", |
| 52 | "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath, |
| 53 | "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath, |
| 54 | "-serial", "stdio", |
| 55 | "-no-reboot", |
| 56 | } |
| 57 | qemuArgs := append(defaultArgs, args...) |
| 58 | pf := cmd.TerminateIfFound(expectedOutput, nil) |
| Tim Windelschmidt | 492434a | 2024-10-22 14:29:55 +0200 | [diff] [blame] | 59 | return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func TestBringupSuccess(t *testing.T) { |
| 63 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 64 | defer ctxC() |
| 65 | |
| 66 | extraArgs := append([]string(nil), "-kernel", xSucceedKernelPath) |
| 67 | |
| 68 | // Run QEMU. Expect the installer to succeed with a predefined error string. |
| 69 | expectedOutput := "_BRINGUP_LAUNCH_SUCCESS_" |
| 70 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 71 | if err != nil { |
| 72 | t.Error(err.Error()) |
| 73 | } |
| 74 | if !result { |
| 75 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 76 | } |
| 77 | } |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 78 | func TestBringupPanic(t *testing.T) { |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 79 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 80 | defer ctxC() |
| 81 | |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 82 | extraArgs := append([]string(nil), "-kernel", xPanicKernelPath) |
| Tim Windelschmidt | 18e9a3f | 2024-04-08 21:51:03 +0200 | [diff] [blame] | 83 | |
| 84 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| 85 | expectedOutput := "root runnable paniced" |
| 86 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 87 | if err != nil { |
| 88 | t.Error(err.Error()) |
| 89 | } |
| 90 | if !result { |
| 91 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 92 | } |
| 93 | } |
| Tim Windelschmidt | 83da422 | 2024-12-16 02:49:50 +0100 | [diff] [blame] | 94 | |
| 95 | func TestBringupError(t *testing.T) { |
| 96 | ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30) |
| 97 | defer ctxC() |
| 98 | |
| 99 | extraArgs := append([]string(nil), "-kernel", xErrorKernelPath) |
| 100 | |
| 101 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| 102 | expectedOutput := "this is an error" |
| 103 | result, err := runQemu(ctx, extraArgs, expectedOutput) |
| 104 | if err != nil { |
| 105 | t.Error(err.Error()) |
| 106 | } |
| 107 | if !result { |
| 108 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 109 | } |
| 110 | } |