| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 3 | |
| 4 | // This package runs the installer image in a VM provided with an empty block |
| 5 | // device. It then examines the installer console output and the blok device to |
| 6 | // determine whether the installation process completed without issue. |
| Mateusz Zalega | edffbb5 | 2022-01-11 15:27:22 +0100 | [diff] [blame] | 7 | package installer |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 8 | |
| 9 | import ( |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 10 | "context" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 11 | "fmt" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 12 | "log" |
| 13 | "os" |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 14 | "path/filepath" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 15 | "syscall" |
| 16 | "testing" |
| Serge Bazanski | 05cf33d | 2023-03-16 20:50:59 +0100 | [diff] [blame] | 17 | "time" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 18 | |
| Tim Windelschmidt | 2a1d1b2 | 2024-02-06 07:07:42 +0100 | [diff] [blame] | 19 | "github.com/bazelbuild/rules_go/go/runfiles" |
| 20 | "github.com/diskfs/go-diskfs" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 21 | "github.com/diskfs/go-diskfs/disk" |
| 22 | "github.com/diskfs/go-diskfs/partition/gpt" |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 23 | |
| Tim Windelschmidt | 2a1d1b2 | 2024-02-06 07:07:42 +0100 | [diff] [blame] | 24 | "source.monogon.dev/metropolis/proto/api" |
| 25 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 26 | mctl "source.monogon.dev/metropolis/cli/metroctl/core" |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 27 | "source.monogon.dev/metropolis/installer/install" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 28 | "source.monogon.dev/osbase/cmd" |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 29 | "source.monogon.dev/osbase/oci" |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 30 | "source.monogon.dev/osbase/oci/osimage" |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 31 | "source.monogon.dev/osbase/structfs" |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 32 | ) |
| 33 | |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 34 | var ( |
| 35 | // These are filled by bazel at linking time with the canonical path of |
| 36 | // their corresponding file. Inside the init function we resolve it |
| 37 | // with the rules_go runfiles package to the real path. |
| 38 | xOvmfCodePath string |
| 39 | xOvmfVarsPath string |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 40 | xInstallerPath string |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 41 | xImagePath string |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 42 | xQEMUPath string |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | func init() { |
| 46 | var err error |
| 47 | for _, path := range []*string{ |
| Tim Windelschmidt | 492434a | 2024-10-22 14:29:55 +0200 | [diff] [blame] | 48 | &xOvmfCodePath, &xOvmfVarsPath, |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 49 | &xInstallerPath, &xImagePath, |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 50 | &xQEMUPath, |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 51 | } { |
| 52 | *path, err = runfiles.Rlocation(*path) |
| 53 | if err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | } |
| Tim Windelschmidt | 4bd25e8 | 2025-07-11 19:36:28 +0200 | [diff] [blame] | 57 | // When running QEMU with snapshot=on set, QEMU creates a copy of the |
| 58 | // provided file in $TMPDIR. If $TMPDIR is set to /tmp, it will always |
| 59 | // be overridden to /var/tmp. This creates an issue for us, as the |
| 60 | // bazel tests only wire up /tmp, with /var/tmp being unaccessible |
| 61 | // because of permissions. Bazel provides $TEST_TMPDIR for this |
| 62 | // usecase, which we resolve to an absolute path and then override |
| 63 | // $TMPDIR. |
| 64 | tmpDir, err := filepath.Abs(os.Getenv("TEST_TMPDIR")) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | os.Setenv("TMPDIR", tmpDir) |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 71 | // Each variable in this block points to either a test dependency or a side |
| 72 | // effect. These variables are initialized in TestMain using Bazel. |
| 73 | var ( |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 74 | // installerImage is a filesystem path pointing at the installer image that |
| 75 | // is generated during the test, and is removed afterwards. |
| 76 | installerImage string |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 77 | bootPath string |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 78 | ) |
| 79 | |
| Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 80 | // runQemu starts a new QEMU process, expecting the given output to appear |
| 81 | // in any line printed. It returns true, if the expected string was found, |
| 82 | // and false otherwise. |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 83 | // |
| Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 84 | // QEMU is killed shortly after the string is found, or when the context is |
| 85 | // cancelled. |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 86 | func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 87 | defaultArgs := []string{ |
| 88 | "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", |
| 89 | "-m", "512", |
| 90 | "-smp", "2", |
| 91 | "-cpu", "host", |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 92 | "-drive", "if=pflash,format=raw,snapshot=on,file=" + xOvmfCodePath, |
| 93 | "-drive", "if=pflash,format=raw,readonly=on,file=" + xOvmfVarsPath, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 94 | "-serial", "stdio", |
| 95 | "-no-reboot", |
| 96 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 97 | qemuArgs := append(defaultArgs, args...) |
| Mateusz Zalega | b838e05 | 2022-08-12 18:08:10 +0200 | [diff] [blame] | 98 | pf := cmd.TerminateIfFound(expectedOutput, nil) |
| Tim Windelschmidt | 8f1efe9 | 2025-04-01 01:28:43 +0200 | [diff] [blame] | 99 | return cmd.RunCommand(ctx, xQEMUPath, qemuArgs, pf) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 102 | // runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the |
| 103 | // same search-through-std{out,err} as runQemu. |
| 104 | func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 105 | args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage) |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 106 | return runQemu(ctx, args, expectedOutput) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 109 | // getStorage creates a sparse file, given a size expressed in mebibytes, and |
| 110 | // returns a path to that file. It may return an error. |
| 111 | func getStorage(size int64) (string, error) { |
| Serge Bazanski | 05cf33d | 2023-03-16 20:50:59 +0100 | [diff] [blame] | 112 | nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage") |
| 113 | if err != nil { |
| 114 | return "", err |
| 115 | } |
| 116 | nodeStorage := filepath.Join(nodeStorageDir, "stor.img") |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 117 | image, err := os.Create(nodeStorage) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 118 | if err != nil { |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 119 | return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 120 | } |
| 121 | if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil { |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 122 | return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 123 | } |
| 124 | image.Close() |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 125 | return nodeStorage, nil |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // qemuDriveParam returns QEMU parameters required to run it with a |
| 129 | // raw-format image at path. |
| 130 | func qemuDriveParam(path string) []string { |
| 131 | return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path} |
| 132 | } |
| 133 | |
| 134 | // checkEspContents verifies the presence of the EFI payload inside of image's |
| 135 | // first partition. It returns nil on success. |
| 136 | func checkEspContents(image *disk.Disk) error { |
| 137 | // Get the ESP. |
| 138 | fs, err := image.GetFilesystem(1) |
| 139 | if err != nil { |
| 140 | return fmt.Errorf("couldn't read the installer ESP: %w", err) |
| 141 | } |
| 142 | // Make sure the EFI payload exists by attempting to open it. |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 143 | efiPayload, err := fs.OpenFile("/"+bootPath, os.O_RDONLY) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 144 | if err != nil { |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 145 | return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", bootPath, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 146 | } |
| 147 | efiPayload.Close() |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func TestMain(m *testing.M) { |
| Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 152 | installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img") |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 153 | |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 154 | installer, err := structfs.OSPathBlob(xInstallerPath) |
| Tim Windelschmidt | 2a1d1b2 | 2024-02-06 07:07:42 +0100 | [diff] [blame] | 155 | if err != nil { |
| 156 | log.Fatal(err) |
| 157 | } |
| 158 | |
| Jan Schär | 2963b68 | 2025-07-17 17:03:44 +0200 | [diff] [blame^] | 159 | image, err := oci.AsImage(oci.ReadLayout(xImagePath)) |
| Tim Windelschmidt | 2a1d1b2 | 2024-02-06 07:07:42 +0100 | [diff] [blame] | 160 | if err != nil { |
| 161 | log.Fatal(err) |
| 162 | } |
| 163 | |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 164 | osImage, err := osimage.Read(image) |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 165 | if err != nil { |
| 166 | log.Fatal(err) |
| 167 | } |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 168 | bootPath, err = install.EFIBootPath(osImage.Config.ProductInfo.Architecture()) |
| Jan Schär | 4b88826 | 2025-05-13 09:12:03 +0000 | [diff] [blame] | 169 | if err != nil { |
| 170 | log.Fatal(err) |
| 171 | } |
| 172 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 173 | iargs := mctl.MakeInstallerImageArgs{ |
| Jan Schär | c1b6df4 | 2025-03-20 08:52:18 +0000 | [diff] [blame] | 174 | Installer: installer, |
| Lorenz Brun | ad13188 | 2023-06-28 16:42:20 +0200 | [diff] [blame] | 175 | TargetPath: installerImage, |
| 176 | NodeParams: &api.NodeParameters{}, |
| Jan Schär | 5fdca56 | 2025-04-14 11:33:29 +0000 | [diff] [blame] | 177 | Image: image, |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 178 | } |
| 179 | if err := mctl.MakeInstallerImage(iargs); err != nil { |
| Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 180 | log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 181 | } |
| 182 | // With common dependencies set up, run the tests. |
| 183 | code := m.Run() |
| 184 | // Clean up. |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 185 | os.Remove(installerImage) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 186 | os.Exit(code) |
| 187 | } |
| 188 | |
| 189 | func TestInstallerImage(t *testing.T) { |
| 190 | // This test examines the installer image, making sure that the GPT and the |
| 191 | // ESP contents are in order. |
| Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 192 | image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 193 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 194 | t.Fatalf("Couldn't open the installer image at %q: %s", installerImage, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 195 | } |
| 196 | // Verify that GPT exists. |
| 197 | ti, err := image.GetPartitionTable() |
| Tim Windelschmidt | 33306eb | 2024-04-11 01:39:48 +0200 | [diff] [blame] | 198 | if err != nil { |
| 199 | t.Fatalf("Couldn't read the installer image partition table: %s", err) |
| 200 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 201 | if ti.Type() != "gpt" { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 202 | t.Fatal("Couldn't verify that the installer image contains a GPT.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 203 | } |
| 204 | // Check that the first partition is likely to be a valid ESP. |
| 205 | pi := ti.GetPartitions() |
| 206 | esp := (pi[0]).(*gpt.Partition) |
| 207 | if esp.Start == 0 || esp.End == 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 208 | t.Fatal("The installer's ESP GPT entry looks off.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 209 | } |
| 210 | // Verify that the image contains only one partition. |
| 211 | second := (pi[1]).(*gpt.Partition) |
| 212 | if second.Name != "" || second.Start != 0 || second.End != 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 213 | t.Fatal("It appears the installer image contains more than one partition.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 214 | } |
| 215 | // Verify the ESP contents. |
| 216 | if err := checkEspContents(image); err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 217 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | func TestNoBlockDevices(t *testing.T) { |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 222 | ctx, ctxC := context.WithCancel(context.Background()) |
| 223 | defer ctxC() |
| 224 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 225 | // No block devices are passed to QEMU aside from the install medium. Expect |
| 226 | // the installer to fail at the device probe stage rather than attempting to |
| 227 | // use the medium as the target device. |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 228 | expectedOutput := "couldn't find a suitable block device" |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 229 | result, err := runQemuWithInstaller(ctx, nil, expectedOutput) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 230 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 231 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 232 | } |
| Tim Windelschmidt | 885668a | 2024-04-19 00:01:06 +0200 | [diff] [blame] | 233 | if !result { |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 234 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | func TestBlockDeviceTooSmall(t *testing.T) { |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 239 | ctx, ctxC := context.WithCancel(context.Background()) |
| 240 | defer ctxC() |
| 241 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 242 | // Prepare the block device the installer will install to. This time the |
| 243 | // target device is too small to host a Metropolis installation. |
| 244 | imagePath, err := getStorage(64) |
| 245 | defer os.Remove(imagePath) |
| 246 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 247 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| Tim Windelschmidt | 96e014e | 2024-09-10 02:26:13 +0200 | [diff] [blame] | 251 | expectedOutput := "couldn't find a suitable block device" |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 252 | result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 253 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 254 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 255 | } |
| Tim Windelschmidt | 885668a | 2024-04-19 00:01:06 +0200 | [diff] [blame] | 256 | if !result { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 257 | t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | func TestInstall(t *testing.T) { |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 262 | ctx, ctxC := context.WithCancel(context.Background()) |
| 263 | defer ctxC() |
| 264 | |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 265 | // Prepare the block device image the installer will install to. |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 266 | // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP |
| 267 | // and a 128 MiB data partition. In addition at the start and end we need |
| 268 | // 1MiB for GPT headers and alignment. |
| 269 | storagePath, err := getStorage(4096*2 + 384 + 128 + 2) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 270 | defer os.Remove(storagePath) |
| 271 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 272 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Run QEMU. Expect the installer to succeed. |
| 276 | expectedOutput := "Installation completed" |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 277 | result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 278 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 279 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 280 | } |
| Tim Windelschmidt | 885668a | 2024-04-19 00:01:06 +0200 | [diff] [blame] | 281 | if !result { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 282 | t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Verify the resulting node image. Check whether the node GPT was created. |
| 286 | storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly) |
| 287 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 288 | t.Fatalf("Couldn't open the resulting node image at %q: %s", storagePath, err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 289 | } |
| 290 | // Verify that GPT exists. |
| 291 | ti, err := storage.GetPartitionTable() |
| Tim Windelschmidt | 096654a | 2024-04-18 23:10:19 +0200 | [diff] [blame] | 292 | if err != nil { |
| 293 | t.Fatalf("Couldn't read the installer image partition table: %s", err) |
| 294 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 295 | if ti.Type() != "gpt" { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 296 | t.Fatal("Couldn't verify that the resulting node image contains a GPT.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 297 | } |
| 298 | // Check that the first partition is likely to be a valid ESP. |
| 299 | pi := ti.GetPartitions() |
| 300 | esp := (pi[0]).(*gpt.Partition) |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 301 | if esp.Name != install.ESPLabel || esp.Start == 0 || esp.End == 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 302 | t.Fatal("The node's ESP GPT entry looks off.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 303 | } |
| 304 | // Verify the system partition's GPT entry. |
| 305 | system := (pi[1]).(*gpt.Partition) |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 306 | if system.Name != install.SystemALabel || system.Start == 0 || system.End == 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 307 | t.Fatal("The node's system partition GPT entry looks off.") |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 308 | } |
| 309 | // Verify the system partition's GPT entry. |
| 310 | systemB := (pi[2]).(*gpt.Partition) |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 311 | if systemB.Name != install.SystemBLabel || systemB.Start == 0 || systemB.End == 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 312 | t.Fatal("The node's system partition GPT entry looks off.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 313 | } |
| 314 | // Verify the data partition's GPT entry. |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 315 | data := (pi[3]).(*gpt.Partition) |
| Jan Schär | e19d279 | 2025-06-23 12:37:58 +0000 | [diff] [blame] | 316 | if data.Name != install.DataLabel || data.Start == 0 || data.End == 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 317 | t.Fatalf("The node's data partition GPT entry looks off: %+v", data) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 318 | } |
| 319 | // Verify that there are no more partitions. |
| Lorenz Brun | 35fcf03 | 2023-06-29 04:15:58 +0200 | [diff] [blame] | 320 | fourth := (pi[4]).(*gpt.Partition) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 321 | if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 322 | t.Fatal("The resulting node image contains more partitions than expected.") |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 323 | } |
| 324 | // Verify the ESP contents. |
| 325 | if err := checkEspContents(storage); err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 326 | t.Fatal(err) |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 327 | } |
| Serge Bazanski | f9bdf31 | 2023-03-16 21:54:49 +0100 | [diff] [blame] | 328 | storage.File.Close() |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 329 | // Run QEMU again. Expect TestOS to launch successfully. |
| 330 | expectedOutput = "_TESTOS_LAUNCH_SUCCESS_" |
| Serge Bazanski | 05cf33d | 2023-03-16 20:50:59 +0100 | [diff] [blame] | 331 | time.Sleep(time.Second) |
| Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 332 | result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 333 | if err != nil { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 334 | t.Fatal(err) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 335 | } |
| Tim Windelschmidt | 885668a | 2024-04-19 00:01:06 +0200 | [diff] [blame] | 336 | if !result { |
| Tim Windelschmidt | d0d5d9d | 2025-03-26 22:07:11 +0100 | [diff] [blame] | 337 | t.Fatalf("QEMU didn't produce the expected output %q", expectedOutput) |
| Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 338 | } |
| Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 339 | } |