mirror of https://github.com/containers/podman.git
Fix rand.Seed() deprecation in golang 1.20
Ref: https://pkg.go.dev/math/rand@go1.20#Seed Note: For `runtime_test.go`, this test-case was never actually doing what appears as it's intent . Fixing it to work as intended would be require incredibly libpod-invasive changes. Do the least-worse thing and simply confirm that consecutive generated names are different. Signed-off-by: Chris Evich <cevich@redhat.com>
This commit is contained in:
parent
0277adf693
commit
221e3023f6
|
@ -5,7 +5,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -118,13 +117,6 @@ type Runtime struct {
|
||||||
secretsManager *secrets.SecretsManager
|
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.
|
// 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
|
// containers/image uses XDG_RUNTIME_DIR to locate the auth file, XDG_CONFIG_HOME is
|
||||||
// use for the containers.conf configuration file.
|
// use for the containers.conf configuration file.
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
// In go 1.20 and later, the global RNG is automatically initialized.
|
||||||
|
// Ref: https://pkg.go.dev/math/rand@go1.20#Seed
|
||||||
|
//go:build !go1.20
|
||||||
|
// +build !go1.20
|
||||||
|
|
||||||
|
package libpod
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -19,10 +18,8 @@ func Test_generateName(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that (*Runtime).generateName returns different names
|
// Test that (*Runtime).generateName returns different names
|
||||||
// if called twice, even if the global RNG has the default
|
// if called twice.
|
||||||
// seed.
|
|
||||||
n1, _ := r.generateName()
|
n1, _ := r.generateName()
|
||||||
rand.Seed(1)
|
|
||||||
n2, _ := r.generateName()
|
n2, _ := r.generateName()
|
||||||
assert.NotEqual(t, n1, n2)
|
assert.NotEqual(t, n1, n2)
|
||||||
}
|
}
|
||||||
|
|
|
@ -430,11 +430,11 @@ func GetPortLock(port string) *lockfile.LockFile {
|
||||||
// collisions during parallel tests
|
// collisions during parallel tests
|
||||||
func GetRandomIPAddress() string {
|
func GetRandomIPAddress() string {
|
||||||
// To avoid IP collisions of initialize random seed for random IP addresses
|
// To avoid IP collisions of initialize random seed for random IP addresses
|
||||||
rand.Seed(time.Now().UnixNano())
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
// Add GinkgoParallelProcess() on top of the IP address
|
// Add GinkgoParallelProcess() on top of the IP address
|
||||||
// in case of the same random seed
|
// in case of the same random seed
|
||||||
ip3 := strconv.Itoa(rand.Intn(230) + GinkgoParallelProcess())
|
ip3 := strconv.Itoa(rng.Intn(230) + GinkgoParallelProcess())
|
||||||
ip4 := strconv.Itoa(rand.Intn(230) + GinkgoParallelProcess())
|
ip4 := strconv.Itoa(rng.Intn(230) + GinkgoParallelProcess())
|
||||||
return "10.88." + ip3 + "." + ip4
|
return "10.88." + ip3 + "." + ip4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue