blob: 69b881e7dba5484070fdbe27445a2752e18e5642 [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"
Tim Windelschmidt4bd25e82025-07-11 19:36:28 +02008 "os"
9 "path/filepath"
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020010 "testing"
11 "time"
12
13 "github.com/bazelbuild/rules_go/go/runfiles"
14
Jan Schär341cd422025-09-04 10:33:21 +020015 "source.monogon.dev/osbase/test/cmd"
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020016)
17
18var (
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 Windelschmidt18e9a3f2024-04-08 21:51:03 +020024 xSucceedKernelPath string
Tim Windelschmidt83da4222024-12-16 02:49:50 +010025 xPanicKernelPath string
26 xErrorKernelPath string
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020027 xQEMUPath string
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020028)
29
30func init() {
31 var err error
32 for _, path := range []*string{
Tim Windelschmidt8f1efe92025-04-01 01:28:43 +020033 &xOvmfCodePath, &xOvmfVarsPath, &xQEMUPath,
Tim Windelschmidt83da4222024-12-16 02:49:50 +010034 &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath,
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020035 } {
36 *path, err = runfiles.Rlocation(*path)
37 if err != nil {
38 panic(err)
39 }
40 }
Tim Windelschmidt4bd25e82025-07-11 19:36:28 +020041 // 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 Windelschmidt18e9a3f2024-04-08 21:51:03 +020053}
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.
61func 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 Windelschmidt8f1efe92025-04-01 01:28:43 +020074 return cmd.RunCommand(ctx, xQEMUPath, qemuArgs, pf)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020075}
76
77func 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 Windelschmidtd0d5d9d2025-03-26 22:07:11 +010087 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020088 }
89 if !result {
90 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
91 }
92}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010093func TestBringupPanic(t *testing.T) {
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020094 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
95 defer ctxC()
96
Tim Windelschmidt83da4222024-12-16 02:49:50 +010097 extraArgs := append([]string(nil), "-kernel", xPanicKernelPath)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020098
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 Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100103 t.Error(err)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +0200104 }
105 if !result {
106 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
107 }
108}
Tim Windelschmidt83da4222024-12-16 02:49:50 +0100109
110func 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 Windelschmidtd0d5d9d2025-03-26 22:07:11 +0100120 t.Error(err)
Tim Windelschmidt83da4222024-12-16 02:49:50 +0100121 }
122 if !result {
123 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
124 }
125}