blob: b560c8cd410b36b8df0edc08d124c77f58dc5f8a [file] [log] [blame]
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +02001package test
2
3import (
4 "context"
5 "testing"
6 "time"
7
8 "github.com/bazelbuild/rules_go/go/runfiles"
9
10 "source.monogon.dev/osbase/cmd"
11)
12
13var (
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
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020019 xSucceedKernelPath string
Tim Windelschmidt83da4222024-12-16 02:49:50 +010020 xPanicKernelPath string
21 xErrorKernelPath string
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020022)
23
24func init() {
25 var err error
26 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020027 &xOvmfCodePath, &xOvmfVarsPath,
Tim Windelschmidt83da4222024-12-16 02:49:50 +010028 &xSucceedKernelPath, &xPanicKernelPath, &xErrorKernelPath,
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020029 } {
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.
43func 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)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020056 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020057}
58
59func 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}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010075func TestBringupPanic(t *testing.T) {
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020076 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
77 defer ctxC()
78
Tim Windelschmidt83da4222024-12-16 02:49:50 +010079 extraArgs := append([]string(nil), "-kernel", xPanicKernelPath)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020080
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}
Tim Windelschmidt83da4222024-12-16 02:49:50 +010091
92func TestBringupError(t *testing.T) {
93 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
94 defer ctxC()
95
96 extraArgs := append([]string(nil), "-kernel", xErrorKernelPath)
97
98 // Run QEMU. Expect the installer to fail with a predefined error string.
99 expectedOutput := "this is an error"
100 result, err := runQemu(ctx, extraArgs, expectedOutput)
101 if err != nil {
102 t.Error(err.Error())
103 }
104 if !result {
105 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
106 }
107}