Browse code

Merge pull request #6405 from gsalgado/migrate-test-getcontainersbyname

Convert TestGetContainersByName into a unit test

Michael Crosby authored on 2014/06/18 06:01:42
Showing 2 changed files
... ...
@@ -114,6 +114,43 @@ func TestGetInfo(t *testing.T) {
114 114
 	}
115 115
 }
116 116
 
117
+func TestGetContainersByName(t *testing.T) {
118
+	eng := engine.New()
119
+	name := "container_name"
120
+	var called bool
121
+	eng.Register("container_inspect", func(job *engine.Job) engine.Status {
122
+		called = true
123
+		if job.Args[0] != name {
124
+			t.Fatalf("name != '%s': %#v", name, job.Args[0])
125
+		}
126
+		if api.APIVERSION.LessThan("1.12") && !job.GetenvBool("dirty") {
127
+			t.Fatal("dirty env variable not set")
128
+		} else if api.APIVERSION.GreaterThanOrEqualTo("1.12") && job.GetenvBool("dirty") {
129
+			t.Fatal("dirty env variable set when it shouldn't")
130
+		}
131
+		v := &engine.Env{}
132
+		v.SetBool("dirty", true)
133
+		if _, err := v.WriteTo(job.Stdout); err != nil {
134
+			return job.Error(err)
135
+		}
136
+		return engine.StatusOK
137
+	})
138
+	r := serveRequest("GET", "/containers/"+name+"/json", nil, eng, t)
139
+	if !called {
140
+		t.Fatal("handler was not called")
141
+	}
142
+	if r.HeaderMap.Get("Content-Type") != "application/json" {
143
+		t.Fatalf("%#v\n", r)
144
+	}
145
+	var stdoutJson interface{}
146
+	if err := json.Unmarshal(r.Body.Bytes(), &stdoutJson); err != nil {
147
+		t.Fatalf("%#v", err)
148
+	}
149
+	if stdoutJson.(map[string]interface{})["dirty"].(float64) != 1 {
150
+		t.Fatalf("%#v", stdoutJson)
151
+	}
152
+}
153
+
117 154
 func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
118 155
 	r := httptest.NewRecorder()
119 156
 	req, err := http.NewRequest(method, target, body)
... ...
@@ -16,7 +16,6 @@ import (
16 16
 
17 17
 	"github.com/dotcloud/docker/api"
18 18
 	"github.com/dotcloud/docker/api/server"
19
-	"github.com/dotcloud/docker/daemon"
20 19
 	"github.com/dotcloud/docker/engine"
21 20
 	"github.com/dotcloud/docker/image"
22 21
 	"github.com/dotcloud/docker/runconfig"
... ...
@@ -502,37 +501,6 @@ func TestGetContainersTop(t *testing.T) {
502 502
 	}
503 503
 }
504 504
 
505
-func TestGetContainersByName(t *testing.T) {
506
-	eng := NewTestEngine(t)
507
-	defer mkDaemonFromEngine(eng, t).Nuke()
508
-
509
-	// Create a container and remove a file
510
-	containerID := createTestContainer(eng,
511
-		&runconfig.Config{
512
-			Image: unitTestImageID,
513
-			Cmd:   []string{"echo", "test"},
514
-		},
515
-		t,
516
-	)
517
-
518
-	r := httptest.NewRecorder()
519
-	req, err := http.NewRequest("GET", "/containers/"+containerID+"/json", nil)
520
-	if err != nil {
521
-		t.Fatal(err)
522
-	}
523
-	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
524
-		t.Fatal(err)
525
-	}
526
-	assertHttpNotError(r, t)
527
-	outContainer := &daemon.Container{}
528
-	if err := json.Unmarshal(r.Body.Bytes(), outContainer); err != nil {
529
-		t.Fatal(err)
530
-	}
531
-	if outContainer.ID != containerID {
532
-		t.Fatalf("Wrong containers retrieved. Expected %s, received %s", containerID, outContainer.ID)
533
-	}
534
-}
535
-
536 505
 func TestPostCommit(t *testing.T) {
537 506
 	eng := NewTestEngine(t)
538 507
 	defer mkDaemonFromEngine(eng, t).Nuke()