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