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