blob: 2a03a75e189259ad929e42660b8c99d94e82c30c [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"
31
32 diskfs "github.com/diskfs/go-diskfs"
33 "github.com/diskfs/go-diskfs/disk"
34 "github.com/diskfs/go-diskfs/partition/gpt"
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010035
Mateusz Zalega43e21072021-10-08 18:05:29 +020036 mctl "source.monogon.dev/metropolis/cli/metroctl/core"
Serge Bazanski97783222021-12-14 16:04:26 +010037 "source.monogon.dev/metropolis/cli/pkg/datafile"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010038 "source.monogon.dev/metropolis/node/build/mkimage/osimage"
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020039 "source.monogon.dev/metropolis/pkg/cmd"
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010040 "source.monogon.dev/metropolis/proto/api"
Mateusz Zalega43e21072021-10-08 18:05:29 +020041)
42
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010043// Each variable in this block points to either a test dependency or a side
44// effect. These variables are initialized in TestMain using Bazel.
45var (
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010046 // 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 Zalega43e21072021-10-08 18:05:29 +020052)
53
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020054// 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 Bazanskie2e03712021-12-17 12:47:03 +010057//
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020058// QEMU is killed shortly after the string is found, or when the context is
59// cancelled.
Serge Bazanskie2e03712021-12-17 12:47:03 +010060func runQemu(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega43e21072021-10-08 18:05:29 +020061 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 Zalega43e21072021-10-08 18:05:29 +020068 "-serial", "stdio",
69 "-no-reboot",
70 }
Mateusz Zalega43e21072021-10-08 18:05:29 +020071 qemuArgs := append(defaultArgs, args...)
Mateusz Zalegaf1234a92022-06-22 13:57:38 +020072 return cmd.RunCommand(ctx, "external/qemu/qemu-x86_64-softmmu", qemuArgs, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +020073}
74
Serge Bazanskie2e03712021-12-17 12:47:03 +010075// runQemuWithInstaller runs the Metropolis Installer in a qemu, performing the
76// same search-through-std{out,err} as runQemu.
77func runQemuWithInstaller(ctx context.Context, args []string, expectedOutput string) (bool, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010078 args = append(args, "-drive", "if=virtio,format=raw,snapshot=on,cache=unsafe,file="+installerImage)
Serge Bazanskie2e03712021-12-17 12:47:03 +010079 return runQemu(ctx, args, expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +010080}
81
Mateusz Zalega43e21072021-10-08 18:05:29 +020082// getStorage creates a sparse file, given a size expressed in mebibytes, and
83// returns a path to that file. It may return an error.
84func getStorage(size int64) (string, error) {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010085 image, err := os.Create(nodeStorage)
Mateusz Zalega43e21072021-10-08 18:05:29 +020086 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010087 return "", fmt.Errorf("couldn't create the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020088 }
89 if err := syscall.Ftruncate(int(image.Fd()), size*1024*1024); err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010090 return "", fmt.Errorf("couldn't resize the block device image at %q: %w", nodeStorage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +020091 }
92 image.Close()
Mateusz Zalega8cde8e72021-11-30 16:22:20 +010093 return nodeStorage, nil
Mateusz Zalega43e21072021-10-08 18:05:29 +020094}
95
96// qemuDriveParam returns QEMU parameters required to run it with a
97// raw-format image at path.
98func qemuDriveParam(path string) []string {
99 return []string{"-drive", "if=virtio,format=raw,snapshot=off,cache=unsafe,file=" + path}
100}
101
102// checkEspContents verifies the presence of the EFI payload inside of image's
103// first partition. It returns nil on success.
104func checkEspContents(image *disk.Disk) error {
105 // Get the ESP.
106 fs, err := image.GetFilesystem(1)
107 if err != nil {
108 return fmt.Errorf("couldn't read the installer ESP: %w", err)
109 }
110 // Make sure the EFI payload exists by attempting to open it.
111 efiPayload, err := fs.OpenFile(osimage.EFIPayloadPath, os.O_RDONLY)
112 if err != nil {
113 return fmt.Errorf("couldn't open the installer's EFI Payload at %q: %w", osimage.EFIPayloadPath, err)
114 }
115 efiPayload.Close()
116 return nil
117}
118
119func TestMain(m *testing.M) {
Serge Bazanski97783222021-12-14 16:04:26 +0100120 installerImage = filepath.Join(os.Getenv("TEST_TMPDIR"), "installer.img")
121 nodeStorage = filepath.Join(os.Getenv("TEST_TMPDIR"), "stor.img")
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100122
Mateusz Zalegaedffbb52022-01-11 15:27:22 +0100123 installer := datafile.MustGet("metropolis/installer/test/kernel.efi")
124 bundle := datafile.MustGet("metropolis/installer/test/testos/testos_bundle.zip")
Mateusz Zalega43e21072021-10-08 18:05:29 +0200125 iargs := mctl.MakeInstallerImageArgs{
Serge Bazanski97783222021-12-14 16:04:26 +0100126 Installer: bytes.NewBuffer(installer),
127 InstallerSize: uint64(len(installer)),
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100128 TargetPath: installerImage,
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100129 NodeParams: &api.NodeParameters{},
Serge Bazanski97783222021-12-14 16:04:26 +0100130 Bundle: bytes.NewBuffer(bundle),
131 BundleSize: uint64(len(bundle)),
Mateusz Zalega43e21072021-10-08 18:05:29 +0200132 }
133 if err := mctl.MakeInstallerImage(iargs); err != nil {
Mateusz Zalega8f72b5d2021-12-03 17:08:59 +0100134 log.Fatalf("Couldn't create the installer image at %q: %v", installerImage, err)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200135 }
136 // With common dependencies set up, run the tests.
137 code := m.Run()
138 // Clean up.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100139 os.Remove(installerImage)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200140 os.Exit(code)
141}
142
143func TestInstallerImage(t *testing.T) {
144 // This test examines the installer image, making sure that the GPT and the
145 // ESP contents are in order.
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100146 image, err := diskfs.OpenWithMode(installerImage, diskfs.ReadOnly)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200147 if err != nil {
Mateusz Zalega8cde8e72021-11-30 16:22:20 +0100148 t.Errorf("Couldn't open the installer image at %q: %s", installerImage, err.Error())
Mateusz Zalega43e21072021-10-08 18:05:29 +0200149 }
150 // Verify that GPT exists.
151 ti, err := image.GetPartitionTable()
152 if ti.Type() != "gpt" {
153 t.Error("Couldn't verify that the installer image contains a GPT.")
154 }
155 // Check that the first partition is likely to be a valid ESP.
156 pi := ti.GetPartitions()
157 esp := (pi[0]).(*gpt.Partition)
158 if esp.Start == 0 || esp.End == 0 {
159 t.Error("The installer's ESP GPT entry looks off.")
160 }
161 // Verify that the image contains only one partition.
162 second := (pi[1]).(*gpt.Partition)
163 if second.Name != "" || second.Start != 0 || second.End != 0 {
164 t.Error("It appears the installer image contains more than one partition.")
165 }
166 // Verify the ESP contents.
167 if err := checkEspContents(image); err != nil {
168 t.Error(err.Error())
169 }
170}
171
172func TestNoBlockDevices(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100173 ctx, ctxC := context.WithCancel(context.Background())
174 defer ctxC()
175
Mateusz Zalega43e21072021-10-08 18:05:29 +0200176 // No block devices are passed to QEMU aside from the install medium. Expect
177 // the installer to fail at the device probe stage rather than attempting to
178 // use the medium as the target device.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100179 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100180 result, err := runQemuWithInstaller(ctx, nil, expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200181 if err != nil {
182 t.Error(err.Error())
183 }
184 if result != true {
185 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
186 }
187}
188
189func TestBlockDeviceTooSmall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100190 ctx, ctxC := context.WithCancel(context.Background())
191 defer ctxC()
192
Mateusz Zalega43e21072021-10-08 18:05:29 +0200193 // Prepare the block device the installer will install to. This time the
194 // target device is too small to host a Metropolis installation.
195 imagePath, err := getStorage(64)
196 defer os.Remove(imagePath)
197 if err != nil {
198 t.Errorf(err.Error())
199 }
200
201 // Run QEMU. Expect the installer to fail with a predefined error string.
Mateusz Zalegacdcc7392021-12-08 15:34:53 +0100202 expectedOutput := "Couldn't find a suitable block device"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100203 result, err := runQemuWithInstaller(ctx, qemuDriveParam(imagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200204 if err != nil {
205 t.Error(err.Error())
206 }
207 if result != true {
208 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
209 }
210}
211
212func TestInstall(t *testing.T) {
Serge Bazanskie2e03712021-12-17 12:47:03 +0100213 ctx, ctxC := context.WithCancel(context.Background())
214 defer ctxC()
215
Mateusz Zalega43e21072021-10-08 18:05:29 +0200216 // Prepare the block device image the installer will install to.
217 storagePath, err := getStorage(4096 + 128 + 128 + 1)
218 defer os.Remove(storagePath)
219 if err != nil {
220 t.Errorf(err.Error())
221 }
222
223 // Run QEMU. Expect the installer to succeed.
224 expectedOutput := "Installation completed"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100225 result, err := runQemuWithInstaller(ctx, qemuDriveParam(storagePath), expectedOutput)
Mateusz Zalega43e21072021-10-08 18:05:29 +0200226 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 // Verify the resulting node image. Check whether the node GPT was created.
234 storage, err := diskfs.OpenWithMode(storagePath, diskfs.ReadOnly)
235 if err != nil {
236 t.Errorf("Couldn't open the resulting node image at %q: %s", storagePath, err.Error())
237 }
238 // Verify that GPT exists.
239 ti, err := storage.GetPartitionTable()
240 if ti.Type() != "gpt" {
241 t.Error("Couldn't verify that the resulting node image contains a GPT.")
242 }
243 // Check that the first partition is likely to be a valid ESP.
244 pi := ti.GetPartitions()
245 esp := (pi[0]).(*gpt.Partition)
246 if esp.Name != osimage.ESPVolumeLabel || esp.Start == 0 || esp.End == 0 {
247 t.Error("The node's ESP GPT entry looks off.")
248 }
249 // Verify the system partition's GPT entry.
250 system := (pi[1]).(*gpt.Partition)
251 if system.Name != osimage.SystemVolumeLabel || system.Start == 0 || system.End == 0 {
252 t.Error("The node's system partition GPT entry looks off.")
253 }
254 // Verify the data partition's GPT entry.
255 data := (pi[2]).(*gpt.Partition)
256 if data.Name != osimage.DataVolumeLabel || data.Start == 0 || data.End == 0 {
257 t.Errorf("The node's data partition GPT entry looks off.")
258 }
259 // Verify that there are no more partitions.
260 fourth := (pi[3]).(*gpt.Partition)
261 if fourth.Name != "" || fourth.Start != 0 || fourth.End != 0 {
262 t.Error("The resulting node image contains more partitions than expected.")
263 }
264 // Verify the ESP contents.
265 if err := checkEspContents(storage); err != nil {
266 t.Error(err.Error())
267 }
Mateusz Zalega4f6fad32022-05-25 16:34:30 +0200268 storage.File.Close()
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100269 // Run QEMU again. Expect TestOS to launch successfully.
270 expectedOutput = "_TESTOS_LAUNCH_SUCCESS_"
Serge Bazanskie2e03712021-12-17 12:47:03 +0100271 result, err = runQemu(ctx, qemuDriveParam(storagePath), expectedOutput)
Lorenz Brun0b93c8d2021-11-09 03:58:40 +0100272 if err != nil {
273 t.Error(err.Error())
274 }
275 if result != true {
276 t.Errorf("QEMU didn't produce the expected output %q", expectedOutput)
277 }
Mateusz Zalega43e21072021-10-08 18:05:29 +0200278}