github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_sql_user_migrate_test.go (about) 1 package google 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/terraform/terraform" 7 ) 8 9 func TestSqlUserMigrateState(t *testing.T) { 10 cases := map[string]struct { 11 StateVersion int 12 Attributes map[string]string 13 Expected map[string]string 14 Meta interface{} 15 ID string 16 ExpectedID string 17 }{ 18 "change id from $NAME to $INSTANCENAME.$NAME": { 19 StateVersion: 0, 20 Attributes: map[string]string{ 21 "name": "tf-user", 22 "instance": "tf-instance", 23 }, 24 Expected: map[string]string{ 25 "name": "tf-user", 26 "instance": "tf-instance", 27 }, 28 Meta: &Config{}, 29 ID: "tf-user", 30 ExpectedID: "tf-instance/tf-user", 31 }, 32 } 33 34 for tn, tc := range cases { 35 is := &terraform.InstanceState{ 36 ID: tc.ID, 37 Attributes: tc.Attributes, 38 } 39 is, err := resourceSqlUserMigrateState( 40 tc.StateVersion, is, tc.Meta) 41 42 if err != nil { 43 t.Fatalf("bad: %s, err: %#v", tn, err) 44 } 45 46 if is.ID != tc.ExpectedID { 47 t.Fatalf("bad ID.\n\n expected: %s\n got: %s", tc.ExpectedID, is.ID) 48 } 49 50 for k, v := range tc.Expected { 51 if is.Attributes[k] != v { 52 t.Fatalf( 53 "bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v", 54 tn, k, v, k, is.Attributes[k], is.Attributes) 55 } 56 } 57 } 58 } 59 60 func TestSqlUserMigrateState_empty(t *testing.T) { 61 var is *terraform.InstanceState 62 var meta *Config 63 64 // should handle nil 65 is, err := resourceSqlUserMigrateState(0, is, meta) 66 67 if err != nil { 68 t.Fatalf("err: %#v", err) 69 } 70 if is != nil { 71 t.Fatalf("expected nil instancestate, got: %#v", is) 72 } 73 74 // should handle non-nil but empty 75 is = &terraform.InstanceState{} 76 is, err = resourceSqlUserMigrateState(0, is, meta) 77 78 if err != nil { 79 t.Fatalf("err: %#v", err) 80 } 81 }