blob: ec4f736c62c99618a0ea9781e22aa18ac9dd96b6 [file] [log] [blame]
Mateusz Zalega43e21072021-10-08 18:05:29 +02001// 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 Zalegaedffbb52022-01-11 15:27:22 +010020package installer
Mateusz Zalega43e21072021-10-08 18:05:29 +020021
22import (
Mateusz Zalega367f7592021-12-20 18:13:31 +010023 "bytes"
Serge Bazanskie2e03712021-12-17 12:47:03 +010024 "context"
Mateusz Zalega43e21072021-10-08 18:05:29 +020025 "fmt"
Mateusz Zalega43e21072021-10-08 18:05:29 +020026 "log"
27 "os"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010028 "path/filepath"
Mateusz Zalega43e21072021-10-08 18:05:29 +020029 "syscall"
30 "testing"
Serge Bazanski05cf33d2023-03-16 20:50:59 +010031 "time"
Mateusz Zalega43e21072021-10-08 18:05:29 +020032
33 diskfs "github.com/diskfs/go-diskfs"
34 "github.com/diskfs/go-diskfs/disk"
35 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010036
Mateusz Zalega43e21072021-10-08 18:05:29 +020037 mctl "source.monogon.dev/metropolis/cli/metroctl/core"
Serge Bazanski97783222021-12-14 16:04:26 +010038 "source.monogon.dev/metropolis/cli/pkg/datafile"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010039 "source.monogon.dev/metropolis/node/build/mkimage/osimage"
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020040 "source.monogon.dev/metropolis/pkg/cmd"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010041 "source.monogon.dev/metropolis/proto/api"
Mateusz Zalega43e21072021-10-08 18:05:29 +020042)
43
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010044// Each variable in this block points to either a test dependency or a side
45// effect. These variables are initialized in TestMain using Bazel.
46var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010047 // installerImage is a filesystem path pointing at the installer image that
48 // is generated during the test, and is removed afterwards.
49 installerImage string
Mateusz Zalega43e21072021-10-08 18:05:29 +020050)
51
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020052// runQemu starts a new QEMU process, expecting the given output to appear
53// in any line printed. It returns true, if the expected string was found,
54// and false otherwise.
Serge Bazanskie2e03712021-12-17 12:47:03 +010055//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020056// QEMU is killed shortly after the string is found, or when the context is
57// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010058func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020059 defaultArgs := []string{
60 "-machine", "q35", "-accel", "kvm", "-nographic", "-nodefaults",
61 "-m", "512",
62 "-smp", "2",
63 "-cpu", "host",
64 "-drive", "if=pflash,format=raw,readonly,file=external/edk2/OVMF_CODE.fd",
65 "-drive", "if=pflash,format=raw,snapshot=on,file=external/edk2/OVMF_VARS.fd",
Mateusz Zalega43e21072021-10-08 18:05:29 +020066 "-serial", "stdio",
67 "-no-reboot",
68 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020069 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegab838e052022-08-12 18:08:10 +020070 pf := cmd.TerminateIfFound(expectedOutput, nil)
Mateusz Zalega6cdc9762022-08-03 17:15:01 +020071 return cmd.RunCommand(ctx, "external/qemu/qemu-x86_64-softmmu", qemuArgs, pf)
Mateusz Zalega43e21072021-10-08 18:05:29 +020072}
73
Serge Bazanskie2e03712021-12-17 12:47:03 +010074// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
75// same search-through-std{out,err} as runQemu.
76func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010077 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010078 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010079}
80
Mateusz Zalega43e21072021-10-08 18:05:29 +020081// getStorage creates a sparse file, given a size expressed in mebibytes, and
82// returns a path to that file. It may return an error.
83func getStorage(size int64) (string, error) {
Serge Bazanski05cf33d2023-03-16 20:50:59 +010084 nodeStorageDir, err := os.MkdirTemp(os.Getenv("TEST_TMPDIR"), "storage")
85 if err != nil {
86 return "", err
87 }
88 nodeStorage := filepath.Join(nodeStorageDir, "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010089 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +020090 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010091 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020092 }
93 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010094 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020095 }
96 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010097 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +020098}
99
100// qemuDriveParam returns QEMU parameters required to run it with a
101// raw-format image at path.
102func qemuDriveParam(path string) []string {
103 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
104}
105
106// checkEspContents verifies the presence of the EFI payload inside of image's
107// first partition. It returns nil on success.
108func checkEspContents(image *disk.Disk) error {
109 // Get the ESP.
110 fs, err := image.GetFilesystem(1)
111 if err != nil {
112 return fmt.Errorf("couldn't read the installer ESP: %w", err)
113 }
114 // Make sure the EFI payload exists by attempting to open it.
115 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
116 if err != nil {
117 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
118 }
119 efiPayload.Close()
120 return nil
121}
122
123func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100124 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100125
Mateusz Zalegaedffbb52022-01-11 15:27:22 +0100126 installer := datafile.MustGet("metropolis/installer/test/kernel.efi")
127 bundle := datafile.MustGet("metropolis/installer/test/testos/testos_bundle.zip")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200128 iargs := mctl.MakeInstallerImageArgs{
Lorenz Brunad131882023-06-28 16:42:20 +0200129 Installer: bytes.NewReader(installer),
130 TargetPath: installerImage,
131 NodeParams: &api.NodeParameters{},
132 Bundle: bytes.NewReader(bundle),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200133 }
134 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100135 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200136 }
137 // With common dependencies set up, run the tests.
138 code := m.Run()
139 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100140 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200141 os.Exit(code)
142}
143
144func 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 Zalega8cde8e72021-11-30 16:22:20 +0100147 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200148 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100149 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200150 }
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
173func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100174 ctx, ctxC := context.WithCancel(context.Background())
175 defer ctxC()
176
Mateusz Zalega43e21072021-10-08 18:05:29 +0200177 // 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 Zalegacdcc7392021-12-08 15:34:53 +0100180 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100181 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200182 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
190func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100191 ctx, ctxC := context.WithCancel(context.Background())
192 defer ctxC()
193
Mateusz Zalega43e21072021-10-08 18:05:29 +0200194 // 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 Zalegacdcc7392021-12-08 15:34:53 +0100203 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100204 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200205 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
213func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100214 ctx, ctxC := context.WithCancel(context.Background())
215 defer ctxC()
216
Mateusz Zalega43e21072021-10-08 18:05:29 +0200217 // Prepare the block device image the installer will install to.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200218 // Needs enough storage for two 4096 MiB system partitions, a 384 MiB ESP
219 // and a 128 MiB data partition. In addition at the start and end we need
220 // 1MiB for GPT headers and alignment.
221 storagePath, err := getStorage(4096*2 + 384 + 128 + 2)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200222 defer os.Remove(storagePath)
223 if err != nil {
224 t.Errorf(err.Error())
225 }
226
227 // Run QEMU. Expect the installer to succeed.
228 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100229 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200230 if err != nil {
231 t.Error(err.Error())
232 }
233 if result != true {
234 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
235 }
236
237 // Verify the resulting node image. Check whether the node GPT was created.
238 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
239 if err != nil {
240 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
241 }
242 // Verify that GPT exists.
243 ti, err := storage.GetPartitionTable()
244 if ti.Type() != "gpt" {
245 t.Error("Couldn't verify that the resulting node image contains a GPT.")
246 }
247 // Check that the first partition is likely to be a valid ESP.
248 pi := ti.GetPartitions()
249 esp := (pi[0]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200250 if esp.Name != osimage.ESPLabel || esp.Start == 0 || esp.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200251 t.Error("The node's ESP GPT entry looks off.")
252 }
253 // Verify the system partition's GPT entry.
254 system := (pi[1]).(*gpt.Partition)
Lorenz Brun35fcf032023-06-29 04:15:58 +0200255 if system.Name != osimage.SystemALabel || system.Start == 0 || system.End == 0 {
256 t.Error("The node's system partition GPT entry looks off.")
257 }
258 // Verify the system partition's GPT entry.
259 systemB := (pi[2]).(*gpt.Partition)
260 if systemB.Name != osimage.SystemBLabel || systemB.Start == 0 || systemB.End == 0 {
Mateusz Zalega43e21072021-10-08 18:05:29 +0200261 t.Error("The node's system partition GPT entry looks off.")
262 }
263 // Verify the data partition's GPT entry.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200264 data := (pi[3]).(*gpt.Partition)
Lorenz Brunad131882023-06-28 16:42:20 +0200265 if data.Name != osimage.DataLabel || data.Start == 0 || data.End == 0 {
266 t.Errorf("The node's data partition GPT entry looks off: %+v", data)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200267 }
268 // Verify that there are no more partitions.
Lorenz Brun35fcf032023-06-29 04:15:58 +0200269 fourth := (pi[4]).(*gpt.Partition)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200270 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
271 t.Error("The resulting node image contains more partitions than expected.")
272 }
273 // Verify the ESP contents.
274 if err := checkEspContents(storage); err != nil {
275 t.Error(err.Error())
276 }
Serge Bazanskif9bdf312023-03-16 21:54:49 +0100277 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100278 // Run QEMU again. Expect TestOS to launch successfully.
279 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanski05cf33d2023-03-16 20:50:59 +0100280 time.Sleep(time.Second)
Serge Bazanskie2e03712021-12-17 12:47:03 +0100281 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100282 if err != nil {
283 t.Error(err.Error())
284 }
285 if result != true {
286 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
287 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200288}