Samuel A. Falvo II | 2dd7d2f | 2014-06-30 16:18:08 -0700 | [diff] [blame] | 1 | // +build acceptance,old |
| 2 | |
Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 3 | package main |
| 4 | |
| 5 | import ( |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 6 | "crypto/rand" |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 7 | "fmt" |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 9 | "os" |
Max Lincoln | 5b250e0 | 2013-12-13 14:49:38 -0300 | [diff] [blame] | 10 | "strings" |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 11 | "time" |
Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // getCredentials will verify existence of needed credential information |
| 15 | // provided through environment variables. This function will not return |
| 16 | // if at least one piece of required information is missing. |
Rafael Garcia | e4a550e | 2013-12-06 17:00:32 -0300 | [diff] [blame] | 17 | func getCredentials() (provider, username, password, apiKey string) { |
Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 18 | provider = os.Getenv("SDK_PROVIDER") |
| 19 | username = os.Getenv("SDK_USERNAME") |
| 20 | password = os.Getenv("SDK_PASSWORD") |
Rafael Garcia | 752cb33 | 2013-12-12 22:16:58 -0300 | [diff] [blame] | 21 | apiKey = os.Getenv("SDK_API_KEY") |
Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 22 | |
| 23 | if (provider == "") || (username == "") || (password == "") { |
| 24 | fmt.Fprintf(os.Stderr, "One or more of the following environment variables aren't set:\n") |
| 25 | fmt.Fprintf(os.Stderr, " SDK_PROVIDER=\"%s\"\n", provider) |
| 26 | fmt.Fprintf(os.Stderr, " SDK_USERNAME=\"%s\"\n", username) |
| 27 | fmt.Fprintf(os.Stderr, " SDK_PASSWORD=\"%s\"\n", password) |
| 28 | os.Exit(1) |
| 29 | } |
| 30 | |
| 31 | return |
| 32 | } |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 33 | |
| 34 | // randomString generates a string of given length, but random content. |
| 35 | // All content will be within the ASCII graphic character set. |
| 36 | // (Implementation from Even Shaw's contribution on |
| 37 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
Samuel A. Falvo II | e3b2d7a | 2013-07-12 11:08:02 -0700 | [diff] [blame] | 38 | func randomString(prefix string, n int) string { |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 39 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 40 | var bytes = make([]byte, n) |
| 41 | rand.Read(bytes) |
| 42 | for i, b := range bytes { |
| 43 | bytes[i] = alphanum[b%byte(len(alphanum))] |
| 44 | } |
| 45 | return prefix + string(bytes) |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // aSuitableImage finds a minimal image for use in dynamically creating servers. |
| 49 | // If none can be found, this function will panic. |
| 50 | func aSuitableImage(api gophercloud.CloudServersProvider) string { |
| 51 | images, err := api.ListImages() |
| 52 | if err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | |
| 56 | // TODO(sfalvo): |
| 57 | // Works for Rackspace, might not work for your provider! |
| 58 | // Need to figure out why ListImages() provides 0 values for |
| 59 | // Ram and Disk fields. |
| 60 | // |
| 61 | // Until then, just return Ubuntu 12.04 LTS. |
| 62 | for i := 0; i < len(images); i++ { |
Max Lincoln | 5b250e0 | 2013-12-13 14:49:38 -0300 | [diff] [blame] | 63 | if strings.Contains(images[i].Name, "Ubuntu 12.04 LTS") { |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 64 | return images[i].Id |
| 65 | } |
| 66 | } |
Max Lincoln | 5b250e0 | 2013-12-13 14:49:38 -0300 | [diff] [blame] | 67 | panic("Image for Ubuntu 12.04 LTS not found.") |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // aSuitableFlavor finds the minimum flavor capable of running the test image |
| 71 | // chosen by aSuitableImage. If none can be found, this function will panic. |
| 72 | func aSuitableFlavor(api gophercloud.CloudServersProvider) string { |
| 73 | flavors, err := api.ListFlavors() |
| 74 | if err != nil { |
| 75 | panic(err) |
| 76 | } |
| 77 | |
| 78 | // TODO(sfalvo): |
| 79 | // Works for Rackspace, might not work for your provider! |
| 80 | // Need to figure out why ListFlavors() provides 0 values for |
| 81 | // Ram and Disk fields. |
| 82 | // |
| 83 | // Until then, just return Ubuntu 12.04 LTS. |
| 84 | for i := 0; i < len(flavors); i++ { |
| 85 | if flavors[i].Id == "2" { |
| 86 | return flavors[i].Id |
| 87 | } |
| 88 | } |
| 89 | panic("Flavor 2 (512MB 1-core 20GB machine) not found.") |
| 90 | } |
| 91 | |
| 92 | // createServer creates a new server in a manner compatible with acceptance testing. |
| 93 | // In particular, it ensures that the name of the server always starts with "ACPTTEST--", |
| 94 | // which the delete servers acceptance test relies on to identify servers to delete. |
| 95 | // Passing in empty image and flavor references will force the use of reasonable defaults. |
| 96 | // An empty name string will result in a dynamically created name prefixed with "ACPTTEST--". |
| 97 | // A blank admin password will cause a password to be automatically generated; however, |
| 98 | // at present no means of recovering this password exists, as no acceptance tests yet require |
| 99 | // this data. |
Samuel A. Falvo II | 8069960 | 2013-07-25 23:35:57 -0700 | [diff] [blame] | 100 | func createServer(servers gophercloud.CloudServersProvider, imageRef, flavorRef, name, adminPass string) (string, error) { |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 101 | if imageRef == "" { |
| 102 | imageRef = aSuitableImage(servers) |
| 103 | } |
| 104 | |
| 105 | if flavorRef == "" { |
| 106 | flavorRef = aSuitableFlavor(servers) |
| 107 | } |
| 108 | |
| 109 | if len(name) < 1 { |
| 110 | name = randomString("ACPTTEST", 16) |
| 111 | } |
| 112 | |
| 113 | if (len(name) < 8) || (name[0:8] != "ACPTTEST") { |
| 114 | name = fmt.Sprintf("ACPTTEST--%s", name) |
| 115 | } |
| 116 | |
Samuel A. Falvo II | 8069960 | 2013-07-25 23:35:57 -0700 | [diff] [blame] | 117 | newServer, err := servers.CreateServer(gophercloud.NewServer{ |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 118 | Name: name, |
| 119 | ImageRef: imageRef, |
| 120 | FlavorRef: flavorRef, |
| 121 | AdminPass: adminPass, |
| 122 | }) |
| 123 | |
Samuel A. Falvo II | 8069960 | 2013-07-25 23:35:57 -0700 | [diff] [blame] | 124 | if err != nil { |
| 125 | return "", err |
| 126 | } |
| 127 | |
| 128 | return newServer.Id, nil |
Samuel A. Falvo II | 8935ca3 | 2013-07-25 21:27:06 -0700 | [diff] [blame] | 129 | } |
Samuel A. Falvo II | 8512e9a | 2013-07-26 22:53:29 -0700 | [diff] [blame] | 130 | |
| 131 | // findAlternativeFlavor locates a flavor to resize a server to. It is guaranteed to be different |
| 132 | // than what aSuitableFlavor() returns. If none could be found, this function will panic. |
| 133 | func findAlternativeFlavor() string { |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 134 | return "3" // 1GB image, up from 512MB image |
Samuel A. Falvo II | f722dbf | 2013-07-29 15:44:30 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Samuel A. Falvo II | 414c15c | 2013-08-01 15:16:46 -0700 | [diff] [blame] | 137 | // findAlternativeImage locates an image to resize or rebuild a server with. It is guaranteed to be |
| 138 | // different than what aSuitableImage() returns. If none could be found, this function will panic. |
| 139 | func findAlternativeImage() string { |
| 140 | return "c6f9c411-e708-4952-91e5-62ded5ea4d3e" |
| 141 | } |
| 142 | |
Samuel A. Falvo II | f722dbf | 2013-07-29 15:44:30 -0700 | [diff] [blame] | 143 | // withIdentity authenticates the user against the provider's identity service, and provides an |
| 144 | // accessor for additional services. |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 145 | func withIdentity(ar bool, f func(gophercloud.AccessProvider)) { |
Rafael Garcia | e4a550e | 2013-12-06 17:00:32 -0300 | [diff] [blame] | 146 | provider, username, password, _ := getCredentials() |
Samuel A. Falvo II | f722dbf | 2013-07-29 15:44:30 -0700 | [diff] [blame] | 147 | acc, err := gophercloud.Authenticate( |
| 148 | provider, |
| 149 | gophercloud.AuthOptions{ |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 150 | Username: username, |
| 151 | Password: password, |
Samuel A. Falvo II | 887d780 | 2013-07-29 17:55:37 -0700 | [diff] [blame] | 152 | AllowReauth: ar, |
Samuel A. Falvo II | f722dbf | 2013-07-29 15:44:30 -0700 | [diff] [blame] | 153 | }, |
| 154 | ) |
| 155 | if err != nil { |
| 156 | panic(err) |
| 157 | } |
| 158 | |
| 159 | f(acc) |
| 160 | } |
| 161 | |
| 162 | // withServerApi acquires the cloud servers API. |
| 163 | func withServerApi(acc gophercloud.AccessProvider, f func(gophercloud.CloudServersProvider)) { |
| 164 | api, err := gophercloud.ServersApi(acc, gophercloud.ApiCriteria{ |
| 165 | Name: "cloudServersOpenStack", |
Samuel A. Falvo II | f722dbf | 2013-07-29 15:44:30 -0700 | [diff] [blame] | 166 | VersionId: "2", |
| 167 | UrlChoice: gophercloud.PublicURL, |
| 168 | }) |
| 169 | if err != nil { |
| 170 | panic(err) |
| 171 | } |
| 172 | |
| 173 | f(api) |
| 174 | } |
Samuel A. Falvo II | 002b651 | 2013-07-29 16:30:40 -0700 | [diff] [blame] | 175 | |
| 176 | // waitForServerState polls, every 10 seconds, for a given server to appear in the indicated state. |
| 177 | // This call will block forever if it never appears in the desired state, so if a timeout is required, |
| 178 | // make sure to call this function in a goroutine. |
| 179 | func waitForServerState(api gophercloud.CloudServersProvider, id, state string) error { |
| 180 | for { |
| 181 | s, err := api.ServerById(id) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | if s.Status == state { |
| 186 | return nil |
| 187 | } |
| 188 | time.Sleep(10 * time.Second) |
| 189 | } |
| 190 | panic("Impossible") |
Mark Peek | d27e253 | 2013-08-27 07:58:09 -0700 | [diff] [blame] | 191 | } |
Mark Peek | d2188c4 | 2013-08-27 08:16:28 -0700 | [diff] [blame] | 192 | |
| 193 | // waitForImageState polls, every 10 seconds, for a given image to appear in the indicated state. |
| 194 | // This call will block forever if it never appears in the desired state, so if a timeout is required, |
| 195 | // make sure to call this function in a goroutine. |
| 196 | func waitForImageState(api gophercloud.CloudServersProvider, id, state string) error { |
| 197 | for { |
| 198 | s, err := api.ImageById(id) |
| 199 | if err != nil { |
| 200 | return err |
| 201 | } |
| 202 | if s.Status == state { |
| 203 | return nil |
| 204 | } |
| 205 | time.Sleep(10 * time.Second) |
| 206 | } |
| 207 | panic("Impossible") |
| 208 | } |