Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 1 | // +build acceptance |
Ash Wilson | 15e0f27 | 2014-10-21 15:33:02 -0400 | [diff] [blame] | 2 | |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 3 | package tools |
| 4 | |
| 5 | import ( |
| 6 | "crypto/rand" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 7 | "errors" |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame^] | 8 | "os" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 9 | "time" |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame^] | 10 | |
| 11 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 14 | // ErrTimeout is returned if WaitFor takes longer than 300 second to happen. |
| 15 | var ErrTimeout = errors.New("Timed out") |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 16 | |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame^] | 17 | // OnlyRS overrides the default Gophercloud behavior of using OS_-prefixed environment variables |
| 18 | // if RS_ variables aren't present. Otherwise, they'll stomp over each other here in the acceptance |
| 19 | // tests, where you need to have both defined. |
| 20 | func OnlyRS(original gophercloud.AuthOptions) gophercloud.AuthOptions { |
| 21 | if os.Getenv("RS_AUTH_URL") == "" { |
| 22 | original.IdentityEndpoint = "" |
| 23 | } |
| 24 | if os.Getenv("RS_USERNAME") == "" { |
| 25 | original.Username = "" |
| 26 | } |
| 27 | if os.Getenv("RS_PASSWORD") == "" { |
| 28 | original.Password = "" |
| 29 | } |
| 30 | if os.Getenv("RS_API_KEY") == "" { |
| 31 | original.APIKey = "" |
| 32 | } |
| 33 | return original |
| 34 | } |
| 35 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 36 | // WaitFor polls a predicate function once per second to wait for a certain state to arrive. |
| 37 | func WaitFor(predicate func() (bool, error)) error { |
| 38 | for i := 0; i < 300; i++ { |
| 39 | time.Sleep(1 * time.Second) |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 40 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 41 | satisfied, err := predicate() |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | if satisfied { |
| 46 | return nil |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 49 | return ErrTimeout |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 52 | // MakeNewPassword generates a new string that's guaranteed to be different than the given one. |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 53 | func MakeNewPassword(oldPass string) string { |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 54 | randomPassword := RandomString("", 16) |
| 55 | for randomPassword == oldPass { |
| 56 | randomPassword = RandomString("", 16) |
| 57 | } |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 58 | return randomPassword |
| 59 | } |
| 60 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 61 | // RandomString generates a string of given length, but random content. |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 62 | // All content will be within the ASCII graphic character set. |
| 63 | // (Implementation from Even Shaw's contribution on |
| 64 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
| 65 | func RandomString(prefix string, n int) string { |
| 66 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 67 | var bytes = make([]byte, n) |
| 68 | rand.Read(bytes) |
| 69 | for i, b := range bytes { |
| 70 | bytes[i] = alphanum[b%byte(len(alphanum))] |
| 71 | } |
| 72 | return prefix + string(bytes) |
| 73 | } |