Browse code

golint: Lint pkg/namesgenerator

Also addded a couple more tests

Updates #14756

Signed-off-by: Dave Tucker <dt@docker.com>

Dave Tucker authored on 2015/07/21 23:06:31
Showing 2 changed files
... ...
@@ -358,6 +358,9 @@ var (
358 358
 	rnd = rand.New(random.NewSource())
359 359
 )
360 360
 
361
+// GetRandomName generates a random name from the list of adjectives and surnames in this package
362
+// formatted as "adjective_surname". For example 'focused_turing'. If retry is non-zero, a random
363
+// integer between 0 and 10 will be added to the end of the name, e.g `focused_turing3`
361 364
 func GetRandomName(retry int) string {
362 365
 begin:
363 366
 	name := fmt.Sprintf("%s_%s", left[rnd.Intn(len(left))], right[rnd.Intn(len(right))])
... ...
@@ -1,6 +1,7 @@
1 1
 package namesgenerator
2 2
 
3 3
 import (
4
+	"strings"
4 5
 	"testing"
5 6
 )
6 7
 
... ...
@@ -12,6 +13,27 @@ func TestGenerateAwesomeNames(t *testing.T) {
12 12
 	}
13 13
 }
14 14
 
15
+func TestNameFormat(t *testing.T) {
16
+	name := GetRandomName(0)
17
+	if !strings.Contains(name, "_") {
18
+		t.Fatalf("Generated name does not contain an underscore")
19
+	}
20
+	if strings.ContainsAny(name, "0123456789") {
21
+		t.Fatalf("Generated name contains numbers!")
22
+	}
23
+}
24
+
25
+func TestNameRetries(t *testing.T) {
26
+	name := GetRandomName(1)
27
+	if !strings.Contains(name, "_") {
28
+		t.Fatalf("Generated name does not contain an underscore")
29
+	}
30
+	if !strings.ContainsAny(name, "0123456789") {
31
+		t.Fatalf("Generated name doesn't contain a number")
32
+	}
33
+
34
+}
35
+
15 36
 // To be awesome, a container name must involve cool inventors, be easy to remember,
16 37
 // be at least mildly funny, and always be politically correct for enterprise adoption.
17 38
 func isAwesome(name string) bool {