Jamie Hannaford | 8ed4fe7 | 2014-11-20 12:01:28 +0100 | [diff] [blame] | 1 | // +build acceptance common |
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" |
Jamie Hannaford | 930df42 | 2014-11-24 14:39:08 +0100 | [diff] [blame] | 8 | mrand "math/rand" |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 9 | "os" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 10 | "time" |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 11 | |
| 12 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 15 | // ErrTimeout is returned if WaitFor takes longer than 300 second to happen. |
| 16 | var ErrTimeout = errors.New("Timed out") |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 17 | |
Ash Wilson | a6ddde7 | 2014-10-22 15:26:42 -0400 | [diff] [blame] | 18 | // OnlyRS overrides the default Gophercloud behavior of using OS_-prefixed environment variables |
| 19 | // if RS_ variables aren't present. Otherwise, they'll stomp over each other here in the acceptance |
| 20 | // tests, where you need to have both defined. |
| 21 | func OnlyRS(original gophercloud.AuthOptions) gophercloud.AuthOptions { |
| 22 | if os.Getenv("RS_AUTH_URL") == "" { |
| 23 | original.IdentityEndpoint = "" |
| 24 | } |
| 25 | if os.Getenv("RS_USERNAME") == "" { |
| 26 | original.Username = "" |
| 27 | } |
| 28 | if os.Getenv("RS_PASSWORD") == "" { |
| 29 | original.Password = "" |
| 30 | } |
| 31 | if os.Getenv("RS_API_KEY") == "" { |
| 32 | original.APIKey = "" |
| 33 | } |
| 34 | return original |
| 35 | } |
| 36 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 37 | // WaitFor polls a predicate function once per second to wait for a certain state to arrive. |
| 38 | func WaitFor(predicate func() (bool, error)) error { |
| 39 | for i := 0; i < 300; i++ { |
| 40 | time.Sleep(1 * time.Second) |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 41 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 42 | satisfied, err := predicate() |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | if satisfied { |
| 47 | return nil |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 50 | return ErrTimeout |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 53 | // 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] | 54 | func MakeNewPassword(oldPass string) string { |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 55 | randomPassword := RandomString("", 16) |
| 56 | for randomPassword == oldPass { |
| 57 | randomPassword = RandomString("", 16) |
| 58 | } |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 59 | return randomPassword |
| 60 | } |
| 61 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 62 | // RandomString generates a string of given length, but random content. |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 63 | // All content will be within the ASCII graphic character set. |
| 64 | // (Implementation from Even Shaw's contribution on |
| 65 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
| 66 | func RandomString(prefix string, n int) string { |
| 67 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 68 | var bytes = make([]byte, n) |
| 69 | rand.Read(bytes) |
| 70 | for i, b := range bytes { |
| 71 | bytes[i] = alphanum[b%byte(len(alphanum))] |
| 72 | } |
| 73 | return prefix + string(bytes) |
| 74 | } |
Ash Wilson | 20b4e87 | 2014-10-22 17:39:21 -0400 | [diff] [blame] | 75 | |
Jamie Hannaford | 930df42 | 2014-11-24 14:39:08 +0100 | [diff] [blame] | 76 | // RandomInt will return a random integer between a specified range. |
| 77 | func RandomInt(min, max int) int { |
| 78 | mrand.Seed(time.Now().Unix()) |
| 79 | return mrand.Intn(max-min) + min |
| 80 | } |
| 81 | |
Ash Wilson | 20b4e87 | 2014-10-22 17:39:21 -0400 | [diff] [blame] | 82 | // Elide returns the first bit of its input string with a suffix of "..." if it's longer than |
| 83 | // a comfortable 40 characters. |
| 84 | func Elide(value string) string { |
| 85 | if len(value) > 40 { |
| 86 | return value[0:37] + "..." |
| 87 | } |
| 88 | return value |
| 89 | } |