blob: 58f1a2251b611686e8a6720755f146e2f97ba9bb [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 Windelschmidt82e6af72024-07-23 00:05:42 +000028 "path/filepath"
Tim Windelschmidt244b5672024-02-06 10:18:56 +010029
30 "github.com/bazelbuild/rules_go/go/runfiles"
Lorenz Brun30167f52021-03-17 17:49:01 +010031)
32
Tim Windelschmidt82e6af72024-07-23 00:05:42 +000033var (
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
40func 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 Brun30167f52021-03-17 17:49:01 +010052func 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 Brun764a2de2021-11-22 16:26:36 +010064 testValue, _ := io.ReadAll(conn)
Lorenz Brun30167f52021-03-17 17:49:01 +010065 if bytes.Equal(testValue, []byte("test123")) {
66 testResultChan <- true
67 } else {
68 testResultChan <- false
69 }
70 }()
71
Tim Windelschmidt244b5672024-02-06 10:18:56 +010072 // 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 Windelschmidt82e6af72024-07-23 00:05:42 +000074 biosPath := filepath.Join(filepath.Dir(xQemuPath), "pc-bios/qboot.rom")
Tim Windelschmidt244b5672024-02-06 10:18:56 +010075
Lorenz Brun30167f52021-03-17 17:49:01 +010076 baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot",
77 "-accel", "kvm", "-cpu", "host",
Tim Windelschmidt244b5672024-02-06 10:18:56 +010078 "-bios", biosPath,
Lorenz Brun30167f52021-03-17 17:49:01 +010079 "-M", "microvm,x-option-roms=off,pic=off,pit=off,rtc=off,isa-serial=off",
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020080 "-kernel", "osbase/test/ktest/linux-testing.elf",
Lorenz Brun30167f52021-03-17 17:49:01 +010081 "-append", "reboot=t console=hvc0 quiet",
Lorenz Brunb6a9d3c2022-01-27 18:56:20 +010082 "-initrd", "metropolis/vm/smoketest/initramfs.cpio.lz4",
Lorenz Brun30167f52021-03-17 17:49:01 +010083 "-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 Windelschmidt82e6af72024-07-23 00:05:42 +000089 qemuCmd := exec.Command(xQemuPath, baseArgs...)
Lorenz Brun30167f52021-03-17 17:49:01 +010090 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}