blob: 3541f5129f1fbe8482f826bf49828e5d184d7196 [file] [log] [blame]
Lorenz Brun547b33f2020-04-23 15:27:06 +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// ktest is a test launcher for running tests inside a custom kernel and passes the results
18// back out.
19package main
20
21import (
Lorenz Brun3ff5af32020-06-24 16:34:11 +020022 "context"
Lorenz Brun547b33f2020-04-23 15:27:06 +020023 "flag"
Lorenz Brun547b33f2020-04-23 15:27:06 +020024 "io"
25 "log"
Lorenz Brun547b33f2020-04-23 15:27:06 +020026 "os"
Lorenz Brun3ff5af32020-06-24 16:34:11 +020027 "time"
28
29 "git.monogon.dev/source/nexantic.git/core/internal/launch"
Lorenz Brun547b33f2020-04-23 15:27:06 +020030)
31
32var (
33 kernelPath = flag.String("kernel-path", "", "Path of the Kernel ELF file")
34 initrdPath = flag.String("initrd-path", "", "Path of the initrd image")
35 cmdline = flag.String("cmdline", "", "Additional kernel command line options")
36)
37
38func main() {
39 flag.Parse()
40
Lorenz Brun3ff5af32020-06-24 16:34:11 +020041 hostFeedbackConn, vmFeedbackConn, err := launch.NewSocketPair()
Lorenz Brun547b33f2020-04-23 15:27:06 +020042 if err != nil {
Lorenz Brun3ff5af32020-06-24 16:34:11 +020043 log.Fatalf("Failed to create socket pair: %v", err)
Lorenz Brun547b33f2020-04-23 15:27:06 +020044 }
Lorenz Brun547b33f2020-04-23 15:27:06 +020045
46 exitCodeChan := make(chan uint8, 1)
47
48 go func() {
Lorenz Brun3ff5af32020-06-24 16:34:11 +020049 defer hostFeedbackConn.Close()
Lorenz Brun547b33f2020-04-23 15:27:06 +020050
51 returnCode := make([]byte, 1)
Lorenz Brun3ff5af32020-06-24 16:34:11 +020052 if _, err := io.ReadFull(hostFeedbackConn, returnCode); err != nil {
Lorenz Brun547b33f2020-04-23 15:27:06 +020053 log.Fatalf("Failed to read socket: %v", err)
54 }
55 exitCodeChan <- returnCode[0]
56 }()
57
Lorenz Brun3ff5af32020-06-24 16:34:11 +020058 if err := launch.RunMicroVM(context.Background(), &launch.MicroVMOptions{
59 KernelPath: *kernelPath,
60 InitramfsPath: *initrdPath,
61 Cmdline: *cmdline,
62 SerialPort: os.Stdout,
63 ExtraChardevs: []*os.File{vmFeedbackConn},
64 DisableHostNetworkInterface: true,
65 }); err != nil {
66 log.Fatalf("Failed to run ktest VM: %v", err)
Lorenz Brun547b33f2020-04-23 15:27:06 +020067 }
Lorenz Brun3ff5af32020-06-24 16:34:11 +020068
Lorenz Brun547b33f2020-04-23 15:27:06 +020069 select {
70 case exitCode := <-exitCodeChan:
71 os.Exit(int(exitCode))
Lorenz Brun3ff5af32020-06-24 16:34:11 +020072 case <-time.After(1 * time.Second):
73 log.Fatal("Failed to get an error code back (test runtime probably crashed)")
Lorenz Brun547b33f2020-04-23 15:27:06 +020074 }
75}