github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/qemu_ppc64le_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 "os/exec" 11 "regexp" 12 "strconv" 13 "testing" 14 15 govmmQemu "github.com/kata-containers/govmm/qemu" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 var qemuVersionArgs = "--version" 20 21 func newTestQemu(machineType string) qemuArch { 22 config := HypervisorConfig{ 23 HypervisorMachineType: machineType, 24 } 25 return newQemuArch(config) 26 } 27 28 func TestQemuPPC64leCPUModel(t *testing.T) { 29 assert := assert.New(t) 30 ppc64le := newTestQemu(QemuPseries) 31 32 expectedOut := defaultCPUModel 33 model := ppc64le.cpuModel() 34 assert.Equal(expectedOut, model) 35 } 36 37 func getQemuVersion() (qemuMajorVersion int, qemuMinorVersion int) { 38 39 cmd := exec.Command(defaultQemuPath, qemuVersionArgs) 40 additionalEnv := "LANG=C" 41 cmd.Env = append(cmd.Env, additionalEnv) 42 out, err := cmd.Output() 43 if err != nil { 44 err = fmt.Errorf("Could not execute command %s %s", defaultQemuPath, qemuVersionArgs) 45 fmt.Println(err.Error()) 46 } 47 48 re := regexp.MustCompile("[0-9]+") 49 qVer := re.FindAllString(string(out), -1) 50 51 qMajor, err := strconv.Atoi(qVer[0]) 52 qMinor, err1 := strconv.Atoi(qVer[1]) 53 54 if err != nil || err1 != nil { 55 err = fmt.Errorf("Could not convert string to int") 56 fmt.Println(err.Error()) 57 } 58 59 return qMajor, qMinor 60 } 61 62 func TestQemuPPC64leMemoryTopology(t *testing.T) { 63 assert := assert.New(t) 64 ppc64le := newTestQemu(QemuPseries) 65 memoryOffset := 1024 66 67 hostMem := uint64(1024) 68 mem := uint64(120) 69 slots := uint8(10) 70 71 qemuMajorVersion, qemuMinorVersion = getQemuVersion() 72 m := ppc64le.memoryTopology(mem, hostMem, slots) 73 74 if qemuMajorVersion <= 2 && qemuMinorVersion < 10 { 75 hostMem = uint64(defaultMemMaxPPC64le) 76 } 77 78 expectedMemory := govmmQemu.Memory{ 79 Size: fmt.Sprintf("%dM", mem), 80 Slots: slots, 81 MaxMem: fmt.Sprintf("%dM", hostMem+uint64(memoryOffset)), 82 } 83 84 assert.Equal(expectedMemory, m) 85 }