Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // This package runs the installer image in a VM provided with an empty block |
| 18 | // device. It then examines the installer console output and the blok device to |
| 19 | // determine whether the installation process completed without issue. |
Mateusz Zalega | edffbb5 | 2022-01-11 15:27:22 +0100 | [diff] [blame] | 20 | package installer |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 21 | |
| 22 | import ( |
Mateusz Zalega | 367f759 | 2021-12-20 18:13:31 +0100 | [diff] [blame] | 23 | "bytes" |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 24 | "context" |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 25 | "fmt" |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 26 | "log" |
| 27 | "os" |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 28 | "path/filepath" |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 29 | "syscall" |
| 30 | "testing" |
| 31 | |
| 32 | diskfs "github.com/diskfs/go-diskfs" |
| 33 | "github.com/diskfs/go-diskfs/disk" |
| 34 | "github.com/diskfs/go-diskfs/partition/gpt" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 35 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 36 | mctl "source.monogon.dev/metropolis/cli/metroctl/core" |
Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 37 | "source.monogon.dev/metropolis/cli/pkg/datafile" |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 38 | "source.monogon.dev/metropolis/node/build/mkimage/osimage" |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 39 | "source.monogon.dev/metropolis/pkg/cmd" |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 40 | "source.monogon.dev/metropolis/proto/api" |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 41 | ) |
| 42 | |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 43 | // Each variable in this block points to either a test dependency or a side |
| 44 | // effect. These variables are initialized in TestMain using Bazel. |
| 45 | var ( |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 46 | // installerImage is a filesystem path pointing at the installer image that |
| 47 | // is generated during the test, and is removed afterwards. |
| 48 | installerImage string |
| 49 | // nodeStorage is a filesystem path pointing at the VM block device image |
| 50 | // Metropolis is installed to during the test. The file is removed afterwards. |
| 51 | nodeStorage string |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 52 | ) |
| 53 | |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 54 | // runQemu starts a new QEMU process, expecting the given output to appear |
| 55 | // in any line printed. It returns true, if the expected string was found, |
| 56 | // and false otherwise. |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 57 | // |
Mateusz Zalega | f1234a9 | 2022-06-22 13:57:38 +0200 | [diff] [blame] | 58 | // QEMU is killed shortly after the string is found, or when the context is |
| 59 | // cancelled. |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 60 | func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 61 | defaultArgs := []string{ |
| 62 | "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", |
| 63 | "-m", "512", |
| 64 | "-smp", "2", |
| 65 | "-cpu", "host", |
| 66 | "-drive", "if=pflash,format=raw,readonly,file=external/edk2/OVMF_CODE.fd", |
| 67 | "-drive", "if=pflash,format=raw,snapshot=on,file=external/edk2/OVMF_VARS.fd", |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 68 | "-serial", "stdio", |
| 69 | "-no-reboot", |
| 70 | } |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 71 | qemuArgs := append(defaultArgs, args...) |
Mateusz Zalega | 6cdc976 | 2022-08-03 17:15:01 +0200 | [diff] [blame^] | 72 | pf := cmd.TerminateIfFound(expectedOutput) |
| 73 | return cmd.RunCommand(ctx, "external/qemu/qemu-x86_64-softmmu", qemuArgs, pf) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 76 | // runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the |
| 77 | // same search-through-std{out,err} as runQemu. |
| 78 | func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) { |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 79 | 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] | 80 | return runQemu(ctx, args, expectedOutput) |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 83 | // getStorage creates a sparse file, given a size expressed in mebibytes, and |
| 84 | // returns a path to that file. It may return an error. |
| 85 | func getStorage(size int64) (string, error) { |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 86 | image, err := os.Create(nodeStorage) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 87 | if err != nil { |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 88 | 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] | 89 | } |
| 90 | if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil { |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 91 | 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] | 92 | } |
| 93 | image.Close() |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 94 | return nodeStorage, nil |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // qemuDriveParam returns QEMU parameters required to run it with a |
| 98 | // raw-format image at path. |
| 99 | func qemuDriveParam(path string) []string { |
| 100 | return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path} |
| 101 | } |
| 102 | |
| 103 | // checkEspContents verifies the presence of the EFI payload inside of image's |
| 104 | // first partition. It returns nil on success. |
| 105 | func checkEspContents(image *disk.Disk) error { |
| 106 | // Get the ESP. |
| 107 | fs, err := image.GetFilesystem(1) |
| 108 | if err != nil { |
| 109 | return fmt.Errorf("couldn't read the installer ESP: %w", err) |
| 110 | } |
| 111 | // Make sure the EFI payload exists by attempting to open it. |
| 112 | efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY) |
| 113 | if err != nil { |
| 114 | return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err) |
| 115 | } |
| 116 | efiPayload.Close() |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func TestMain(m *testing.M) { |
Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 121 | installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img") |
| 122 | nodeStorage = filepath.Join(os.Getenv("TEST_TMPDIR"), "stor.img") |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 123 | |
Mateusz Zalega | edffbb5 | 2022-01-11 15:27:22 +0100 | [diff] [blame] | 124 | installer := datafile.MustGet("metropolis/installer/test/kernel.efi") |
| 125 | bundle := datafile.MustGet("metropolis/installer/test/testos/testos_bundle.zip") |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 126 | iargs := mctl.MakeInstallerImageArgs{ |
Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 127 | Installer: bytes.NewBuffer(installer), |
| 128 | InstallerSize: uint64(len(installer)), |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 129 | TargetPath: installerImage, |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 130 | NodeParams: &api.NodeParameters{}, |
Serge Bazanski | 9778322 | 2021-12-14 16:04:26 +0100 | [diff] [blame] | 131 | Bundle: bytes.NewBuffer(bundle), |
| 132 | BundleSize: uint64(len(bundle)), |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 133 | } |
| 134 | if err := mctl.MakeInstallerImage(iargs); err != nil { |
Mateusz Zalega | 8f72b5d | 2021-12-03 17:08:59 +0100 | [diff] [blame] | 135 | 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] | 136 | } |
| 137 | // With common dependencies set up, run the tests. |
| 138 | code := m.Run() |
| 139 | // Clean up. |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 140 | os.Remove(installerImage) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 141 | os.Exit(code) |
| 142 | } |
| 143 | |
| 144 | func TestInstallerImage(t *testing.T) { |
| 145 | // This test examines the installer image, making sure that the GPT and the |
| 146 | // ESP contents are in order. |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 147 | image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 148 | if err != nil { |
Mateusz Zalega | 8cde8e7 | 2021-11-30 16:22:20 +0100 | [diff] [blame] | 149 | t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error()) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 150 | } |
| 151 | // Verify that GPT exists. |
| 152 | ti, err := image.GetPartitionTable() |
| 153 | if ti.Type() != "gpt" { |
| 154 | t.Error("Couldn't verify that the installer image contains a GPT.") |
| 155 | } |
| 156 | // Check that the first partition is likely to be a valid ESP. |
| 157 | pi := ti.GetPartitions() |
| 158 | esp := (pi[0]).(*gpt.Partition) |
| 159 | if esp.Start == 0 || esp.End == 0 { |
| 160 | t.Error("The installer's ESP GPT entry looks off.") |
| 161 | } |
| 162 | // Verify that the image contains only one partition. |
| 163 | second := (pi[1]).(*gpt.Partition) |
| 164 | if second.Name != "" || second.Start != 0 || second.End != 0 { |
| 165 | t.Error("It appears the installer image contains more than one partition.") |
| 166 | } |
| 167 | // Verify the ESP contents. |
| 168 | if err := checkEspContents(image); err != nil { |
| 169 | t.Error(err.Error()) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | func TestNoBlockDevices(t *testing.T) { |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 174 | ctx, ctxC := context.WithCancel(context.Background()) |
| 175 | defer ctxC() |
| 176 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 177 | // No block devices are passed to QEMU aside from the install medium. Expect |
| 178 | // the installer to fail at the device probe stage rather than attempting to |
| 179 | // use the medium as the target device. |
Mateusz Zalega | cdcc739 | 2021-12-08 15:34:53 +0100 | [diff] [blame] | 180 | expectedOutput := "Couldn't find a suitable block device" |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 181 | result, err := runQemuWithInstaller(ctx, nil, expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 182 | if err != nil { |
| 183 | t.Error(err.Error()) |
| 184 | } |
| 185 | if result != true { |
| 186 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestBlockDeviceTooSmall(t *testing.T) { |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 191 | ctx, ctxC := context.WithCancel(context.Background()) |
| 192 | defer ctxC() |
| 193 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 194 | // Prepare the block device the installer will install to. This time the |
| 195 | // target device is too small to host a Metropolis installation. |
| 196 | imagePath, err := getStorage(64) |
| 197 | defer os.Remove(imagePath) |
| 198 | if err != nil { |
| 199 | t.Errorf(err.Error()) |
| 200 | } |
| 201 | |
| 202 | // Run QEMU. Expect the installer to fail with a predefined error string. |
Mateusz Zalega | cdcc739 | 2021-12-08 15:34:53 +0100 | [diff] [blame] | 203 | expectedOutput := "Couldn't find a suitable block device" |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 204 | result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 205 | if err != nil { |
| 206 | t.Error(err.Error()) |
| 207 | } |
| 208 | if result != true { |
| 209 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func TestInstall(t *testing.T) { |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 214 | ctx, ctxC := context.WithCancel(context.Background()) |
| 215 | defer ctxC() |
| 216 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 217 | // Prepare the block device image the installer will install to. |
| 218 | storagePath, err := getStorage(4096 + 128 + 128 + 1) |
| 219 | defer os.Remove(storagePath) |
| 220 | if err != nil { |
| 221 | t.Errorf(err.Error()) |
| 222 | } |
| 223 | |
| 224 | // Run QEMU. Expect the installer to succeed. |
| 225 | expectedOutput := "Installation completed" |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 226 | result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 227 | if err != nil { |
| 228 | t.Error(err.Error()) |
| 229 | } |
| 230 | if result != true { |
| 231 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 232 | } |
| 233 | |
| 234 | // Verify the resulting node image. Check whether the node GPT was created. |
| 235 | storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly) |
| 236 | if err != nil { |
| 237 | t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error()) |
| 238 | } |
| 239 | // Verify that GPT exists. |
| 240 | ti, err := storage.GetPartitionTable() |
| 241 | if ti.Type() != "gpt" { |
| 242 | t.Error("Couldn't verify that the resulting node image contains a GPT.") |
| 243 | } |
| 244 | // Check that the first partition is likely to be a valid ESP. |
| 245 | pi := ti.GetPartitions() |
| 246 | esp := (pi[0]).(*gpt.Partition) |
| 247 | if esp.Name != osimage.ESPVolumeLabel || esp.Start == 0 || esp.End == 0 { |
| 248 | t.Error("The node's ESP GPT entry looks off.") |
| 249 | } |
| 250 | // Verify the system partition's GPT entry. |
| 251 | system := (pi[1]).(*gpt.Partition) |
| 252 | if system.Name != osimage.SystemVolumeLabel || system.Start == 0 || system.End == 0 { |
| 253 | t.Error("The node's system partition GPT entry looks off.") |
| 254 | } |
| 255 | // Verify the data partition's GPT entry. |
| 256 | data := (pi[2]).(*gpt.Partition) |
| 257 | if data.Name != osimage.DataVolumeLabel || data.Start == 0 || data.End == 0 { |
| 258 | t.Errorf("The node's data partition GPT entry looks off.") |
| 259 | } |
| 260 | // Verify that there are no more partitions. |
| 261 | fourth := (pi[3]).(*gpt.Partition) |
| 262 | if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 { |
| 263 | t.Error("The resulting node image contains more partitions than expected.") |
| 264 | } |
| 265 | // Verify the ESP contents. |
| 266 | if err := checkEspContents(storage); err != nil { |
| 267 | t.Error(err.Error()) |
| 268 | } |
Mateusz Zalega | 4f6fad3 | 2022-05-25 16:34:30 +0200 | [diff] [blame] | 269 | storage.File.Close() |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 270 | // Run QEMU again. Expect TestOS to launch successfully. |
| 271 | expectedOutput = "_TESTOS_LAUNCH_SUCCESS_" |
Serge Bazanski | e2e0371 | 2021-12-17 12:47:03 +0100 | [diff] [blame] | 272 | result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput) |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame] | 273 | if err != nil { |
| 274 | t.Error(err.Error()) |
| 275 | } |
| 276 | if result != true { |
| 277 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 278 | } |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 279 | } |