1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Separate the GenerateRandomAlphaOnlyString function from stringutils

Signed-off-by: chaowang <chaowang@localhost.localdomain>
This commit is contained in:
chaowang 2017-10-28 08:28:19 +08:00
parent bf1376f44a
commit 7c35a24182
9 changed files with 58 additions and 30 deletions

View file

@ -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)

View file

@ -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",

View file

@ -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)

View file

@ -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}

View file

@ -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 \
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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" +

View file

@ -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)
}