From 309dae631add10f412656c5187bd3ee36b63d6b1 Mon Sep 17 00:00:00 2001 From: Wainer dos Santos Moschetta Date: Mon, 3 May 2021 15:42:58 -0400 Subject: [PATCH] virtcontainers: check that both initrd and image are not set This changed valid() in hypervisor to check the case where both initrd and image path are set; in this case it returns an error. Fixes #1868 Signed-off-by: Wainer dos Santos Moschetta --- src/runtime/virtcontainers/hypervisor.go | 4 ++++ src/runtime/virtcontainers/hypervisor_test.go | 11 +++++++++++ src/runtime/virtcontainers/qemu_test.go | 1 - 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/runtime/virtcontainers/hypervisor.go b/src/runtime/virtcontainers/hypervisor.go index a1949b056e..0186c397ed 100644 --- a/src/runtime/virtcontainers/hypervisor.go +++ b/src/runtime/virtcontainers/hypervisor.go @@ -547,6 +547,10 @@ func (conf *HypervisorConfig) Valid() error { return fmt.Errorf("Missing image and initrd path") } + if conf.ImagePath != "" && conf.InitrdPath != "" { + return fmt.Errorf("Image and initrd path cannot be both set") + } + if err := conf.CheckTemplateConfig(); err != nil { return err } diff --git a/src/runtime/virtcontainers/hypervisor_test.go b/src/runtime/virtcontainers/hypervisor_test.go index c2eaa107cc..d8c4f6448c 100644 --- a/src/runtime/virtcontainers/hypervisor_test.go +++ b/src/runtime/virtcontainers/hypervisor_test.go @@ -134,6 +134,17 @@ func TestHypervisorConfigIsValid(t *testing.T) { testHypervisorConfigValid(t, hypervisorConfig, true) } +func TestHypervisorConfigBothInitrdAndImage(t *testing.T) { + hypervisorConfig := &HypervisorConfig{ + KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel), + ImagePath: fmt.Sprintf("%s/%s", testDir, testImage), + InitrdPath: fmt.Sprintf("%s/%s", testDir, testInitrd), + HypervisorPath: "", + } + + testHypervisorConfigValid(t, hypervisorConfig, false) +} + func TestHypervisorConfigValidTemplateConfig(t *testing.T) { hypervisorConfig := &HypervisorConfig{ KernelPath: fmt.Sprintf("%s/%s", testDir, testKernel), diff --git a/src/runtime/virtcontainers/qemu_test.go b/src/runtime/virtcontainers/qemu_test.go index eb10857dd2..deeb482dc6 100644 --- a/src/runtime/virtcontainers/qemu_test.go +++ b/src/runtime/virtcontainers/qemu_test.go @@ -25,7 +25,6 @@ import ( func newQemuConfig() HypervisorConfig { return HypervisorConfig{ KernelPath: testQemuKernelPath, - ImagePath: testQemuImagePath, InitrdPath: testQemuInitrdPath, HypervisorPath: testQemuPath, NumVCPUs: defaultVCPUs,