fix unit test breakage by adding seed method to util/rand

This commit is contained in:
Mike Danese 2015-07-22 15:01:51 -07:00
parent a558edf036
commit fdb5cefae7
2 changed files with 13 additions and 5 deletions

View File

@ -17,13 +17,13 @@ limitations under the License.
package serviceaccount package serviceaccount
import ( import (
"math/rand"
"reflect" "reflect"
"testing" "testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
utilrand "github.com/GoogleCloudPlatform/kubernetes/pkg/util/rand"
) )
type testGenerator struct { type testGenerator struct {
@ -372,7 +372,7 @@ func TestTokenCreation(t *testing.T) {
for k, tc := range testcases { for k, tc := range testcases {
// Re-seed to reset name generation // Re-seed to reset name generation
rand.Seed(1) utilrand.Seed(1)
generator := &testGenerator{Token: "ABC"} generator := &testGenerator{Token: "ABC"}

View File

@ -26,8 +26,8 @@ import (
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789") var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
var numLetters = len(letters) var numLetters = len(letters)
var rng = struct { var rng = struct {
sync.Mutex
rand *rand.Rand rand *rand.Rand
lock sync.Mutex
}{ }{
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
} }
@ -39,10 +39,18 @@ func String(n int) string {
panic("out-of-bounds value") panic("out-of-bounds value")
} }
b := make([]rune, n) b := make([]rune, n)
rng.lock.Lock() rng.Lock()
defer rng.lock.Unlock() defer rng.Unlock()
for i := range b { for i := range b {
b[i] = letters[rng.rand.Intn(numLetters)] b[i] = letters[rng.rand.Intn(numLetters)]
} }
return string(b) return string(b)
} }
// Seed seeds the rng with the provided seed.
func Seed(seed int64) {
rng.Lock()
defer rng.Unlock()
rng.rand = rand.New(rand.NewSource(seed))
}