github.com/HashDataInc/packer@v1.3.2/helper/config/decode_test.go (about) 1 package config 2 3 import ( 4 "reflect" 5 "testing" 6 "time" 7 8 "github.com/hashicorp/packer/template/interpolate" 9 ) 10 11 func TestDecode(t *testing.T) { 12 type Target struct { 13 Name string 14 Address string 15 Time time.Duration 16 } 17 18 cases := map[string]struct { 19 Input []interface{} 20 Output *Target 21 Opts *DecodeOpts 22 }{ 23 "basic": { 24 []interface{}{ 25 map[string]interface{}{ 26 "name": "bar", 27 "time": "5s", 28 }, 29 }, 30 &Target{ 31 Name: "bar", 32 Time: 5 * time.Second, 33 }, 34 nil, 35 }, 36 37 "variables": { 38 []interface{}{ 39 map[string]interface{}{ 40 "name": "{{user `name`}}", 41 }, 42 map[string]interface{}{ 43 "packer_user_variables": map[string]string{ 44 "name": "bar", 45 }, 46 }, 47 }, 48 &Target{ 49 Name: "bar", 50 }, 51 nil, 52 }, 53 54 "filter": { 55 []interface{}{ 56 map[string]interface{}{ 57 "name": "{{user `name`}}", 58 "address": "{{user `name`}}", 59 }, 60 map[string]interface{}{ 61 "packer_user_variables": map[string]string{ 62 "name": "bar", 63 }, 64 }, 65 }, 66 &Target{ 67 Name: "bar", 68 Address: "{{user `name`}}", 69 }, 70 &DecodeOpts{ 71 Interpolate: true, 72 InterpolateFilter: &interpolate.RenderFilter{ 73 Include: []string{"name"}, 74 }, 75 }, 76 }, 77 78 "build name": { 79 []interface{}{ 80 map[string]interface{}{ 81 "name": "{{build_name}}", 82 }, 83 map[string]interface{}{ 84 "packer_build_name": "foo", 85 }, 86 }, 87 &Target{ 88 Name: "foo", 89 }, 90 nil, 91 }, 92 93 "build type": { 94 []interface{}{ 95 map[string]interface{}{ 96 "name": "{{build_type}}", 97 }, 98 map[string]interface{}{ 99 "packer_builder_type": "foo", 100 }, 101 }, 102 &Target{ 103 Name: "foo", 104 }, 105 nil, 106 }, 107 } 108 109 for k, tc := range cases { 110 var result Target 111 err := Decode(&result, tc.Opts, tc.Input...) 112 if err != nil { 113 t.Fatalf("err: %s\n\n%s", k, err) 114 } 115 116 if !reflect.DeepEqual(&result, tc.Output) { 117 t.Fatalf("bad:\n\n%#v\n\n%#v", &result, tc.Output) 118 } 119 } 120 }