blob: d22a578f18b9695d37495c6b0b71eec19ca1e944 [file] [log] [blame]
Lorenz Brun30167f52021-03-17 17:49:01 +01001// 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 Bazanski216fe7b2021-05-21 18:36:16 +020017// 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 Brun30167f52021-03-17 17:49:01 +010019package main
20
21import (
22 "bytes"
Lorenz Brun764a2de2021-11-22 16:26:36 +010023 "io"
Lorenz Brun30167f52021-03-17 17:49:01 +010024 "log"
25 "net"
26 "os"
27 "os/exec"
Tim Windelschmidt244b5672024-02-06 10:18:56 +010028
29 "github.com/bazelbuild/rules_go/go/runfiles"
Lorenz Brun30167f52021-03-17 17:49:01 +010030)
31
32func 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 Brun764a2de2021-11-22 16:26:36 +010044 testValue, _ := io.ReadAll(conn)
Lorenz Brun30167f52021-03-17 17:49:01 +010045 if bytes.Equal(testValue, []byte("test123")) {
46 testResultChan <- true
47 } else {
48 testResultChan <- false
49 }
50 }()
51
Tim Windelschmidt244b5672024-02-06 10:18:56 +010052 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 Brun30167f52021-03-17 17:49:01 +010064 baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot",
65 "-accel", "kvm", "-cpu", "host",
Tim Windelschmidt244b5672024-02-06 10:18:56 +010066 "-bios", biosPath,
Lorenz Brun30167f52021-03-17 17:49:01 +010067 "-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 Brunb6a9d3c2022-01-27 18:56:20 +010070 "-initrd", "metropolis/vm/smoketest/initramfs.cpio.lz4",
Lorenz Brun30167f52021-03-17 17:49:01 +010071 "-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 Windelschmidt244b5672024-02-06 10:18:56 +010077 qemuCmd := exec.Command(qemuPath, baseArgs...)
Lorenz Brun30167f52021-03-17 17:49:01 +010078 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}