blob: 87d33090832150ec385b740b8f6c0e1e54182995 [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
20 xFailedKernelPath string
21)
22
23func init() {
24 var err error
25 for _, path := range []*string{
Tim Windelschmidt492434a2024-10-22 14:29:55 +020026 &xOvmfCodePath, &xOvmfVarsPath,
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020027 &xSucceedKernelPath, &xFailedKernelPath,
28 } {
29 *path, err = runfiles.Rlocation(*path)
30 if err != nil {
31 panic(err)
32 }
33 }
34}
35
36// runQemu starts a new QEMU process, expecting the given output to appear
37// in any line printed. It returns true, if the expected string was found,
38// and false otherwise.
39//
40// QEMU is killed shortly after the string is found, or when the context is
41// cancelled.
42func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
43 defaultArgs := []string{
44 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
45 "-m", "512",
46 "-smp", "2",
47 "-cpu", "host",
48 "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath,
49 "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath,
50 "-serial", "stdio",
51 "-no-reboot",
52 }
53 qemuArgs := append(defaultArgs, args...)
54 pf := cmd.TerminateIfFound(expectedOutput, nil)
Tim Windelschmidt492434a2024-10-22 14:29:55 +020055 return cmd.RunCommand(ctx, "qemu-system-x86_64", qemuArgs, pf)
Tim Windelschmidt18e9a3f2024-04-08 21:51:03 +020056}
57
58func TestBringupSuccess(t *testing.T) {
59 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
60 defer ctxC()
61
62 extraArgs := append([]string(nil), "-kernel", xSucceedKernelPath)
63
64 // Run QEMU. Expect the installer to succeed with a predefined error string.
65 expectedOutput := "_BRINGUP_LAUNCH_SUCCESS_"
66 result, err := runQemu(ctx, extraArgs, expectedOutput)
67 if err != nil {
68 t.Error(err.Error())
69 }
70 if !result {
71 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
72 }
73}
74func TestBringupFailed(t *testing.T) {
75 ctx, ctxC := context.WithTimeout(context.Background(), time.Second*30)
76 defer ctxC()
77
78 extraArgs := append([]string(nil), "-kernel", xFailedKernelPath)
79
80 // Run QEMU. Expect the installer to fail with a predefined error string.
81 expectedOutput := "root runnable paniced"
82 result, err := runQemu(ctx, extraArgs, expectedOutput)
83 if err != nil {
84 t.Error(err.Error())
85 }
86 if !result {
87 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
88 }
89}