Before this commit it was possible to set a duration using an integer or a float. Go's time.Duration is an int64 internally an mapstructure will take advantage of this and load the number as a int64 but `1` means one ns which is unexpected/confusing. To avoid confusion and enforce readability this forces users to pass a string with a unit for a duration; ex "56s".
135 lines
2.1 KiB
Go
135 lines
2.1 KiB
Go
package config
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
)
|
|
|
|
func TestDecode(t *testing.T) {
|
|
type Target struct {
|
|
Name string
|
|
Address string
|
|
Time DurationString
|
|
Trilean Trilean
|
|
}
|
|
|
|
cases := map[string]struct {
|
|
Input []interface{}
|
|
Output *Target
|
|
Opts *DecodeOpts
|
|
}{
|
|
"basic": {
|
|
[]interface{}{
|
|
map[string]interface{}{
|
|
"name": "bar",
|
|
"time": "5s",
|
|
"trilean": "true",
|
|
},
|
|
},
|
|
&Target{
|
|
Name: "bar",
|
|
Time: "5s",
|
|
Trilean: TriTrue,
|
|
},
|
|
nil,
|
|
},
|
|
|
|
"empty-string-trilean": {
|
|
[]interface{}{
|
|
map[string]interface{}{
|
|
"trilean": "",
|
|
},
|
|
},
|
|
&Target{
|
|
Trilean: TriUnset,
|
|
},
|
|
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"},
|
|
},
|
|
},
|
|
},
|
|
|
|
"build name": {
|
|
[]interface{}{
|
|
map[string]interface{}{
|
|
"name": "{{build_name}}",
|
|
},
|
|
map[string]interface{}{
|
|
"packer_build_name": "foo",
|
|
},
|
|
},
|
|
&Target{
|
|
Name: "foo",
|
|
},
|
|
nil,
|
|
},
|
|
|
|
"build type": {
|
|
[]interface{}{
|
|
map[string]interface{}{
|
|
"name": "{{build_type}}",
|
|
},
|
|
map[string]interface{}{
|
|
"packer_builder_type": "foo",
|
|
},
|
|
},
|
|
&Target{
|
|
Name: "foo",
|
|
},
|
|
nil,
|
|
},
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|