mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Fixes #11721 removed GenerateRandomString
Signed-off-by: Peter Esbensen <pkesbensen@gmail.com> gofmt Signed-off-by: Peter Esbensen <pkesbensen@gmail.com>
This commit is contained in:
parent
11287f7714
commit
6896016b7c
4 changed files with 6 additions and 41 deletions
|
@ -11,7 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/ioutils"
|
"github.com/docker/docker/pkg/ioutils"
|
||||||
"github.com/docker/docker/pkg/stringutils"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Installer is a standard interface for objects which can "install" themselves
|
// Installer is a standard interface for objects which can "install" themselves
|
||||||
|
@ -78,7 +78,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) {
|
||||||
func New() *Engine {
|
func New() *Engine {
|
||||||
eng := &Engine{
|
eng := &Engine{
|
||||||
handlers: make(map[string]Handler),
|
handlers: make(map[string]Handler),
|
||||||
id: stringutils.GenerateRandomString(),
|
id: stringid.GenerateRandomID(),
|
||||||
Stdout: os.Stdout,
|
Stdout: os.Stdout,
|
||||||
Stderr: os.Stderr,
|
Stderr: os.Stderr,
|
||||||
Stdin: os.Stdin,
|
Stdin: os.Stdin,
|
||||||
|
|
|
@ -1,23 +1,10 @@
|
||||||
package stringutils
|
package stringutils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
|
||||||
mathrand "math/rand"
|
mathrand "math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Generate 32 chars random string
|
|
||||||
func GenerateRandomString() string {
|
|
||||||
id := make([]byte, 32)
|
|
||||||
|
|
||||||
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
||||||
panic(err) // This shouldn't happen
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate alpha only random stirng with length n
|
// Generate alpha only random stirng with length n
|
||||||
func GenerateRandomAlphaOnlyString(n int) string {
|
func GenerateRandomAlphaOnlyString(n int) string {
|
||||||
// make a really long string
|
// make a really long string
|
||||||
|
|
|
@ -2,28 +2,6 @@ package stringutils
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestRandomString(t *testing.T) {
|
|
||||||
str := GenerateRandomString()
|
|
||||||
if len(str) != 64 {
|
|
||||||
t.Fatalf("Id returned is incorrect: %s", str)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRandomStringUniqueness(t *testing.T) {
|
|
||||||
repeats := 25
|
|
||||||
set := make(map[string]struct{}, repeats)
|
|
||||||
for i := 0; i < repeats; i = i + 1 {
|
|
||||||
str := GenerateRandomString()
|
|
||||||
if len(str) != 64 {
|
|
||||||
t.Fatalf("Id returned is incorrect: %s", str)
|
|
||||||
}
|
|
||||||
if _, ok := set[str]; ok {
|
|
||||||
t.Fatalf("Random number is repeated")
|
|
||||||
}
|
|
||||||
set[str] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testLengthHelper(generator func(int) string, t *testing.T) {
|
func testLengthHelper(generator func(int) string, t *testing.T) {
|
||||||
expectedLength := 20
|
expectedLength := 20
|
||||||
s := generator(expectedLength)
|
s := generator(expectedLength)
|
||||||
|
|
|
@ -24,7 +24,7 @@ import (
|
||||||
"github.com/docker/docker/pkg/fileutils"
|
"github.com/docker/docker/pkg/fileutils"
|
||||||
"github.com/docker/docker/pkg/ioutils"
|
"github.com/docker/docker/pkg/ioutils"
|
||||||
"github.com/docker/docker/pkg/jsonmessage"
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
"github.com/docker/docker/pkg/stringutils"
|
"github.com/docker/docker/pkg/stringid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type KeyValuePair struct {
|
type KeyValuePair struct {
|
||||||
|
@ -313,7 +313,7 @@ var globalTestID string
|
||||||
// new directory.
|
// new directory.
|
||||||
func TestDirectory(templateDir string) (dir string, err error) {
|
func TestDirectory(templateDir string) (dir string, err error) {
|
||||||
if globalTestID == "" {
|
if globalTestID == "" {
|
||||||
globalTestID = stringutils.GenerateRandomString()[:4]
|
globalTestID = stringid.GenerateRandomID()[:4]
|
||||||
}
|
}
|
||||||
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
|
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
|
|
Loading…
Add table
Reference in a new issue