aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2022-09-02 21:00:35 +0200
committerGitHub <noreply@github.com>2022-09-02 21:00:35 +0200
commit2a7a6bd9a4bf642a594ec59a3dcbad9c96bab1ab (patch)
tree9dd92517284ad24b2f740ab7113ab0583c05a9d8
parent9476b7831690006a384a791d3ee1178d04fc6311 (diff)
parentb667d7340c1717720216ebdb1c62052006e7ac5f (diff)
downloadpodman-2a7a6bd9a4bf642a594ec59a3dcbad9c96bab1ab.tar.gz
podman-2a7a6bd9a4bf642a594ec59a3dcbad9c96bab1ab.tar.bz2
podman-2a7a6bd9a4bf642a594ec59a3dcbad9c96bab1ab.zip
Merge pull request #15581 from dfr/random-names
libpod: Ensure that generated container names are random
-rw-r--r--libpod/runtime.go8
-rw-r--r--libpod/runtime_test.go28
2 files changed, 36 insertions, 0 deletions
diff --git a/libpod/runtime.go b/libpod/runtime.go
index 9b97fd724..1503b2344 100644
--- a/libpod/runtime.go
+++ b/libpod/runtime.go
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
+ "math/rand"
"os"
"path/filepath"
"strings"
@@ -112,6 +113,13 @@ type Runtime struct {
secretsManager *secrets.SecretsManager
}
+func init() {
+ // generateName calls namesgenerator.GetRandomName which the
+ // global RNG from math/rand. Seed it here to make sure we
+ // don't get the same name every time.
+ rand.Seed(time.Now().UnixNano())
+}
+
// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set.
// containers/image uses XDG_RUNTIME_DIR to locate the auth file, XDG_CONFIG_HOME is
// use for the containers.conf configuration file.
diff --git a/libpod/runtime_test.go b/libpod/runtime_test.go
new file mode 100644
index 000000000..2e16c7fcd
--- /dev/null
+++ b/libpod/runtime_test.go
@@ -0,0 +1,28 @@
+package libpod
+
+import (
+ "math/rand"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func Test_generateName(t *testing.T) {
+ state, path, _, err := getEmptyBoltState()
+ assert.NoError(t, err)
+ defer os.RemoveAll(path)
+ defer state.Close()
+
+ r := &Runtime{
+ state: state,
+ }
+
+ // Test that (*Runtime).generateName returns different names
+ // if called twice, even if the global RNG has the default
+ // seed.
+ n1, _ := r.generateName()
+ rand.Seed(1)
+ n2, _ := r.generateName()
+ assert.NotEqual(t, n1, n2)
+}