Merge pull request #21710 from hashicorp/jbardin/acctest-random
only seed math/rand once
This commit is contained in:
commit
edd005cdaa
|
@ -16,24 +16,25 @@ import (
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
// Helpers for generating random tidbits for use in identifiers to prevent
|
// Helpers for generating random tidbits for use in identifiers to prevent
|
||||||
// collisions in acceptance tests.
|
// collisions in acceptance tests.
|
||||||
|
|
||||||
// RandInt generates a random integer
|
// RandInt generates a random integer
|
||||||
func RandInt() int {
|
func RandInt() int {
|
||||||
reseed()
|
|
||||||
return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
return rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomWithPrefix is used to generate a unique name with a prefix, for
|
// RandomWithPrefix is used to generate a unique name with a prefix, for
|
||||||
// randomizing names in acceptance tests
|
// randomizing names in acceptance tests
|
||||||
func RandomWithPrefix(name string) string {
|
func RandomWithPrefix(name string) string {
|
||||||
reseed()
|
|
||||||
return fmt.Sprintf("%s-%d", name, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
return fmt.Sprintf("%s-%d", name, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandIntRange(min int, max int) int {
|
func RandIntRange(min int, max int) int {
|
||||||
reseed()
|
|
||||||
source := rand.New(rand.NewSource(time.Now().UnixNano()))
|
source := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
rangeMax := max - min
|
rangeMax := max - min
|
||||||
|
|
||||||
|
@ -48,7 +49,6 @@ func RandString(strlen int) string {
|
||||||
// RandStringFromCharSet generates a random string by selecting characters from
|
// RandStringFromCharSet generates a random string by selecting characters from
|
||||||
// the charset provided
|
// the charset provided
|
||||||
func RandStringFromCharSet(strlen int, charSet string) string {
|
func RandStringFromCharSet(strlen int, charSet string) string {
|
||||||
reseed()
|
|
||||||
result := make([]byte, strlen)
|
result := make([]byte, strlen)
|
||||||
for i := 0; i < strlen; i++ {
|
for i := 0; i < strlen; i++ {
|
||||||
result[i] = charSet[rand.Intn(len(charSet))]
|
result[i] = charSet[rand.Intn(len(charSet))]
|
||||||
|
@ -129,11 +129,6 @@ func pemEncode(b []byte, block string) (string, error) {
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seeds random with current timestamp
|
|
||||||
func reseed() {
|
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// CharSetAlphaNum is the alphanumeric character set for use with
|
// CharSetAlphaNum is the alphanumeric character set for use with
|
||||||
// RandStringFromCharSet
|
// RandStringFromCharSet
|
||||||
|
|
Loading…
Reference in New Issue