Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/acceptance/tools" |
| 11 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 12 | "github.com/rackspace/gophercloud/pagination" |
| 13 | ) |
| 14 | |
| 15 | func TestListServers(t *testing.T) { |
| 16 | client, err := newClient() |
| 17 | if err != nil { |
| 18 | t.Fatalf("Unable to create a compute client: %v", err) |
| 19 | } |
| 20 | |
| 21 | t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6") |
| 22 | |
| 23 | pager := servers.List(client) |
| 24 | count, pages := 0, 0 |
| 25 | pager.EachPage(func(page pagination.Page) (bool, error) { |
| 26 | pages++ |
| 27 | t.Logf("---") |
| 28 | |
| 29 | servers, err := servers.ExtractServers(page) |
| 30 | if err != nil { |
| 31 | return false, err |
| 32 | } |
| 33 | |
| 34 | for _, s := range servers { |
| 35 | t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6) |
| 36 | count++ |
| 37 | } |
| 38 | |
| 39 | return true, nil |
| 40 | }) |
| 41 | |
| 42 | fmt.Printf("--------\n%d servers listed on %d pages.\n", count, pages) |
| 43 | } |
| 44 | |
| 45 | func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) { |
| 46 | name := tools.RandomString("ACPTTEST", 16) |
| 47 | t.Logf("Attempting to create server: %s\n", name) |
| 48 | |
Ash Wilson | e845851 | 2014-09-29 08:50:09 -0400 | [diff] [blame^] | 49 | server, err := servers.Create(client, &servers.CreateOpts{ |
| 50 | Name: name, |
| 51 | FlavorRef: choices.FlavorID, |
| 52 | ImageRef: choices.ImageID, |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 53 | }).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 54 | if err != nil { |
| 55 | t.Fatalf("Unable to create server: %v", err) |
| 56 | } |
| 57 | |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 58 | return server, err |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestCreateDestroyServer(t *testing.T) { |
| 62 | choices, err := ComputeChoicesFromEnv() |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | |
| 67 | client, err := newClient() |
| 68 | if err != nil { |
| 69 | t.Fatalf("Unable to create a compute client: %v", err) |
| 70 | } |
| 71 | |
| 72 | name := tools.RandomString("ACPTTEST", 16) |
| 73 | t.Logf("Attempting to create server: %s\n", name) |
| 74 | |
| 75 | server, err := createServer(t, client, choices) |
| 76 | if err != nil { |
| 77 | t.Fatalf("Unable to create server: %v", err) |
| 78 | } |
| 79 | defer func() { |
| 80 | servers.Delete(client, server.ID) |
| 81 | t.Logf("Server deleted.") |
| 82 | }() |
| 83 | |
| 84 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 85 | t.Fatalf("Unable to wait for server: %v", err) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestUpdateServer(t *testing.T) { |
| 90 | client, err := newClient() |
| 91 | if err != nil { |
| 92 | t.Fatalf("Unable to create a compute client: %v", err) |
| 93 | } |
| 94 | |
| 95 | choices, err := ComputeChoicesFromEnv() |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | |
| 100 | server, err := createServer(t, client, choices) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | defer servers.Delete(client, server.ID) |
| 105 | |
| 106 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | |
| 110 | alternateName := tools.RandomString("ACPTTEST", 16) |
| 111 | for alternateName == server.Name { |
| 112 | alternateName = tools.RandomString("ACPTTEST", 16) |
| 113 | } |
| 114 | |
| 115 | t.Logf("Attempting to rename the server to %s.", alternateName) |
| 116 | |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 117 | updated, err := servers.Update(client, server.ID, map[string]interface{}{ |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 118 | "name": alternateName, |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 119 | }).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 120 | if err != nil { |
| 121 | t.Fatalf("Unable to rename server: %v", err) |
| 122 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 123 | |
| 124 | if updated.ID != server.ID { |
| 125 | t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID) |
| 126 | } |
| 127 | |
| 128 | err = tools.WaitFor(func() (bool, error) { |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 129 | latest, err := servers.Get(client, updated.ID).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 130 | if err != nil { |
| 131 | return false, err |
| 132 | } |
| 133 | |
| 134 | return latest.Name == alternateName, nil |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | func TestActionChangeAdminPassword(t *testing.T) { |
| 139 | t.Parallel() |
| 140 | |
| 141 | client, err := newClient() |
| 142 | if err != nil { |
| 143 | t.Fatalf("Unable to create a compute client: %v", err) |
| 144 | } |
| 145 | |
| 146 | choices, err := ComputeChoicesFromEnv() |
| 147 | if err != nil { |
| 148 | t.Fatal(err) |
| 149 | } |
| 150 | |
| 151 | server, err := createServer(t, client, choices) |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | defer servers.Delete(client, server.ID) |
| 156 | |
| 157 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | |
| 161 | randomPassword := tools.MakeNewPassword(server.AdminPass) |
| 162 | err = servers.ChangeAdminPassword(client, server.ID, randomPassword) |
| 163 | if err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | |
| 167 | if err = waitForStatus(client, server, "PASSWORD"); err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | |
| 171 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func TestActionReboot(t *testing.T) { |
| 177 | t.Parallel() |
| 178 | |
| 179 | client, err := newClient() |
| 180 | if err != nil { |
| 181 | t.Fatalf("Unable to create a compute client: %v", err) |
| 182 | } |
| 183 | |
| 184 | choices, err := ComputeChoicesFromEnv() |
| 185 | if err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | |
| 189 | server, err := createServer(t, client, choices) |
| 190 | if err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | defer servers.Delete(client, server.ID) |
| 194 | |
| 195 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 196 | t.Fatal(err) |
| 197 | } |
| 198 | |
| 199 | err = servers.Reboot(client, server.ID, "aldhjflaskhjf") |
| 200 | if err == nil { |
| 201 | t.Fatal("Expected the SDK to provide an ArgumentError here") |
| 202 | } |
| 203 | |
| 204 | t.Logf("Attempting reboot of server %s", server.ID) |
| 205 | err = servers.Reboot(client, server.ID, servers.OSReboot) |
| 206 | if err != nil { |
| 207 | t.Fatalf("Unable to reboot server: %v", err) |
| 208 | } |
| 209 | |
| 210 | if err = waitForStatus(client, server, "REBOOT"); 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 TestActionRebuild(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 | |
| 242 | t.Logf("Attempting to rebuild server %s", server.ID) |
| 243 | |
| 244 | newPassword := tools.MakeNewPassword(server.AdminPass) |
| 245 | newName := tools.RandomString("ACPTTEST", 16) |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 246 | rebuilt, err := servers.Rebuild(client, server.ID, newName, newPassword, choices.ImageID, nil).Extract() |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 251 | if rebuilt.ID != server.ID { |
| 252 | t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID) |
| 253 | } |
| 254 | |
| 255 | if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | |
| 259 | if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | } |
| 263 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 264 | func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) { |
| 265 | if err := waitForStatus(client, server, "ACTIVE"); err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 266 | t.Fatal(err) |
| 267 | } |
| 268 | |
| 269 | t.Logf("Attempting to resize server [%s]", server.ID) |
| 270 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 271 | if err := servers.Resize(client, server.ID, choices.FlavorIDResize); err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 272 | t.Fatal(err) |
| 273 | } |
| 274 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 275 | if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 276 | t.Fatal(err) |
| 277 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | func TestActionResizeConfirm(t *testing.T) { |
| 281 | t.Parallel() |
| 282 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 283 | choices, err := ComputeChoicesFromEnv() |
| 284 | if err != nil { |
| 285 | t.Fatal(err) |
| 286 | } |
| 287 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 288 | client, err := newClient() |
| 289 | if err != nil { |
| 290 | t.Fatalf("Unable to create a compute client: %v", err) |
| 291 | } |
| 292 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 293 | server, err := createServer(t, client, choices) |
| 294 | if err != nil { |
| 295 | t.Fatal(err) |
| 296 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 297 | defer servers.Delete(client, server.ID) |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 298 | resizeServer(t, client, server, choices) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 299 | |
| 300 | t.Logf("Attempting to confirm resize for server %s", server.ID) |
| 301 | |
| 302 | if err = servers.ConfirmResize(client, server.ID); err != nil { |
| 303 | t.Fatal(err) |
| 304 | } |
| 305 | |
| 306 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 307 | t.Fatal(err) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | func TestActionResizeRevert(t *testing.T) { |
| 312 | t.Parallel() |
| 313 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 314 | choices, err := ComputeChoicesFromEnv() |
| 315 | if err != nil { |
| 316 | t.Fatal(err) |
| 317 | } |
| 318 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 319 | client, err := newClient() |
| 320 | if err != nil { |
| 321 | t.Fatalf("Unable to create a compute client: %v", err) |
| 322 | } |
| 323 | |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 324 | server, err := createServer(t, client, choices) |
| 325 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 326 | t.Fatal(err) |
| 327 | } |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 328 | defer servers.Delete(client, server.ID) |
| 329 | resizeServer(t, client, server, choices) |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 330 | |
| 331 | t.Logf("Attempting to revert resize for server %s", server.ID) |
| 332 | |
| 333 | if err := servers.RevertResize(client, server.ID); err != nil { |
| 334 | t.Fatal(err) |
| 335 | } |
| 336 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 337 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 338 | t.Fatal(err) |
| 339 | } |
| 340 | } |