bug fix and add test

This commit is contained in:
Matthew Hooker 2018-09-06 11:55:11 -07:00
parent 42910a5f8c
commit 8a7d43dd44
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
7 changed files with 48 additions and 24 deletions

View File

@ -2,18 +2,39 @@ package command
import (
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/packer/packer"
"github.com/stretchr/testify/assert"
)
func TestFix(t *testing.T) {
s := &strings.Builder{}
ui := &packer.BasicUi{
Writer: s,
}
c := &FixCommand{
Meta: testMeta(t),
}
c.Ui = ui
args := []string{filepath.Join(testFixture("fix"), "template.json")}
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
expected := `{
"builders": [
{
"type": "dummy"
}
],
"push": {
"name": "foo/bar"
}
}`
assert.Equal(t, expected, strings.TrimSpace(s.String()))
}
func TestFix_invalidTemplate(t *testing.T) {

View File

@ -5,6 +5,10 @@ import "github.com/mitchellh/mapstructure"
type FixerDockerEmail struct{}
func (FixerDockerEmail) Fix(input map[string]interface{}) (map[string]interface{}, error) {
if input["post-processors"] == nil {
return input, nil
}
// Our template type we'll use for this fixer only
type template struct {
Builders []map[string]interface{}
@ -27,7 +31,7 @@ func (FixerDockerEmail) Fix(input map[string]interface{}) (map[string]interface{
}
// Go through each post-processor and delete `docker_login` if present
pps := tpl.postProcessors()
pps := tpl.ppList()
for _, pp := range pps {
_, ok := pp["login_email"]
@ -38,7 +42,7 @@ func (FixerDockerEmail) Fix(input map[string]interface{}) (map[string]interface{
}
input["builders"] = tpl.Builders
input["post-processors"] = pps
input["post-processors"] = tpl.PostProcessors
return input, nil
}

View File

@ -8,6 +8,9 @@ import (
type FixerManifestFilename struct{}
func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]interface{}, error) {
if input["post-processors"] == nil {
return input, nil
}
// Our template type we'll use for this fixer only
type template struct {
@ -21,7 +24,7 @@ func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]inter
}
// Go through each post-processor and get out all the complex configs
pps := tpl.postProcessors()
pps := tpl.ppList()
for _, pp := range pps {
ppTypeRaw, ok := pp["type"]
@ -47,7 +50,7 @@ func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]inter
}
input["post-processors"] = pps
input["post-processors"] = tpl.PostProcessors
return input, nil
}

View File

@ -1,8 +1,9 @@
package fix
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixerManifestPPFilename_Impl(t *testing.T) {
@ -43,11 +44,7 @@ func TestFixerManifestPPFilename_Fix(t *testing.T) {
}
output, err := f.Fix(input)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.NoError(t, err)
if !reflect.DeepEqual(output, expected) {
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
}
assert.Equal(t, expected, output)
}

View File

@ -1,8 +1,6 @@
package fix
import (
"github.com/mitchellh/mapstructure"
)
import "github.com/mitchellh/mapstructure"
// FixerVagrantPPOverride is a Fixer that replaces the provider-specific
// overrides for the Vagrant post-processor with the new style introduced
@ -10,6 +8,10 @@ import (
type FixerVagrantPPOverride struct{}
func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]interface{}, error) {
if input["post-processors"] == nil {
return input, nil
}
// Our template type we'll use for this fixer only
type template struct {
PP `mapstructure:",squash"`
@ -21,7 +23,7 @@ func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]inte
return nil, err
}
pps := tpl.postProcessors()
pps := tpl.ppList()
// Go through each post-processor and make the fix if necessary
possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
@ -52,7 +54,7 @@ func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]inte
}
}
input["post-processors"] = pps
input["post-processors"] = tpl.PostProcessors
return input, nil
}

View File

@ -1,8 +1,9 @@
package fix
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixerVagrantPPOverride_Impl(t *testing.T) {
@ -69,11 +70,7 @@ func TestFixerVagrantPPOverride_Fix(t *testing.T) {
}
output, err := f.Fix(input)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.NoError(t, err)
if !reflect.DeepEqual(output, expected) {
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
}
assert.Equal(t, expected, output)
}

View File

@ -6,7 +6,7 @@ type PP struct {
}
// postProcessors converts the variable structure of the template to a list
func (pp *PP) postProcessors() []map[string]interface{} {
func (pp *PP) ppList() []map[string]interface{} {
pps := make([]map[string]interface{}, 0, len(pp.PostProcessors))
for _, rawPP := range pp.PostProcessors {
switch pp := rawPP.(type) {