Fixer and tests to convert 'clone_from_vmxc_path' -> 'clone_from_vmcx_path'

This commit is contained in:
DanHam 2018-07-19 20:10:20 +01:00
parent da21c25791
commit c8f54d5291
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
4 changed files with 120 additions and 0 deletions

View File

@ -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:

View File

@ -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",
}
}

View File

@ -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`
}

View File

@ -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")
}
}