github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/acrn_test.go (about) 1 // Copyright (c) 2019 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package virtcontainers 7 8 import ( 9 "context" 10 "fmt" 11 "path/filepath" 12 "testing" 13 14 "github.com/kata-containers/runtime/virtcontainers/device/config" 15 "github.com/kata-containers/runtime/virtcontainers/persist" 16 "github.com/kata-containers/runtime/virtcontainers/types" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func newAcrnConfig() HypervisorConfig { 21 return HypervisorConfig{ 22 KernelPath: testAcrnKernelPath, 23 ImagePath: testAcrnImagePath, 24 HypervisorPath: testAcrnPath, 25 HypervisorCtlPath: testAcrnCtlPath, 26 NumVCPUs: defaultVCPUs, 27 MemorySize: defaultMemSzMiB, 28 BlockDeviceDriver: config.VirtioBlock, 29 DefaultBridges: defaultBridges, 30 DefaultMaxVCPUs: MaxAcrnVCPUs(), 31 // Adding this here, as hypervisorconfig.valid() 32 // forcefully adds it even when 9pfs is not supported 33 Msize9p: defaultMsize9p, 34 } 35 } 36 37 func testAcrnKernelParameters(t *testing.T, kernelParams []Param, debug bool) { 38 assert := assert.New(t) 39 acrnConfig := newAcrnConfig() 40 acrnConfig.KernelParams = kernelParams 41 42 if debug == true { 43 acrnConfig.Debug = true 44 } 45 46 a := &Acrn{ 47 config: acrnConfig, 48 arch: &acrnArchBase{}, 49 } 50 51 expected := fmt.Sprintf("panic=1 maxcpus=%d foo=foo bar=bar", a.config.DefaultMaxVCPUs) 52 53 params := a.kernelParameters() 54 assert.Equal(params, expected) 55 } 56 57 func TestAcrnKernelParameters(t *testing.T) { 58 params := []Param{ 59 { 60 Key: "foo", 61 Value: "foo", 62 }, 63 { 64 Key: "bar", 65 Value: "bar", 66 }, 67 } 68 69 testAcrnKernelParameters(t, params, true) 70 testAcrnKernelParameters(t, params, false) 71 } 72 73 func TestAcrnCapabilities(t *testing.T) { 74 assert := assert.New(t) 75 a := &Acrn{ 76 ctx: context.Background(), 77 arch: &acrnArchBase{}, 78 } 79 80 caps := a.capabilities() 81 assert.True(caps.IsBlockDeviceSupported()) 82 assert.True(caps.IsBlockDeviceHotplugSupported()) 83 } 84 85 func testAcrnAddDevice(t *testing.T, devInfo interface{}, devType deviceType, expected []Device) { 86 assert := assert.New(t) 87 a := &Acrn{ 88 ctx: context.Background(), 89 arch: &acrnArchBase{}, 90 } 91 92 err := a.addDevice(devInfo, devType) 93 assert.NoError(err) 94 assert.Exactly(a.acrnConfig.Devices, expected) 95 } 96 97 func TestAcrnAddDeviceSerialPortDev(t *testing.T) { 98 name := "serial.test" 99 hostPath := "/tmp/serial.sock" 100 101 expectedOut := []Device{ 102 ConsoleDevice{ 103 Name: name, 104 Backend: Socket, 105 PortType: SerialBE, 106 Path: hostPath, 107 }, 108 } 109 110 socket := types.Socket{ 111 HostPath: hostPath, 112 Name: name, 113 } 114 115 testAcrnAddDevice(t, socket, serialPortDev, expectedOut) 116 } 117 118 func TestAcrnAddDeviceBlockDev(t *testing.T) { 119 path := "/tmp/test.img" 120 index := 1 121 122 expectedOut := []Device{ 123 BlockDevice{ 124 FilePath: path, 125 Index: index, 126 }, 127 } 128 129 drive := config.BlockDrive{ 130 File: path, 131 Index: index, 132 } 133 134 testAcrnAddDevice(t, drive, blockDev, expectedOut) 135 } 136 137 func TestAcrnHotplugUnsupportedDeviceType(t *testing.T) { 138 assert := assert.New(t) 139 140 acrnConfig := newAcrnConfig() 141 a := &Acrn{ 142 ctx: context.Background(), 143 id: "acrnTest", 144 config: acrnConfig, 145 } 146 147 _, err := a.hotplugAddDevice(&memoryDevice{0, 128, uint64(0), false}, fsDev) 148 assert.Error(err) 149 } 150 151 func TestAcrnUpdateBlockDeviceInvalidPath(t *testing.T) { 152 assert := assert.New(t) 153 154 path := "" 155 index := 1 156 157 acrnConfig := newAcrnConfig() 158 a := &Acrn{ 159 ctx: context.Background(), 160 id: "acrnBlkTest", 161 config: acrnConfig, 162 } 163 164 drive := &config.BlockDrive{ 165 File: path, 166 Index: index, 167 } 168 169 err := a.updateBlockDevice(drive) 170 assert.Error(err) 171 } 172 173 func TestAcrnUpdateBlockDeviceInvalidIdx(t *testing.T) { 174 assert := assert.New(t) 175 176 path := "/tmp/test.img" 177 index := AcrnBlkDevPoolSz + 1 178 179 acrnConfig := newAcrnConfig() 180 a := &Acrn{ 181 ctx: context.Background(), 182 id: "acrnBlkTest", 183 config: acrnConfig, 184 } 185 186 drive := &config.BlockDrive{ 187 File: path, 188 Index: index, 189 } 190 191 err := a.updateBlockDevice(drive) 192 assert.Error(err) 193 } 194 195 func TestAcrnGetSandboxConsole(t *testing.T) { 196 assert := assert.New(t) 197 198 store, err := persist.GetDriver() 199 assert.NoError(err) 200 201 a := &Acrn{ 202 ctx: context.Background(), 203 store: store, 204 } 205 sandboxID := "testSandboxID" 206 expected := filepath.Join(a.store.RunVMStoragePath(), sandboxID, consoleSocket) 207 208 result, err := a.getSandboxConsole(sandboxID) 209 assert.NoError(err) 210 assert.Equal(result, expected) 211 } 212 213 func TestAcrnCreateSandbox(t *testing.T) { 214 assert := assert.New(t) 215 acrnConfig := newAcrnConfig() 216 store, err := persist.GetDriver() 217 assert.NoError(err) 218 219 a := &Acrn{ 220 store: store, 221 } 222 223 sandbox := &Sandbox{ 224 ctx: context.Background(), 225 id: "testSandbox", 226 config: &SandboxConfig{ 227 HypervisorConfig: acrnConfig, 228 }, 229 state: types.SandboxState{BlockIndexMap: make(map[int]struct{})}, 230 } 231 232 err = globalSandboxList.addSandbox(sandbox) 233 assert.NoError(err) 234 235 defer globalSandboxList.removeSandbox(sandbox.id) 236 237 //set PID to 1 to ignore hypercall to get UUID and set a random UUID 238 a.state.PID = 1 239 a.state.UUID = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" 240 err = a.createSandbox(context.Background(), sandbox.id, NetworkNamespace{}, &sandbox.config.HypervisorConfig, false) 241 assert.NoError(err) 242 assert.Exactly(acrnConfig, a.config) 243 } 244 245 func TestAcrnMemoryTopology(t *testing.T) { 246 mem := uint32(1000) 247 assert := assert.New(t) 248 249 a := &Acrn{ 250 arch: &acrnArchBase{}, 251 config: HypervisorConfig{ 252 MemorySize: mem, 253 }, 254 } 255 256 expectedOut := Memory{ 257 Size: fmt.Sprintf("%dM", mem), 258 } 259 260 memory, err := a.memoryTopology() 261 assert.NoError(err) 262 assert.Exactly(memory, expectedOut) 263 }