github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/helper/schema/provider_test.go (about) 1 package schema 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/hashicorp/terraform/config" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestProvider_impl(t *testing.T) { 13 var _ terraform.ResourceProvider = new(Provider) 14 } 15 16 func TestProviderConfigure(t *testing.T) { 17 cases := []struct { 18 P *Provider 19 Config map[string]interface{} 20 Err bool 21 }{ 22 { 23 P: &Provider{}, 24 Config: nil, 25 Err: false, 26 }, 27 28 { 29 P: &Provider{ 30 Schema: map[string]*Schema{ 31 "foo": &Schema{ 32 Type: TypeInt, 33 Optional: true, 34 }, 35 }, 36 37 ConfigureFunc: func(d *ResourceData) (interface{}, error) { 38 if d.Get("foo").(int) == 42 { 39 return nil, nil 40 } 41 42 return nil, fmt.Errorf("nope") 43 }, 44 }, 45 Config: map[string]interface{}{ 46 "foo": 42, 47 }, 48 Err: false, 49 }, 50 51 { 52 P: &Provider{ 53 Schema: map[string]*Schema{ 54 "foo": &Schema{ 55 Type: TypeInt, 56 Optional: true, 57 }, 58 }, 59 60 ConfigureFunc: func(d *ResourceData) (interface{}, error) { 61 if d.Get("foo").(int) == 42 { 62 return nil, nil 63 } 64 65 return nil, fmt.Errorf("nope") 66 }, 67 }, 68 Config: map[string]interface{}{ 69 "foo": 52, 70 }, 71 Err: true, 72 }, 73 } 74 75 for i, tc := range cases { 76 c, err := config.NewRawConfig(tc.Config) 77 if err != nil { 78 t.Fatalf("err: %s", err) 79 } 80 81 err = tc.P.Configure(terraform.NewResourceConfig(c)) 82 if err != nil != tc.Err { 83 t.Fatalf("%d: %s", i, err) 84 } 85 } 86 } 87 88 func TestProviderResources(t *testing.T) { 89 cases := []struct { 90 P *Provider 91 Result []terraform.ResourceType 92 }{ 93 { 94 P: &Provider{}, 95 Result: []terraform.ResourceType{}, 96 }, 97 98 { 99 P: &Provider{ 100 ResourcesMap: map[string]*Resource{ 101 "foo": nil, 102 "bar": nil, 103 }, 104 }, 105 Result: []terraform.ResourceType{ 106 terraform.ResourceType{Name: "bar"}, 107 terraform.ResourceType{Name: "foo"}, 108 }, 109 }, 110 111 { 112 P: &Provider{ 113 ResourcesMap: map[string]*Resource{ 114 "foo": nil, 115 "bar": &Resource{Importer: &ResourceImporter{}}, 116 "baz": nil, 117 }, 118 }, 119 Result: []terraform.ResourceType{ 120 terraform.ResourceType{Name: "bar", Importable: true}, 121 terraform.ResourceType{Name: "baz"}, 122 terraform.ResourceType{Name: "foo"}, 123 }, 124 }, 125 } 126 127 for i, tc := range cases { 128 actual := tc.P.Resources() 129 if !reflect.DeepEqual(actual, tc.Result) { 130 t.Fatalf("%d: %#v", i, actual) 131 } 132 } 133 } 134 135 func TestProviderDataSources(t *testing.T) { 136 cases := []struct { 137 P *Provider 138 Result []terraform.DataSource 139 }{ 140 { 141 P: &Provider{}, 142 Result: []terraform.DataSource{}, 143 }, 144 145 { 146 P: &Provider{ 147 DataSourcesMap: map[string]*Resource{ 148 "foo": nil, 149 "bar": nil, 150 }, 151 }, 152 Result: []terraform.DataSource{ 153 terraform.DataSource{Name: "bar"}, 154 terraform.DataSource{Name: "foo"}, 155 }, 156 }, 157 } 158 159 for i, tc := range cases { 160 actual := tc.P.DataSources() 161 if !reflect.DeepEqual(actual, tc.Result) { 162 t.Fatalf("%d: got %#v; want %#v", i, actual, tc.Result) 163 } 164 } 165 } 166 167 func TestProviderValidate(t *testing.T) { 168 cases := []struct { 169 P *Provider 170 Config map[string]interface{} 171 Err bool 172 }{ 173 { 174 P: &Provider{ 175 Schema: map[string]*Schema{ 176 "foo": &Schema{}, 177 }, 178 }, 179 Config: nil, 180 Err: true, 181 }, 182 } 183 184 for i, tc := range cases { 185 c, err := config.NewRawConfig(tc.Config) 186 if err != nil { 187 t.Fatalf("err: %s", err) 188 } 189 190 _, es := tc.P.Validate(terraform.NewResourceConfig(c)) 191 if len(es) > 0 != tc.Err { 192 t.Fatalf("%d: %#v", i, es) 193 } 194 } 195 } 196 197 func TestProviderValidateResource(t *testing.T) { 198 cases := []struct { 199 P *Provider 200 Type string 201 Config map[string]interface{} 202 Err bool 203 }{ 204 { 205 P: &Provider{}, 206 Type: "foo", 207 Config: nil, 208 Err: true, 209 }, 210 211 { 212 P: &Provider{ 213 ResourcesMap: map[string]*Resource{ 214 "foo": &Resource{}, 215 }, 216 }, 217 Type: "foo", 218 Config: nil, 219 Err: false, 220 }, 221 } 222 223 for i, tc := range cases { 224 c, err := config.NewRawConfig(tc.Config) 225 if err != nil { 226 t.Fatalf("err: %s", err) 227 } 228 229 _, es := tc.P.ValidateResource(tc.Type, terraform.NewResourceConfig(c)) 230 if len(es) > 0 != tc.Err { 231 t.Fatalf("%d: %#v", i, es) 232 } 233 } 234 } 235 236 func TestProviderImportState_default(t *testing.T) { 237 p := &Provider{ 238 ResourcesMap: map[string]*Resource{ 239 "foo": &Resource{ 240 Importer: &ResourceImporter{}, 241 }, 242 }, 243 } 244 245 states, err := p.ImportState(&terraform.InstanceInfo{ 246 Type: "foo", 247 }, "bar") 248 if err != nil { 249 t.Fatalf("err: %s", err) 250 } 251 252 if len(states) != 1 { 253 t.Fatalf("bad: %#v", states) 254 } 255 if states[0].ID != "bar" { 256 t.Fatalf("bad: %#v", states) 257 } 258 } 259 260 func TestProviderImportState_setsId(t *testing.T) { 261 var val string 262 stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) { 263 val = d.Id() 264 return []*ResourceData{d}, nil 265 } 266 267 p := &Provider{ 268 ResourcesMap: map[string]*Resource{ 269 "foo": &Resource{ 270 Importer: &ResourceImporter{ 271 State: stateFunc, 272 }, 273 }, 274 }, 275 } 276 277 _, err := p.ImportState(&terraform.InstanceInfo{ 278 Type: "foo", 279 }, "bar") 280 if err != nil { 281 t.Fatalf("err: %s", err) 282 } 283 284 if val != "bar" { 285 t.Fatal("should set id") 286 } 287 } 288 289 func TestProviderImportState_setsType(t *testing.T) { 290 var tVal string 291 stateFunc := func(d *ResourceData, meta interface{}) ([]*ResourceData, error) { 292 d.SetId("foo") 293 tVal = d.State().Ephemeral.Type 294 return []*ResourceData{d}, nil 295 } 296 297 p := &Provider{ 298 ResourcesMap: map[string]*Resource{ 299 "foo": &Resource{ 300 Importer: &ResourceImporter{ 301 State: stateFunc, 302 }, 303 }, 304 }, 305 } 306 307 _, err := p.ImportState(&terraform.InstanceInfo{ 308 Type: "foo", 309 }, "bar") 310 if err != nil { 311 t.Fatalf("err: %s", err) 312 } 313 314 if tVal != "foo" { 315 t.Fatal("should set type") 316 } 317 } 318 319 func TestProviderMeta(t *testing.T) { 320 p := new(Provider) 321 if v := p.Meta(); v != nil { 322 t.Fatalf("bad: %#v", v) 323 } 324 325 expected := 42 326 p.SetMeta(42) 327 if v := p.Meta(); !reflect.DeepEqual(v, expected) { 328 t.Fatalf("bad: %#v", v) 329 } 330 }