Separate the GenerateRandomAlphaOnlyString function from stringutils
Signed-off-by: chaowang <chaowang@localhost.localdomain>
diff --git a/integration-cli/cli/build/fakestorage/storage.go b/integration-cli/cli/build/fakestorage/storage.go
index 25cd872..eb03636 100644
--- a/integration-cli/cli/build/fakestorage/storage.go
+++ b/integration-cli/cli/build/fakestorage/storage.go
@@ -14,7 +14,7 @@
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/request"
"github.com/docker/docker/internal/test/environment"
- "github.com/docker/docker/pkg/stringutils"
+ "github.com/docker/docker/internal/testutil"
"github.com/stretchr/testify/require"
)
@@ -124,8 +124,8 @@
func newRemoteFileServer(t testingT, ctx *fakecontext.Fake) *remoteFileServer {
var (
- image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
- container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
+ image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
+ container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
)
ensureHTTPServerImage(t)
diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go
index f6ab192..89e62c1 100644
--- a/integration-cli/docker_cli_build_test.go
+++ b/integration-cli/docker_cli_build_test.go
@@ -23,8 +23,8 @@
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
"github.com/docker/docker/integration-cli/cli/build/fakegit"
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
+ "github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/archive"
- "github.com/docker/docker/pkg/stringutils"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
digest "github.com/opencontainers/go-digest"
@@ -3185,7 +3185,7 @@
// FIXME(vdemeester) should be a unit test
func (s *DockerSuite) TestBuildInvalidTag(c *check.C) {
- name := "abcd:" + stringutils.GenerateRandomAlphaOnlyString(200)
+ name := "abcd:" + testutil.GenerateRandomAlphaOnlyString(200)
buildImage(name, build.WithDockerfile("FROM "+minimalBaseImage()+"\nMAINTAINER quux\n")).Assert(c, icmd.Expected{
ExitCode: 125,
Err: "invalid reference format",
diff --git a/integration-cli/docker_cli_run_test.go b/integration-cli/docker_cli_run_test.go
index 8198fde..eb01e34 100644
--- a/integration-cli/docker_cli_run_test.go
+++ b/integration-cli/docker_cli_run_test.go
@@ -26,10 +26,10 @@
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
+ "github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/mount"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/stringid"
- "github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/runconfig"
"github.com/docker/go-connections/nat"
"github.com/docker/libnetwork/resolvconf"
@@ -1828,7 +1828,7 @@
}
func eqToBaseDiff(out string, c *check.C) bool {
- name := "eqToBaseDiff" + stringutils.GenerateRandomAlphaOnlyString(32)
+ name := "eqToBaseDiff" + testutil.GenerateRandomAlphaOnlyString(32)
dockerCmd(c, "run", "--name", name, "busybox", "echo", "hello")
cID := getIDByName(c, name)
baseDiff, _ := dockerCmd(c, "diff", cID)
diff --git a/integration-cli/docker_cli_tag_test.go b/integration-cli/docker_cli_tag_test.go
index 907977f..ee94a9b 100644
--- a/integration-cli/docker_cli_tag_test.go
+++ b/integration-cli/docker_cli_tag_test.go
@@ -6,8 +6,8 @@
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli/build"
+ "github.com/docker/docker/internal/testutil"
"github.com/docker/docker/pkg/stringid"
- "github.com/docker/docker/pkg/stringutils"
"github.com/go-check/check"
)
@@ -34,7 +34,7 @@
// ensure we don't allow the use of invalid tags; these tag operations should fail
func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
- longTag := stringutils.GenerateRandomAlphaOnlyString(121)
+ longTag := testutil.GenerateRandomAlphaOnlyString(121)
invalidTags := []string{"repo:fo$z$", "repo:Foo@3cc", "repo:Foo$3", "repo:Foo*3", "repo:Fo^3", "repo:Foo!3", "repo:%goodbye", "repo:#hashtagit", "repo:F)xcz(", "repo:-foo", "repo:..", longTag}
diff --git a/integration-cli/utils_test.go b/integration-cli/utils_test.go
index d176c7f..1146e1b 100644
--- a/integration-cli/utils_test.go
+++ b/integration-cli/utils_test.go
@@ -7,7 +7,7 @@
"path/filepath"
"strings"
- "github.com/docker/docker/pkg/stringutils"
+ "github.com/docker/docker/internal/testutil"
"github.com/go-check/check"
"github.com/gotestyourself/gotestyourself/icmd"
"github.com/pkg/errors"
@@ -60,7 +60,7 @@
if platform == "windows" {
tmp = os.Getenv("TEMP")
}
- path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
+ path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, testutil.GenerateRandomAlphaOnlyString(10)))
if platform == "windows" {
return filepath.FromSlash(path) // Using \
}
diff --git a/internal/testutil/stringutils.go b/internal/testutil/stringutils.go
new file mode 100644
index 0000000..76cf8d8
--- /dev/null
+++ b/internal/testutil/stringutils.go
@@ -0,0 +1,14 @@
+package testutil
+
+import "math/rand"
+
+// GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
+func GenerateRandomAlphaOnlyString(n int) string {
+ // make a really long string
+ letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
+ b := make([]byte, n)
+ for i := range b {
+ b[i] = letters[rand.Intn(len(letters))]
+ }
+ return string(b)
+}
diff --git a/internal/testutil/stringutils_test.go b/internal/testutil/stringutils_test.go
new file mode 100644
index 0000000..2985a06
--- /dev/null
+++ b/internal/testutil/stringutils_test.go
@@ -0,0 +1,33 @@
+package testutil
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func testLengthHelper(generator func(int) string, t *testing.T) {
+ expectedLength := 20
+ s := generator(expectedLength)
+ assert.Equal(t, expectedLength, len(s))
+}
+
+func testUniquenessHelper(generator func(int) string, t *testing.T) {
+ repeats := 25
+ set := make(map[string]struct{}, repeats)
+ for i := 0; i < repeats; i = i + 1 {
+ str := generator(64)
+ assert.Equal(t, 64, len(str))
+ _, ok := set[str]
+ assert.False(t, ok, "Random number is repeated")
+ set[str] = struct{}{}
+ }
+}
+
+func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
+ testLengthHelper(GenerateRandomAlphaOnlyString, t)
+}
+
+func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
+ testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
+}
diff --git a/pkg/stringutils/stringutils.go b/pkg/stringutils/stringutils.go
index 8c4c398..b294de2 100644
--- a/pkg/stringutils/stringutils.go
+++ b/pkg/stringutils/stringutils.go
@@ -7,17 +7,6 @@
"strings"
)
-// GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
-func GenerateRandomAlphaOnlyString(n int) string {
- // make a really long string
- letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
- b := make([]byte, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
-}
-
// GenerateRandomASCIIString generates an ASCII random string with length n.
func GenerateRandomASCIIString(n int) string {
chars := "abcdefghijklmnopqrstuvwxyz" +
diff --git a/pkg/stringutils/stringutils_test.go b/pkg/stringutils/stringutils_test.go
index 8af2bdc..15b3cf8 100644
--- a/pkg/stringutils/stringutils_test.go
+++ b/pkg/stringutils/stringutils_test.go
@@ -34,14 +34,6 @@
return true
}
-func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
- testLengthHelper(GenerateRandomAlphaOnlyString, t)
-}
-
-func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
- testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
-}
-
func TestGenerateRandomAsciiStringLength(t *testing.T) {
testLengthHelper(GenerateRandomASCIIString, t)
}