blob: b12ef4b8a42a5880d5813aa7eae1f2b44a521ec1 [file] [log] [blame]
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -07001package main
2
3import (
4 "fmt"
5 "flag"
6 "github.com/rackspace/gophercloud"
7 "time"
8)
9
10var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.")
11
12func main() {
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070013 flag.Parse()
14
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070015 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 II8512e9a2013-07-26 22:53:29 -070024
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070025 if !*quiet {
26 fmt.Println("Done.")
27 }
28 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070029 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070030}
31
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070032// Perform the resize test, but reject the resize request.
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070033func 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 II887d7802013-07-29 17:55:37 -070041 waitForServerState(api, id, "VERIFY_RESIZE")
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070042
43 err = api.RevertResize(id)
44 if err != nil {
45 panic(err)
46 }
47 })
48 done <- true
49}
50
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070051// Perform the resize test, but accept the resize request.
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070052func 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 II887d7802013-07-29 17:55:37 -070060 waitForServerState(api, id, "VERIFY_RESIZE")
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070061
62 err = api.ConfirmResize(id)
63 if err != nil {
64 panic(err)
65 }
66 })
67 done <- true
68}
69
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070070func 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 II887d7802013-07-29 17:55:37 -070089 // 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 II8512e9a2013-07-26 22:53:29 -070096 err = api.DeleteServerById(id)
97 if err != nil {
98 panic(err)
99 }
100}