From f894610c572976026f4cf6841f4095718827e4f8 Mon Sep 17 00:00:00 2001 From: Nicolas Lacasse Date: Tue, 20 Nov 2018 15:09:04 -0800 Subject: 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 --- runsc/test/testutil/testutil.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'runsc') 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. -- cgit v1.2.3