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. |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 23 | "bufio" |
| 24 | "bytes" |
| 25 | "fmt" |
| 26 | "io" |
| 27 | "log" |
| 28 | "os" |
| 29 | "os/exec" |
| 30 | "syscall" |
| 31 | "testing" |
| 32 | |
| 33 | diskfs "github.com/diskfs/go-diskfs" |
| 34 | "github.com/diskfs/go-diskfs/disk" |
| 35 | "github.com/diskfs/go-diskfs/partition/gpt" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 36 | "source.monogon.dev/metropolis/proto/api" |
| 37 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 38 | mctl "source.monogon.dev/metropolis/cli/metroctl/core" |
| 39 | osimage "source.monogon.dev/metropolis/node/build/mkimage/osimage" |
| 40 | ) |
| 41 | |
| 42 | const ( |
| 43 | InstallerEFIPayload = "metropolis/node/installer/kernel.efi" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 44 | TestOSBundle = "metropolis/test/installer/testos/testos_bundle.zip" |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 45 | InstallerImage = "metropolis/test/installer/installer.img" |
| 46 | NodeStorage = "metropolis/test/installer/stor.img" |
| 47 | ) |
| 48 | |
| 49 | // runQemu starts a QEMU process and waits for it to finish. args is |
| 50 | // concatenated to the list of predefined default arguments. It returns true if |
| 51 | // expectedOutput is found in the serial port output. It may return an error. |
| 52 | func runQemu(args []string, expectedOutput string) (bool, error) { |
| 53 | // Prepare the default parameter list. |
| 54 | defaultArgs := []string{ |
| 55 | "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults", |
| 56 | "-m", "512", |
| 57 | "-smp", "2", |
| 58 | "-cpu", "host", |
| 59 | "-drive", "if=pflash,format=raw,readonly,file=external/edk2/OVMF_CODE.fd", |
| 60 | "-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] | 61 | "-serial", "stdio", |
| 62 | "-no-reboot", |
| 63 | } |
| 64 | // Join the parameter lists and prepare the Qemu command, but don't run it |
| 65 | // just yet. |
| 66 | qemuArgs := append(defaultArgs, args...) |
| 67 | qemuCmd := exec.Command("external/qemu/qemu-x86_64-softmmu", qemuArgs...) |
| 68 | |
| 69 | // Copy the stdout and stderr output so that it could be matched against |
| 70 | // expectedOutput later. |
| 71 | var outBuf, errBuf bytes.Buffer |
| 72 | outWriter := bufio.NewWriter(&outBuf) |
| 73 | errWriter := bufio.NewWriter(&errBuf) |
| 74 | qemuCmd.Stdout = io.MultiWriter(os.Stdout, outWriter) |
| 75 | qemuCmd.Stderr = io.MultiWriter(os.Stderr, errWriter) |
| 76 | if err := qemuCmd.Run(); err != nil { |
| 77 | return false, fmt.Errorf("couldn't start QEMU: %w", err) |
| 78 | } |
| 79 | outWriter.Flush() |
| 80 | errWriter.Flush() |
| 81 | |
| 82 | // Try matching against expectedOutput and return the result. |
| 83 | result := bytes.Contains(outBuf.Bytes(), []byte(expectedOutput)) || |
| 84 | bytes.Contains(errBuf.Bytes(), []byte(expectedOutput)) |
| 85 | return result, nil |
| 86 | } |
| 87 | |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 88 | // runQemuWithInstaller starts a QEMU process and waits for it to finish. args is |
| 89 | // concatenated to the list of predefined default arguments. It returns true if |
| 90 | // expectedOutput is found in the serial port output. It may return an error. |
| 91 | func runQemuWithInstaller(args []string, expectedOutput string) (bool, error) { |
| 92 | args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+InstallerImage) |
| 93 | return runQemu(args, expectedOutput) |
| 94 | } |
| 95 | |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 96 | // getStorage creates a sparse file, given a size expressed in mebibytes, and |
| 97 | // returns a path to that file. It may return an error. |
| 98 | func getStorage(size int64) (string, error) { |
| 99 | image, err := os.Create(NodeStorage) |
| 100 | if err != nil { |
| 101 | return "", fmt.Errorf("couldn't create the block device image at %q: %w", NodeStorage, err) |
| 102 | } |
| 103 | if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil { |
| 104 | return "", fmt.Errorf("couldn't resize the block device image at %q: %w", NodeStorage, err) |
| 105 | } |
| 106 | image.Close() |
| 107 | return NodeStorage, nil |
| 108 | } |
| 109 | |
| 110 | // qemuDriveParam returns QEMU parameters required to run it with a |
| 111 | // raw-format image at path. |
| 112 | func qemuDriveParam(path string) []string { |
| 113 | return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path} |
| 114 | } |
| 115 | |
| 116 | // checkEspContents verifies the presence of the EFI payload inside of image's |
| 117 | // first partition. It returns nil on success. |
| 118 | func checkEspContents(image *disk.Disk) error { |
| 119 | // Get the ESP. |
| 120 | fs, err := image.GetFilesystem(1) |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("couldn't read the installer ESP: %w", err) |
| 123 | } |
| 124 | // Make sure the EFI payload exists by attempting to open it. |
| 125 | efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY) |
| 126 | if err != nil { |
| 127 | return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err) |
| 128 | } |
| 129 | efiPayload.Close() |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | func TestMain(m *testing.M) { |
| 134 | // Build the installer image with metroctl, given the EFI executable |
| 135 | // generated by Metropolis buildsystem. This mimics standard usage of |
| 136 | // metroctl CLI. |
| 137 | installer, err := os.Open(InstallerEFIPayload) |
| 138 | if err != nil { |
| 139 | log.Fatalf("Couldn't open the installer EFI executable at %q: %s", InstallerEFIPayload, err.Error()) |
| 140 | } |
| 141 | info, err := installer.Stat() |
| 142 | if err != nil { |
| 143 | log.Fatalf("Couldn't stat the installer EFI executable: %s", err.Error()) |
| 144 | } |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 145 | bundle, err := os.Open(TestOSBundle) |
| 146 | if err != nil { |
| 147 | log.Fatalf("failed to open TestOS bundle: %v", err) |
| 148 | } |
| 149 | bundleStat, err := bundle.Stat() |
| 150 | if err != nil { |
| 151 | log.Fatalf("failed to stat() TestOS bundle: %v", err) |
| 152 | } |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 153 | iargs := mctl.MakeInstallerImageArgs{ |
| 154 | Installer: installer, |
| 155 | InstallerSize: uint64(info.Size()), |
| 156 | TargetPath: InstallerImage, |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 157 | NodeParams: &api.NodeParameters{}, |
| 158 | Bundle: bundle, |
| 159 | BundleSize: uint64(bundleStat.Size()), |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 160 | } |
| 161 | if err := mctl.MakeInstallerImage(iargs); err != nil { |
| 162 | log.Fatalf("Couldn't create the installer image at %q: %s", InstallerImage, err.Error()) |
| 163 | } |
| 164 | // With common dependencies set up, run the tests. |
| 165 | code := m.Run() |
| 166 | // Clean up. |
| 167 | os.Remove(InstallerImage) |
| 168 | os.Exit(code) |
| 169 | } |
| 170 | |
| 171 | func TestInstallerImage(t *testing.T) { |
| 172 | // This test examines the installer image, making sure that the GPT and the |
| 173 | // ESP contents are in order. |
| 174 | image, err := diskfs.OpenWithMode(InstallerImage, diskfs.ReadOnly) |
| 175 | if err != nil { |
| 176 | t.Errorf("Couldn't open the installer image at %q: %s", InstallerImage, err.Error()) |
| 177 | } |
| 178 | // Verify that GPT exists. |
| 179 | ti, err := image.GetPartitionTable() |
| 180 | if ti.Type() != "gpt" { |
| 181 | t.Error("Couldn't verify that the installer image contains a GPT.") |
| 182 | } |
| 183 | // Check that the first partition is likely to be a valid ESP. |
| 184 | pi := ti.GetPartitions() |
| 185 | esp := (pi[0]).(*gpt.Partition) |
| 186 | if esp.Start == 0 || esp.End == 0 { |
| 187 | t.Error("The installer's ESP GPT entry looks off.") |
| 188 | } |
| 189 | // Verify that the image contains only one partition. |
| 190 | second := (pi[1]).(*gpt.Partition) |
| 191 | if second.Name != "" || second.Start != 0 || second.End != 0 { |
| 192 | t.Error("It appears the installer image contains more than one partition.") |
| 193 | } |
| 194 | // Verify the ESP contents. |
| 195 | if err := checkEspContents(image); err != nil { |
| 196 | t.Error(err.Error()) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestNoBlockDevices(t *testing.T) { |
| 201 | // No block devices are passed to QEMU aside from the install medium. Expect |
| 202 | // the installer to fail at the device probe stage rather than attempting to |
| 203 | // use the medium as the target device. |
| 204 | expectedOutput := "couldn't find a suitable block device" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 205 | result, err := runQemuWithInstaller(nil, expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 206 | if err != nil { |
| 207 | t.Error(err.Error()) |
| 208 | } |
| 209 | if result != true { |
| 210 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestBlockDeviceTooSmall(t *testing.T) { |
| 215 | // Prepare the block device the installer will install to. This time the |
| 216 | // target device is too small to host a Metropolis installation. |
| 217 | imagePath, err := getStorage(64) |
| 218 | defer os.Remove(imagePath) |
| 219 | if err != nil { |
| 220 | t.Errorf(err.Error()) |
| 221 | } |
| 222 | |
| 223 | // Run QEMU. Expect the installer to fail with a predefined error string. |
| 224 | expectedOutput := "couldn't find a suitable block device" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 225 | result, err := runQemuWithInstaller(qemuDriveParam(imagePath), expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 226 | if err != nil { |
| 227 | t.Error(err.Error()) |
| 228 | } |
| 229 | if result != true { |
| 230 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | func TestInstall(t *testing.T) { |
| 235 | // Prepare the block device image the installer will install to. |
| 236 | storagePath, err := getStorage(4096 + 128 + 128 + 1) |
| 237 | defer os.Remove(storagePath) |
| 238 | if err != nil { |
| 239 | t.Errorf(err.Error()) |
| 240 | } |
| 241 | |
| 242 | // Run QEMU. Expect the installer to succeed. |
| 243 | expectedOutput := "Installation completed" |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 244 | result, err := runQemuWithInstaller(qemuDriveParam(storagePath), expectedOutput) |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 245 | if err != nil { |
| 246 | t.Error(err.Error()) |
| 247 | } |
| 248 | if result != true { |
| 249 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 250 | } |
| 251 | |
| 252 | // Verify the resulting node image. Check whether the node GPT was created. |
| 253 | storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly) |
| 254 | if err != nil { |
| 255 | t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error()) |
| 256 | } |
| 257 | // Verify that GPT exists. |
| 258 | ti, err := storage.GetPartitionTable() |
| 259 | if ti.Type() != "gpt" { |
| 260 | t.Error("Couldn't verify that the resulting node image contains a GPT.") |
| 261 | } |
| 262 | // Check that the first partition is likely to be a valid ESP. |
| 263 | pi := ti.GetPartitions() |
| 264 | esp := (pi[0]).(*gpt.Partition) |
| 265 | if esp.Name != osimage.ESPVolumeLabel || esp.Start == 0 || esp.End == 0 { |
| 266 | t.Error("The node's ESP GPT entry looks off.") |
| 267 | } |
| 268 | // Verify the system partition's GPT entry. |
| 269 | system := (pi[1]).(*gpt.Partition) |
| 270 | if system.Name != osimage.SystemVolumeLabel || system.Start == 0 || system.End == 0 { |
| 271 | t.Error("The node's system partition GPT entry looks off.") |
| 272 | } |
| 273 | // Verify the data partition's GPT entry. |
| 274 | data := (pi[2]).(*gpt.Partition) |
| 275 | if data.Name != osimage.DataVolumeLabel || data.Start == 0 || data.End == 0 { |
| 276 | t.Errorf("The node's data partition GPT entry looks off.") |
| 277 | } |
| 278 | // Verify that there are no more partitions. |
| 279 | fourth := (pi[3]).(*gpt.Partition) |
| 280 | if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 { |
| 281 | t.Error("The resulting node image contains more partitions than expected.") |
| 282 | } |
| 283 | // Verify the ESP contents. |
| 284 | if err := checkEspContents(storage); err != nil { |
| 285 | t.Error(err.Error()) |
| 286 | } |
Lorenz Brun | 0b93c8d | 2021-11-09 03:58:40 +0100 | [diff] [blame^] | 287 | // Run QEMU again. Expect TestOS to launch successfully. |
| 288 | expectedOutput = "_TESTOS_LAUNCH_SUCCESS_" |
| 289 | result, err = runQemu(qemuDriveParam(storagePath), expectedOutput) |
| 290 | if err != nil { |
| 291 | t.Error(err.Error()) |
| 292 | } |
| 293 | if result != true { |
| 294 | t.Errorf("QEMU didn't produce the expected output %q", expectedOutput) |
| 295 | } |
Mateusz Zalega | 43e2107 | 2021-10-08 18:05:29 +0200 | [diff] [blame] | 296 | } |