Browse code

improve tests on the engine

Victor Vieux authored on 2013/11/19 10:05:05
Showing 5 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+package engine
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+func TestRegister(t *testing.T) {
7
+	if err := Register("dummy1", nil); err != nil {
8
+		t.Fatal(err)
9
+	}
10
+
11
+	if err := Register("dummy1", nil); err == nil {
12
+		t.Fatalf("Expecting error, got none")
13
+	}
14
+
15
+	eng := newTestEngine(t)
16
+
17
+	//Should fail because globan handlers are copied
18
+	//at the engine creation
19
+	if err := eng.Register("dummy1", nil); err == nil {
20
+		t.Fatalf("Expecting error, got none")
21
+	}
22
+
23
+	if err := eng.Register("dummy2", nil); err != nil {
24
+		t.Fatal(err)
25
+	}
26
+
27
+	if err := eng.Register("dummy2", nil); err == nil {
28
+		t.Fatalf("Expecting error, got none")
29
+	}
30
+}
31
+
32
+func TestJob(t *testing.T) {
33
+	eng := newTestEngine(t)
34
+	job1 := eng.Job("dummy1", "--level=awesome")
35
+
36
+	if job1.handler != nil {
37
+		t.Fatalf("job1.handler should be empty")
38
+	}
39
+
40
+	h := func(j *Job) string {
41
+		return j.Name
42
+	}
43
+
44
+	eng.Register("dummy2", h)
45
+	job2 := eng.Job("dummy2", "--level=awesome")
46
+
47
+	if job2.handler == nil {
48
+		t.Fatalf("job2.handler shouldn't be nil")
49
+	}
50
+
51
+	if job2.handler(job2) != job2.Name {
52
+		t.Fatalf("handler dummy2 was not found in job2")
53
+	}
54
+}
... ...
@@ -23,7 +23,101 @@ func TestSetenv(t *testing.T) {
23 23
 	if val := job.Getenv("foo"); val != "bar" {
24 24
 		t.Fatalf("Getenv returns incorrect value: %s", val)
25 25
 	}
26
+
27
+	job.Setenv("bar", "")
28
+	if val := job.Getenv("bar"); val != "" {
29
+		t.Fatalf("Getenv returns incorrect value: %s", val)
30
+	}
26 31
 	if val := job.Getenv("nonexistent"); val != "" {
27 32
 		t.Fatalf("Getenv returns incorrect value: %s", val)
28 33
 	}
29 34
 }
