From 87d91710b9a00b75ce70038af82b69695aae08d5 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Mon, 15 Apr 2019 10:39:11 +0100 Subject: [PATCH] utils: Remove code duplication Replace the two versions of `makeRuntimeConfigFileData()` with a single `MakeRuntimeConfigFileData()` in a new `katatestutils` package and a new `katautils.GetDefaultHypervisorConfig()` to query the default hypervisor details. This isn't ideal but a new package had to be created to avoid circular dependencies. It was also required since test code cannot be exported from a package. Signed-off-by: James O. D. Hunt --- cli/kata-env_test.go | 99 ++++++++++++------------------------ pkg/katatestutils/utils.go | 87 +++++++++++++++++++++++++++++++ pkg/katautils/config.go | 13 +++-- pkg/katautils/config_test.go | 74 ++++++++++++--------------- 4 files changed, 160 insertions(+), 113 deletions(-) create mode 100644 pkg/katatestutils/utils.go diff --git a/cli/kata-env_test.go b/cli/kata-env_test.go index 7ee8d2e8fb..3bb1b589c7 100644 --- a/cli/kata-env_test.go +++ b/cli/kata-env_test.go @@ -24,8 +24,7 @@ import ( specs "github.com/opencontainers/runtime-spec/specs-go" "github.com/urfave/cli" - "strconv" - + "github.com/kata-containers/runtime/pkg/katatestutils" "github.com/kata-containers/runtime/pkg/katautils" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" "github.com/stretchr/testify/assert" @@ -36,12 +35,6 @@ const testShimVersion = "shim version 0.1" const testNetmonVersion = "netmon version 0.1" const testHypervisorVersion = "QEMU emulator version 2.7.0+git.741f430a96-6.1, Copyright (c) 2003-2016 Fabrice Bellard and the QEMU Project developers" -const defaultVCPUCount uint32 = 1 -const defaultMaxVCPUCount uint32 = 0 -const defaultMemSize uint32 = 2048 // MiB -const defaultMsize9p uint32 = 8192 -const defaultGuestHookPath string = "" - var ( hypervisorDebug = false proxyDebug = false @@ -81,47 +74,6 @@ func createConfig(configPath string, fileData string) error { return nil } -func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath, kernelParams, machineType, shimPath, proxyPath, netmonPath, logPath string, disableBlock bool, blockDeviceDriver string, enableIOThreads bool, hotplugVFIOOnRootBus, disableNewNetNs bool) string { - return ` - # Runtime configuration file - - [hypervisor.` + hypervisor + `] - path = "` + hypervisorPath + `" - kernel = "` + kernelPath + `" - block_device_driver = "` + blockDeviceDriver + `" - kernel_params = "` + kernelParams + `" - image = "` + imagePath + `" - machine_type = "` + machineType + `" - default_vcpus = ` + strconv.FormatUint(uint64(defaultVCPUCount), 10) + ` - default_maxvcpus = ` + strconv.FormatUint(uint64(defaultMaxVCPUCount), 10) + ` - default_memory = ` + strconv.FormatUint(uint64(defaultMemSize), 10) + ` - disable_block_device_use = ` + strconv.FormatBool(disableBlock) + ` - enable_iothreads = ` + strconv.FormatBool(enableIOThreads) + ` - hotplug_vfio_on_root_bus = ` + strconv.FormatBool(hotplugVFIOOnRootBus) + ` - msize_9p = ` + strconv.FormatUint(uint64(defaultMsize9p), 10) + ` - enable_debug = ` + strconv.FormatBool(hypervisorDebug) + ` - guest_hook_path = "` + defaultGuestHookPath + `" - - [proxy.kata] - enable_debug = ` + strconv.FormatBool(proxyDebug) + ` - path = "` + proxyPath + `" - - [shim.kata] - path = "` + shimPath + `" - enable_debug = ` + strconv.FormatBool(shimDebug) + ` - - [agent.kata] - - [netmon] - path = "` + netmonPath + `" - enable_debug = ` + strconv.FormatBool(netmonDebug) + ` - - [runtime] - enable_debug = ` + strconv.FormatBool(runtimeDebug) + ` - enable_tracing = ` + strconv.FormatBool(runtimeTrace) + ` - disable_new_netns= ` + strconv.FormatBool(disableNewNetNs) -} - func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeConfig, err error) { const logPath = "/log/path" hypervisorPath := filepath.Join(prefixDir, "hypervisor") @@ -172,23 +124,38 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC return "", oci.RuntimeConfig{}, err } - runtimeConfig := makeRuntimeConfigFileData( - "qemu", - hypervisorPath, - kernelPath, - imagePath, - kernelParams, - machineType, - shimPath, - proxyPath, - netmonPath, - logPath, - disableBlock, - blockStorageDriver, - enableIOThreads, - hotplugVFIOOnRootBus, - disableNewNetNs, - ) + hypConfig := katautils.GetDefaultHypervisorConfig() + + configFileOptions := katatestutils.RuntimeConfigOptions{ + Hypervisor: "qemu", + HypervisorPath: hypervisorPath, + KernelPath: kernelPath, + ImagePath: imagePath, + KernelParams: kernelParams, + MachineType: machineType, + ShimPath: shimPath, + ProxyPath: proxyPath, + NetmonPath: netmonPath, + LogPath: logPath, + DefaultGuestHookPath: hypConfig.GuestHookPath, + DisableBlock: disableBlock, + BlockDeviceDriver: blockStorageDriver, + EnableIOThreads: enableIOThreads, + HotplugVFIOOnRootBus: hotplugVFIOOnRootBus, + DisableNewNetNs: disableNewNetNs, + DefaultVCPUCount: hypConfig.NumVCPUs, + DefaultMaxVCPUCount: hypConfig.DefaultMaxVCPUs, + DefaultMemSize: hypConfig.MemorySize, + DefaultMsize9p: hypConfig.Msize9p, + HypervisorDebug: hypervisorDebug, + RuntimeDebug: runtimeDebug, + RuntimeTrace: runtimeTrace, + ProxyDebug: proxyDebug, + ShimDebug: shimDebug, + NetmonDebug: netmonDebug, + } + + runtimeConfig := katatestutils.MakeRuntimeConfigFileData(configFileOptions) configFile = path.Join(prefixDir, "runtime.toml") err = createConfig(configFile, runtimeConfig) diff --git a/pkg/katatestutils/utils.go b/pkg/katatestutils/utils.go new file mode 100644 index 0000000000..ef2dcc6549 --- /dev/null +++ b/pkg/katatestutils/utils.go @@ -0,0 +1,87 @@ +// Copyright (c) 2018-2019 Intel Corporation +// Copyright (c) 2018 HyperHQ Inc. +// +// SPDX-License-Identifier: Apache-2.0 +// + +package katatestutils + +import "strconv" + +type RuntimeConfigOptions struct { + Hypervisor string + HypervisorPath string + DefaultVCPUCount uint32 + DefaultMaxVCPUCount uint32 + DefaultMemSize uint32 + DefaultMsize9p uint32 + DefaultGuestHookPath string + KernelPath string + ImagePath string + KernelParams string + MachineType string + ShimPath string + ProxyPath string + NetmonPath string + LogPath string + BlockDeviceDriver string + AgentTraceMode string + AgentTraceType string + DisableBlock bool + EnableIOThreads bool + HotplugVFIOOnRootBus bool + DisableNewNetNs bool + HypervisorDebug bool + RuntimeDebug bool + RuntimeTrace bool + ProxyDebug bool + ShimDebug bool + NetmonDebug bool + AgentDebug bool + AgentTrace bool +} + +func MakeRuntimeConfigFileData(config RuntimeConfigOptions) string { + return ` + # Runtime configuration file + + [hypervisor.` + config.Hypervisor + `] + path = "` + config.HypervisorPath + `" + kernel = "` + config.KernelPath + `" + block_device_driver = "` + config.BlockDeviceDriver + `" + kernel_params = "` + config.KernelParams + `" + image = "` + config.ImagePath + `" + machine_type = "` + config.MachineType + `" + default_vcpus = ` + strconv.FormatUint(uint64(config.DefaultVCPUCount), 10) + ` + default_maxvcpus = ` + strconv.FormatUint(uint64(config.DefaultMaxVCPUCount), 10) + ` + default_memory = ` + strconv.FormatUint(uint64(config.DefaultMemSize), 10) + ` + disable_block_device_use = ` + strconv.FormatBool(config.DisableBlock) + ` + enable_iothreads = ` + strconv.FormatBool(config.EnableIOThreads) + ` + hotplug_vfio_on_root_bus = ` + strconv.FormatBool(config.HotplugVFIOOnRootBus) + ` + msize_9p = ` + strconv.FormatUint(uint64(config.DefaultMsize9p), 10) + ` + enable_debug = ` + strconv.FormatBool(config.HypervisorDebug) + ` + guest_hook_path = "` + config.DefaultGuestHookPath + `" + + [proxy.kata] + enable_debug = ` + strconv.FormatBool(config.ProxyDebug) + ` + path = "` + config.ProxyPath + `" + + [shim.kata] + path = "` + config.ShimPath + `" + enable_debug = ` + strconv.FormatBool(config.ShimDebug) + ` + + [agent.kata] + enable_debug = ` + strconv.FormatBool(config.AgentDebug) + ` + enable_tracing = ` + strconv.FormatBool(config.AgentTrace) + ` + trace_mode = "` + config.AgentTraceMode + `"` + ` + trace_type = "` + config.AgentTraceType + `"` + ` + + [netmon] + path = "` + config.NetmonPath + `" + enable_debug = ` + strconv.FormatBool(config.NetmonDebug) + ` + + [runtime] + enable_debug = ` + strconv.FormatBool(config.RuntimeDebug) + ` + enable_tracing = ` + strconv.FormatBool(config.RuntimeTrace) + ` + disable_new_netns= ` + strconv.FormatBool(config.DisableNewNetNs) +} diff --git a/pkg/katautils/config.go b/pkg/katautils/config.go index 1a434d7d53..24554d5b65 100644 --- a/pkg/katautils/config.go +++ b/pkg/katautils/config.go @@ -752,10 +752,8 @@ func updateRuntimeConfig(configPath string, tomlConf tomlConfig, config *oci.Run return nil } -func initConfig() (config oci.RuntimeConfig, err error) { - var defaultAgentConfig interface{} - - defaultHypervisorConfig := vc.HypervisorConfig{ +func GetDefaultHypervisorConfig() vc.HypervisorConfig { + return vc.HypervisorConfig{ HypervisorPath: defaultHypervisorPath, KernelPath: defaultKernelPath, ImagePath: defaultImagePath, @@ -767,6 +765,7 @@ func initConfig() (config oci.RuntimeConfig, err error) { DefaultMaxVCPUs: defaultMaxVCPUCount, MemorySize: defaultMemSize, MemOffset: defaultMemOffset, + DisableBlockDeviceUse: defaultDisableBlockDeviceUse, DefaultBridges: defaultBridgesCount, MemPrealloc: defaultEnableMemPrealloc, HugePages: defaultEnableHugePages, @@ -782,6 +781,10 @@ func initConfig() (config oci.RuntimeConfig, err error) { HotplugVFIOOnRootBus: defaultHotplugVFIOOnRootBus, GuestHookPath: defaultGuestHookPath, } +} + +func initConfig() (config oci.RuntimeConfig, err error) { + var defaultAgentConfig interface{} err = config.InterNetworkModel.SetModel(defaultInterNetworkingModel) if err != nil { @@ -792,7 +795,7 @@ func initConfig() (config oci.RuntimeConfig, err error) { config = oci.RuntimeConfig{ HypervisorType: defaultHypervisor, - HypervisorConfig: defaultHypervisorConfig, + HypervisorConfig: GetDefaultHypervisorConfig(), AgentType: defaultAgent, AgentConfig: defaultAgentConfig, ProxyType: defaultProxy, diff --git a/pkg/katautils/config_test.go b/pkg/katautils/config_test.go index b91dce21e8..0bb7065f28 100644 --- a/pkg/katautils/config_test.go +++ b/pkg/katautils/config_test.go @@ -15,11 +15,11 @@ import ( "path/filepath" "reflect" goruntime "runtime" - "strconv" "strings" "syscall" "testing" + "github.com/kata-containers/runtime/pkg/katatestutils" vc "github.com/kata-containers/runtime/virtcontainers" "github.com/kata-containers/runtime/virtcontainers/pkg/oci" "github.com/kata-containers/runtime/virtcontainers/utils" @@ -30,6 +30,7 @@ var ( hypervisorDebug = false proxyDebug = false runtimeDebug = false + runtimeTrace = false shimDebug = false netmonDebug = false ) @@ -43,46 +44,6 @@ type testRuntimeConfig struct { LogPath string } -func makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath, kernelParams, machineType, shimPath, proxyPath, netmonPath, logPath string, disableBlock bool, blockDeviceDriver string, enableIOThreads bool, hotplugVFIOOnRootBus, disableNewNetNs bool) string { - return ` - # Runtime configuration file - - [hypervisor.` + hypervisor + `] - path = "` + hypervisorPath + `" - kernel = "` + kernelPath + `" - block_device_driver = "` + blockDeviceDriver + `" - kernel_params = "` + kernelParams + `" - image = "` + imagePath + `" - machine_type = "` + machineType + `" - default_vcpus = ` + strconv.FormatUint(uint64(defaultVCPUCount), 10) + ` - default_maxvcpus = ` + strconv.FormatUint(uint64(defaultMaxVCPUCount), 10) + ` - default_memory = ` + strconv.FormatUint(uint64(defaultMemSize), 10) + ` - disable_block_device_use = ` + strconv.FormatBool(disableBlock) + ` - enable_iothreads = ` + strconv.FormatBool(enableIOThreads) + ` - hotplug_vfio_on_root_bus = ` + strconv.FormatBool(hotplugVFIOOnRootBus) + ` - msize_9p = ` + strconv.FormatUint(uint64(defaultMsize9p), 10) + ` - enable_debug = ` + strconv.FormatBool(hypervisorDebug) + ` - guest_hook_path = "` + defaultGuestHookPath + `" - - [proxy.kata] - enable_debug = ` + strconv.FormatBool(proxyDebug) + ` - path = "` + proxyPath + `" - - [shim.kata] - path = "` + shimPath + `" - enable_debug = ` + strconv.FormatBool(shimDebug) + ` - - [agent.kata] - - [netmon] - path = "` + netmonPath + `" - enable_debug = ` + strconv.FormatBool(netmonDebug) + ` - - [runtime] - enable_debug = ` + strconv.FormatBool(runtimeDebug) + ` - disable_new_netns= ` + strconv.FormatBool(disableNewNetNs) -} - func createConfig(configPath string, fileData string) error { err := ioutil.WriteFile(configPath, []byte(fileData), testFileMode) @@ -121,7 +82,36 @@ func createAllRuntimeConfigFiles(dir, hypervisor string) (config testRuntimeConf hotplugVFIOOnRootBus := true disableNewNetNs := false - runtimeConfigFileData := makeRuntimeConfigFileData(hypervisor, hypervisorPath, kernelPath, imagePath, kernelParams, machineType, shimPath, proxyPath, netmonPath, logPath, disableBlockDevice, blockDeviceDriver, enableIOThreads, hotplugVFIOOnRootBus, disableNewNetNs) + configFileOptions := katatestutils.RuntimeConfigOptions{ + Hypervisor: "qemu", + HypervisorPath: hypervisorPath, + KernelPath: kernelPath, + ImagePath: imagePath, + KernelParams: kernelParams, + MachineType: machineType, + ShimPath: shimPath, + ProxyPath: proxyPath, + NetmonPath: netmonPath, + LogPath: logPath, + DefaultGuestHookPath: defaultGuestHookPath, + DisableBlock: disableBlockDevice, + BlockDeviceDriver: blockDeviceDriver, + EnableIOThreads: enableIOThreads, + HotplugVFIOOnRootBus: hotplugVFIOOnRootBus, + DisableNewNetNs: disableNewNetNs, + DefaultVCPUCount: defaultVCPUCount, + DefaultMaxVCPUCount: defaultMaxVCPUCount, + DefaultMemSize: defaultMemSize, + DefaultMsize9p: defaultMsize9p, + HypervisorDebug: hypervisorDebug, + RuntimeDebug: runtimeDebug, + RuntimeTrace: runtimeTrace, + ProxyDebug: proxyDebug, + ShimDebug: shimDebug, + NetmonDebug: netmonDebug, + } + + runtimeConfigFileData := katatestutils.MakeRuntimeConfigFileData(configFileOptions) configPath := path.Join(dir, "runtime.toml") err = createConfig(configPath, runtimeConfigFileData)