| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [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 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 17 | // This is a small smoke test which will run in a container on top of Metropolis |
| 18 | // Kubernetes. It exercises Metropolis' KVM device plugin, |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 19 | package main |
| 20 | |
| 21 | import ( |
| 22 | "bytes" |
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 23 | "io" |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 24 | "log" |
| 25 | "net" |
| 26 | "os" |
| 27 | "os/exec" |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 28 | "path/filepath" |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame] | 29 | |
| 30 | "github.com/bazelbuild/rules_go/go/runfiles" |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 31 | ) |
| 32 | |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 33 | var ( |
| 34 | // These are filled by bazel at linking time with the canonical path of |
| 35 | // their corresponding file. Inside the init function we resolve it |
| 36 | // with the rules_go runfiles package to the real path. |
| 37 | xQemuPath string |
| 38 | ) |
| 39 | |
| 40 | func init() { |
| 41 | var err error |
| 42 | for _, path := range []*string{ |
| 43 | &xQemuPath, |
| 44 | } { |
| 45 | *path, err = runfiles.Rlocation(*path) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 52 | func main() { |
| 53 | testSocket, err := net.Listen("unix", "@metropolis/vm/smoketest") |
| 54 | if err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | |
| 58 | testResultChan := make(chan bool) |
| 59 | go func() { |
| 60 | conn, err := testSocket.Accept() |
| 61 | if err != nil { |
| 62 | panic(err) |
| 63 | } |
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 64 | testValue, _ := io.ReadAll(conn) |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 65 | if bytes.Equal(testValue, []byte("test123")) { |
| 66 | testResultChan <- true |
| 67 | } else { |
| 68 | testResultChan <- false |
| 69 | } |
| 70 | }() |
| 71 | |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame] | 72 | // TODO(lorenz): This explicitly doesn't use our own qboot because it cannot be built in a musl configuration. |
| 73 | // This will be fixed once we have a proper multi-target toolchain. |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 74 | biosPath := filepath.Join(filepath.Dir(xQemuPath), "pc-bios/qboot.rom") |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame] | 75 | |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 76 | baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot", |
| 77 | "-accel", "kvm", "-cpu", "host", |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame] | 78 | "-bios", biosPath, |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 79 | "-M", "microvm,x-option-roms=off,pic=off,pit=off,rtc=off,isa-serial=off", |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 80 | "-kernel", "osbase/test/ktest/linux-testing.elf", |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 81 | "-append", "reboot=t console=hvc0 quiet", |
| Lorenz Brun | b6a9d3c | 2022-01-27 18:56:20 +0100 | [diff] [blame] | 82 | "-initrd", "metropolis/vm/smoketest/initramfs.cpio.lz4", |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 83 | "-device", "virtio-rng-device,max-bytes=1024,period=1000", |
| 84 | "-device", "virtio-serial-device,max_ports=16", |
| 85 | "-chardev", "stdio,id=con0", "-device", "virtconsole,chardev=con0", |
| 86 | "-chardev", "socket,id=test,path=metropolis/vm/smoketest,abstract=on", |
| 87 | "-device", "virtserialport,chardev=test", |
| 88 | } |
| Tim Windelschmidt | 82e6af7 | 2024-07-23 00:05:42 +0000 | [diff] [blame] | 89 | qemuCmd := exec.Command(xQemuPath, baseArgs...) |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 90 | qemuCmd.Stdout = os.Stdout |
| 91 | qemuCmd.Stderr = os.Stderr |
| 92 | if err := qemuCmd.Run(); err != nil { |
| 93 | log.Fatalf("running QEMU failed: %v", err) |
| 94 | } |
| 95 | testResult := <-testResultChan |
| 96 | if testResult { |
| 97 | return |
| 98 | } else { |
| 99 | os.Exit(1) |
| 100 | } |
| 101 | } |