Merge pull request #2844 from jongwu/unit_test

enable unit test on arm
This commit is contained in:
James O. D. Hunt 2021-10-25 10:58:21 +01:00 committed by GitHub
commit ec3aa1694b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 54 additions and 14 deletions

1
src/agent/Cargo.lock generated
View File

@ -546,6 +546,7 @@ dependencies = [
"scopeguard",
"serde",
"serde_json",
"serial_test",
"slog",
"slog-scope",
"slog-stdlog",

View File

@ -20,6 +20,7 @@ scan_fmt = "0.2.3"
scopeguard = "1.0.0"
thiserror = "1.0.26"
regex = "1"
serial_test = "0.5.1"
# Async helpers
async-trait = "0.1.42"

View File

@ -464,7 +464,10 @@ mod tests {
baremount(src, dst, "bind", MsFlags::MS_BIND, "", logger)
}
use serial_test::serial;
#[tokio::test]
#[serial]
async fn set_sandbox_storage() {
let logger = slog::Logger::root(slog::Discard, o!());
let mut s = Sandbox::new(&logger).unwrap();
@ -499,6 +502,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn remove_sandbox_storage() {
skip_if_not_root!();
@ -555,6 +559,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn unset_and_remove_sandbox_storage() {
skip_if_not_root!();
@ -606,6 +611,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn unset_sandbox_storage() {
let logger = slog::Logger::root(slog::Discard, o!());
let mut s = Sandbox::new(&logger).unwrap();
@ -689,6 +695,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn get_container_entry_exist() {
skip_if_not_root!();
let logger = slog::Logger::root(slog::Discard, o!());
@ -702,6 +709,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn get_container_no_entry() {
let logger = slog::Logger::root(slog::Discard, o!());
let mut s = Sandbox::new(&logger).unwrap();
@ -711,6 +719,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn add_and_get_container() {
skip_if_not_root!();
let logger = slog::Logger::root(slog::Discard, o!());
@ -722,6 +731,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn update_shared_pidns() {
skip_if_not_root!();
let logger = slog::Logger::root(slog::Discard, o!());
@ -740,6 +750,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn add_guest_hooks() {
let logger = slog::Logger::root(slog::Discard, o!());
let mut s = Sandbox::new(&logger).unwrap();
@ -763,6 +774,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn test_sandbox_set_destroy() {
let logger = slog::Logger::root(slog::Discard, o!());
let mut s = Sandbox::new(&logger).unwrap();

View File

@ -979,7 +979,10 @@ mod tests {
);
}
use serial_test::serial;
#[tokio::test]
#[serial]
async fn create_tmpfs() {
skip_if_not_root!();
@ -994,6 +997,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn spawn_thread() {
skip_if_not_root!();
@ -1023,6 +1027,7 @@ mod tests {
}
#[tokio::test]
#[serial]
async fn verify_container_cleanup_watching() {
skip_if_not_root!();

View File

@ -147,8 +147,15 @@ func getDistroDetails() (name, version string, err error) {
// centos: 3.10.0-957.12.1.el7.x86_64
// fedora: 5.0.9-200.fc29.x86_64
//
// For some self compiled kernel, the kernel version will be with "+" as its suffix
// For example:
// 5.12.0-rc4+
// These kernel version can't be parsed by the current lib and lead to panic
// therefore the '+' should be removed.
//
func fixKernelVersion(version string) string {
return strings.Replace(version, "_", "-", -1)
version = strings.Replace(version, "_", "-", -1)
return strings.Replace(version, "+", "", -1)
}
// handleDistroName checks that the current distro is compatible with

View File

@ -308,11 +308,24 @@ func (h hypervisor) GetEntropySource() string {
return h.EntropySource
}
// Current cpu number should not larger than defaultMaxVCPUs()
func getCurrentCpuNum() uint32 {
var cpu uint32
h := hypervisor{}
cpu = uint32(goruntime.NumCPU())
if cpu > h.defaultMaxVCPUs() {
cpu = h.defaultMaxVCPUs()
}
return cpu
}
func (h hypervisor) defaultVCPUs() uint32 {
numCPUs := goruntime.NumCPU()
numCPUs := getCurrentCpuNum()
if h.NumVCPUs < 0 || h.NumVCPUs > int32(numCPUs) {
return uint32(numCPUs)
return numCPUs
}
if h.NumVCPUs == 0 { // or unspecified
return defaultVCPUCount

View File

@ -14,7 +14,6 @@ import (
"path"
"path/filepath"
"reflect"
goruntime "runtime"
"strings"
"syscall"
"testing"
@ -156,7 +155,7 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf
KernelParams: vc.DeserializeParams(strings.Fields(kernelParams)),
HypervisorMachineType: machineType,
NumVCPUs: defaultVCPUCount,
DefaultMaxVCPUs: uint32(goruntime.NumCPU()),
DefaultMaxVCPUs: getCurrentCpuNum(),
MemorySize: defaultMemSize,
DisableBlockDeviceUse: disableBlockDevice,
BlockDeviceDriver: defaultBlockDeviceDriver,
@ -919,13 +918,13 @@ func TestNewClhHypervisorConfig(t *testing.T) {
func TestHypervisorDefaults(t *testing.T) {
assert := assert.New(t)
numCPUs := goruntime.NumCPU()
numCPUs := getCurrentCpuNum()
h := hypervisor{}
assert.Equal(h.machineType(), defaultMachineType, "default hypervisor machine type wrong")
assert.Equal(h.defaultVCPUs(), defaultVCPUCount, "default vCPU number is wrong")
assert.Equal(h.defaultMaxVCPUs(), uint32(numCPUs), "default max vCPU number is wrong")
assert.Equal(h.defaultMaxVCPUs(), numCPUs, "default max vCPU number is wrong")
assert.Equal(h.defaultMemSz(), defaultMemSize, "default memory size is wrong")
machineType := "foo"
@ -934,23 +933,23 @@ func TestHypervisorDefaults(t *testing.T) {
// auto inferring
h.NumVCPUs = -1
assert.Equal(h.defaultVCPUs(), uint32(numCPUs), "default vCPU number is wrong")
assert.Equal(h.defaultVCPUs(), numCPUs, "default vCPU number is wrong")
h.NumVCPUs = 2
assert.Equal(h.defaultVCPUs(), uint32(2), "default vCPU number is wrong")
h.NumVCPUs = int32(numCPUs) + 1
assert.Equal(h.defaultVCPUs(), uint32(numCPUs), "default vCPU number is wrong")
assert.Equal(h.defaultVCPUs(), numCPUs, "default vCPU number is wrong")
h.DefaultMaxVCPUs = 2
assert.Equal(h.defaultMaxVCPUs(), uint32(2), "default max vCPU number is wrong")
h.DefaultMaxVCPUs = uint32(numCPUs) + 1
assert.Equal(h.defaultMaxVCPUs(), uint32(numCPUs), "default max vCPU number is wrong")
h.DefaultMaxVCPUs = numCPUs + 1
assert.Equal(h.defaultMaxVCPUs(), numCPUs, "default max vCPU number is wrong")
maxvcpus := vc.MaxQemuVCPUs()
h.DefaultMaxVCPUs = maxvcpus + 1
assert.Equal(h.defaultMaxVCPUs(), uint32(numCPUs), "default max vCPU number is wrong")
assert.Equal(h.defaultMaxVCPUs(), numCPUs, "default max vCPU number is wrong")
h.MemorySize = 1024
assert.Equal(h.defaultMemSz(), uint32(1024), "default memory size is wrong")

View File

@ -9,6 +9,7 @@ import (
"context"
"fmt"
"os"
"runtime"
"testing"
"time"
@ -22,7 +23,8 @@ import (
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
func TestTemplateFactory(t *testing.T) {
if os.Geteuid() != 0 {
// template is broken on arm64, so, temporarily disable it on arm64
if runtime.GOARCH == "arm64" || os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

View File

@ -569,7 +569,7 @@ func (conf *HypervisorConfig) Valid() error {
conf.BlockDeviceDriver = config.VirtioBlockCCW
}
if conf.DefaultMaxVCPUs == 0 {
if conf.DefaultMaxVCPUs == 0 || conf.DefaultMaxVCPUs > defaultMaxQemuVCPUs {
conf.DefaultMaxVCPUs = defaultMaxQemuVCPUs
}