From c8f54d5291c95dc2032fa04a4987f8b063997576 Mon Sep 17 00:00:00 2001 From: DanHam Date: Thu, 19 Jul 2018 20:10:20 +0100 Subject: [PATCH] Fixer and tests to convert 'clone_from_vmxc_path' -> 'clone_from_vmcx_path' --- command/fix.go | 2 + fix/fixer.go | 2 + fix/fixer_hyperv_vmxc_typo.go | 52 ++++++++++++++++++++++++ fix/fixer_hyperv_vmxc_typo_test.go | 64 ++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 fix/fixer_hyperv_vmxc_typo.go create mode 100644 fix/fixer_hyperv_vmxc_typo_test.go diff --git a/command/fix.go b/command/fix.go index b8c1023c3..ca096306c 100644 --- a/command/fix.go +++ b/command/fix.go @@ -155,6 +155,8 @@ Fixes that are run: elevated username and password strings hyperv-deprecations Removes the deprecated "vhd_temp_path" setting from Hyper-V ISO builder templates + hyperv-vmxc-typo Corrects a typo in the "clone_from_vmxc_path" + setting. Replaces with "clone_from_vmcx_path". Options: diff --git a/fix/fixer.go b/fix/fixer.go index cc8aa1258..c6278bda6 100644 --- a/fix/fixer.go +++ b/fix/fixer.go @@ -37,6 +37,7 @@ func init() { "docker-email": new(FixerDockerEmail), "powershell-escapes": new(FixerPowerShellEscapes), "hyperv-deprecations": new(FixerHypervDeprecations), + "hyperv-vmxc-typo": new(FixerHypervVmxcTypo), } FixerOrder = []string{ @@ -57,5 +58,6 @@ func init() { "docker-email", "powershell-escapes", "hyperv-deprecations", + "hyperv-vmxc-typo", } } diff --git a/fix/fixer_hyperv_vmxc_typo.go b/fix/fixer_hyperv_vmxc_typo.go new file mode 100644 index 000000000..fbc057c54 --- /dev/null +++ b/fix/fixer_hyperv_vmxc_typo.go @@ -0,0 +1,52 @@ +package fix + +import ( + "github.com/mitchellh/mapstructure" +) + +// FixerHypervVmxcTypo fixes the typo in "clone_from_vmxc_path" replacing +// it with "clone_from_vmcx_path" in Hyper-V VMCX builder templates +type FixerHypervVmxcTypo struct{} + +func (FixerHypervVmxcTypo) Fix(input map[string]interface{}) (map[string]interface{}, error) { + // The type we'll decode into; we only care about builders + type template struct { + Builders []map[string]interface{} + } + + // Decode the input into our structure, if we can + var tpl template + if err := mapstructure.Decode(input, &tpl); err != nil { + return nil, err + } + + for _, builder := range tpl.Builders { + builderTypeRaw, ok := builder["type"] + if !ok { + continue + } + + builderType, ok := builderTypeRaw.(string) + if !ok { + continue + } + + if builderType != "hyperv-vmcx" { + continue + } + + path, ok := builder["clone_from_vmxc_path"] + if ok { + delete(builder, "clone_from_vmxc_path") + builder["clone_from_vmcx_path"] = path + } + } + + input["builders"] = tpl.Builders + return input, nil +} + +func (FixerHypervVmxcTypo) Synopsis() string { + return `Fixes a typo replacing "clone_from_vmxc_path" with "clone_from_vmcx_path" ` + + `in Hyper-V VMCX builder templates` +} diff --git a/fix/fixer_hyperv_vmxc_typo_test.go b/fix/fixer_hyperv_vmxc_typo_test.go new file mode 100644 index 000000000..dbbea7072 --- /dev/null +++ b/fix/fixer_hyperv_vmxc_typo_test.go @@ -0,0 +1,64 @@ +package fix + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFixerHypervVmxcTypo_impl(t *testing.T) { + var _ Fixer = new(FixerHypervVmxcTypo) +} + +func TestFixerHypervVmxcTypo_Fix(t *testing.T) { + cases := []struct { + Input map[string]interface{} + Expected map[string]interface{} + }{ + // No "clone_from_vmxc_path" in template - noop + { + Input: map[string]interface{}{ + "type": "hyperv-vmcx", + "temp_path": "C:/some/temp/path", + }, + + Expected: map[string]interface{}{ + "type": "hyperv-vmcx", + "temp_path": "C:/some/temp/path", + }, + }, + + // "clone_from_vmxc_path" should be replaced with + // "clone_from_vmcx_path" in template + { + Input: map[string]interface{}{ + "type": "hyperv-vmcx", + "clone_from_vmxc_path": "C:/some/vmcx/path", + }, + + Expected: map[string]interface{}{ + "type": "hyperv-vmcx", + "clone_from_vmcx_path": "C:/some/vmcx/path", + }, + }, + } + + for _, tc := range cases { + var f FixerHypervVmxcTypo + + input := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Input}, + } + + expected := map[string]interface{}{ + "builders": []map[string]interface{}{tc.Expected}, + } + + output, err := f.Fix(input) + if err != nil { + t.Fatalf("err: %s", err) + } + + assert.Equal(t, expected, output, "Should be equal") + } +}