Browse code

moving random.go from utils

Closes #10962
Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>

Srini Brahmaroutu authored on 2015/02/26 03:27:14
Showing 5 changed files
... ...
@@ -10,8 +10,8 @@ import (
10 10
 	"sync"
11 11
 	"time"
12 12
 
13
+	"github.com/docker/docker/pkg/common"
13 14
 	"github.com/docker/docker/pkg/ioutils"
14
-	"github.com/docker/docker/utils"
15 15
 )
16 16
 
17 17
 // Installer is a standard interface for objects which can "install" themselves
... ...
@@ -77,7 +77,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) {
77 77
 func New() *Engine {
78 78
 	eng := &Engine{
79 79
 		handlers: make(map[string]Handler),
80
-		id:       utils.RandomString(),
80
+		id:       common.RandomString(),
81 81
 		Stdout:   os.Stdout,
82 82
 		Stderr:   os.Stderr,
83 83
 		Stdin:    os.Stdin,
... ...
@@ -36,3 +36,12 @@ func GenerateRandomID() string {
36 36
 		return value
37 37
 	}
38 38
 }
39
+
40
+func RandomString() string {
41
+	id := make([]byte, 32)
42
+
43
+	if _, err := io.ReadFull(rand.Reader, id); err != nil {
44
+		panic(err) // This shouldn't happen
45
+	}
46
+	return hex.EncodeToString(id)
47
+}
39 48
new file mode 100644
... ...
@@ -0,0 +1,59 @@
0
+package common
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+func TestShortenId(t *testing.T) {
7
+	id := GenerateRandomID()
8
+	truncID := TruncateID(id)
9
+	if len(truncID) != 12 {
10
+		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
11
+	}
12
+}
13
+
14
+func TestShortenIdEmpty(t *testing.T) {
15
+	id := ""
16
+	truncID := TruncateID(id)
17
+	if len(truncID) > len(id) {
18
+		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
19
+	}
20
+}
21
+
22
+func TestShortenIdInvalid(t *testing.T) {
23
+	id := "1234"
24
+	truncID := TruncateID(id)
25
+	if len(truncID) != len(id) {
26
+		t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID)
27
+	}
28
+}
29
+
30
+func TestGenerateRandomID(t *testing.T) {
31
+	id := GenerateRandomID()
32
+
33
+	if len(id) != 64 {
34
+		t.Fatalf("Id returned is incorrect: %s", id)
35
+	}
36
+}
37
+
38
+func TestRandomString(t *testing.T) {
39
+	id := RandomString()
40
+	if len(id) != 64 {
41
+		t.Fatalf("Id returned is incorrect: %s", id)
42
+	}
43
+}
44
+
45
+func TestRandomStringUniqueness(t *testing.T) {
46
+	repeats := 25
47
+	set := make(map[string]struct{}, repeats)
48
+	for i := 0; i < repeats; i = i + 1 {
49
+		id := RandomString()
50
+		if len(id) != 64 {
51
+			t.Fatalf("Id returned is incorrect: %s", id)
52
+		}
53
+		if _, ok := set[id]; ok {
54
+			t.Fatalf("Random number is repeated")
55
+		}
56
+		set[id] = struct{}{}
57
+	}
58
+}
0 59
deleted file mode 100644
... ...
@@ -1,16 +0,0 @@
1
-package utils
2
-
3
-import (
4
-	"crypto/rand"
5
-	"encoding/hex"
6
-	"io"
7
-)
8
-
9
-func RandomString() string {
10
-	id := make([]byte, 32)
11
-
12
-	if _, err := io.ReadFull(rand.Reader, id); err != nil {
13
-		panic(err) // This shouldn't happen
14
-	}
15
-	return hex.EncodeToString(id)
16
-}
... ...
@@ -21,6 +21,7 @@ import (
21 21
 	log "github.com/Sirupsen/logrus"
22 22
 	"github.com/docker/docker/autogen/dockerversion"
23 23
 	"github.com/docker/docker/pkg/archive"
24
+	"github.com/docker/docker/pkg/common"
24 25
 	"github.com/docker/docker/pkg/fileutils"
25 26
 	"github.com/docker/docker/pkg/ioutils"
26 27
 )
... ...
@@ -311,7 +312,7 @@ var globalTestID string
311 311
 // new directory.
312 312
 func TestDirectory(templateDir string) (dir string, err error) {
313 313
 	if globalTestID == "" {
314
-		globalTestID = RandomString()[:4]
314
+		globalTestID = common.RandomString()[:4]
315 315
 	}
316 316
 	prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2))
317 317
 	if prefix == "" {