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