Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 1 | package instances |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 7 | db "github.com/rackspace/gophercloud/openstack/db/v1/databases" |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame^] | 8 | "github.com/rackspace/gophercloud/openstack/db/v1/users" |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | fake "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 14 | var instanceID = "d4603f69-ec7e-4e9b-803f-600b9205576f" |
| 15 | |
| 16 | var expectedInstance = &Instance{ |
| 17 | Created: "2014-02-13T21:47:13", |
| 18 | Updated: "2014-02-13T21:47:13", |
| 19 | Flavor: Flavor{ |
| 20 | ID: "1", |
| 21 | Links: []gophercloud.Link{ |
| 22 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 23 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/flavors/1", Rel: "bookmark"}, |
| 24 | }, |
| 25 | }, |
| 26 | Hostname: "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com", |
| 27 | ID: instanceID, |
| 28 | Links: []gophercloud.Link{ |
| 29 | gophercloud.Link{Href: "https://my-openstack.com/v1.0/1234/instances/1", Rel: "self"}, |
| 30 | }, |
| 31 | Name: "json_rack_instance", |
| 32 | Status: "BUILD", |
| 33 | Volume: Volume{Size: 2}, |
| 34 | } |
| 35 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 36 | func TestCreate(t *testing.T) { |
| 37 | th.SetupHTTP() |
| 38 | defer th.TeardownHTTP() |
| 39 | |
| 40 | HandleCreateInstanceSuccessfully(t) |
| 41 | |
| 42 | opts := CreateOpts{ |
| 43 | Name: "json_rack_instance", |
| 44 | FlavorRef: "1", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 45 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 46 | db.CreateOpts{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"}, |
| 47 | db.CreateOpts{Name: "nextround"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 48 | }, |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame^] | 49 | Users: users.BatchCreateOpts{ |
| 50 | users.CreateOpts{ |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 51 | Name: "demouser", |
| 52 | Password: "demopassword", |
Jamie Hannaford | 85f1033 | 2015-02-12 11:51:37 +0100 | [diff] [blame] | 53 | Databases: db.BatchCreateOpts{ |
Jamie Hannaford | 56d0c2e | 2015-02-12 11:50:18 +0100 | [diff] [blame] | 54 | db.CreateOpts{Name: "sampledb"}, |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 55 | }, |
| 56 | }, |
| 57 | }, |
| 58 | Size: 2, |
| 59 | } |
| 60 | |
| 61 | instance, err := Create(fake.ServiceClient(), opts).Extract() |
| 62 | |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 63 | th.AssertNoErr(t, err) |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 64 | th.AssertDeepEquals(t, expectedInstance, instance) |
Jamie Hannaford | 9fdda58 | 2015-02-10 12:15:43 +0100 | [diff] [blame] | 65 | } |
Jamie Hannaford | 9068424 | 2015-02-10 12:46:07 +0100 | [diff] [blame] | 66 | |
| 67 | func TestInstanceList(t *testing.T) { |
| 68 | th.SetupHTTP() |
| 69 | defer th.TeardownHTTP() |
| 70 | |
| 71 | HandleListInstanceSuccessfully(t) |
| 72 | |
| 73 | expectedInstance := Instance{ |
| 74 | Flavor: Flavor{ |
| 75 | ID: "1", |
| 76 | Links: []gophercloud.Link{ |
| 77 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/flavors/1", Rel: "self"}, |
| 78 | gophercloud.Link{Href: "https://openstack.example.com/flavors/1", Rel: "bookmark"}, |
| 79 | }, |
| 80 | }, |
| 81 | ID: "8fb081af-f237-44f5-80cc-b46be1840ca9", |
| 82 | Links: []gophercloud.Link{ |
| 83 | gophercloud.Link{Href: "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9", Rel: "self"}, |
| 84 | }, |
| 85 | Name: "xml_rack_instance", |
| 86 | Status: "ACTIVE", |
| 87 | Volume: Volume{Size: 2}, |
| 88 | } |
| 89 | |
| 90 | pages := 0 |
| 91 | err := List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 92 | pages++ |
| 93 | |
| 94 | actual, err := ExtractInstances(page) |
| 95 | if err != nil { |
| 96 | return false, err |
| 97 | } |
| 98 | |
| 99 | if len(actual) != 1 { |
| 100 | t.Fatalf("Expected 1 DB instance, got %d", len(actual)) |
| 101 | } |
| 102 | th.CheckDeepEquals(t, expectedInstance, actual[0]) |
| 103 | |
| 104 | return true, nil |
| 105 | }) |
| 106 | |
| 107 | th.AssertNoErr(t, err) |
| 108 | |
| 109 | if pages != 1 { |
| 110 | t.Errorf("Expected 1 page, saw %d", pages) |
| 111 | } |
| 112 | } |
Jamie Hannaford | 821015f | 2015-02-10 12:58:36 +0100 | [diff] [blame] | 113 | |
| 114 | func TestGetInstance(t *testing.T) { |
| 115 | th.SetupHTTP() |
| 116 | defer th.TeardownHTTP() |
| 117 | |
| 118 | HandleGetInstanceSuccessfully(t, instanceID) |
| 119 | |
| 120 | instance, err := Get(fake.ServiceClient(), instanceID).Extract() |
| 121 | |
| 122 | th.AssertNoErr(t, err) |
| 123 | th.AssertDeepEquals(t, instance, expectedInstance) |
| 124 | } |
Jamie Hannaford | 5b16b63 | 2015-02-10 13:36:23 +0100 | [diff] [blame] | 125 | |
| 126 | func TestDeleteInstance(t *testing.T) { |
| 127 | th.SetupHTTP() |
| 128 | defer th.TeardownHTTP() |
| 129 | |
| 130 | HandleDeleteInstanceSuccessfully(t, instanceID) |
| 131 | |
| 132 | res := Delete(fake.ServiceClient(), instanceID) |
| 133 | th.AssertNoErr(t, res.Err) |
| 134 | } |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 135 | |
| 136 | func TestEnableRootUser(t *testing.T) { |
| 137 | th.SetupHTTP() |
| 138 | defer th.TeardownHTTP() |
| 139 | |
| 140 | HandleEnableRootUserSuccessfully(t, instanceID) |
| 141 | |
Jamie Hannaford | 3aba0b1 | 2015-02-13 14:33:39 +0100 | [diff] [blame^] | 142 | expected := &users.User{Name: "root", Password: "secretsecret"} |
Jamie Hannaford | 94164fa | 2015-02-10 13:58:45 +0100 | [diff] [blame] | 143 | |
| 144 | user, err := EnableRootUser(fake.ServiceClient(), instanceID).Extract() |
| 145 | th.AssertNoErr(t, err) |
| 146 | th.AssertDeepEquals(t, expected, user) |
| 147 | } |
Jamie Hannaford | a74d425 | 2015-02-10 15:35:01 +0100 | [diff] [blame] | 148 | |
| 149 | func TestIsRootEnabled(t *testing.T) { |
| 150 | th.SetupHTTP() |
| 151 | defer th.TeardownHTTP() |
| 152 | |
| 153 | HandleIsRootEnabledSuccessfully(t, instanceID) |
| 154 | |
| 155 | isEnabled, err := IsRootEnabled(fake.ServiceClient(), instanceID) |
| 156 | |
| 157 | th.AssertNoErr(t, err) |
| 158 | th.AssertEquals(t, true, isEnabled) |
| 159 | } |
Jamie Hannaford | 219ca59 | 2015-02-10 15:59:05 +0100 | [diff] [blame] | 160 | |
| 161 | func TestRestartService(t *testing.T) { |
| 162 | th.SetupHTTP() |
| 163 | defer th.TeardownHTTP() |
| 164 | |
| 165 | HandleRestartSuccessfully(t, instanceID) |
| 166 | |
| 167 | res := RestartService(fake.ServiceClient(), instanceID) |
| 168 | |
| 169 | th.AssertNoErr(t, res.Err) |
| 170 | } |
| 171 | |
| 172 | func TestResizeInstance(t *testing.T) { |
| 173 | th.SetupHTTP() |
| 174 | defer th.TeardownHTTP() |
| 175 | |
| 176 | HandleResizeInstanceSuccessfully(t, instanceID) |
| 177 | |
| 178 | res := ResizeInstance(fake.ServiceClient(), instanceID, "2") |
| 179 | |
| 180 | th.AssertNoErr(t, res.Err) |
| 181 | } |
| 182 | |
| 183 | func TestResizeVolume(t *testing.T) { |
| 184 | th.SetupHTTP() |
| 185 | defer th.TeardownHTTP() |
| 186 | |
| 187 | HandleResizeVolSuccessfully(t, instanceID) |
| 188 | |
| 189 | res := ResizeVolume(fake.ServiceClient(), instanceID, 4) |
| 190 | |
| 191 | th.AssertNoErr(t, res.Err) |
| 192 | } |