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