Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 1 | package tools |
| 2 | |
| 3 | import ( |
| 4 | "crypto/rand" |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 5 | "encoding/json" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 6 | "errors" |
Jamie Hannaford | 930df42 | 2014-11-24 14:39:08 +0100 | [diff] [blame] | 7 | mrand "math/rand" |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 8 | "testing" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 9 | "time" |
| 10 | ) |
| 11 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 12 | // ErrTimeout is returned if WaitFor takes longer than 300 second to happen. |
| 13 | var ErrTimeout = errors.New("Timed out") |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 14 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 15 | // WaitFor polls a predicate function once per second to wait for a certain state to arrive. |
| 16 | func WaitFor(predicate func() (bool, error)) error { |
| 17 | for i := 0; i < 300; i++ { |
| 18 | time.Sleep(1 * time.Second) |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 19 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 20 | satisfied, err := predicate() |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | if satisfied { |
| 25 | return nil |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 28 | return ErrTimeout |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 31 | // 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] | 32 | func MakeNewPassword(oldPass string) string { |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 33 | randomPassword := RandomString("", 16) |
| 34 | for randomPassword == oldPass { |
| 35 | randomPassword = RandomString("", 16) |
| 36 | } |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 37 | return randomPassword |
| 38 | } |
| 39 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 40 | // RandomString generates a string of given length, but random content. |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 41 | // All content will be within the ASCII graphic character set. |
| 42 | // (Implementation from Even Shaw's contribution on |
| 43 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
| 44 | func RandomString(prefix string, n int) string { |
| 45 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 46 | var bytes = make([]byte, n) |
| 47 | rand.Read(bytes) |
| 48 | for i, b := range bytes { |
| 49 | bytes[i] = alphanum[b%byte(len(alphanum))] |
| 50 | } |
| 51 | return prefix + string(bytes) |
| 52 | } |
Ash Wilson | 20b4e87 | 2014-10-22 17:39:21 -0400 | [diff] [blame] | 53 | |
Jamie Hannaford | 930df42 | 2014-11-24 14:39:08 +0100 | [diff] [blame] | 54 | // RandomInt will return a random integer between a specified range. |
| 55 | func RandomInt(min, max int) int { |
| 56 | mrand.Seed(time.Now().Unix()) |
| 57 | return mrand.Intn(max-min) + min |
| 58 | } |
| 59 | |
Ash Wilson | 20b4e87 | 2014-10-22 17:39:21 -0400 | [diff] [blame] | 60 | // Elide returns the first bit of its input string with a suffix of "..." if it's longer than |
| 61 | // a comfortable 40 characters. |
| 62 | func Elide(value string) string { |
| 63 | if len(value) > 40 { |
| 64 | return value[0:37] + "..." |
| 65 | } |
| 66 | return value |
| 67 | } |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 68 | |
| 69 | // PrintResource returns a resource as a readable structure |
| 70 | func PrintResource(t *testing.T, resource interface{}) { |
| 71 | b, _ := json.MarshalIndent(resource, "", " ") |
| 72 | t.Logf(string(b)) |
| 73 | } |