[proxmox] Adds proxmox fixer and fixer test

Add fixer for proxmox to proxmox-iso. Updated gofmt.
This commit is contained in:
Jeff Wong 2020-10-08 00:04:43 -07:00
parent a140c13943
commit 93531b3ec5
No known key found for this signature in database
GPG Key ID: D4EEB78E484F8A83
3 changed files with 123 additions and 1 deletions

View File

@ -4,4 +4,4 @@ import (
"github.com/hashicorp/packer/builder/proxmox/iso"
)
type Builder = proxmoxiso.Builder
type Builder = proxmoxiso.Builder

49
fix/fixer_proxmox_type.go Normal file
View File

@ -0,0 +1,49 @@
package fix
import (
"github.com/mitchellh/mapstructure"
)
// FixerProxmoxType updates proxmox builder types to proxmox-iso
type FixerProxmoxType struct{}
func (FixerProxmoxType) DeprecatedOptions() []string {
return []string{}
}
func (FixerProxmoxType) Fix(input map[string]interface{}) (map[string]interface{}, error) {
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 != "proxmox" {
continue
}
builder["type"] = "proxmox-iso"
}
input["builders"] = tpl.Builders
return input, nil
}
func (FixerProxmoxType) Synopsis() string {
return `Updates the builder type proxmox to proxmox-iso`
}

View File

@ -0,0 +1,73 @@
package fix
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixerProxmoxType_Impl(t *testing.T) {
var raw interface{}
raw = new(FixerProxmoxType)
if _, ok := raw.(Fixer); !ok {
t.Fatalf("must be a Fixer")
}
}
func TestFixerProxmoxType_Fix(t *testing.T) {
cases := []struct {
Input map[string]interface{}
Expected map[string]interface{}
}{
{
Input: map[string]interface{}{
"type": "proxmox",
},
Expected: map[string]interface{}{
"type": "proxmox-iso",
},
},
{
Input: map[string]interface{}{
"type": "proxmox-iso",
},
Expected: map[string]interface{}{
"type": "proxmox-iso",
},
},
{
Input: map[string]interface{}{
"type": "proxmox-clone",
},
Expected: map[string]interface{}{
"type": "proxmox-clone",
},
},
}
for _, tc := range cases {
var f FixerProxmoxType
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")
}
}