blob: b3f3ea73681694dd3a0255740d9780415c32768c [file] [log] [blame]
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07001// +build acceptance
Ash Wilson15e0f272014-10-21 15:33:02 -04002
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07003package tools
4
5import (
6 "crypto/rand"
Ash Wilsonfd566482014-09-23 15:47:35 -04007 "errors"
Ash Wilsona6ddde72014-10-22 15:26:42 -04008 "os"
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07009 "time"
Ash Wilsona6ddde72014-10-22 15:26:42 -040010
11 "github.com/rackspace/gophercloud"
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070012)
13
Ash Wilsonfd566482014-09-23 15:47:35 -040014// ErrTimeout is returned if WaitFor takes longer than 300 second to happen.
15var ErrTimeout = errors.New("Timed out")
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070016
Ash Wilsona6ddde72014-10-22 15:26:42 -040017// OnlyRS overrides the default Gophercloud behavior of using OS_-prefixed environment variables
18// if RS_ variables aren't present. Otherwise, they'll stomp over each other here in the acceptance
19// tests, where you need to have both defined.
20func OnlyRS(original gophercloud.AuthOptions) gophercloud.AuthOptions {
21 if os.Getenv("RS_AUTH_URL") == "" {
22 original.IdentityEndpoint = ""
23 }
24 if os.Getenv("RS_USERNAME") == "" {
25 original.Username = ""
26 }
27 if os.Getenv("RS_PASSWORD") == "" {
28 original.Password = ""
29 }
30 if os.Getenv("RS_API_KEY") == "" {
31 original.APIKey = ""
32 }
33 return original
34}
35
Ash Wilsonfd566482014-09-23 15:47:35 -040036// WaitFor polls a predicate function once per second to wait for a certain state to arrive.
37func WaitFor(predicate func() (bool, error)) error {
38 for i := 0; i < 300; i++ {
39 time.Sleep(1 * time.Second)
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070040
Ash Wilsonfd566482014-09-23 15:47:35 -040041 satisfied, err := predicate()
42 if err != nil {
43 return err
44 }
45 if satisfied {
46 return nil
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070047 }
48 }
Ash Wilsonfd566482014-09-23 15:47:35 -040049 return ErrTimeout
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070050}
51
Ash Wilsonfd566482014-09-23 15:47:35 -040052// MakeNewPassword generates a new string that's guaranteed to be different than the given one.
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070053func MakeNewPassword(oldPass string) string {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070054 randomPassword := RandomString("", 16)
55 for randomPassword == oldPass {
56 randomPassword = RandomString("", 16)
57 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070058 return randomPassword
59}
60
Ash Wilsonfd566482014-09-23 15:47:35 -040061// RandomString generates a string of given length, but random content.
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070062// All content will be within the ASCII graphic character set.
63// (Implementation from Even Shaw's contribution on
64// http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go).
65func RandomString(prefix string, n int) string {
66 const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
67 var bytes = make([]byte, n)
68 rand.Read(bytes)
69 for i, b := range bytes {
70 bytes[i] = alphanum[b%byte(len(alphanum))]
71 }
72 return prefix + string(bytes)
73}