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