commit
13a44c4d91
|
@ -2,18 +2,39 @@ package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFix(t *testing.T) {
|
func TestFix(t *testing.T) {
|
||||||
|
s := &strings.Builder{}
|
||||||
|
ui := &packer.BasicUi{
|
||||||
|
Writer: s,
|
||||||
|
}
|
||||||
c := &FixCommand{
|
c := &FixCommand{
|
||||||
Meta: testMeta(t),
|
Meta: testMeta(t),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Ui = ui
|
||||||
|
|
||||||
args := []string{filepath.Join(testFixture("fix"), "template.json")}
|
args := []string{filepath.Join(testFixture("fix"), "template.json")}
|
||||||
if code := c.Run(args); code != 0 {
|
if code := c.Run(args); code != 0 {
|
||||||
fatalCommand(t, c.Meta)
|
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) {
|
func TestFix_invalidTemplate(t *testing.T) {
|
||||||
|
|
|
@ -5,10 +5,14 @@ import "github.com/mitchellh/mapstructure"
|
||||||
type FixerDockerEmail struct{}
|
type FixerDockerEmail struct{}
|
||||||
|
|
||||||
func (FixerDockerEmail) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
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
|
// Our template type we'll use for this fixer only
|
||||||
type template struct {
|
type template struct {
|
||||||
Builders []map[string]interface{}
|
Builders []map[string]interface{}
|
||||||
PostProcessors []map[string]interface{} `mapstructure:"post-processors"`
|
PP `mapstructure:",squash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the input into our structure, if we can
|
// Decode the input into our structure, if we can
|
||||||
|
@ -27,7 +31,9 @@ func (FixerDockerEmail) Fix(input map[string]interface{}) (map[string]interface{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through each post-processor and delete `docker_login` if present
|
// Go through each post-processor and delete `docker_login` if present
|
||||||
for _, pp := range tpl.PostProcessors {
|
pps := tpl.ppList()
|
||||||
|
|
||||||
|
for _, pp := range pps {
|
||||||
_, ok := pp["login_email"]
|
_, ok := pp["login_email"]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -8,10 +8,13 @@ import (
|
||||||
type FixerManifestFilename struct{}
|
type FixerManifestFilename struct{}
|
||||||
|
|
||||||
func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
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
|
// Our template type we'll use for this fixer only
|
||||||
type template struct {
|
type template struct {
|
||||||
PostProcessors []interface{} `mapstructure:"post-processors"`
|
PP `mapstructure:",squash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the input into our structure, if we can
|
// Decode the input into our structure, if we can
|
||||||
|
@ -21,20 +24,7 @@ func (FixerManifestFilename) Fix(input map[string]interface{}) (map[string]inter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through each post-processor and get out all the complex configs
|
// Go through each post-processor and get out all the complex configs
|
||||||
pps := make([]map[string]interface{}, 0, len(tpl.PostProcessors))
|
pps := tpl.ppList()
|
||||||
for _, rawPP := range tpl.PostProcessors {
|
|
||||||
switch pp := rawPP.(type) {
|
|
||||||
case string:
|
|
||||||
case map[string]interface{}:
|
|
||||||
pps = append(pps, pp)
|
|
||||||
case []interface{}:
|
|
||||||
for _, innerRawPP := range pp {
|
|
||||||
if innerPP, ok := innerRawPP.(map[string]interface{}); ok {
|
|
||||||
pps = append(pps, innerPP)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, pp := range pps {
|
for _, pp := range pps {
|
||||||
ppTypeRaw, ok := pp["type"]
|
ppTypeRaw, ok := pp["type"]
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package fix
|
package fix
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFixerManifestPPFilename_Impl(t *testing.T) {
|
func TestFixerManifestPPFilename_Impl(t *testing.T) {
|
||||||
|
@ -43,11 +44,7 @@ func TestFixerManifestPPFilename_Fix(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := f.Fix(input)
|
output, err := f.Fix(input)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(output, expected) {
|
assert.Equal(t, expected, output)
|
||||||
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package fix
|
package fix
|
||||||
|
|
||||||
import (
|
import "github.com/mitchellh/mapstructure"
|
||||||
"github.com/mitchellh/mapstructure"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FixerVagrantPPOverride is a Fixer that replaces the provider-specific
|
// FixerVagrantPPOverride is a Fixer that replaces the provider-specific
|
||||||
// overrides for the Vagrant post-processor with the new style introduced
|
// overrides for the Vagrant post-processor with the new style introduced
|
||||||
|
@ -10,9 +8,13 @@ import (
|
||||||
type FixerVagrantPPOverride struct{}
|
type FixerVagrantPPOverride struct{}
|
||||||
|
|
||||||
func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]interface{}, error) {
|
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
|
// Our template type we'll use for this fixer only
|
||||||
type template struct {
|
type template struct {
|
||||||
PostProcessors []interface{} `mapstructure:"post-processors"`
|
PP `mapstructure:",squash"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode the input into our structure, if we can
|
// Decode the input into our structure, if we can
|
||||||
|
@ -21,21 +23,7 @@ func (FixerVagrantPPOverride) Fix(input map[string]interface{}) (map[string]inte
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go through each post-processor and get out all the complex configs
|
pps := tpl.ppList()
|
||||||
pps := make([]map[string]interface{}, 0, len(tpl.PostProcessors))
|
|
||||||
for _, rawPP := range tpl.PostProcessors {
|
|
||||||
switch pp := rawPP.(type) {
|
|
||||||
case string:
|
|
||||||
case map[string]interface{}:
|
|
||||||
pps = append(pps, pp)
|
|
||||||
case []interface{}:
|
|
||||||
for _, innerRawPP := range pp {
|
|
||||||
if innerPP, ok := innerRawPP.(map[string]interface{}); ok {
|
|
||||||
pps = append(pps, innerPP)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Go through each post-processor and make the fix if necessary
|
// Go through each post-processor and make the fix if necessary
|
||||||
possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
|
possible := []string{"aws", "digitalocean", "virtualbox", "vmware"}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package fix
|
package fix
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFixerVagrantPPOverride_Impl(t *testing.T) {
|
func TestFixerVagrantPPOverride_Impl(t *testing.T) {
|
||||||
|
@ -69,11 +70,7 @@ func TestFixerVagrantPPOverride_Fix(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := f.Fix(input)
|
output, err := f.Fix(input)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(output, expected) {
|
assert.Equal(t, expected, output)
|
||||||
t.Fatalf("unexpected: %#v\nexpected: %#v\n", output, expected)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
package fix
|
||||||
|
|
||||||
|
// PP is a convenient way to interact with the post-processors within a fixer
|
||||||
|
type PP struct {
|
||||||
|
PostProcessors []interface{} `mapstructure:"post-processors"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// postProcessors converts the variable structure of the template to a list
|
||||||
|
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) {
|
||||||
|
case string:
|
||||||
|
case map[string]interface{}:
|
||||||
|
pps = append(pps, pp)
|
||||||
|
case []interface{}:
|
||||||
|
for _, innerRawPP := range pp {
|
||||||
|
if innerPP, ok := innerRawPP.(map[string]interface{}); ok {
|
||||||
|
pps = append(pps, innerPP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pps
|
||||||
|
}
|
Loading…
Reference in New Issue