mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
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
This commit is contained in:
committed by
Shentubot
parent
8b314b0bf4
commit
f894610c57
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user