Fixer and tests to remove deprecated 'vhd_temp_path' Hyper-V ISO setting

This commit is contained in:
DanHam 2018-07-10 17:31:34 +01:00
parent 674bad0ab4
commit 28087cb9f7
No known key found for this signature in database
GPG Key ID: 58E79AEDD6AA987E
3 changed files with 112 additions and 0 deletions

View File

@ -36,6 +36,7 @@ func init() {
"amazon-private-ip": new(FixerAmazonPrivateIP),
"docker-email": new(FixerDockerEmail),
"powershell-escapes": new(FixerPowerShellEscapes),
"hyperv-deprecations": new(FixerHypervDeprecations),
}
FixerOrder = []string{
@ -55,5 +56,6 @@ func init() {
"amazon-private-ip",
"docker-email",
"powershell-escapes",
"hyperv-deprecations",
}
}

View File

@ -0,0 +1,50 @@
package fix
import (
"github.com/mitchellh/mapstructure"
)
// FixerHypervDeprecations removes the deprecated "vhd_temp_path" setting
// from Hyper-V ISO builder templates
type FixerHypervDeprecations struct{}
func (FixerHypervDeprecations) 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-iso" {
continue
}
_, ok = builder["vhd_temp_path"]
if ok {
delete(builder, "vhd_temp_path")
}
}
input["builders"] = tpl.Builders
return input, nil
}
func (FixerHypervDeprecations) Synopsis() string {
return `Removes the deprecated "vhd_temp_path" setting from Hyper-V ISO builder templates`
}

View File

@ -0,0 +1,60 @@
package fix
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixerHypervDeprecations_impl(t *testing.T) {
var _ Fixer = new(FixerHypervDeprecations)
}
func TestFixerHypervDeprecations_Fix(t *testing.T) {
cases := []struct {
Input map[string]interface{}
Expected map[string]interface{}
}{
// No vhd_temp_path field in template - noop
{
Input: map[string]interface{}{
"type": "hyperv-iso",
},
Expected: map[string]interface{}{
"type": "hyperv-iso",
},
},
// Deprecated vhd_temp_path field in template should be deleted
{
Input: map[string]interface{}{
"type": "hyperv-iso",
"vhd_temp_path": "foopath",
},
Expected: map[string]interface{}{
"type": "hyperv-iso",
},
},
}
for _, tc := range cases {
var f FixerHypervDeprecations
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")
}
}