mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
fix unit test breakage by adding seed method to util/rand
This commit is contained in:
parent
a558edf036
commit
fdb5cefae7
@ -17,13 +17,13 @@ limitations under the License.
|
||||
package serviceaccount
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/testclient"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
utilrand "github.com/GoogleCloudPlatform/kubernetes/pkg/util/rand"
|
||||
)
|
||||
|
||||
type testGenerator struct {
|
||||
@ -372,7 +372,7 @@ func TestTokenCreation(t *testing.T) {
|
||||
for k, tc := range testcases {
|
||||
|
||||
// Re-seed to reset name generation
|
||||
rand.Seed(1)
|
||||
utilrand.Seed(1)
|
||||
|
||||
generator := &testGenerator{Token: "ABC"}
|
||||
|
||||
|
@ -26,8 +26,8 @@ import (
|
||||
var letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
|
||||
var numLetters = len(letters)
|
||||
var rng = struct {
|
||||
sync.Mutex
|
||||
rand *rand.Rand
|
||||
lock sync.Mutex
|
||||
}{
|
||||
rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())),
|
||||
}
|
||||
@ -39,10 +39,18 @@ func String(n int) string {
|
||||
panic("out-of-bounds value")
|
||||
}
|
||||
b := make([]rune, n)
|
||||
rng.lock.Lock()
|
||||
defer rng.lock.Unlock()
|
||||
rng.Lock()
|
||||
defer rng.Unlock()
|
||||
for i := range b {
|
||||
b[i] = letters[rng.rand.Intn(numLetters)]
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user