blob: 5b9b4cb4d87f1eaed535c46542b200d0e0bc05f6 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +02004package test
5
6import (
7 "context"
8 "testing"
9 "time"
10
11 "github.com/bazelbuild/rules_go/go/runfiles"
12
13 "source.monogon.dev/osbase/cmd"
14)
15
16var (
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 Windelschmidt18e9a3f2024-04-08 21:51:03 +020022 xSucceedKernelPath string
Tim Windelschmidt83da4222024-12-16 02:49:50 +010023 xPanicKernelPath string
24 xErrorKernelPath string
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020025 xQEMUPath string
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020026)
27
28func init() {
29 var err error
30 for _, path := range []*string{
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020031 &xOvmfCodePath, &xOvmfVarsPath, &xQEMUPath,
Tim Windelschmidt83da4222024-12-16 02:49:50 +010032 &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath,
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020033 } {
34 *path, err = runfiles.Rlocation(*path)
35 if err != nil {
36 panic(err)
37 }
38 }
39}
40
41// runQemu starts a new QEMU process, expecting the given output to appear
42// in any line printed. It returns true, if the expected string was found,
43// and false otherwise.
44//
45// QEMU is killed shortly after the string is found, or when the context is
46// cancelled.
47func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
48 defaultArgs := []string{
49 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
50 "-m", "512",
51 "-smp", "2",
52 "-cpu", "host",
53 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
54 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
55 "-serial", "stdio",
56 "-no-reboot",
57 }
58 qemuArgs := append(defaultArgs, args...)
59 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020060 return cmd.RunCommand(ctx, xQEMUPath, qemuArgs, pf)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020061}
62
63func TestBringupSuccess(t *testing.T) {
64 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
65 defer ctxC()
66
67 extraArgs := append([]string(nil), "-kernel", xSucceedKernelPath)
68
69 // Run QEMU. Expect the installer to succeed with a predefined error string.
70 expectedOutput := "_BRINGUP_LAUNCH_SUCCESS_"
71 result, err := runQemu(ctx, extraArgs, expectedOutput)
72 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +010073 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020074 }
75 if !result {
76 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
77 }
78}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010079func TestBringupPanic(t *testing.T) {
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020080 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
81 defer ctxC()
82
Tim Windelschmidt83da4222024-12-16 02:49:50 +010083 extraArgs := append([]string(nil), "-kernel", xPanicKernelPath)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020084
85 // Run QEMU. Expect the installer to fail with a predefined error string.
86 expectedOutput := "root runnable paniced"
87 result, err := runQemu(ctx, extraArgs, expectedOutput)
88 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +010089 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020090 }
91 if !result {
92 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
93 }
94}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010095
96func TestBringupError(t *testing.T) {
97 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
98 defer ctxC()
99
100 extraArgs := append([]string(nil), "-kernel", xErrorKernelPath)
101
102 // Run QEMU. Expect the installer to fail with a predefined error string.
103 expectedOutput := "this is an error"
104 result, err := runQemu(ctx, extraArgs, expectedOutput)
105 if err != nil {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100106 t.Error(err)
Tim Windelschmidt83da4222024-12-16 02:49:50 +0100107 }
108 if !result {
109 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
110 }
111}