| 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 | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame^] | 28 | |
| 29 | "github.com/bazelbuild/rules_go/go/runfiles" |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | func main() { |
| 33 | testSocket, err := net.Listen("unix", "@metropolis/vm/smoketest") |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | |
| 38 | testResultChan := make(chan bool) |
| 39 | go func() { |
| 40 | conn, err := testSocket.Accept() |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| Lorenz Brun | 764a2de | 2021-11-22 16:26:36 +0100 | [diff] [blame] | 44 | testValue, _ := io.ReadAll(conn) |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 45 | if bytes.Equal(testValue, []byte("test123")) { |
| 46 | testResultChan <- true |
| 47 | } else { |
| 48 | testResultChan <- false |
| 49 | } |
| 50 | }() |
| 51 | |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame^] | 52 | qemuPath, err := runfiles.Rlocation("qemu/qemu-x86_64-softmmu") |
| 53 | if err != nil { |
| 54 | panic(err) |
| 55 | } |
| 56 | |
| 57 | // TODO(lorenz): This explicitly doesn't use our own qboot because it cannot be built in a musl configuration. |
| 58 | // This will be fixed once we have a proper multi-target toolchain. |
| 59 | biosPath, err := runfiles.Rlocation("qemu/pc-bios/qboot.rom") |
| 60 | if err != nil { |
| 61 | panic(err) |
| 62 | } |
| 63 | |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 64 | baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot", |
| 65 | "-accel", "kvm", "-cpu", "host", |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame^] | 66 | "-bios", biosPath, |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 67 | "-M", "microvm,x-option-roms=off,pic=off,pit=off,rtc=off,isa-serial=off", |
| 68 | "-kernel", "metropolis/test/ktest/linux-testing.elf", |
| 69 | "-append", "reboot=t console=hvc0 quiet", |
| Lorenz Brun | b6a9d3c | 2022-01-27 18:56:20 +0100 | [diff] [blame] | 70 | "-initrd", "metropolis/vm/smoketest/initramfs.cpio.lz4", |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 71 | "-device", "virtio-rng-device,max-bytes=1024,period=1000", |
| 72 | "-device", "virtio-serial-device,max_ports=16", |
| 73 | "-chardev", "stdio,id=con0", "-device", "virtconsole,chardev=con0", |
| 74 | "-chardev", "socket,id=test,path=metropolis/vm/smoketest,abstract=on", |
| 75 | "-device", "virtserialport,chardev=test", |
| 76 | } |
| Tim Windelschmidt | 244b567 | 2024-02-06 10:18:56 +0100 | [diff] [blame^] | 77 | qemuCmd := exec.Command(qemuPath, baseArgs...) |
| Lorenz Brun | 30167f5 | 2021-03-17 17:49:01 +0100 | [diff] [blame] | 78 | qemuCmd.Stdout = os.Stdout |
| 79 | qemuCmd.Stderr = os.Stderr |
| 80 | if err := qemuCmd.Run(); err != nil { |
| 81 | log.Fatalf("running QEMU failed: %v", err) |
| 82 | } |
| 83 | testResult := <-testResultChan |
| 84 | if testResult { |
| 85 | return |
| 86 | } else { |
| 87 | os.Exit(1) |
| 88 | } |
| 89 | } |