github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/qemu_arm64_test.go (about)

     1  // Copyright (c) 2018 IBM
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  
    14  	govmmQemu "github.com/kata-containers/govmm/qemu"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func qemuConfig(machineType string) HypervisorConfig {
    19  	return HypervisorConfig{
    20  		HypervisorMachineType: machineType,
    21  	}
    22  }
    23  
    24  func newTestQemu(machineType string) qemuArch {
    25  	config := qemuConfig(machineType)
    26  	return newQemuArch(config)
    27  }
    28  
    29  func TestQemuArm64CPUModel(t *testing.T) {
    30  	assert := assert.New(t)
    31  	arm64 := newTestQemu(QemuVirt)
    32  
    33  	expectedOut := defaultCPUModel
    34  	model := arm64.cpuModel()
    35  	assert.Equal(expectedOut, model)
    36  }
    37  
    38  func TestQemuArm64MemoryTopology(t *testing.T) {
    39  	assert := assert.New(t)
    40  	arm64 := newTestQemu(QemuVirt)
    41  
    42  	hostMem := uint64(4096)
    43  	mem := uint64(1024)
    44  	slots := uint8(3)
    45  	expectedMemory := govmmQemu.Memory{
    46  		Size:   fmt.Sprintf("%dM", mem),
    47  		Slots:  slots,
    48  		MaxMem: fmt.Sprintf("%dM", hostMem),
    49  	}
    50  
    51  	m := arm64.memoryTopology(mem, hostMem, slots)
    52  	assert.Equal(expectedMemory, m)
    53  }
    54  
    55  func TestMaxQemuVCPUs(t *testing.T) {
    56  	assert := assert.New(t)
    57  
    58  	vCPUs := MaxQemuVCPUs()
    59  	assert.Equal(uint32(123), vCPUs)
    60  }
    61  
    62  func TestQemuArm64AppendBridges(t *testing.T) {
    63  	var devices []govmmQemu.Device
    64  	assert := assert.New(t)
    65  
    66  	arm64 := newTestQemu(QemuVirt)
    67  
    68  	arm64.bridges(1)
    69  	bridges := arm64.getBridges()
    70  	assert.Len(bridges, 1)
    71  
    72  	devices = []govmmQemu.Device{}
    73  	devices = arm64.appendBridges(devices)
    74  	assert.Len(devices, 1)
    75  
    76  	expectedOut := []govmmQemu.Device{
    77  		govmmQemu.BridgeDevice{
    78  			Type:    govmmQemu.PCIBridge,
    79  			Bus:     defaultBridgeBus,
    80  			ID:      bridges[0].ID,
    81  			Chassis: 1,
    82  			SHPC:    true,
    83  			Addr:    "2",
    84  		},
    85  	}
    86  
    87  	assert.Equal(expectedOut, devices)
    88  }
    89  
    90  func TestQemuArm64AppendImage(t *testing.T) {
    91  	var devices []govmmQemu.Device
    92  	assert := assert.New(t)
    93  
    94  	f, err := ioutil.TempFile("", "img")
    95  	assert.NoError(err)
    96  	defer func() { _ = f.Close() }()
    97  	defer func() { _ = os.Remove(f.Name()) }()
    98  
    99  	imageStat, err := f.Stat()
   100  	assert.NoError(err)
   101  
   102  	// save default supportedQemuMachines options
   103  	machinesCopy := make([]govmmQemu.Machine, len(supportedQemuMachines))
   104  	assert.Equal(len(supportedQemuMachines), copy(machinesCopy, supportedQemuMachines))
   105  
   106  	cfg := qemuConfig(QemuVirt)
   107  	cfg.ImagePath = f.Name()
   108  	arm64 := newQemuArch(cfg)
   109  	for _, m := range arm64.(*qemuArm64).supportedQemuMachines {
   110  		assert.Contains(m.Options, qemuNvdimmOption)
   111  	}
   112  
   113  	expectedOut := []govmmQemu.Device{
   114  		govmmQemu.Object{
   115  			Driver:   govmmQemu.NVDIMM,
   116  			Type:     govmmQemu.MemoryBackendFile,
   117  			DeviceID: "nv0",
   118  			ID:       "mem0",
   119  			MemPath:  f.Name(),
   120  			Size:     (uint64)(imageStat.Size()),
   121  		},
   122  	}
   123  
   124  	devices, err = arm64.appendImage(devices, f.Name())
   125  	assert.NoError(err)
   126  	assert.Equal(expectedOut, devices)
   127  
   128  	// restore default supportedQemuMachines options
   129  	assert.Equal(len(supportedQemuMachines), copy(supportedQemuMachines, machinesCopy))
   130  }
   131  
   132  func TestQemuArm64WithInitrd(t *testing.T) {
   133  	assert := assert.New(t)
   134  
   135  	cfg := qemuConfig(QemuVirt)
   136  	cfg.InitrdPath = "dummy-initrd"
   137  	arm64 := newQemuArch(cfg)
   138  
   139  	for _, m := range arm64.(*qemuArm64).supportedQemuMachines {
   140  		assert.NotContains(m.Options, qemuNvdimmOption)
   141  	}
   142  }