2015-05-27 13:44:10 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-06-13 17:53:45 -04:00
|
|
|
"time"
|
2015-05-27 13:44:10 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecode(t *testing.T) {
|
|
|
|
type Target struct {
|
|
|
|
Name string
|
|
|
|
Address string
|
2015-06-13 17:53:45 -04:00
|
|
|
Time time.Duration
|
2015-05-27 13:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input []interface{}
|
|
|
|
Output *Target
|
|
|
|
Opts *DecodeOpts
|
|
|
|
}{
|
|
|
|
"basic": {
|
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "bar",
|
2015-06-13 17:53:45 -04:00
|
|
|
"time": "5s",
|
2015-05-27 13:44:10 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&Target{
|
|
|
|
Name: "bar",
|
2015-06-13 17:53:45 -04:00
|
|
|
Time: 5 * time.Second,
|
2015-05-27 13:44:10 -04:00
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
"variables": {
|
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "{{user `name`}}",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"packer_user_variables": map[string]string{
|
|
|
|
"name": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Target{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
"filter": {
|
|
|
|
[]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"name": "{{user `name`}}",
|
|
|
|
"address": "{{user `name`}}",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"packer_user_variables": map[string]string{
|
|
|
|
"name": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&Target{
|
|
|
|
Name: "bar",
|
|
|
|
Address: "{{user `name`}}",
|
|
|
|
},
|
|
|
|
&DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Include: []string{"name"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, tc := range cases {
|
|
|
|
var result Target
|
|
|
|
err := Decode(&result, tc.Opts, tc.Input...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s\n\n%s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&result, tc.Output) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", &result, tc.Output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|