Samuel A. Falvo II | 2dd7d2f | 2014-06-30 16:18:08 -0700 | [diff] [blame] | 1 | // +build acceptance,old |
| 2 | |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 3 | package main |
| 4 | |
| 5 | import ( |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 6 | "flag" |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 7 | "fmt" |
Max Lincoln | 28b4956 | 2013-12-13 13:23:44 -0300 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.") |
| 13 | |
| 14 | func main() { |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 15 | flag.Parse() |
| 16 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 17 | withIdentity(false, func(acc gophercloud.AccessProvider) { |
| 18 | withServerApi(acc, func(api gophercloud.CloudServersProvider) { |
| 19 | // These tests are going to take some time to complete. |
| 20 | // So, we'll do two tests at the same time to help amortize test time. |
| 21 | done := make(chan bool) |
| 22 | go resizeRejectTest(api, done) |
| 23 | go resizeAcceptTest(api, done) |
Jon Perritt | 2be65d1 | 2013-12-13 17:21:09 -0600 | [diff] [blame] | 24 | _ = <-done |
| 25 | _ = <-done |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 26 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 27 | if !*quiet { |
| 28 | fmt.Println("Done.") |
| 29 | } |
| 30 | }) |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 31 | }) |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 34 | // Perform the resize test, but reject the resize request. |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 35 | func resizeRejectTest(api gophercloud.CloudServersProvider, done chan bool) { |
| 36 | withServer(api, func(id string) { |
| 37 | newFlavorId := findAlternativeFlavor() |
| 38 | err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "") |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 43 | waitForServerState(api, id, "VERIFY_RESIZE") |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 44 | |
| 45 | err = api.RevertResize(id) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | }) |
| 50 | done <- true |
| 51 | } |
| 52 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 53 | // Perform the resize test, but accept the resize request. |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 54 | func resizeAcceptTest(api gophercloud.CloudServersProvider, done chan bool) { |
| 55 | withServer(api, func(id string) { |
| 56 | newFlavorId := findAlternativeFlavor() |
| 57 | err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "") |
| 58 | if err != nil { |
| 59 | panic(err) |
| 60 | } |
| 61 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 62 | waitForServerState(api, id, "VERIFY_RESIZE") |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 63 | |
| 64 | err = api.ConfirmResize(id) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | }) |
| 69 | done <- true |
| 70 | } |
| 71 | |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 72 | func withServer(api gophercloud.CloudServersProvider, f func(string)) { |
| 73 | id, err := createServer(api, "", "", "", "") |
| 74 | if err != nil { |
| 75 | panic(err) |
| 76 | } |
| 77 | |
| 78 | for { |
| 79 | s, err := api.ServerById(id) |
| 80 | if err != nil { |
| 81 | panic(err) |
| 82 | } |
| 83 | if s.Status == "ACTIVE" { |
| 84 | break |
| 85 | } |
| 86 | time.Sleep(10 * time.Second) |
| 87 | } |
| 88 | |
| 89 | f(id) |
| 90 | |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 91 | // I've learned that resizing an instance can fail if a delete request |
| 92 | // comes in prior to its completion. This ends up leaving the server |
| 93 | // in an error state, and neither the resize NOR the delete complete. |
| 94 | // This is a bug in OpenStack, as far as I'm concerned, but thankfully, |
| 95 | // there's an easy work-around -- just wait for your server to return to |
| 96 | // active state first! |
| 97 | waitForServerState(api, id, "ACTIVE") |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 98 | err = api.DeleteServerById(id) |
| 99 | if err != nil { |
| 100 | panic(err) |
| 101 | } |
| 102 | } |