github.com/mingfang/terraform@v0.11.12-beta1/helper/schema/core_schema_test.go (about) 1 package schema 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/davecgh/go-spew/spew" 10 11 "github.com/hashicorp/terraform/config/configschema" 12 ) 13 14 func TestSchemaMapCoreConfigSchema(t *testing.T) { 15 tests := map[string]struct { 16 Schema map[string]*Schema 17 Want *configschema.Block 18 }{ 19 "empty": { 20 map[string]*Schema{}, 21 &configschema.Block{}, 22 }, 23 "primitives": { 24 map[string]*Schema{ 25 "int": { 26 Type: TypeInt, 27 Required: true, 28 }, 29 "float": { 30 Type: TypeFloat, 31 Optional: true, 32 }, 33 "bool": { 34 Type: TypeBool, 35 Computed: true, 36 }, 37 "string": { 38 Type: TypeString, 39 Optional: true, 40 Computed: true, 41 }, 42 }, 43 &configschema.Block{ 44 Attributes: map[string]*configschema.Attribute{ 45 "int": { 46 Type: cty.Number, 47 Required: true, 48 }, 49 "float": { 50 Type: cty.Number, 51 Optional: true, 52 }, 53 "bool": { 54 Type: cty.Bool, 55 Computed: true, 56 }, 57 "string": { 58 Type: cty.String, 59 Optional: true, 60 Computed: true, 61 }, 62 }, 63 BlockTypes: map[string]*configschema.NestedBlock{}, 64 }, 65 }, 66 "simple collections": { 67 map[string]*Schema{ 68 "list": { 69 Type: TypeList, 70 Required: true, 71 Elem: &Schema{ 72 Type: TypeInt, 73 }, 74 }, 75 "set": { 76 Type: TypeSet, 77 Optional: true, 78 Elem: &Schema{ 79 Type: TypeString, 80 }, 81 }, 82 "map": { 83 Type: TypeMap, 84 Optional: true, 85 Elem: &Schema{ 86 Type: TypeBool, 87 }, 88 }, 89 "map_default_type": { 90 Type: TypeMap, 91 Optional: true, 92 // Maps historically don't have elements because we 93 // assumed they would be strings, so this needs to work 94 // for pre-existing schemas. 95 }, 96 }, 97 &configschema.Block{ 98 Attributes: map[string]*configschema.Attribute{ 99 "list": { 100 Type: cty.List(cty.Number), 101 Required: true, 102 }, 103 "set": { 104 Type: cty.Set(cty.String), 105 Optional: true, 106 }, 107 "map": { 108 Type: cty.Map(cty.Bool), 109 Optional: true, 110 }, 111 "map_default_type": { 112 Type: cty.Map(cty.String), 113 Optional: true, 114 }, 115 }, 116 BlockTypes: map[string]*configschema.NestedBlock{}, 117 }, 118 }, 119 "sub-resource collections": { 120 map[string]*Schema{ 121 "list": { 122 Type: TypeList, 123 Required: true, 124 Elem: &Resource{ 125 Schema: map[string]*Schema{}, 126 }, 127 MinItems: 1, 128 MaxItems: 2, 129 }, 130 "set": { 131 Type: TypeSet, 132 Required: true, 133 Elem: &Resource{ 134 Schema: map[string]*Schema{}, 135 }, 136 }, 137 "map": { 138 Type: TypeMap, 139 Optional: true, 140 Elem: &Resource{ 141 Schema: map[string]*Schema{}, 142 }, 143 }, 144 }, 145 &configschema.Block{ 146 Attributes: map[string]*configschema.Attribute{}, 147 BlockTypes: map[string]*configschema.NestedBlock{ 148 "list": { 149 Nesting: configschema.NestingList, 150 Block: configschema.Block{}, 151 MinItems: 1, 152 MaxItems: 2, 153 }, 154 "set": { 155 Nesting: configschema.NestingSet, 156 Block: configschema.Block{}, 157 MinItems: 1, // because schema is Required 158 }, 159 "map": { 160 Nesting: configschema.NestingMap, 161 Block: configschema.Block{}, 162 }, 163 }, 164 }, 165 }, 166 "nested attributes and blocks": { 167 map[string]*Schema{ 168 "foo": { 169 Type: TypeList, 170 Required: true, 171 Elem: &Resource{ 172 Schema: map[string]*Schema{ 173 "bar": { 174 Type: TypeList, 175 Required: true, 176 Elem: &Schema{ 177 Type: TypeList, 178 Elem: &Schema{ 179 Type: TypeString, 180 }, 181 }, 182 }, 183 "baz": { 184 Type: TypeSet, 185 Optional: true, 186 Elem: &Resource{ 187 Schema: map[string]*Schema{}, 188 }, 189 }, 190 }, 191 }, 192 }, 193 }, 194 &configschema.Block{ 195 Attributes: map[string]*configschema.Attribute{}, 196 BlockTypes: map[string]*configschema.NestedBlock{ 197 "foo": &configschema.NestedBlock{ 198 Nesting: configschema.NestingList, 199 Block: configschema.Block{ 200 Attributes: map[string]*configschema.Attribute{ 201 "bar": { 202 Type: cty.List(cty.List(cty.String)), 203 Required: true, 204 }, 205 }, 206 BlockTypes: map[string]*configschema.NestedBlock{ 207 "baz": { 208 Nesting: configschema.NestingSet, 209 Block: configschema.Block{}, 210 }, 211 }, 212 }, 213 MinItems: 1, // because schema is Required 214 }, 215 }, 216 }, 217 }, 218 "sensitive": { 219 map[string]*Schema{ 220 "string": { 221 Type: TypeString, 222 Optional: true, 223 Sensitive: true, 224 }, 225 }, 226 &configschema.Block{ 227 Attributes: map[string]*configschema.Attribute{ 228 "string": { 229 Type: cty.String, 230 Optional: true, 231 Sensitive: true, 232 }, 233 }, 234 BlockTypes: map[string]*configschema.NestedBlock{}, 235 }, 236 }, 237 } 238 239 for name, test := range tests { 240 t.Run(name, func(t *testing.T) { 241 got := schemaMap(test.Schema).CoreConfigSchema() 242 if !reflect.DeepEqual(got, test.Want) { 243 t.Errorf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(test.Want)) 244 } 245 }) 246 } 247 }