blob: d2fd298d38b005695a9d09019ebe79420beef1a1 [file] [log] [blame]
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07001package tools
2
3import (
4 "crypto/rand"
Joe Topjian66a046c2017-01-19 22:07:26 -07005 "encoding/json"
Ash Wilsonfd566482014-09-23 15:47:35 -04006 "errors"
Jamie Hannaford930df422014-11-24 14:39:08 +01007 mrand "math/rand"
Joe Topjian66a046c2017-01-19 22:07:26 -07008 "testing"
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07009 "time"
10)
11
Ash Wilsonfd566482014-09-23 15:47:35 -040012// ErrTimeout is returned if WaitFor takes longer than 300 second to happen.
13var ErrTimeout = errors.New("Timed out")
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070014
Ash Wilsonfd566482014-09-23 15:47:35 -040015// WaitFor polls a predicate function once per second to wait for a certain state to arrive.
16func WaitFor(predicate func() (bool, error)) error {
17 for i := 0; i < 300; i++ {
18 time.Sleep(1 * time.Second)
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070019
Ash Wilsonfd566482014-09-23 15:47:35 -040020 satisfied, err := predicate()
21 if err != nil {
22 return err
23 }
24 if satisfied {
25 return nil
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070026 }
27 }
Ash Wilsonfd566482014-09-23 15:47:35 -040028 return ErrTimeout
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070029}
30
Ash Wilsonfd566482014-09-23 15:47:35 -040031// MakeNewPassword generates a new string that's guaranteed to be different than the given one.
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070032func MakeNewPassword(oldPass string) string {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070033 randomPassword := RandomString("", 16)
34 for randomPassword == oldPass {
35 randomPassword = RandomString("", 16)
36 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070037 return randomPassword
38}
39
Ash Wilsonfd566482014-09-23 15:47:35 -040040// RandomString generates a string of given length, but random content.
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070041// 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).
44func 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 Wilson20b4e872014-10-22 17:39:21 -040053
Jamie Hannaford930df422014-11-24 14:39:08 +010054// RandomInt will return a random integer between a specified range.
55func RandomInt(min, max int) int {
56 mrand.Seed(time.Now().Unix())
57 return mrand.Intn(max-min) + min
58}
59
Ash Wilson20b4e872014-10-22 17:39:21 -040060// Elide returns the first bit of its input string with a suffix of "..." if it's longer than
61// a comfortable 40 characters.
62func Elide(value string) string {
63 if len(value) > 40 {
64 return value[0:37] + "..."
65 }
66 return value
67}
Joe Topjian66a046c2017-01-19 22:07:26 -070068
69// PrintResource returns a resource as a readable structure
70func PrintResource(t *testing.T, resource interface{}) {
71 b, _ := json.MarshalIndent(resource, "", " ")
72 t.Logf(string(b))
73}