mirror of https://github.com/hashicorp/packer
add fixer, fixer testspull/5284/head
parent
9e2e467b31
commit
2d4bc70d7b
@ -0,0 +1,45 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// FixerEnhancedNetworking is a Fixer that replaces the "enhanced_networking" configuration key
|
||||
// with the clearer "ena_support". This disambiguates ena_support from sriov_support.
|
||||
type FixerEnhancedNetworking struct{}
|
||||
|
||||
func (FixerEnhancedNetworking) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
||||
// Our template type we'll use for this fixer only
|
||||
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
|
||||
}
|
||||
|
||||
// Go through each builder and replace the enhanced_networking if we can
|
||||
for _, builder := range tpl.Builders {
|
||||
enhancedNetworkingRaw, ok := builder["enhanced_networking"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
enhancedNetworkingString, ok := enhancedNetworkingRaw.(bool)
|
||||
if !ok {
|
||||
// TODO: error?
|
||||
continue
|
||||
}
|
||||
|
||||
delete(builder, "enhanced_networking")
|
||||
builder["ena_support"] = enhancedNetworkingString
|
||||
}
|
||||
|
||||
input["builders"] = tpl.Builders
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func (FixerEnhancedNetworking) Synopsis() string {
|
||||
return `Replaces "enhanced_networking" in builders with "ena_support"`
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
package fix
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFixerEnhancedNetworking_Impl(t *testing.T) {
|
||||
var _ Fixer = new(FixerEnhancedNetworking)
|
||||
}
|
||||
|
||||
func TestFixerEnhancedNetworking(t *testing.T) {
|
||||
cases := []struct {
|
||||
Input map[string]interface{}
|
||||
Expected map[string]interface{}
|
||||
}{
|
||||
// Attach field == false
|
||||
{
|
||||
Input: map[string]interface{}{
|
||||
"type": "ebs",
|
||||
"enhanced_networking": false,
|
||||
},
|
||||
|
||||
Expected: map[string]interface{}{
|
||||
"type": "ebs",
|
||||
"ena_support": false,
|
||||
},
|
||||
},
|
||||
|
||||
// Attach field == true
|
||||
{
|
||||
Input: map[string]interface{}{
|
||||
"type": "ebs",
|
||||
"enhanced_networking": true,
|
||||
},
|
||||
|
||||
Expected: map[string]interface{}{
|
||||
"type": "ebs",
|
||||
"ena_support": true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
var f FixerEnhancedNetworking
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(output, expected) {
|
||||
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue