blob: 558b2cfd724fa410ba3659afb734670b28060c67 [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() {
13 provider, username, password := getCredentials()
14 flag.Parse()
15
16 acc, err := gophercloud.Authenticate(
17 provider,
18 gophercloud.AuthOptions{
19 Username: username,
20 Password: password,
21 },
22 )
23 if err != nil {
24 panic(err)
25 }
26
27 api, err := gophercloud.ServersApi(acc, gophercloud.ApiCriteria{
28 Name: "cloudServersOpenStack",
29 Region: "DFW",
30 VersionId: "2",
31 UrlChoice: gophercloud.PublicURL,
32 })
33 if err != nil {
34 panic(err)
35 }
36
37 // These tests are going to take some time to complete.
38 // So, we'll do two tests at the same time to help amortize test time.
39 done := make(chan bool)
40 go resizeRejectTest(api, done)
41 go resizeAcceptTest(api, done)
42 _ = <- done
43 _ = <- done
44
45 if !*quiet {
46 fmt.Println("Done.")
47 }
48}
49
50func resizeRejectTest(api gophercloud.CloudServersProvider, done chan bool) {
51 withServer(api, func(id string) {
52 newFlavorId := findAlternativeFlavor()
53 err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "")
54 if err != nil {
55 panic(err)
56 }
57
58 waitForVerifyResize(api, id)
59
60 err = api.RevertResize(id)
61 if err != nil {
62 panic(err)
63 }
64 })
65 done <- true
66}
67
68func resizeAcceptTest(api gophercloud.CloudServersProvider, done chan bool) {
69 withServer(api, func(id string) {
70 newFlavorId := findAlternativeFlavor()
71 err := api.ResizeServer(id, randomString("ACPTTEST", 24), newFlavorId, "")
72 if err != nil {
73 panic(err)
74 }
75
76 waitForVerifyResize(api, id)
77
78 err = api.ConfirmResize(id)
79 if err != nil {
80 panic(err)
81 }
82 })
83 done <- true
84}
85
86func waitForVerifyResize(api gophercloud.CloudServersProvider, id string) {
87 for {
88 s, err := api.ServerById(id)
89 if err != nil {
90 panic(err)
91 }
92 if s.Status == "VERIFY_RESIZE" {
93 break
94 }
95 time.Sleep(10 * time.Second)
96 }
97}
98
99func withServer(api gophercloud.CloudServersProvider, f func(string)) {
100 id, err := createServer(api, "", "", "", "")
101 if err != nil {
102 panic(err)
103 }
104
105 for {
106 s, err := api.ServerById(id)
107 if err != nil {
108 panic(err)
109 }
110 if s.Status == "ACTIVE" {
111 break
112 }
113 time.Sleep(10 * time.Second)
114 }
115
116 f(id)
117
118 err = api.DeleteServerById(id)
119 if err != nil {
120 panic(err)
121 }
122}