Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 6 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/openstack/db/v1/users" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | fake "github.com/rackspace/gophercloud/testhelper/client" |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 11 | ) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 12 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 13 | func TestCreate(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 16 | HandleCreate(t) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 17 | |
| 18 | opts := CreateOpts{ |
| 19 | Name: "json_rack_instance", |
| 20 | FlavorRef: "1", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 21 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 22 | db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"}, |
| 23 | db.CreateOpts{Name: "nextround"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 24 | }, |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 25 | Users: users.BatchCreateOpts{ |
| 26 | users.CreateOpts{ |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 27 | Name: "demouser", |
| 28 | Password: "demopassword", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 29 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 30 | db.CreateOpts{Name: "sampledb"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 31 | }, |
| 32 | }, |
| 33 | }, |
| 34 | Size: 2, |
| 35 | } |
| 36 | |
| 37 | instance, err := Create(fake.ServiceClient(), opts).Extract() |
| 38 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 39 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 40 | th.AssertDeepEquals(t, &expectedInstance, instance) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 41 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 42 | |
| 43 | func TestInstanceList(t *testing.T) { |
| 44 | th.SetupHTTP() |
| 45 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 46 | HandleList(t) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 47 | |
| 48 | pages := 0 |
| 49 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 50 | pages++ |
| 51 | |
| 52 | actual, err := ExtractInstances(page) |
| 53 | if err != nil { |
| 54 | return false, err |
| 55 | } |
| 56 | |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 57 | th.CheckDeepEquals(t, []Instance{expectedInstance}, actual) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 58 | return true, nil |
| 59 | }) |
| 60 | |
| 61 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 62 | th.AssertEquals(t, 1, pages) |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 63 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 64 | |
| 65 | func TestGetInstance(t *testing.T) { |
| 66 | th.SetupHTTP() |
| 67 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 68 | HandleGet(t) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 69 | |
| 70 | instance, err := Get(fake.ServiceClient(), instanceID).Extract() |
| 71 | |
| 72 | th.AssertNoErr(t, err) |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 73 | th.AssertDeepEquals(t, &expectedInstance, instance) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 74 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 75 | |
| 76 | func TestDeleteInstance(t *testing.T) { |
| 77 | th.SetupHTTP() |
| 78 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 79 | HandleDelete(t) |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 80 | |
| 81 | res := Delete(fake.ServiceClient(), instanceID) |
| 82 | th.AssertNoErr(t, res.Err) |
| 83 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 84 | |
| 85 | func TestEnableRootUser(t *testing.T) { |
| 86 | th.SetupHTTP() |
| 87 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 88 | HandleEnableRoot(t) |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 89 | |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame] | 90 | expected := &users.User{Name: "root", Password: "secretsecret"} |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 91 | user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract() |
Jamie Hannaford | d3a78ef | 2015-02-18 12:17:16 +0100 | [diff] [blame] | 92 | |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 93 | th.AssertNoErr(t, err) |
| 94 | th.AssertDeepEquals(t, expected, user) |
| 95 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 96 | |
| 97 | func TestIsRootEnabled(t *testing.T) { |
| 98 | th.SetupHTTP() |
| 99 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 100 | HandleIsRootEnabled(t) |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 101 | |
| 102 | isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID) |
| 103 | |
| 104 | th.AssertNoErr(t, err) |
| 105 | th.AssertEquals(t, true, isEnabled) |
| 106 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 107 | |
| 108 | func TestRestartService(t *testing.T) { |
| 109 | th.SetupHTTP() |
| 110 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 111 | HandleRestart(t) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 112 | |
| 113 | res := RestartService(fake.ServiceClient(), instanceID) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 114 | th.AssertNoErr(t, res.Err) |
| 115 | } |
| 116 | |
| 117 | func TestResizeInstance(t *testing.T) { |
| 118 | th.SetupHTTP() |
| 119 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 120 | HandleResize(t) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 121 | |
| 122 | res := ResizeInstance(fake.ServiceClient(), instanceID, "2") |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 123 | th.AssertNoErr(t, res.Err) |
| 124 | } |
| 125 | |
| 126 | func TestResizeVolume(t *testing.T) { |
| 127 | th.SetupHTTP() |
| 128 | defer th.TeardownHTTP() |
Jamie Hannaford | 4a17028 | 2015-02-18 14:16:57 +0100 | [diff] [blame^] | 129 | HandleResizeVol(t) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 130 | |
| 131 | res := ResizeVolume(fake.ServiceClient(), instanceID, 4) |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 132 | th.AssertNoErr(t, res.Err) |
| 133 | } |