diff options
author | Nicolas Lacasse <nlacasse@google.com> | 2018-11-20 15:09:04 -0800 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-11-20 15:10:18 -0800 |
commit | f894610c572976026f4cf6841f4095718827e4f8 (patch) | |
tree | 2da8b097e5485dc5df91ef63b860da260e3fab41 /runsc/test/testutil/testutil.go | |
parent | 8b314b0bf402da58f90ccaac852a880d375f0885 (diff) |
Use math.Rand to generate a random test container id.
We were relying on time.UnixNano, but that was causing collisions.
Now we generate 20 bytes of entropy from rand.Read, and base32-encode it to get
a valid container id.
PiperOrigin-RevId: 222313867
Change-Id: Iaeea9b9582d36de55f9f02f55de6a5de3f739371
Diffstat (limited to 'runsc/test/testutil/testutil.go')
-rw-r--r-- | runsc/test/testutil/testutil.go | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/runsc/test/testutil/testutil.go b/runsc/test/testutil/testutil.go index 162ffe09f..b8f981053 100644 --- a/runsc/test/testutil/testutil.go +++ b/runsc/test/testutil/testutil.go @@ -18,10 +18,12 @@ package testutil import ( "bufio" "context" + "encoding/base32" "encoding/json" "fmt" "io" "io/ioutil" + "math/rand" "net/http" "os" "os/exec" @@ -41,6 +43,10 @@ import ( "gvisor.googlesource.com/gvisor/runsc/specutils" ) +func init() { + rand.Seed(time.Now().UnixNano()) +} + // RaceEnabled is set to true if it was built with '--race' option. var RaceEnabled = false @@ -220,7 +226,15 @@ func writeSpec(dir string, spec *specs.Spec) error { // name, sometimes between test runs the socket does not get cleaned up quickly // enough, causing container creation to fail. func UniqueContainerID() string { - return fmt.Sprintf("test-container-%d", time.Now().UnixNano()) + // Read 20 random bytes. + b := make([]byte, 20) + // "[Read] always returns len(p) and a nil error." --godoc + if _, err := rand.Read(b); err != nil { + panic("rand.Read failed: " + err.Error()) + } + // base32 encode the random bytes, so that the name is a valid + // container id and can be used as a socket name in the filesystem. + return fmt.Sprintf("test-container-%s", base32.StdEncoding.EncodeToString(b)) } // Copy copies file from src to dst. |