Browse code

Engine: basic testing harness

Solomon Hykes authored on 2013/10/23 17:35:26
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,29 @@
0
+package engine
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+func TestNewJob(t *testing.T) {
7
+	job := mkJob(t, "dummy", "--level=awesome")
8
+	if job.Name != "dummy" {
9
+		t.Fatalf("Wrong job name: %s", job.Name)
10
+	}
11
+	if len(job.Args) != 1 {
12
+		t.Fatalf("Wrong number of job arguments: %d", len(job.Args))
13
+	}
14
+	if job.Args[0] != "--level=awesome" {
15
+		t.Fatalf("Wrong job arguments: %s", job.Args[0])
16
+	}
17
+}
18
+
19
+func TestSetenv(t *testing.T) {
20
+	job := mkJob(t, "dummy")
21
+	job.Setenv("foo", "bar")
22
+	if val := job.Getenv("foo"); val != "bar" {
23
+		t.Fatalf("Getenv returns incorrect value: %s", val)
24
+	}
25
+	if val := job.Getenv("nonexistent"); val != "" {
26
+		t.Fatalf("Getenv returns incorrect value: %s", val)
27
+	}
28
+}
0 29
new file mode 100644
... ...
@@ -0,0 +1,46 @@
0
+package engine
1
+
2
+import (
3
+	"testing"
4
+	"runtime"
5
+	"strings"
6
+	"fmt"
7
+	"io/ioutil"
8
+	"github.com/dotcloud/docker/utils"
9
+)
10
+
11
+var globalTestID string
12
+
13
+func init() {
14
+	Register("dummy", func(job *Job) string { return ""; })
15
+}
16
+
17
+func mkEngine(t *testing.T) *Engine {
18
+	// Use the caller function name as a prefix.
19
+	// This helps trace temp directories back to their test.
20
+	pc, _, _, _ := runtime.Caller(1)
21
+	callerLongName := runtime.FuncForPC(pc).Name()
22
+	parts := strings.Split(callerLongName, ".")
23
+	callerShortName := parts[len(parts)-1]
24
+	if globalTestID == "" {
25
+		globalTestID = utils.RandomString()[:4]
26
+	}
27
+	prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, callerShortName)
28
+	root, err := ioutil.TempDir("", prefix)
29
+	if err != nil {
30
+		t.Fatal(err)
31
+	}
32
+	eng, err := New(root)
33
+	if err != nil {
34
+		t.Fatal(err)
35
+	}
36
+	return eng
37
+}
38
+
39
+func mkJob(t *testing.T, name string, args ...string) *Job {
40
+	job, err := mkEngine(t).Job(name, args...)
41
+	if err != nil {
42
+		t.Fatal(err)
43
+	}
44
+	return job
45
+}
0 46
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+package utils
1
+
2
+import (
3
+	"io"
4
+	"crypto/rand"
5
+	"encoding/hex"
6
+)
7
+
8
+func RandomString() string {
9
+	id := make([]byte, 32)
10
+	_, err := io.ReadFull(rand.Reader, id)
11
+	if err != nil {
12
+		panic(err) // This shouldn't happen
13
+	}
14
+	return hex.EncodeToString(id)
15
+}