Jamie Hannaford | 2d48037 | 2014-10-22 10:47:09 +0200 | [diff] [blame] | 1 | // +build acceptance compute servers |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 6 | "os" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/openstack" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/openstack/networking/v2/networks" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/pagination" |
| 15 | ) |
| 16 | |
| 17 | func TestListServers(t *testing.T) { |
| 18 | client, err := newClient() |
| 19 | if err != nil { |
| 20 | t.Fatalf("Unable to create a compute client: %v", err) |
| 21 | } |
| 22 | |
| 23 | t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6") |
| 24 | |
Jamie Hannaford | 1e7841b | 2014-10-16 12:45:49 +0200 | [diff] [blame] | 25 | pager := servers.List(client, servers.ListOpts{}) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 26 | count, pages := 0, 0 |
| 27 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 28 | pages++ |
| 29 | t.Logf("---") |
| 30 | |
| 31 | servers, err := servers.ExtractServers(page) |
| 32 | if err != nil { |
| 33 | return false, err |
| 34 | } |
| 35 | |
| 36 | for _, s := range servers { |
| 37 | t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6) |
| 38 | count++ |
| 39 | } |
| 40 | |
| 41 | return true, nil |
| 42 | }) |
| 43 | |
Ash Wilson | 9a9f5bc | 2014-10-24 14:55:40 -0400 | [diff] [blame] | 44 | t.Logf("--------\n%d servers listed on %d pages.\n", count, pages) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 47 | func networkingClient() (*gophercloud.ServiceClient, error) { |
Ash Wilson | 1904fce | 2014-10-22 15:50:48 -0400 | [diff] [blame] | 48 | opts, err := openstack.AuthOptionsFromEnv() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | provider, err := openstack.AuthenticatedClient(opts) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ |
| 59 | Name: "neutron", |
| 60 | Region: os.Getenv("OS_REGION_NAME"), |
| 61 | }) |
| 62 | } |
| 63 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 64 | func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) { |
Ash Wilson | 14c3741 | 2014-10-24 14:45:27 -0400 | [diff] [blame] | 65 | if testing.Short() { |
Jon Perritt | 8135c6f | 2014-10-23 12:29:14 -0500 | [diff] [blame] | 66 | t.Skip("Skipping test that requires server creation in short mode.") |
| 67 | } |
| 68 | |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 69 | var network networks.Network |
| 70 | |
| 71 | networkingClient, err := networkingClient() |
| 72 | if err != nil { |
| 73 | t.Fatalf("Unable to create a networking client: %v", err) |
| 74 | } |
| 75 | |
| 76 | pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1}) |
| 77 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 78 | networks, err := networks.ExtractNetworks(page) |
| 79 | if err != nil { |
| 80 | t.Errorf("Failed to extract networks: %v", err) |
| 81 | return false, err |
| 82 | } |
| 83 | |
| 84 | if len(networks) == 0 { |
Jon Perritt | 3525c64 | 2014-10-21 17:12:06 -0500 | [diff] [blame] | 85 | t.Fatalf("No networks to attach to server") |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 86 | return false, err |
| 87 | } |
| 88 | |
| 89 | network = networks[0] |
| 90 | |
| 91 | return false, nil |
| 92 | }) |
| 93 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 94 | name := tools.RandomString("ACPTTEST", 16) |
| 95 | t.Logf("Attempting to create server: %s\n", name) |
| 96 | |
Ash Wilson | 488e730 | 2014-10-02 11:04:25 -0400 | [diff] [blame] | 97 | server, err := servers.Create(client, servers.CreateOpts{ |
Ash Wilson | e845851 | 2014-09-29 08:50:09 -0400 | [diff] [blame] | 98 | Name: name, |
| 99 | FlavorRef: choices.FlavorID, |
| 100 | ImageRef: choices.ImageID, |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 101 | Networks: []servers.Network{ |
| 102 | servers.Network{UUID: network.ID}, |
| 103 | }, |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 104 | }).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 105 | if err != nil { |
| 106 | t.Fatalf("Unable to create server: %v", err) |
| 107 | } |
| 108 | |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 109 | return server, err |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func TestCreateDestroyServer(t *testing.T) { |
| 113 | choices, err := ComputeChoicesFromEnv() |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | |
| 118 | client, err := newClient() |
| 119 | if err != nil { |
| 120 | t.Fatalf("Unable to create a compute client: %v", err) |
| 121 | } |
| 122 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 123 | server, err := createServer(t, client, choices) |
| 124 | if err != nil { |
| 125 | t.Fatalf("Unable to create server: %v", err) |
| 126 | } |
| 127 | defer func() { |
| 128 | servers.Delete(client, server.ID) |
| 129 | t.Logf("Server deleted.") |
| 130 | }() |
| 131 | |
| 132 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 133 | t.Fatalf("Unable to wait for server: %v", err) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestUpdateServer(t *testing.T) { |
| 138 | client, err := newClient() |
| 139 | if err != nil { |
| 140 | t.Fatalf("Unable to create a compute client: %v", err) |
| 141 | } |
| 142 | |
| 143 | choices, err := ComputeChoicesFromEnv() |
| 144 | if err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | |
| 148 | server, err := createServer(t, client, choices) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | defer servers.Delete(client, server.ID) |
| 153 | |
| 154 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | |
| 158 | alternateName := tools.RandomString("ACPTTEST", 16) |
| 159 | for alternateName == server.Name { |
| 160 | alternateName = tools.RandomString("ACPTTEST", 16) |
| 161 | } |
| 162 | |
| 163 | t.Logf("Attempting to rename the server to %s.", alternateName) |
| 164 | |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 165 | updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 166 | if err != nil { |
| 167 | t.Fatalf("Unable to rename server: %v", err) |
| 168 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 169 | |
| 170 | if updated.ID != server.ID { |
| 171 | t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID) |
| 172 | } |
| 173 | |
| 174 | err = tools.WaitFor(func() (bool, error) { |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 175 | latest, err := servers.Get(client, updated.ID).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 176 | if err != nil { |
| 177 | return false, err |
| 178 | } |
| 179 | |
| 180 | return latest.Name == alternateName, nil |
| 181 | }) |
| 182 | } |
| 183 | |
| 184 | func TestActionChangeAdminPassword(t *testing.T) { |
| 185 | t.Parallel() |
| 186 | |
| 187 | client, err := newClient() |
| 188 | if err != nil { |
| 189 | t.Fatalf("Unable to create a compute client: %v", err) |
| 190 | } |
| 191 | |
| 192 | choices, err := ComputeChoicesFromEnv() |
| 193 | if err != nil { |
| 194 | t.Fatal(err) |
| 195 | } |
| 196 | |
| 197 | server, err := createServer(t, client, choices) |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | defer servers.Delete(client, server.ID) |
| 202 | |
| 203 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 204 | t.Fatal(err) |
| 205 | } |
| 206 | |
| 207 | randomPassword := tools.MakeNewPassword(server.AdminPass) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 208 | res := servers.ChangeAdminPassword(client, server.ID, randomPassword) |
Jamie Hannaford | a262174 | 2014-10-16 14:35:49 +0200 | [diff] [blame] | 209 | if res.Err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 210 | t.Fatal(err) |
| 211 | } |
| 212 | |
| 213 | if err = waitForStatus(client, server, "PASSWORD"); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | |
| 217 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestActionReboot(t *testing.T) { |
| 223 | t.Parallel() |
| 224 | |
| 225 | client, err := newClient() |
| 226 | if err != nil { |
| 227 | t.Fatalf("Unable to create a compute client: %v", err) |
| 228 | } |
| 229 | |
| 230 | choices, err := ComputeChoicesFromEnv() |
| 231 | if err != nil { |
| 232 | t.Fatal(err) |
| 233 | } |
| 234 | |
| 235 | server, err := createServer(t, client, choices) |
| 236 | if err != nil { |
| 237 | t.Fatal(err) |
| 238 | } |
| 239 | defer servers.Delete(client, server.ID) |
| 240 | |
| 241 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 242 | t.Fatal(err) |
| 243 | } |
| 244 | |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 245 | res := servers.Reboot(client, server.ID, "aldhjflaskhjf") |
Jamie Hannaford | a262174 | 2014-10-16 14:35:49 +0200 | [diff] [blame] | 246 | if res.Err == nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 247 | t.Fatal("Expected the SDK to provide an ArgumentError here") |
| 248 | } |
| 249 | |
| 250 | t.Logf("Attempting reboot of server %s", server.ID) |
Jamie Hannaford | a262174 | 2014-10-16 14:35:49 +0200 | [diff] [blame] | 251 | res = servers.Reboot(client, server.ID, servers.OSReboot) |
| 252 | if res.Err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 253 | t.Fatalf("Unable to reboot server: %v", err) |
| 254 | } |
| 255 | |
| 256 | if err = waitForStatus(client, server, "REBOOT"); err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | |
| 260 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 261 | t.Fatal(err) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func TestActionRebuild(t *testing.T) { |
| 266 | t.Parallel() |
| 267 | |
| 268 | client, err := newClient() |
| 269 | if err != nil { |
| 270 | t.Fatalf("Unable to create a compute client: %v", err) |
| 271 | } |
| 272 | |
| 273 | choices, err := ComputeChoicesFromEnv() |
| 274 | if err != nil { |
| 275 | t.Fatal(err) |
| 276 | } |
| 277 | |
| 278 | server, err := createServer(t, client, choices) |
| 279 | if err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | defer servers.Delete(client, server.ID) |
| 283 | |
| 284 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | |
| 288 | t.Logf("Attempting to rebuild server %s", server.ID) |
| 289 | |
Jamie Hannaford | 5bfa2e8 | 2014-10-16 16:32:42 +0200 | [diff] [blame] | 290 | rebuildOpts := servers.RebuildOpts{ |
| 291 | Name: tools.RandomString("ACPTTEST", 16), |
| 292 | AdminPass: tools.MakeNewPassword(server.AdminPass), |
| 293 | ImageID: choices.ImageID, |
| 294 | } |
| 295 | |
| 296 | rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 297 | if err != nil { |
| 298 | t.Fatal(err) |
| 299 | } |
| 300 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 301 | if rebuilt.ID != server.ID { |
| 302 | t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID) |
| 303 | } |
| 304 | |
| 305 | if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | |
| 309 | if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | } |
| 313 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 314 | func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) { |
| 315 | if err := waitForStatus(client, server, "ACTIVE"); err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 316 | t.Fatal(err) |
| 317 | } |
| 318 | |
| 319 | t.Logf("Attempting to resize server [%s]", server.ID) |
| 320 | |
Ash Wilson | 14c3741 | 2014-10-24 14:45:27 -0400 | [diff] [blame] | 321 | opts := &servers.ResizeOpts{ |
| 322 | FlavorRef: choices.FlavorIDResize, |
| 323 | } |
| 324 | if res := servers.Resize(client, server.ID, opts); res.Err != nil { |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 325 | t.Fatal(res.Err) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 328 | if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 329 | t.Fatal(err) |
| 330 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | func TestActionResizeConfirm(t *testing.T) { |
| 334 | t.Parallel() |
| 335 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 336 | choices, err := ComputeChoicesFromEnv() |
| 337 | if err != nil { |
| 338 | t.Fatal(err) |
| 339 | } |
| 340 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 341 | client, err := newClient() |
| 342 | if err != nil { |
| 343 | t.Fatalf("Unable to create a compute client: %v", err) |
| 344 | } |
| 345 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 346 | server, err := createServer(t, client, choices) |
| 347 | if err != nil { |
| 348 | t.Fatal(err) |
| 349 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 350 | defer servers.Delete(client, server.ID) |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 351 | resizeServer(t, client, server, choices) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 352 | |
| 353 | t.Logf("Attempting to confirm resize for server %s", server.ID) |
| 354 | |
Jamie Hannaford | a262174 | 2014-10-16 14:35:49 +0200 | [diff] [blame] | 355 | if res := servers.ConfirmResize(client, server.ID); res.Err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 356 | t.Fatal(err) |
| 357 | } |
| 358 | |
| 359 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 360 | t.Fatal(err) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | func TestActionResizeRevert(t *testing.T) { |
| 365 | t.Parallel() |
| 366 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 367 | choices, err := ComputeChoicesFromEnv() |
| 368 | if err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 372 | client, err := newClient() |
| 373 | if err != nil { |
| 374 | t.Fatalf("Unable to create a compute client: %v", err) |
| 375 | } |
| 376 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 377 | server, err := createServer(t, client, choices) |
| 378 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 379 | t.Fatal(err) |
| 380 | } |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 381 | defer servers.Delete(client, server.ID) |
| 382 | resizeServer(t, client, server, choices) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 383 | |
| 384 | t.Logf("Attempting to revert resize for server %s", server.ID) |
| 385 | |
Jamie Hannaford | a262174 | 2014-10-16 14:35:49 +0200 | [diff] [blame] | 386 | if res := servers.RevertResize(client, server.ID); res.Err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 387 | t.Fatal(err) |
| 388 | } |
| 389 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 390 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 391 | t.Fatal(err) |
| 392 | } |
| 393 | } |