Browse code

Separate the GenerateRandomAlphaOnlyString function from stringutils

Signed-off-by: chaowang <chaowang@localhost.localdomain>

chaowang authored on 2017/10/28 09:28:19
Showing 9 changed files
... ...
@@ -14,7 +14,7 @@ import (
14 14
 	"github.com/docker/docker/integration-cli/cli/build/fakecontext"
15 15
 	"github.com/docker/docker/integration-cli/request"
16 16
 	"github.com/docker/docker/internal/test/environment"
17
-	"github.com/docker/docker/pkg/stringutils"
17
+	"github.com/docker/docker/internal/testutil"
18 18
 	"github.com/stretchr/testify/require"
19 19
 )
20 20
 
... ...
@@ -124,8 +124,8 @@ func (f *remoteFileServer) Close() error {
124 124
 
125 125
 func newRemoteFileServer(t testingT, ctx *fakecontext.Fake) *remoteFileServer {
126 126
 	var (
127
-		image     = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
128
-		container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10)))
127
+		image     = fmt.Sprintf("fileserver-img-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
128
+		container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(testutil.GenerateRandomAlphaOnlyString(10)))
129 129
 	)
130 130
 
131 131
 	ensureHTTPServerImage(t)
... ...
@@ -23,8 +23,8 @@ import (
23 23
 	"github.com/docker/docker/integration-cli/cli/build/fakecontext"
24 24
 	"github.com/docker/docker/integration-cli/cli/build/fakegit"
25 25
 	"github.com/docker/docker/integration-cli/cli/build/fakestorage"
26
+	"github.com/docker/docker/internal/testutil"
26 27
 	"github.com/docker/docker/pkg/archive"
27
-	"github.com/docker/docker/pkg/stringutils"
28 28
 	"github.com/go-check/check"
29 29
 	"github.com/gotestyourself/gotestyourself/icmd"
30 30
 	digest "github.com/opencontainers/go-digest"
... ...
@@ -3185,7 +3185,7 @@ func (s *DockerSuite) TestBuildOnBuildOutput(c *check.C) {
3185 3185
 
3186 3186
 // FIXME(vdemeester) should be a unit test
3187 3187
 func (s *DockerSuite) TestBuildInvalidTag(c *check.C) {
3188
-	name := "abcd:" + stringutils.GenerateRandomAlphaOnlyString(200)
3188
+	name := "abcd:" + testutil.GenerateRandomAlphaOnlyString(200)
3189 3189
 	buildImage(name, build.WithDockerfile("FROM "+minimalBaseImage()+"\nMAINTAINER quux\n")).Assert(c, icmd.Expected{
3190 3190
 		ExitCode: 125,
3191 3191
 		Err:      "invalid reference format",
... ...
@@ -26,10 +26,10 @@ import (
26 26
 	"github.com/docker/docker/integration-cli/cli"
27 27
 	"github.com/docker/docker/integration-cli/cli/build"
28 28
 	"github.com/docker/docker/integration-cli/cli/build/fakecontext"
29
+	"github.com/docker/docker/internal/testutil"
29 30
 	"github.com/docker/docker/pkg/mount"
30 31
 	"github.com/docker/docker/pkg/parsers/kernel"
31 32
 	"github.com/docker/docker/pkg/stringid"
32
-	"github.com/docker/docker/pkg/stringutils"
33 33
 	"github.com/docker/docker/runconfig"
34 34
 	"github.com/docker/go-connections/nat"
35 35
 	"github.com/docker/libnetwork/resolvconf"
... ...
@@ -1828,7 +1828,7 @@ func testRunWriteSpecialFilesAndNotCommit(c *check.C, name, path string) {
1828 1828
 }
1829 1829
 
1830 1830
 func eqToBaseDiff(out string, c *check.C) bool {
1831
-	name := "eqToBaseDiff" + stringutils.GenerateRandomAlphaOnlyString(32)
1831
+	name := "eqToBaseDiff" + testutil.GenerateRandomAlphaOnlyString(32)
1832 1832
 	dockerCmd(c, "run", "--name", name, "busybox", "echo", "hello")
1833 1833
 	cID := getIDByName(c, name)
1834 1834
 	baseDiff, _ := dockerCmd(c, "diff", cID)
... ...
@@ -6,8 +6,8 @@ import (
6 6
 
7 7
 	"github.com/docker/docker/integration-cli/checker"
8 8
 	"github.com/docker/docker/integration-cli/cli/build"
9
+	"github.com/docker/docker/internal/testutil"
9 10
 	"github.com/docker/docker/pkg/stringid"
10
-	"github.com/docker/docker/pkg/stringutils"
11 11
 	"github.com/go-check/check"
12 12
 )
13 13
 
... ...
@@ -34,7 +34,7 @@ func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) {
34 34
 
35 35
 // ensure we don't allow the use of invalid tags; these tag operations should fail
36 36
 func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
37
-	longTag := stringutils.GenerateRandomAlphaOnlyString(121)
37
+	longTag := testutil.GenerateRandomAlphaOnlyString(121)
38 38
 
39 39
 	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}
40 40
 
... ...
@@ -7,7 +7,7 @@ import (
7 7
 	"path/filepath"
8 8
 	"strings"
9 9
 
10
-	"github.com/docker/docker/pkg/stringutils"
10
+	"github.com/docker/docker/internal/testutil"
11 11
 	"github.com/go-check/check"
12 12
 	"github.com/gotestyourself/gotestyourself/icmd"
13 13
 	"github.com/pkg/errors"
... ...
@@ -60,7 +60,7 @@ func RandomTmpDirPath(s string, platform string) string {
60 60
 	if platform == "windows" {
61 61
 		tmp = os.Getenv("TEMP")
62 62
 	}
63
-	path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10)))
63
+	path := filepath.Join(tmp, fmt.Sprintf("%s.%s", s, testutil.GenerateRandomAlphaOnlyString(10)))
64 64
 	if platform == "windows" {
65 65
 		return filepath.FromSlash(path) // Using \
66 66
 	}
67 67
new file mode 100644
... ...
@@ -0,0 +1,14 @@
0
+package testutil
1
+
2
+import "math/rand"
3
+
4
+// GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
5
+func GenerateRandomAlphaOnlyString(n int) string {
6
+	// make a really long string
7
+	letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
8
+	b := make([]byte, n)
9
+	for i := range b {
10
+		b[i] = letters[rand.Intn(len(letters))]
11
+	}
12
+	return string(b)
13
+}
0 14
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+package testutil
1
+
2
+import (
3
+	"testing"
4
+
5
+	"github.com/stretchr/testify/assert"
6
+)
7
+
8
+func testLengthHelper(generator func(int) string, t *testing.T) {
9
+	expectedLength := 20
10
+	s := generator(expectedLength)
11
+	assert.Equal(t, expectedLength, len(s))
12
+}
13
+
14
+func testUniquenessHelper(generator func(int) string, t *testing.T) {
15
+	repeats := 25
16
+	set := make(map[string]struct{}, repeats)
17
+	for i := 0; i < repeats; i = i + 1 {
18
+		str := generator(64)
19
+		assert.Equal(t, 64, len(str))
20
+		_, ok := set[str]
21
+		assert.False(t, ok, "Random number is repeated")
22
+		set[str] = struct{}{}
23
+	}
24
+}
25
+
26
+func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
27
+	testLengthHelper(GenerateRandomAlphaOnlyString, t)
28
+}
29
+
30
+func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
31
+	testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
32
+}
... ...
@@ -7,17 +7,6 @@ import (
7 7
 	"strings"
8 8
 )
9 9
 
10
-// GenerateRandomAlphaOnlyString generates an alphabetical random string with length n.
11
-func GenerateRandomAlphaOnlyString(n int) string {
12
-	// make a really long string
13
-	letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
14
-	b := make([]byte, n)
15
-	for i := range b {
16
-		b[i] = letters[rand.Intn(len(letters))]
17
-	}
18
-	return string(b)
19
-}
20
-
21 10
 // GenerateRandomASCIIString generates an ASCII random string with length n.
22 11
 func GenerateRandomASCIIString(n int) string {
23 12
 	chars := "abcdefghijklmnopqrstuvwxyz" +
... ...
@@ -34,14 +34,6 @@ func isASCII(s string) bool {
34 34
 	return true
35 35
 }
36 36
 
37
-func TestGenerateRandomAlphaOnlyStringLength(t *testing.T) {
38
-	testLengthHelper(GenerateRandomAlphaOnlyString, t)
39
-}
40
-
41
-func TestGenerateRandomAlphaOnlyStringUniqueness(t *testing.T) {
42
-	testUniquenessHelper(GenerateRandomAlphaOnlyString, t)
43
-}
44
-
45 37
 func TestGenerateRandomAsciiStringLength(t *testing.T) {
46 38
 	testLengthHelper(GenerateRandomASCIIString, t)
47 39
 }