Skip to content

Commit

Permalink
json: Add test for invalid json
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau authored and baude committed Jun 26, 2023
1 parent bfc5016 commit e3c6969
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/config/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
})
}

})
}
Expand All @@ -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)
Expand Down

0 comments on commit e3c6969

Please sign in to comment.