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" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 8 | "time" |
| 9 | ) |
| 10 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 11 | // ErrTimeout is returned if WaitFor takes longer than 300 second to happen. |
| 12 | var ErrTimeout = errors.New("Timed out") |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 13 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 14 | // WaitFor polls a predicate function once per second to wait for a certain state to arrive. |
| 15 | func WaitFor(predicate func() (bool, error)) error { |
| 16 | for i := 0; i < 300; i++ { |
| 17 | time.Sleep(1 * time.Second) |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 18 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 19 | satisfied, err := predicate() |
| 20 | if err != nil { |
| 21 | return err |
| 22 | } |
| 23 | if satisfied { |
| 24 | return nil |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 25 | } |
| 26 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 27 | return ErrTimeout |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 30 | // 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] | 31 | func MakeNewPassword(oldPass string) string { |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 32 | randomPassword := RandomString("", 16) |
| 33 | for randomPassword == oldPass { |
| 34 | randomPassword = RandomString("", 16) |
| 35 | } |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 36 | return randomPassword |
| 37 | } |
| 38 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 39 | // RandomString generates a string of given length, but random content. |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 40 | // All content will be within the ASCII graphic character set. |
| 41 | // (Implementation from Even Shaw's contribution on |
| 42 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
| 43 | func RandomString(prefix string, n int) string { |
| 44 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 45 | var bytes = make([]byte, n) |
| 46 | rand.Read(bytes) |
| 47 | for i, b := range bytes { |
| 48 | bytes[i] = alphanum[b%byte(len(alphanum))] |
| 49 | } |
| 50 | return prefix + string(bytes) |
| 51 | } |