From be3cbac37e04a6eca9628b3ddf2845282fdd1dd6 Mon Sep 17 00:00:00 2001 From: chaowang Date: Sat, 28 Oct 2017 08:28:19 +0800 Subject: [PATCH] Separate the GenerateRandomAlphaOnlyString function from stringutils Signed-off-by: chaowang Upstream-commit: 7c35a2418265336a572976e2ced378ef4b6f1666 Component: engine --- .../cli/build/fakestorage/storage.go | 6 ++-- .../integration-cli/docker_cli_build_test.go | 4 +-- .../integration-cli/docker_cli_run_test.go | 4 +-- .../integration-cli/docker_cli_tag_test.go | 4 +-- .../engine/integration-cli/utils_test.go | 4 +-- .../engine/internal/testutil/stringutils.go | 14 ++++++++ .../internal/testutil/stringutils_test.go | 33 +++++++++++++++++++ .../engine/pkg/stringutils/stringutils.go | 11 ------- .../pkg/stringutils/stringutils_test.go | 8 ----- 9 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 components/engine/internal/testutil/stringutils.go create mode 100644 components/engine/internal/testutil/stringutils_test.go diff --git a/components/engine/integration-cli/cli/build/fakestorage/storage.go b/components/engine/integration-cli/cli/build/fakestorage/storage.go index 25cd872f50..eb0363628d 100644 --- a/components/engine/integration-cli/cli/build/fakestorage/storage.go +++ b/components/engine/integration-cli/cli/build/fakestorage/storage.go @@ -14,7 +14,7 @@ import ( "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 (f *remoteFileServer) Close() error { 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/components/engine/integration-cli/docker_cli_build_test.go b/components/engine/integration-cli/docker_cli_build_test.go index f6ab1923b9..89e62c14e0 100644 --- a/components/engine/integration-cli/docker_cli_build_test.go +++ b/components/engine/integration-cli/docker_cli_build_test.go @@ -23,8 +23,8 @@ import ( "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 @@ func (s *DockerSuite) TestBuildOnBuildOutput(c *check.C) { // 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/components/engine/integration-cli/docker_cli_run_test.go b/components/engine/integration-cli/docker_cli_run_test.go index 8198fded72..eb01e34634 100644 --- a/components/engine/integration-cli/docker_cli_run_test.go +++ b/components/engine/integration-cli/docker_cli_run_test.go @@ -26,10 +26,10 @@ import ( "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 testRunWriteSpecialFilesAndNotCommit(c *check.C, name, path string) { } 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/components/engine/integration-cli/docker_cli_tag_test.go b/components/engine/integration-cli/docker_cli_tag_test.go index 907977fc06..ee94a9b14e 100644 --- a/components/engine/integration-cli/docker_cli_tag_test.go +++ b/components/engine/integration-cli/docker_cli_tag_test.go @@ -6,8 +6,8 @@ import ( "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 @@ func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) { // 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/components/engine/integration-cli/utils_test.go b/components/engine/integration-cli/utils_test.go index d176c7f062..1146e1b2fc 100644 --- a/components/engine/integration-cli/utils_test.go +++ b/components/engine/integration-cli/utils_test.go @@ -7,7 +7,7 @@ import ( "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 @@ func RandomTmpDirPath(s string, platform string) string { 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/components/engine/internal/testutil/stringutils.go b/components/engine/internal/testutil/stringutils.go new file mode 100644 index 0000000000..76cf8d86a9 --- /dev/null +++ b/components/engine/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/components/engine/internal/testutil/stringutils_test.go b/components/engine/internal/testutil/stringutils_test.go new file mode 100644 index 0000000000..2985a06553 --- /dev/null +++ b/components/engine/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/components/engine/pkg/stringutils/stringutils.go b/components/engine/pkg/stringutils/stringutils.go index 8c4c39875e..b294de2c23 100644 --- a/components/engine/pkg/stringutils/stringutils.go +++ b/components/engine/pkg/stringutils/stringutils.go @@ -7,17 +7,6 @@ import ( "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/components/engine/pkg/stringutils/stringutils_test.go b/components/engine/pkg/stringutils/stringutils_test.go index 8af2bdcc0b..15b3cf8e86 100644 --- a/components/engine/pkg/stringutils/stringutils_test.go +++ b/components/engine/pkg/stringutils/stringutils_test.go @@ -34,14 +34,6 @@ func isASCII(s string) bool { 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) }