jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 4 | "encoding/base64" |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 5 | "encoding/json" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 6 | "net/http" |
| 7 | "testing" |
| 8 | |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 9 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones" |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/pagination" |
| 12 | th "github.com/gophercloud/gophercloud/testhelper" |
| 13 | "github.com/gophercloud/gophercloud/testhelper/client" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 16 | func TestListServers(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 17 | th.SetupHTTP() |
| 18 | defer th.TeardownHTTP() |
Ash Wilson | a70510a | 2014-10-23 11:54:03 -0400 | [diff] [blame] | 19 | HandleServerListSuccessfully(t) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 20 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 21 | pages := 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 22 | err := servers.List(client.ServiceClient(), servers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 23 | pages++ |
| 24 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 25 | actual, err := servers.ExtractServers(page) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 26 | if err != nil { |
| 27 | return false, err |
| 28 | } |
| 29 | |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 30 | if len(actual) != 3 { |
| 31 | t.Fatalf("Expected 3 servers, got %d", len(actual)) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 32 | } |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 33 | th.CheckDeepEquals(t, ServerHerp, actual[0]) |
| 34 | th.CheckDeepEquals(t, ServerDerp, actual[1]) |
jrperritt | 1fa9250 | 2016-07-21 19:22:59 -0500 | [diff] [blame] | 35 | th.CheckDeepEquals(t, ServerMerp, actual[2]) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 36 | |
| 37 | return true, nil |
| 38 | }) |
| 39 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 40 | th.AssertNoErr(t, err) |
Jamie Hannaford | cf00172 | 2014-10-16 12:54:07 +0200 | [diff] [blame] | 41 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 42 | if pages != 1 { |
| 43 | t.Errorf("Expected 1 page, saw %d", pages) |
| 44 | } |
| 45 | } |
| 46 | |
Jon Perritt | d27a9c7 | 2015-02-18 11:33:28 -0700 | [diff] [blame] | 47 | func TestListAllServers(t *testing.T) { |
| 48 | th.SetupHTTP() |
| 49 | defer th.TeardownHTTP() |
| 50 | HandleServerListSuccessfully(t) |
| 51 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 52 | allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages() |
Jon Perritt | d27a9c7 | 2015-02-18 11:33:28 -0700 | [diff] [blame] | 53 | th.AssertNoErr(t, err) |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 54 | actual, err := servers.ExtractServers(allPages) |
Jon Perritt | d27a9c7 | 2015-02-18 11:33:28 -0700 | [diff] [blame] | 55 | th.AssertNoErr(t, err) |
| 56 | th.CheckDeepEquals(t, ServerHerp, actual[0]) |
| 57 | th.CheckDeepEquals(t, ServerDerp, actual[1]) |
| 58 | } |
| 59 | |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 60 | func TestListAllServersWithExtensions(t *testing.T) { |
| 61 | th.SetupHTTP() |
| 62 | defer th.TeardownHTTP() |
| 63 | HandleServerListSuccessfully(t) |
| 64 | |
| 65 | type ServerWithExt struct { |
| 66 | servers.Server |
| 67 | availabilityzones.ServerExt |
| 68 | } |
| 69 | |
| 70 | allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages() |
| 71 | th.AssertNoErr(t, err) |
| 72 | |
| 73 | var actual []ServerWithExt |
| 74 | err = servers.ExtractServersInto(allPages, &actual) |
| 75 | th.AssertNoErr(t, err) |
| 76 | th.AssertEquals(t, 3, len(actual)) |
| 77 | th.AssertEquals(t, "nova", actual[0].AvailabilityZone) |
| 78 | } |
| 79 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 80 | func TestCreateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 83 | HandleServerCreationSuccessfully(t, SingleServerBody) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 84 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 85 | actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{ |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 86 | Name: "derp", |
| 87 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 88 | FlavorRef: "1", |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 89 | }).Extract() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 90 | th.AssertNoErr(t, err) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 91 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 92 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 95 | func TestCreateServerWithCustomField(t *testing.T) { |
| 96 | th.SetupHTTP() |
| 97 | defer th.TeardownHTTP() |
| 98 | HandleServerCreationWithCustomFieldSuccessfully(t, SingleServerBody) |
| 99 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 100 | actual, err := servers.Create(client.ServiceClient(), CreateOptsWithCustomField{ |
| 101 | CreateOpts: servers.CreateOpts{ |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 102 | Name: "derp", |
| 103 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 104 | FlavorRef: "1", |
| 105 | }, |
| 106 | Foo: "bar", |
| 107 | }).Extract() |
| 108 | th.AssertNoErr(t, err) |
| 109 | |
| 110 | th.CheckDeepEquals(t, ServerDerp, *actual) |
| 111 | } |
| 112 | |
Joe Topjian | f464c96 | 2016-09-12 08:02:43 -0600 | [diff] [blame] | 113 | func TestCreateServerWithMetadata(t *testing.T) { |
| 114 | th.SetupHTTP() |
| 115 | defer th.TeardownHTTP() |
| 116 | HandleServerCreationWithMetadata(t, SingleServerBody) |
| 117 | |
| 118 | actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{ |
| 119 | Name: "derp", |
| 120 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 121 | FlavorRef: "1", |
| 122 | Metadata: map[string]string{ |
| 123 | "abc": "def", |
| 124 | }, |
| 125 | }).Extract() |
| 126 | th.AssertNoErr(t, err) |
| 127 | |
| 128 | th.CheckDeepEquals(t, ServerDerp, *actual) |
| 129 | } |
| 130 | |
Gavin Williams | cd65a06 | 2016-11-08 19:05:47 +0000 | [diff] [blame] | 131 | func TestCreateServerWithUserdataString(t *testing.T) { |
| 132 | th.SetupHTTP() |
| 133 | defer th.TeardownHTTP() |
| 134 | HandleServerCreationWithUserdata(t, SingleServerBody) |
| 135 | |
| 136 | actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{ |
| 137 | Name: "derp", |
| 138 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 139 | FlavorRef: "1", |
| 140 | UserData: []byte("userdata string"), |
| 141 | }).Extract() |
| 142 | th.AssertNoErr(t, err) |
| 143 | |
| 144 | th.CheckDeepEquals(t, ServerDerp, *actual) |
| 145 | } |
| 146 | |
| 147 | func TestCreateServerWithUserdataEncoded(t *testing.T) { |
| 148 | th.SetupHTTP() |
| 149 | defer th.TeardownHTTP() |
| 150 | HandleServerCreationWithUserdata(t, SingleServerBody) |
| 151 | |
| 152 | encoded := base64.StdEncoding.EncodeToString([]byte("userdata string")) |
| 153 | |
| 154 | actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{ |
| 155 | Name: "derp", |
| 156 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 157 | FlavorRef: "1", |
| 158 | UserData: []byte(encoded), |
| 159 | }).Extract() |
| 160 | th.AssertNoErr(t, err) |
| 161 | |
| 162 | th.CheckDeepEquals(t, ServerDerp, *actual) |
| 163 | } |
| 164 | |
jrperritt | 0d7ed5d | 2016-08-16 11:23:26 -0500 | [diff] [blame] | 165 | func TestCreateServerWithImageNameAndFlavorName(t *testing.T) { |
| 166 | th.SetupHTTP() |
| 167 | defer th.TeardownHTTP() |
| 168 | HandleServerCreationSuccessfully(t, SingleServerBody) |
| 169 | |
| 170 | actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{ |
| 171 | Name: "derp", |
| 172 | ImageName: "cirros-0.3.2-x86_64-disk", |
| 173 | FlavorName: "m1.tiny", |
| 174 | ServiceClient: client.ServiceClient(), |
| 175 | }).Extract() |
| 176 | th.AssertNoErr(t, err) |
| 177 | |
| 178 | th.CheckDeepEquals(t, ServerDerp, *actual) |
| 179 | } |
| 180 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 181 | func TestDeleteServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 182 | th.SetupHTTP() |
| 183 | defer th.TeardownHTTP() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 184 | HandleServerDeletionSuccessfully(t) |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 185 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 186 | res := servers.Delete(client.ServiceClient(), "asdfasdfasdf") |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 187 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Ian Duffy | 370c430 | 2016-01-21 10:44:56 +0000 | [diff] [blame] | 190 | func TestForceDeleteServer(t *testing.T) { |
| 191 | th.SetupHTTP() |
| 192 | defer th.TeardownHTTP() |
| 193 | HandleServerForceDeletionSuccessfully(t) |
| 194 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 195 | res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf") |
Ian Duffy | 370c430 | 2016-01-21 10:44:56 +0000 | [diff] [blame] | 196 | th.AssertNoErr(t, res.Err) |
| 197 | } |
| 198 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 199 | func TestGetServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 200 | th.SetupHTTP() |
| 201 | defer th.TeardownHTTP() |
Ash Wilson | 189c95c | 2014-10-23 11:41:35 -0400 | [diff] [blame] | 202 | HandleServerGetSuccessfully(t) |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 203 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 204 | client := client.ServiceClient() |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 205 | actual, err := servers.Get(client, "1234asdf").Extract() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 206 | if err != nil { |
| 207 | t.Fatalf("Unexpected Get error: %v", err) |
| 208 | } |
| 209 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 210 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Joe Topjian | 0f64da0 | 2017-03-09 18:59:53 -0700 | [diff] [blame^] | 213 | func TestGetServerWithExtensions(t *testing.T) { |
| 214 | th.SetupHTTP() |
| 215 | defer th.TeardownHTTP() |
| 216 | HandleServerGetSuccessfully(t) |
| 217 | |
| 218 | var s struct { |
| 219 | servers.Server |
| 220 | availabilityzones.ServerExt |
| 221 | } |
| 222 | |
| 223 | err := servers.Get(client.ServiceClient(), "1234asdf").ExtractInto(&s) |
| 224 | th.AssertNoErr(t, err) |
| 225 | th.AssertEquals(t, "nova", s.AvailabilityZone) |
| 226 | |
| 227 | err = servers.Get(client.ServiceClient(), "1234asdf").ExtractInto(s) |
| 228 | if err == nil { |
| 229 | t.Errorf("Expected error when providing non-pointer struct") |
| 230 | } |
| 231 | } |
| 232 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 233 | func TestUpdateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 234 | th.SetupHTTP() |
| 235 | defer th.TeardownHTTP() |
Ash Wilson | 189c95c | 2014-10-23 11:41:35 -0400 | [diff] [blame] | 236 | HandleServerUpdateSuccessfully(t) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 237 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 238 | client := client.ServiceClient() |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 239 | actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 240 | if err != nil { |
| 241 | t.Fatalf("Unexpected Update error: %v", err) |
| 242 | } |
| 243 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 244 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | func TestChangeServerAdminPassword(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 248 | th.SetupHTTP() |
| 249 | defer th.TeardownHTTP() |
Ash Wilson | 1c1eb88 | 2014-10-21 18:14:31 -0400 | [diff] [blame] | 250 | HandleAdminPasswordChangeSuccessfully(t) |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 251 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 252 | res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 253 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 254 | } |
| 255 | |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 256 | func TestGetPassword(t *testing.T) { |
| 257 | th.SetupHTTP() |
| 258 | defer th.TeardownHTTP() |
| 259 | HandlePasswordGetSuccessfully(t) |
| 260 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 261 | res := servers.GetPassword(client.ServiceClient(), "1234asdf") |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 262 | th.AssertNoErr(t, res.Err) |
| 263 | } |
| 264 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 265 | func TestRebootServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 266 | th.SetupHTTP() |
| 267 | defer th.TeardownHTTP() |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 268 | HandleRebootSuccessfully(t) |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 269 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 270 | res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{ |
| 271 | Type: servers.SoftReboot, |
Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 272 | }) |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 273 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | func TestRebuildServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 277 | th.SetupHTTP() |
| 278 | defer th.TeardownHTTP() |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 279 | HandleRebuildSuccessfully(t, SingleServerBody) |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 280 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 281 | opts := servers.RebuildOpts{ |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 282 | Name: "new-name", |
| 283 | AdminPass: "swordfish", |
| 284 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 285 | AccessIPv4: "1.2.3.4", |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 286 | } |
| 287 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 288 | actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 289 | th.AssertNoErr(t, err) |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 290 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 291 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | func TestResizeServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 295 | th.SetupHTTP() |
| 296 | defer th.TeardownHTTP() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 297 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 298 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 299 | th.TestMethod(t, r, "POST") |
| 300 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 301 | th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`) |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 302 | |
| 303 | w.WriteHeader(http.StatusAccepted) |
| 304 | }) |
| 305 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 306 | res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"}) |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 307 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | func TestConfirmResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 311 | th.SetupHTTP() |
| 312 | defer th.TeardownHTTP() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 313 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 314 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 315 | th.TestMethod(t, r, "POST") |
| 316 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 317 | th.TestJSONRequest(t, r, `{ "confirmResize": null }`) |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 318 | |
| 319 | w.WriteHeader(http.StatusNoContent) |
| 320 | }) |
| 321 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 322 | res := servers.ConfirmResize(client.ServiceClient(), "1234asdf") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 323 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | func TestRevertResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 327 | th.SetupHTTP() |
| 328 | defer th.TeardownHTTP() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 329 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 330 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 331 | th.TestMethod(t, r, "POST") |
| 332 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 333 | th.TestJSONRequest(t, r, `{ "revertResize": null }`) |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 334 | |
| 335 | w.WriteHeader(http.StatusAccepted) |
| 336 | }) |
| 337 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 338 | res := servers.RevertResize(client.ServiceClient(), "1234asdf") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 339 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 340 | } |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 341 | |
| 342 | func TestRescue(t *testing.T) { |
| 343 | th.SetupHTTP() |
| 344 | defer th.TeardownHTTP() |
| 345 | |
Alex Gaynor | 6c003d2 | 2014-11-13 13:52:05 -0800 | [diff] [blame] | 346 | HandleServerRescueSuccessfully(t) |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 347 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 348 | res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{ |
Alex Gaynor | 3116817 | 2014-11-12 16:27:47 -0800 | [diff] [blame] | 349 | AdminPass: "1234567890", |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 350 | }) |
| 351 | th.AssertNoErr(t, res.Err) |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 352 | adminPass, _ := res.Extract() |
Alex Gaynor | 0160cff | 2014-11-13 10:17:48 -0800 | [diff] [blame] | 353 | th.AssertEquals(t, "1234567890", adminPass) |
Alex Gaynor | 810d489 | 2014-11-12 15:43:36 -0800 | [diff] [blame] | 354 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 355 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 356 | func TestGetMetadatum(t *testing.T) { |
| 357 | th.SetupHTTP() |
| 358 | defer th.TeardownHTTP() |
| 359 | |
| 360 | HandleMetadatumGetSuccessfully(t) |
| 361 | |
| 362 | expected := map[string]string{"foo": "bar"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 363 | actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract() |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 364 | th.AssertNoErr(t, err) |
| 365 | th.AssertDeepEquals(t, expected, actual) |
| 366 | } |
| 367 | |
| 368 | func TestCreateMetadatum(t *testing.T) { |
| 369 | th.SetupHTTP() |
| 370 | defer th.TeardownHTTP() |
| 371 | |
| 372 | HandleMetadatumCreateSuccessfully(t) |
| 373 | |
| 374 | expected := map[string]string{"foo": "bar"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 375 | actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract() |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 376 | th.AssertNoErr(t, err) |
| 377 | th.AssertDeepEquals(t, expected, actual) |
| 378 | } |
| 379 | |
| 380 | func TestDeleteMetadatum(t *testing.T) { |
| 381 | th.SetupHTTP() |
| 382 | defer th.TeardownHTTP() |
| 383 | |
| 384 | HandleMetadatumDeleteSuccessfully(t) |
| 385 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 386 | err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr() |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 387 | th.AssertNoErr(t, err) |
| 388 | } |
| 389 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 390 | func TestGetMetadata(t *testing.T) { |
| 391 | th.SetupHTTP() |
| 392 | defer th.TeardownHTTP() |
| 393 | |
| 394 | HandleMetadataGetSuccessfully(t) |
| 395 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 396 | expected := map[string]string{"foo": "bar", "this": "that"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 397 | actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract() |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 398 | th.AssertNoErr(t, err) |
| 399 | th.AssertDeepEquals(t, expected, actual) |
| 400 | } |
| 401 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 402 | func TestResetMetadata(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 403 | th.SetupHTTP() |
| 404 | defer th.TeardownHTTP() |
| 405 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 406 | HandleMetadataResetSuccessfully(t) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 407 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 408 | expected := map[string]string{"foo": "bar", "this": "that"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 409 | actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{ |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 410 | "foo": "bar", |
| 411 | "this": "that", |
| 412 | }).Extract() |
| 413 | th.AssertNoErr(t, err) |
| 414 | th.AssertDeepEquals(t, expected, actual) |
| 415 | } |
| 416 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 417 | func TestUpdateMetadata(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 418 | th.SetupHTTP() |
| 419 | defer th.TeardownHTTP() |
| 420 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 421 | HandleMetadataUpdateSuccessfully(t) |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 422 | |
| 423 | expected := map[string]string{"foo": "baz", "this": "those"} |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 424 | actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{ |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 425 | "foo": "baz", |
| 426 | "this": "those", |
| 427 | }).Extract() |
| 428 | th.AssertNoErr(t, err) |
| 429 | th.AssertDeepEquals(t, expected, actual) |
| 430 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 431 | |
| 432 | func TestListAddresses(t *testing.T) { |
| 433 | th.SetupHTTP() |
| 434 | defer th.TeardownHTTP() |
| 435 | HandleAddressListSuccessfully(t) |
| 436 | |
| 437 | expected := ListAddressesExpected |
| 438 | pages := 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 439 | err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 440 | pages++ |
| 441 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 442 | actual, err := servers.ExtractAddresses(page) |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 443 | th.AssertNoErr(t, err) |
| 444 | |
| 445 | if len(actual) != 2 { |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 446 | t.Fatalf("Expected 2 networks, got %d", len(actual)) |
| 447 | } |
| 448 | th.CheckDeepEquals(t, expected, actual) |
| 449 | |
| 450 | return true, nil |
| 451 | }) |
| 452 | th.AssertNoErr(t, err) |
| 453 | th.CheckEquals(t, 1, pages) |
| 454 | } |
| 455 | |
| 456 | func TestListAddressesByNetwork(t *testing.T) { |
| 457 | th.SetupHTTP() |
| 458 | defer th.TeardownHTTP() |
| 459 | HandleNetworkAddressListSuccessfully(t) |
| 460 | |
| 461 | expected := ListNetworkAddressesExpected |
| 462 | pages := 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 463 | err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 464 | pages++ |
| 465 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 466 | actual, err := servers.ExtractNetworkAddresses(page) |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 467 | th.AssertNoErr(t, err) |
| 468 | |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 469 | if len(actual) != 2 { |
| 470 | t.Fatalf("Expected 2 addresses, got %d", len(actual)) |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 471 | } |
| 472 | th.CheckDeepEquals(t, expected, actual) |
| 473 | |
| 474 | return true, nil |
| 475 | }) |
| 476 | th.AssertNoErr(t, err) |
| 477 | th.CheckEquals(t, 1, pages) |
| 478 | } |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 479 | |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 480 | func TestCreateServerImage(t *testing.T) { |
| 481 | th.SetupHTTP() |
| 482 | defer th.TeardownHTTP() |
| 483 | HandleCreateServerImageSuccessfully(t) |
| 484 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 485 | _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID() |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 486 | th.AssertNoErr(t, err) |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 487 | } |
Kevin Pike | 25d4569 | 2015-04-23 17:20:09 -0700 | [diff] [blame] | 488 | |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 489 | func TestMarshalPersonality(t *testing.T) { |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 490 | name := "/etc/test" |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 491 | contents := []byte("asdfasdf") |
| 492 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 493 | personality := servers.Personality{ |
| 494 | &servers.File{ |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 495 | Path: name, |
| 496 | Contents: contents, |
| 497 | }, |
| 498 | } |
| 499 | |
Kevin Pike | a2bfaea | 2015-04-21 11:45:59 -0700 | [diff] [blame] | 500 | data, err := json.Marshal(personality) |
| 501 | if err != nil { |
| 502 | t.Fatal(err) |
| 503 | } |
| 504 | |
| 505 | var actual []map[string]string |
| 506 | err = json.Unmarshal(data, &actual) |
| 507 | if err != nil { |
| 508 | t.Fatal(err) |
| 509 | } |
| 510 | |
| 511 | if len(actual) != 1 { |
| 512 | t.Fatal("expected personality length 1") |
| 513 | } |
Kevin Pike | 92e10b5 | 2015-04-10 15:16:57 -0700 | [diff] [blame] | 514 | |
| 515 | if actual[0]["path"] != name { |
| 516 | t.Fatal("file path incorrect") |
| 517 | } |
| 518 | |
| 519 | if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) { |
| 520 | t.Fatal("file contents incorrect") |
| 521 | } |
| 522 | } |