diff --git a/pkg/config/json_test.go b/pkg/config/json_test.go index 1a8099f5..a0cface1 100644 --- a/pkg/config/json_test.go +++ b/pkg/config/json_test.go @@ -114,6 +114,31 @@ var jsonTests = map[string]jsonTest{ }, } +type invalidJSONTest struct { + json string +} + +var invalidJSONTests = map[string]invalidJSONTest{ + "TestEmptyBootloaderKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"empty",VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"}}`, + }, + "TestInvalidBootloaderKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"invalid",VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"}}`, + }, + "TestMissingBootloaderKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"}}`, + }, + "TestEmptyDeviceKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"kind":"","DevName":"virtio-blk","ImagePath":"/virtioblk1","ReadOnly":false,"DeviceIdentifier":""}]}`, + }, + "TestInvalidDeviceKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"kind":"invalid","DevName":"virtio-blk","ImagePath":"/virtioblk1","ReadOnly":false,"DeviceIdentifier":""}]}`, + }, + "TestMissingDeviceKind": { + json: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"DevName":"virtio-blk","ImagePath":"/virtioblk1","ReadOnly":false,"DeviceIdentifier":""}]}`, + }, +} + func TestJSON(t *testing.T) { t.Run("json", func(t *testing.T) { for name := range jsonTests { @@ -122,6 +147,12 @@ func TestJSON(t *testing.T) { testJSON(t, &test) }) } + for name := range invalidJSONTests { + t.Run(name, func(t *testing.T) { + test := invalidJSONTests[name] + testInvalidJSON(t, &test) + }) + } }) } @@ -139,6 +170,12 @@ func testJSON(t *testing.T, test *jsonTest) { require.Equal(t, *vm, unmarshalledVM) } +func testInvalidJSON(t *testing.T, test *invalidJSONTest) { + var vm VirtualMachine + err := json.Unmarshal([]byte(test.json), &vm) + require.Error(t, err) +} + func newLinuxVM(*testing.T) *VirtualMachine { bootloader := NewLinuxBootloader("/vmlinuz", "/initrd", "console=hvc0") vm := NewVirtualMachine(3, 4_000_000_000, bootloader)