github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/helper/schema/field_reader_config_test.go (about) 1 package schema 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/hashicorp/terraform/config" 8 "github.com/hashicorp/terraform/terraform" 9 ) 10 11 func TestConfigFieldReader_impl(t *testing.T) { 12 var _ FieldReader = new(ConfigFieldReader) 13 } 14 15 func TestConfigFieldReader(t *testing.T) { 16 testFieldReader(t, func(s map[string]*Schema) FieldReader { 17 return &ConfigFieldReader{ 18 Schema: s, 19 20 Config: testConfig(t, map[string]interface{}{ 21 "bool": true, 22 "float": 3.1415, 23 "int": 42, 24 "string": "string", 25 26 "list": []interface{}{"foo", "bar"}, 27 28 "listInt": []interface{}{21, 42}, 29 30 "map": map[string]interface{}{ 31 "foo": "bar", 32 "bar": "baz", 33 }, 34 35 "set": []interface{}{10, 50}, 36 "setDeep": []interface{}{ 37 map[string]interface{}{ 38 "index": 10, 39 "value": "foo", 40 }, 41 map[string]interface{}{ 42 "index": 50, 43 "value": "bar", 44 }, 45 }, 46 }), 47 } 48 }) 49 } 50 51 func testConfig( 52 t *testing.T, raw map[string]interface{}) *terraform.ResourceConfig { 53 rc, err := config.NewRawConfig(raw) 54 if err != nil { 55 t.Fatalf("err: %s", err) 56 } 57 58 return terraform.NewResourceConfig(rc) 59 } 60 61 func TestConfigFieldReader_DefaultHandling(t *testing.T) { 62 schema := map[string]*Schema{ 63 "strWithDefault": &Schema{ 64 Type: TypeString, 65 Default: "ImADefault", 66 }, 67 "strWithDefaultFunc": &Schema{ 68 Type: TypeString, 69 DefaultFunc: func() (interface{}, error) { 70 return "FuncDefault", nil 71 }, 72 }, 73 } 74 75 cases := map[string]struct { 76 Addr []string 77 Result FieldReadResult 78 Config *terraform.ResourceConfig 79 Err bool 80 }{ 81 "gets default value when no config set": { 82 []string{"strWithDefault"}, 83 FieldReadResult{ 84 Value: "ImADefault", 85 Exists: true, 86 Computed: false, 87 }, 88 testConfig(t, map[string]interface{}{}), 89 false, 90 }, 91 "config overrides default value": { 92 []string{"strWithDefault"}, 93 FieldReadResult{ 94 Value: "fromConfig", 95 Exists: true, 96 Computed: false, 97 }, 98 testConfig(t, map[string]interface{}{ 99 "strWithDefault": "fromConfig", 100 }), 101 false, 102 }, 103 "gets default from function when no config set": { 104 []string{"strWithDefaultFunc"}, 105 FieldReadResult{ 106 Value: "FuncDefault", 107 Exists: true, 108 Computed: false, 109 }, 110 testConfig(t, map[string]interface{}{}), 111 false, 112 }, 113 "config overrides default function": { 114 []string{"strWithDefaultFunc"}, 115 FieldReadResult{ 116 Value: "fromConfig", 117 Exists: true, 118 Computed: false, 119 }, 120 testConfig(t, map[string]interface{}{ 121 "strWithDefaultFunc": "fromConfig", 122 }), 123 false, 124 }, 125 } 126 127 for name, tc := range cases { 128 r := &ConfigFieldReader{ 129 Schema: schema, 130 Config: tc.Config, 131 } 132 out, err := r.ReadField(tc.Addr) 133 if (err != nil) != tc.Err { 134 t.Fatalf("%s: err: %s", name, err) 135 } 136 if s, ok := out.Value.(*Set); ok { 137 // If it is a set, convert to a list so its more easily checked. 138 out.Value = s.List() 139 } 140 if !reflect.DeepEqual(tc.Result, out) { 141 t.Fatalf("%s: bad: %#v", name, out) 142 } 143 } 144 }