35
+
36
+func TestSetenvBool(t *testing.T) {
37
+	job := mkJob(t, "dummy")
38
+	job.SetenvBool("foo", true)
39
+	if val := job.GetenvBool("foo"); !val {
40
+		t.Fatalf("GetenvBool returns incorrect value: %b", val)
41
+	}
42
+
43
+	job.SetenvBool("bar", false)
44
+	if val := job.GetenvBool("bar"); val {
45
+		t.Fatalf("GetenvBool returns incorrect value: %b", val)
46
+	}
47
+
48
+	if val := job.GetenvBool("nonexistent"); val {
49
+		t.Fatalf("GetenvBool returns incorrect value: %b", val)
50
+	}
51
+}
52
+
53
+func TestSetenvInt(t *testing.T) {
54
+	job := mkJob(t, "dummy")
55
+
56
+	job.SetenvInt("foo", -42)
57
+	if val := job.GetenvInt("foo"); val != -42 {
58
+		t.Fatalf("GetenvInt returns incorrect value: %d", val)
59
+	}
60
+
61
+	job.SetenvInt("bar", 42)
62
+	if val := job.GetenvInt("bar"); val != 42 {
63
+		t.Fatalf("GetenvInt returns incorrect value: %d", val)
64
+	}
65
+	if val := job.GetenvInt("nonexistent"); val != -1 {
66
+		t.Fatalf("GetenvInt returns incorrect value: %d", val)
67
+	}
68
+}
69
+
70
+func TestSetenvList(t *testing.T) {
71
+	job := mkJob(t, "dummy")
72
+
73
+	job.SetenvList("foo", []string{"bar"})
74
+	if val := job.GetenvList("foo"); len(val) != 1 || val[0] != "bar" {
75
+		t.Fatalf("GetenvList returns incorrect value: %v", val)
76
+	}
77
+
78
+	job.SetenvList("bar", nil)
79
+	if val := job.GetenvList("bar"); val != nil {
80
+		t.Fatalf("GetenvList returns incorrect value: %v", val)
81
+	}
82
+	if val := job.GetenvList("nonexistent"); val != nil {
83
+		t.Fatalf("GetenvList returns incorrect value: %v", val)
84
+	}
85
+}
86
+
87
+func TestImportEnv(t *testing.T) {
88
+	type dummy struct {
89
+		DummyInt         int
90
+		DummyStringArray []string
91
+	}
92
+
93
+	job := mkJob(t, "dummy")
94
+	if err := job.ImportEnv(&dummy{42, []string{"foo", "bar"}}); err != nil {
95
+		t.Fatal(err)
96
+	}
97
+
98
+	dmy := dummy{}
99
+	if err := job.ExportEnv(&dmy); err != nil {
100
+		t.Fatal(err)
101
+	}
102
+
103
+	if dmy.DummyInt != 42 {
104
+		t.Fatalf("Expected 42, got %d", dmy.DummyInt)
105
+	}
106
+
107
+	if len(dmy.DummyStringArray) != 2 || dmy.DummyStringArray[0] != "foo" || dmy.DummyStringArray[1] != "bar" {
108
+		t.Fatalf("Expected {foo, bar}, got %v", dmy.DummyStringArray)
109
+	}
110
+
111
+}
112
+
113
+func TestEnviron(t *testing.T) {
114
+	job := mkJob(t, "dummy")
115
+	job.Setenv("foo", "bar")
116
+	val, exists := job.Environ()["foo"]
117
+	if !exists {
118
+		t.Fatalf("foo not found in the environ")
119
+	}
120
+	if val != "bar" {
121
+		t.Fatalf("bar not found in the environ")
122
+	}
123
+}
30 124
new file mode 100644
... ...
@@ -0,0 +1,38 @@
0
+package engine
1
+
2
+import (
3
+	"fmt"
4
+	"github.com/dotcloud/docker/utils"
5
+	"io/ioutil"
6
+	"runtime"
7
+	"strings"
8
+	"testing"
9
+)
10
+
11
+var globalTestID string
12
+
13
+func newTestEngine(t *testing.T) *Engine {
14
+	// Use the caller function name as a prefix.
15
+	// This helps trace temp directories back to their test.
16
+	pc, _, _, _ := runtime.Caller(1)
17
+	callerLongName := runtime.FuncForPC(pc).Name()
18
+	parts := strings.Split(callerLongName, ".")
19
+	callerShortName := parts[len(parts)-1]
20
+	if globalTestID == "" {
21
+		globalTestID = utils.RandomString()[:4]
22
+	}
23
+	prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, callerShortName)
24
+	root, err := ioutil.TempDir("", prefix)
25
+	if err != nil {
26
+		t.Fatal(err)
27
+	}
28
+	eng, err := New(root)
29
+	if err != nil {
30
+		t.Fatal(err)
31
+	}
32
+	return eng
33
+}
34
+
35
+func mkJob(t *testing.T, name string, args ...string) *Job {
36
+	return newTestEngine(t).Job(name, args...)
37
+}
... ...
@@ -205,8 +205,12 @@ func (job *Job) SetenvInt(key string, value int64) {
205 205
 	job.Setenv(key, fmt.Sprintf("%d", value))
206 206
 }
207 207
 
208
+// Returns nil if key not found
208 209
 func (job *Job) GetenvList(key string) []string {
209 210
 	sval := job.Getenv(key)
211
+	if sval == "" {
212
+		return nil
213
+	}
210 214
 	l := make([]string, 0, 1)
211 215
 	if err := json.Unmarshal([]byte(sval), &l); err != nil {
212 216
 		l = append(l, sval)
... ...
@@ -234,7 +238,7 @@ func (job *Job) Setenv(key, value string) {
234 234
 // DecodeEnv decodes `src` as a json dictionary, and adds
235 235
 // each decoded key-value pair to the environment.
236 236
 //
237
-// If `text` cannot be decoded as a json dictionary, an error
237
+// If `src` cannot be decoded as a json dictionary, an error
238 238
 // is returned.
239 239
 func (job *Job) DecodeEnv(src io.Reader) error {
240 240
 	m := make(map[string]interface{})
241 241
deleted file mode 100644
... ...
@@ -1,42 +0,0 @@
1
-package engine
2
-
3
-import (
4
-	"fmt"
5
-	"github.com/dotcloud/docker/utils"
6
-	"io/ioutil"
7
-	"runtime"
8
-	"strings"
9
-	"testing"
10
-)
11
-
12
-var globalTestID string
13
-
14
-func init() {
15
-	Register("dummy", func(job *Job) string { return "" })
16
-}
17
-
18
-func newTestEngine(t *testing.T) *Engine {
19
-	// Use the caller function name as a prefix.
20
-	// This helps trace temp directories back to their test.
21
-	pc, _, _, _ := runtime.Caller(1)
22
-	callerLongName := runtime.FuncForPC(pc).Name()
23
-	parts := strings.Split(callerLongName, ".")
24
-	callerShortName := parts[len(parts)-1]
25
-	if globalTestID == "" {
26
-		globalTestID = utils.RandomString()[:4]
27
-	}
28
-	prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, callerShortName)
29
-	root, err := ioutil.TempDir("", prefix)
30
-	if err != nil {
31
-		t.Fatal(err)
32
-	}
33
-	eng, err := New(root)
34
-	if err != nil {
35
-		t.Fatal(err)
36
-	}
37
-	return eng
38
-}
39
-
40
-func mkJob(t *testing.T, name string, args ...string) *Job {
41
-	return newTestEngine(t).Job(name, args...)
42
-}