blob: 085b87554089ef8a337dd1f3e70eea5dccb39a4a [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 Windelschmidt18e9a3f2024-04-08 21:51:03 +020025)
26
27func init() {
28 var err error
29 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020030 &xOvmfCodePath, &xOvmfVarsPath,
Tim Windelschmidt83da4222024-12-16 02:49:50 +010031 &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath,
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020032 } {
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.
46func 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 Windelschmidt492434a2024-10-22 14:29:55 +020059 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020060}
61
62func 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 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +010072 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020073 }
74 if !result {
75 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
76 }
77}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010078func TestBringupPanic(t *testing.T) {
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020079 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
80 defer ctxC()
81
Tim Windelschmidt83da4222024-12-16 02:49:50 +010082 extraArgs := append([]string(nil), "-kernel", xPanicKernelPath)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020083
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 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +010088 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020089 }
90 if !result {
91 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
92 }
93}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010094
95func 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 {
Tim Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100105 t.Error(err)
Tim Windelschmidt83da4222024-12-16 02:49:50 +0100106 }
107 if !result {
108 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
109 }
110}