Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
config: Fix test that was using system files
Browse files Browse the repository at this point in the history
The `TestMinimalRuntimeConfig` should not be using the real
resource files that might be installed on a system so make temporary
files instead to better control the test.

Split out `TestMinimalRuntimeConfigWithVsock` to reduce cyclomatic
complexity (along with dropping the config file delete at the end - not
required as the entire test-specific directory gets auto-deleted).

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Aug 28, 2018
1 parent dcf3229 commit 1ba4841
Showing 1 changed file with 76 additions and 7 deletions.
83 changes: 76 additions & 7 deletions cli/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,38 @@ func TestMinimalRuntimeConfig(t *testing.T) {
shimPath := path.Join(dir, "shim")
proxyPath := path.Join(dir, "proxy")

imagePath := path.Join(dir, "image.img")
initrdPath := path.Join(dir, "initrd.img")

hypervisorPath := path.Join(dir, "hypervisor")
kernelPath := path.Join(dir, "kernel")

savedDefaultImagePath := defaultImagePath
savedDefaultInitrdPath := defaultInitrdPath
savedDefaultHypervisorPath := defaultHypervisorPath
savedDefaultKernelPath := defaultKernelPath

defer func() {
defaultImagePath = savedDefaultImagePath
defaultInitrdPath = savedDefaultInitrdPath
defaultHypervisorPath = savedDefaultHypervisorPath
defaultKernelPath = savedDefaultKernelPath
}()

// Temporarily change the defaults to avoid this test using the real
// resource files that might be installed on the system!
defaultImagePath = imagePath
defaultInitrdPath = initrdPath
defaultHypervisorPath = hypervisorPath
defaultKernelPath = kernelPath

for _, file := range []string{defaultImagePath, defaultInitrdPath, defaultHypervisorPath, defaultKernelPath} {
err = writeFile(file, "foo", testFileMode)
if err != nil {
t.Fatal(err)
}
}

runtimeMinimalConfig := `
# Runtime configuration file
Expand Down Expand Up @@ -555,9 +587,50 @@ func TestMinimalRuntimeConfig(t *testing.T) {
if reflect.DeepEqual(config, expectedConfig) == false {
t.Fatalf("Got %+v\n expecting %+v", config, expectedConfig)
}
}

func TestMinimalRuntimeConfigWithVsock(t *testing.T) {
dir, err := ioutil.TempDir(testDir, "minimal-runtime-config-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

imagePath := path.Join(dir, "image.img")
initrdPath := path.Join(dir, "initrd.img")
proxyPath := path.Join(dir, "proxy")
shimPath := path.Join(dir, "shim")
hypervisorPath := path.Join(dir, "hypervisor")
kernelPath := path.Join(dir, "kernel")

savedDefaultImagePath := defaultImagePath
savedDefaultInitrdPath := defaultInitrdPath
savedDefaultHypervisorPath := defaultHypervisorPath
savedDefaultKernelPath := defaultKernelPath

defer func() {
defaultImagePath = savedDefaultImagePath
defaultInitrdPath = savedDefaultInitrdPath
defaultHypervisorPath = savedDefaultHypervisorPath
defaultKernelPath = savedDefaultKernelPath
}()

// Temporarily change the defaults to avoid this test using the real
// resource files that might be installed on the system!
defaultImagePath = imagePath
defaultInitrdPath = initrdPath
defaultHypervisorPath = hypervisorPath
defaultKernelPath = kernelPath

for _, file := range []string{proxyPath, shimPath, hypervisorPath, kernelPath} {
err = writeFile(file, "foo", testFileMode)
if err != nil {
t.Fatal(err)
}
}

// minimal config with vsock enabled
runtimeMinimalConfig = `
runtimeMinimalConfig := `
# Runtime configuration file
[hypervisor.qemu]
use_vsock = true
Expand All @@ -579,13 +652,13 @@ func TestMinimalRuntimeConfig(t *testing.T) {
utils.VHostVSockDevicePath = "/dev/null"
utils.VSockDevicePath = "/dev/null"

configPath = path.Join(dir, "runtime.toml")
configPath := path.Join(dir, "runtime.toml")
err = createConfig(configPath, runtimeMinimalConfig)
if err != nil {
t.Fatal(err)
}

_, config, err = loadConfiguration(configPath, false)
_, config, err := loadConfiguration(configPath, false)
if err != nil {
t.Fatal(err)
}
Expand All @@ -601,10 +674,6 @@ func TestMinimalRuntimeConfig(t *testing.T) {
if config.HypervisorConfig.UseVSock != true {
t.Fatalf("use_vsock must be true, got %v", config.HypervisorConfig.UseVSock)
}

if err := os.Remove(configPath); err != nil {
t.Fatal(err)
}
}

func TestNewQemuHypervisorConfig(t *testing.T) {
Expand Down

0 comments on commit 1ba4841

Please sign in to comment.