-
Notifications
You must be signed in to change notification settings - Fork 42
json: Add backward compatibility with VfkitMagic #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,21 +4,12 @@ import ( | |
| "encoding/json" | ||
| "fmt" | ||
| "reflect" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func contains(strings []string, val string) bool { | ||
| for _, str := range strings { | ||
| if val == str { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| // This sets all the fields of the `obj` struct to non-empty values. | ||
| // This will be used to test JSON serialization as extensively as possible to | ||
| // avoid breaking backwards compatibility. | ||
|
|
@@ -34,7 +25,7 @@ func fillStruct(t *testing.T, obj interface{}, skipFields []string) { | |
| fieldVal := val.FieldByIndex(e.Index) | ||
| typeName := val.Type().Name() | ||
|
|
||
| if contains(skipFields, field.Name) { | ||
| if slices.Contains(skipFields, field.Name) { | ||
| continue | ||
| } | ||
| switch fieldVal.Kind() { | ||
|
|
@@ -339,6 +330,55 @@ type jsonStabilityTest struct { | |
| expectedJSON string | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will help to add a comment that this old json does not include vfkitMagic and parsing it should use the default value. |
||
| func testVirtioNetVfkitMagicJson(t *testing.T, vfkitMagic bool, useVfkitMagicDefault bool) { | ||
| const ( | ||
| jsonVfkitMagicDefault = `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"unixSocketPath":"/some/path/to/socket","macAddress":"00:11:22:33:44:55"}]}` | ||
| jsonVfkitMagicFalse = `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"unixSocketPath":"/some/path/to/socket","macAddress":"00:11:22:33:44:55","vfkitMagic":false}]}` | ||
| jsonVfkitMagicTrue = `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"unixSocketPath":"/some/path/to/socket","macAddress":"00:11:22:33:44:55","vfkitMagic":true}]}` | ||
| ) | ||
| var jsonStr string | ||
| var unmarshalledVM VirtualMachine | ||
|
|
||
| if useVfkitMagicDefault { | ||
| jsonStr = jsonVfkitMagicDefault | ||
| } else { | ||
| if vfkitMagic { | ||
| jsonStr = jsonVfkitMagicTrue | ||
| } else { | ||
| jsonStr = jsonVfkitMagicFalse | ||
| } | ||
| } | ||
| err := json.Unmarshal([]byte(jsonStr), &unmarshalledVM) | ||
| require.NoError(t, err) | ||
| netDevs := unmarshalledVM.VirtioNetDevices() | ||
| require.Len(t, netDevs, 1) | ||
| require.Equal(t, vfkitMagic, netDevs[0].VfkitMagic) | ||
|
|
||
cfergeau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| vm := newLinuxVM(t) | ||
| dev, err := VirtioNetNew("00:11:22:33:44:55") | ||
| require.NoError(t, err) | ||
| dev.SetUnixSocketPath("/some/path/to/socket") | ||
| if !useVfkitMagicDefault { | ||
| dev.VfkitMagic = vfkitMagic | ||
| } | ||
cfergeau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| err = vm.AddDevice(dev) | ||
| require.NoError(t, err) | ||
| require.Equal(t, vfkitMagic, dev.VfkitMagic) | ||
|
|
||
| require.Equal(t, *vm, unmarshalledVM) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will will help to comment about the check - I think what you try to do here is verify that json from older version is manipulated to match json created by current version. |
||
| } | ||
|
|
||
| func TestVirtioNetBackwardsCompat(t *testing.T) { | ||
| /* Check that the vfkitMagic default is true when deserializing json */ | ||
| t.Run("VfkitMagicJsonDefault", func(t *testing.T) { testVirtioNetVfkitMagicJson(t, true, true) }) | ||
|
|
||
| /* Check that the vfkitMagic default can be overridden */ | ||
| t.Run("VfkitMagicJsonDefaultOverride", func(t *testing.T) { testVirtioNetVfkitMagicJson(t, false, false) }) | ||
|
|
||
| /* Check that explicitly setting vfkitMagic to true works as expected */ | ||
| t.Run("VfkitMagicJsonExplicitDefault", func(t *testing.T) { testVirtioNetVfkitMagicJson(t, true, false) }) | ||
| } | ||
|
Comment on lines
+371
to
+380
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add test coverage for empty The tests verify backward compatibility when Add a fourth subtest: /* Check that vfkitMagic is cleared when UnixSocketPath is empty */
t.Run("VfkitMagicClearedWhenNoSocket", func(t *testing.T) {
jsonStr := `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"macAddress":"00:11:22:33:44:55","vfkitMagic":true}]}`
var unmarshalledVM VirtualMachine
err := json.Unmarshal([]byte(jsonStr), &unmarshalledVM)
require.NoError(t, err)
netDevs := unmarshalledVM.VirtioNetDevices()
require.Len(t, netDevs, 1)
require.False(t, netDevs[0].VfkitMagic, "VfkitMagic should be false when UnixSocketPath is empty")
})Based on learnings. 🤖 Prompt for AI Agents |
||
|
|
||
| func TestJSON(t *testing.T) { | ||
| t.Run("json", func(t *testing.T) { | ||
| for name := range jsonTests { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it break anything if we keep the default true value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it should because this field is only used when
UnixSocketPathis set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This breaks one of the test cases:
We could change the default even in the !unixsocketpath case, but then there are more tests to modify, json serialization of a virtionet device will always have
vfkitMagicset even when it’s not used, …In my opinion it’s preferrable to keep the default to false when unixSocketPath is not used.