Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions pkg/unikontainers/hypervisors/qemu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) 2023-2026, Nubificus LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hypervisors

import (
"runtime"
"testing"

"github.com/urunc-dev/urunc/pkg/unikontainers/types"
)

func TestQemuUsesKVM(t *testing.T) {
q := &Qemu{}

if !q.UsesKVM() {
t.Fatal("expected UsesKVM() to return true")
}
}

func TestQemuSupportsSharedfs(t *testing.T) {
q := &Qemu{}

if !q.SupportsSharedfs("9pfs") {
t.Fatal("expected SupportsSharedfs() to return true")
}
}

func TestQemuPath(t *testing.T) {
q := &Qemu{
binaryPath: "/usr/bin/qemu-system-x86_64",
}

if q.Path() != "/usr/bin/qemu-system-x86_64" {
t.Fatal("unexpected path")
}
}

func TestQemuPreExec(t *testing.T) {
q := &Qemu{}

err := q.PreExec(types.ExecArgs{})

if err != nil {
t.Fatalf("expected nil error got %v", err)
}
}

func TestGetVirtioNetArg(t *testing.T) {
got := getVirtioNetArg()

expected := "-device virtio-net-pci,netdev=net0"

if runtime.GOARCH == "arm64" {
expected = "-device virtio-net-device,netdev=net0"
}

if got != expected {
t.Fatalf(
"expected %s got %s",
expected,
got,
)
}
}

type mockUnikernel struct{}

func (m mockUnikernel) Init(types.UnikernelParams) error {
return nil
}

func (m mockUnikernel) CommandString() (string, error) {
return "", nil
}

func (m mockUnikernel) SupportsBlock() bool {
return false
}

func (m mockUnikernel) SupportsFS(string) bool {
return false
}

func (m mockUnikernel) MonitorNetCli(string, string) string {
return ""
}

func (m mockUnikernel) MonitorBlockCli() []types.MonitorBlockArgs {
return nil
}

func (m mockUnikernel) MonitorCli() types.MonitorCliArgs {
return types.MonitorCliArgs{}
}

func TestQemuBuildExecCmd(t *testing.T) {
q := &Qemu{
binaryPath: "/usr/bin/qemu-system-x86_64",
}

args := types.ExecArgs{
MemSizeB: 512 * 1024 * 1024,
VCPUs: 2,
UnikernelPath: "/tmp/kernel",
Command: "console=ttyS0",
}

cmd, err := q.BuildExecCmd(
args,
mockUnikernel{},
)

if err != nil {
t.Fatalf(
"unexpected error: %v",
err,
)
}

if len(cmd) == 0 {
t.Fatal(
"expected non-empty command",
)
}

if cmd[0] != "/usr/bin/qemu-system-x86_64" {
t.Fatalf(
"unexpected binary: %s",
cmd[0],
)
}
}