Skip to content

Commit bc4e8f6

Browse files
committed
Add ValidateDevices function to validate devices parsed from CmdLine
This function runs validation on all devices added via CmdLine, ensuring they are properly configured before VM startup. It eliminates redundant individual calls to dev.Validate(), centralizing validation logic. Also includes a test case to verify ValidateDevices behavior.
1 parent b4e7210 commit bc4e8f6

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

cmd/vfkit/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ func newVMConfiguration(opts *cmdline.Options) (*config.VirtualMachine, error) {
104104
return nil, err
105105
}
106106

107+
if err := vmConfig.ValidateDevices(); err != nil {
108+
return nil, err
109+
}
110+
107111
if err := vmConfig.AddIgnitionFileFromCmdLine(opts.IgnitionPath); err != nil {
108112
return nil, fmt.Errorf("failed to add ignition file: %w", err)
109113
}

pkg/config/config_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

33
import (
4+
"os"
5+
"os/exec"
6+
"path/filepath"
47
"testing"
58

69
"github.com/stretchr/testify/assert"
@@ -44,3 +47,24 @@ func TestNetworkBlockDevice_NoDevice(t *testing.T) {
4447
nbdItem := vm.NetworkBlockDevice("nbd2")
4548
require.Nil(t, nbdItem)
4649
}
50+
51+
func TestVirtualMachine_ValidateBlockDevices(t *testing.T) {
52+
vm := &VirtualMachine{}
53+
54+
tmpDir := t.TempDir()
55+
imagePath := filepath.Join(tmpDir, "disk.qcow2")
56+
size := "1G"
57+
58+
cmd := exec.Command("qemu-img", "create", "-f", "qcow2", imagePath, size)
59+
err := cmd.Run()
60+
require.NoError(t, err)
61+
62+
dev, err := VirtioBlkNew(imagePath)
63+
require.NoError(t, err)
64+
vm.Devices = append(vm.Devices, dev)
65+
defer os.Remove(imagePath)
66+
67+
err = vm.ValidateDevices()
68+
require.Error(t, err)
69+
require.ErrorContains(t, err, "vfkit does not support qcow2 image format")
70+
}

pkg/config/virtio.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ func (v *VirtioBalloon) Validate() error {
153153
return nil
154154
}
155155

156+
func (v *DirectorySharingConfig) Validate() error {
157+
return nil
158+
}
159+
156160
type option struct {
157161
key string
158162
value string
@@ -299,7 +303,7 @@ func (dev *VirtioSerial) FromOptions(options []option) error {
299303
}
300304
}
301305

302-
return dev.Validate()
306+
return nil
303307
}
304308

305309
// VirtioInputNew creates a new input device for the virtual machine.
@@ -344,7 +348,7 @@ func (dev *VirtioInput) FromOptions(options []option) error {
344348
return fmt.Errorf("unknown option for virtio-input devices: %s", option.key)
345349
}
346350
}
347-
return dev.Validate()
351+
return nil
348352
}
349353

350354
// VirtioGPUNew creates a new gpu device for the virtual machine.
@@ -403,7 +407,7 @@ func (dev *VirtioGPU) FromOptions(options []option) error {
403407
dev.Height = defaultVirtioGPUResolutionHeight
404408
}
405409

406-
return dev.Validate()
410+
return nil
407411
}
408412

409413
// VirtioNetNew creates a new network device for the virtual machine. It will
@@ -506,7 +510,7 @@ func (dev *VirtioNet) FromOptions(options []option) error {
506510
}
507511
}
508512

509-
return dev.Validate()
513+
return nil
510514
}
511515

512516
// VirtioRngNew creates a new random number generator device to feed entropy
@@ -713,10 +717,6 @@ func (dev *VirtioFs) FromOptions(options []option) error {
713717
return nil
714718
}
715719

716-
func (dev *VirtioFs) Validate() error {
717-
return nil
718-
}
719-
720720
// RosettaShareNew RosettaShare creates a new rosetta share for running x86_64 binaries on M1 machines.
721721
// It will share a directory containing the linux rosetta binaries with the
722722
// virtual machine. This directory can be mounted in the VM using `mount -t
@@ -762,10 +762,6 @@ func (dev *RosettaShare) FromOptions(options []option) error {
762762
return nil
763763
}
764764

765-
func (dev *RosettaShare) Validate() error {
766-
return nil
767-
}
768-
769765
func networkBlockDeviceNewEmpty() *NetworkBlockDevice {
770766
return &NetworkBlockDevice{
771767
NetworkBlockStorageConfig: NetworkBlockStorageConfig{

0 commit comments

Comments
 (0)