blob: a2ef3c87e611923045a5b9ed769154f70bab6b45 [file] [log] [blame]
Samuel A. Falvo II2dd7d2f2014-06-30 16:18:08 -07001// +build acceptance,old
2
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -07003package main
4
5import (
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -07006 "flag"
Jon Perritt2be65d12013-12-13 17:21:09 -06007 "fmt"
Max Lincoln28b49562013-12-13 13:23:44 -03008 "github.com/rackspace/gophercloud"
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -07009 "time"
10)
11
12var quiet = flag.Bool("quiet", false, "Quiet mode, for acceptance testing. $? still indicates errors though.")
13
14func main() {
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070015 flag.Parse()
16
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070017 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 Perritt2be65d12013-12-13 17:21:09 -060024 _ = <-done
25 _ = <-done
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070026
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070027 if !*quiet {
28 fmt.Println("Done.")
29 }
30 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070031 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070032}
33
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070034// Perform the resize test, but reject the resize request.
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070035func 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 II887d7802013-07-29 17:55:37 -070043 waitForServerState(api, id, "VERIFY_RESIZE")
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070044
45 err = api.RevertResize(id)
46 if err != nil {
47 panic(err)
48 }
49 })
50 done <- true
51}
52
Samuel A. Falvo II887d7802013-07-29 17:55:37 -070053// Perform the resize test, but accept the resize request.
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070054func 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 II887d7802013-07-29 17:55:37 -070062 waitForServerState(api, id, "VERIFY_RESIZE")
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070063
64 err = api.ConfirmResize(id)
65 if err != nil {
66 panic(err)
67 }
68 })
69 done <- true
70}
71
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -070072func 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 II887d7802013-07-29 17:55:37 -070091 // 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 II8512e9a2013-07-26 22:53:29 -070098 err = api.DeleteServerById(id)
99 if err != nil {
100 panic(err)
101 }
102}