Browse Source

Add/improve unit tests

Guillaume J. Charmes 12 năm trước cách đây
mục cha
commit
24816a8b80
2 tập tin đã thay đổi với 284 bổ sung142 xóa
  1. 11 10
      api.go
  2. 273 132
      api_test.go

+ 11 - 10
api.go

@@ -40,10 +40,11 @@ func httpError(w http.ResponseWriter, err error) {
 }
 
 func getAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
-	var out auth.AuthConfig
-	out.Username = srv.runtime.authConfig.Username
-	out.Email = srv.runtime.authConfig.Email
-	b, err := json.Marshal(out)
+	config := &auth.AuthConfig{
+		Username: srv.runtime.authConfig.Username,
+		Email:    srv.runtime.authConfig.Email,
+	}
+	b, err := json.Marshal(config)
 	if err != nil {
 		return nil, err
 	}
@@ -51,8 +52,8 @@ func getAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[strin
 }
 
 func postAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
-	var config auth.AuthConfig
-	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
+	config := &auth.AuthConfig{}
+	if err := json.NewDecoder(r.Body).Decode(config); err != nil {
 		return nil, err
 	}
 
@@ -69,7 +70,7 @@ func postAuth(srv *Server, w http.ResponseWriter, r *http.Request, vars map[stri
 		srv.runtime.authConfig = newAuthConfig
 	}
 	if status != "" {
-		b, err := json.Marshal(ApiAuth{status})
+		b, err := json.Marshal(&ApiAuth{Status: status})
 		if err != nil {
 			return nil, err
 		}
@@ -113,7 +114,7 @@ func getContainersExport(srv *Server, w http.ResponseWriter, r *http.Request, va
 	return nil, nil
 }
 
-func getImages(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
+func getImagesJson(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
 	if err := parseForm(r); err != nil {
 		return nil, err
 	}
@@ -474,7 +475,7 @@ func postContainersWait(srv *Server, w http.ResponseWriter, r *http.Request, var
 	if err != nil {
 		return nil, err
 	}
-	b, err := json.Marshal(ApiWait{status})
+	b, err := json.Marshal(&ApiWait{StatusCode: status})
 	if err != nil {
 		return nil, err
 	}
@@ -551,7 +552,7 @@ func ListenAndServe(addr string, srv *Server, logging bool) error {
 			"/auth":                         getAuth,
 			"/version":                      getVersion,
 			"/info":                         getInfo,
-			"/images/json":                  getImages,
+			"/images/json":                  getImagesJson,
 			"/images/viz":                   getImagesViz,
 			"/images/search":                getImagesSearch,
 			"/images/{name:.*}/history":     getImagesHistory,

+ 273 - 132
api_test.go

@@ -12,7 +12,7 @@ import (
 	"time"
 )
 
-func TestAuth(t *testing.T) {
+func TestGetAuth(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -50,29 +50,39 @@ func TestAuth(t *testing.T) {
 		t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
 	}
 
-	authConfig = &auth.AuthConfig{}
+	if runtime.authConfig.Username != authConfig.Username ||
+		runtime.authConfig.Password != authConfig.Password ||
+		runtime.authConfig.Email != authConfig.Email {
+		t.Fatalf("The auth configuration hasn't been set correctly")
+	}
+}
 
-	req, err = http.NewRequest("GET", "/auth", nil)
+func TestGetVersion(t *testing.T) {
+	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
 	}
+	defer nuke(runtime)
 
-	body, err = getAuth(srv, nil, req, nil)
+	srv := &Server{runtime: runtime}
+
+	body, err := getVersion(srv, nil, nil, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	err = json.Unmarshal(body, authConfig)
+	v := &ApiVersion{}
+
+	err = json.Unmarshal(body, v)
 	if err != nil {
 		t.Fatal(err)
 	}
-
-	if authConfig.Username != "utest" {
-		t.Errorf("Expected username to be utest, %s found", authConfig.Username)
+	if v.Version != VERSION {
+		t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
 	}
 }
 
-func TestVersion(t *testing.T) {
+func TestGetInfo(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -81,27 +91,21 @@ func TestVersion(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
-	body, err := getVersion(srv, nil, nil, nil)
+	body, err := getInfo(srv, nil, nil, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
-
-	v := &ApiVersion{}
-
-	err = json.Unmarshal(body, v)
+	infos := &ApiInfo{}
+	err = json.Unmarshal(body, infos)
 	if err != nil {
 		t.Fatal(err)
 	}
-	if v.Version != VERSION {
-		t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
+	if infos.Version != VERSION {
+		t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
 	}
 }
 
-func TestContainersExport(t *testing.T) {
-	//FIXME: Implement this test
-}
-
-func TestGetImages(t *testing.T) {
+func TestGetImagesJson(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -111,12 +115,12 @@ func TestGetImages(t *testing.T) {
 	srv := &Server{runtime: runtime}
 
 	// FIXME: Do more tests with filter
-	req, err := http.NewRequest("GET", "/images?quiet=0&all=0", nil)
+	req, err := http.NewRequest("GET", "/images/json?quiet=0&all=0", nil)
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	body, err := getImages(srv, nil, req, nil)
+	body, err := getImagesJson(srv, nil, req, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -136,7 +140,12 @@ func TestGetImages(t *testing.T) {
 	}
 }
 
-func TestInfo(t *testing.T) {
+func TestGetImagesViz(t *testing.T) {
+	//FIXME: Implement this test (or remove this endpoint)
+	t.Log("Test on implemented")
+}
+
+func TestGetImagesSearch(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -145,21 +154,28 @@ func TestInfo(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
-	body, err := getInfo(srv, nil, nil, nil)
+	req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
 	if err != nil {
 		t.Fatal(err)
 	}
-	infos := &ApiInfo{}
-	err = json.Unmarshal(body, infos)
+
+	body, err := getImagesSearch(srv, nil, req, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
-	if infos.Version != VERSION {
-		t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
+
+	results := []ApiSearch{}
+
+	err = json.Unmarshal(body, &results)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(results) < 2 {
+		t.Errorf("Excepted at least 2 lines, %d found", len(results))
 	}
 }
 
-func TestHistory(t *testing.T) {
+func TestGetImagesHistory(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -184,7 +200,7 @@ func TestHistory(t *testing.T) {
 	}
 }
 
-func TestImagesSearch(t *testing.T) {
+func TestGetImagesByName(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -193,28 +209,92 @@ func TestImagesSearch(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
-	req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
+	body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	body, err := getImagesSearch(srv, nil, req, nil)
+	img := &Image{}
+
+	err = json.Unmarshal(body, img)
 	if err != nil {
 		t.Fatal(err)
 	}
+	if img.Comment != "Imported from http://get.docker.io/images/busybox" {
+		t.Errorf("Error inspecting image")
+	}
+}
 
-	results := []ApiSearch{}
+func TestGetContainersPs(t *testing.T) {
+	runtime, err := newTestRuntime()
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer nuke(runtime)
 
-	err = json.Unmarshal(body, &results)
+	srv := &Server{runtime: runtime}
+
+	container, err := NewBuilder(runtime).Create(&Config{
+		Image: GetTestImage(runtime).Id,
+		Cmd:   []string{"echo", "test"},
+	})
 	if err != nil {
 		t.Fatal(err)
 	}
-	if len(results) < 2 {
-		t.Errorf("Excepted at least 2 lines, %d found", len(results))
+	defer runtime.Destroy(container)
+
+	req, err := http.NewRequest("GET", "/containers?quiet=1&all=1", nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	body, err := getContainersPs(srv, nil, req, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	containers := []ApiContainers{}
+	err = json.Unmarshal(body, &containers)
+	if err != nil {
+		t.Fatal(err)
+	}
+	if len(containers) != 1 {
+		t.Fatalf("Excepted %d container, %d found", 1, len(containers))
+	}
+	if containers[0].Id != container.ShortId() {
+		t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ShortId(), containers[0].Id)
 	}
 }
 
-func TestGetImage(t *testing.T) {
+func TestGetContainersExport(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
+func TestGetContainerChanges(t *testing.T) {
+	// FIXME: Implement this test
+	t.Log("Test on implemented")
+	// r := httptest.NewRecorder()
+
+	// req, err := http.NewRequest("GET", "/containers/"+id+"/changes", nil)
+	// if err != nil {
+	// 	t.Fatal(err)
+	// }
+
+	// body, err := getContainersChanges(srv, r, req, nil)
+	// if err != nil {
+	// 	t.Fatal(err)
+	// }
+
+	// if body == nil {
+	// 	t.Fatalf("Body expected, received: nil\n")
+	// }
+
+	// if r.Code != http.StatusOK {
+	// 	t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
+	// }
+}
+
+func TestPostAuth(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -223,22 +303,58 @@ func TestGetImage(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
-	body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
+	authConfigOrig := &auth.AuthConfig{
+		Username: "utest",
+		Email:    "utest@yopmail.com",
+	}
+	runtime.authConfig = authConfigOrig
+
+	body, err := getAuth(srv, nil, nil, nil)
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	img := &Image{}
-
-	err = json.Unmarshal(body, img)
+	authConfig := &auth.AuthConfig{}
+	err = json.Unmarshal(body, authConfig)
 	if err != nil {
 		t.Fatal(err)
 	}
-	if img.Comment != "Imported from http://get.docker.io/images/busybox" {
-		t.Errorf("Error inspecting image")
+
+	if authConfig.Username != authConfigOrig.Username || authConfig.Email != authConfigOrig.Email {
+		t.Errorf("The retrieve auth mismatch with the one set.")
 	}
 }
 
+func TestPostCommit(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
+func TestPostBuild(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
+func TestPostImagesCreate(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
+func TestPostImagesInsert(t *testing.T) {
+	//FIXME: Implement this test (or remove this endpoint)
+	t.Log("Test on implemented")
+}
+
+func TestPostImagesPush(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
+func TestPostImagesTag(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
 func TestPostContainersCreate(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
@@ -295,7 +411,7 @@ func TestPostContainersCreate(t *testing.T) {
 	}
 }
 
-func TestGetContainersPs(t *testing.T) {
+func TestPostContainersKill(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -304,38 +420,47 @@ func TestGetContainersPs(t *testing.T) {
 
 	srv := &Server{runtime: runtime}
 
-	container, err := NewBuilder(runtime).Create(&Config{
-		Image: GetTestImage(runtime).Id,
-		Cmd:   []string{"echo", "test"},
-	})
+	container, err := NewBuilder(runtime).Create(
+		&Config{
+			Image:     GetTestImage(runtime).Id,
+			Cmd:       []string{"/bin/cat"},
+			OpenStdin: true,
+		},
+	)
 	if err != nil {
 		t.Fatal(err)
 	}
 	defer runtime.Destroy(container)
 
-	req, err := http.NewRequest("GET", "/containers?quiet=1&all=1", nil)
-	if err != nil {
+	if err := container.Start(); err != nil {
 		t.Fatal(err)
 	}
 
-	body, err := getContainersPs(srv, nil, req, nil)
-	if err != nil {
-		t.Fatal(err)
+	// Give some time to the process to start
+	container.WaitTimeout(500 * time.Millisecond)
+
+	if !container.State.Running {
+		t.Errorf("Container should be running")
 	}
-	containers := []ApiContainers{}
-	err = json.Unmarshal(body, &containers)
+
+	r := httptest.NewRecorder()
+
+	body, err := postContainersKill(srv, r, nil, map[string]string{"name": container.Id})
 	if err != nil {
 		t.Fatal(err)
 	}
-	if len(containers) != 1 {
-		t.Fatalf("Excepted %d container, %d found", 1, len(containers))
+	if body != nil {
+		t.Fatalf("No body expected, received: %s\n", body)
 	}
-	if containers[0].Id != container.ShortId() {
-		t.Fatalf("Container ID mismatch. Expected: %s, received: %s\n", container.ShortId(), containers[0].Id)
+	if r.Code != http.StatusNoContent {
+		t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
+	}
+	if container.State.Running {
+		t.Fatalf("The container hasn't been killed")
 	}
 }
 
-func TestPostContainersStart(t *testing.T) {
+func TestPostContainersRestart(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
@@ -356,9 +481,24 @@ func TestPostContainersStart(t *testing.T) {
 	}
 	defer runtime.Destroy(container)
 
+	if err := container.Start(); err != nil {
+		t.Fatal(err)
+	}
+
+	// Give some time to the process to start
+	container.WaitTimeout(500 * time.Millisecond)
+
+	if !container.State.Running {
+		t.Errorf("Container should be running")
+	}
+
 	r := httptest.NewRecorder()
 
-	body, err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id})
+	req, err := http.NewRequest("POST", "/containers/"+container.Id+"/restart?t=1", bytes.NewReader([]byte{}))
+	if err != nil {
+		t.Fatal(err)
+	}
+	body, err := postContainersRestart(srv, r, req, map[string]string{"name": container.Id})
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -369,15 +509,11 @@ func TestPostContainersStart(t *testing.T) {
 		t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
 	}
 
-	// Give some time to the process to start
+	// Give some time to the process to restart
 	container.WaitTimeout(500 * time.Millisecond)
 
 	if !container.State.Running {
-		t.Errorf("Container should be running")
-	}
-
-	if _, err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
-		t.Fatalf("A running containter should be able to be started")
+		t.Fatalf("Container should be running")
 	}
 
 	if err := container.Kill(); err != nil {
@@ -385,34 +521,56 @@ func TestPostContainersStart(t *testing.T) {
 	}
 }
 
-func testContainerRestart(t *testing.T, srv *Server, id string) {
+func TestPostContainersStart(t *testing.T) {
+	runtime, err := newTestRuntime()
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer nuke(runtime)
 
-	r := httptest.NewRecorder()
+	srv := &Server{runtime: runtime}
 
-	req, err := http.NewRequest("POST", "/containers/"+id+"/restart?t=1", nil)
+	container, err := NewBuilder(runtime).Create(
+		&Config{
+			Image:     GetTestImage(runtime).Id,
+			Cmd:       []string{"/bin/cat"},
+			OpenStdin: true,
+		},
+	)
 	if err != nil {
 		t.Fatal(err)
 	}
+	defer runtime.Destroy(container)
 
-	body, err := postContainersRestart(srv, r, req, nil)
+	r := httptest.NewRecorder()
+
+	body, err := postContainersStart(srv, r, nil, map[string]string{"name": container.Id})
 	if err != nil {
 		t.Fatal(err)
 	}
-
 	if body != nil {
 		t.Fatalf("No body expected, received: %s\n", body)
 	}
-
 	if r.Code != http.StatusNoContent {
 		t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
 	}
+
+	// Give some time to the process to start
+	container.WaitTimeout(500 * time.Millisecond)
+
+	if !container.State.Running {
+		t.Errorf("Container should be running")
+	}
+
+	if _, err = postContainersStart(srv, r, nil, map[string]string{"name": container.Id}); err == nil {
+		t.Fatalf("A running containter should be able to be started")
+	}
+
+	if err := container.Kill(); err != nil {
+		t.Fatal(err)
+	}
 }
 
-// 	testContainerRestart(t, srv, id)
-// 	testContainerKill(t, srv, id)
-// 	testContainerWait(t, srv, id)
-// 	testDeleteContainer(t, srv, id)
-// 	testListContainers(t, srv, 0)
 func TestPostContainersStop(t *testing.T) {
 	runtime, err := newTestRuntime()
 	if err != nil {
@@ -467,54 +625,55 @@ func TestPostContainersStop(t *testing.T) {
 	}
 }
 
-func testContainerKill(t *testing.T, srv *Server, id string) {
-
-	r := httptest.NewRecorder()
-
-	req, err := http.NewRequest("POST", "/containers/"+id+"/kill", nil)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	body, err := postContainersKill(srv, r, req, nil)
+func TestPostContainersWait(t *testing.T) {
+	runtime, err := newTestRuntime()
 	if err != nil {
 		t.Fatal(err)
 	}
+	defer nuke(runtime)
 
-	if body != nil {
-		t.Fatalf("No body expected, received: %s\n", body)
-	}
-
-	if r.Code != http.StatusNoContent {
-		t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
-	}
-}
-
-func testContainerWait(t *testing.T, srv *Server, id string) {
-
-	r := httptest.NewRecorder()
-
-	req, err := http.NewRequest("POST", "/containers/"+id+"/wait", nil)
-	req.Header.Set("Content-Type", "plain/text")
+	srv := &Server{runtime: runtime}
 
+	container, err := NewBuilder(runtime).Create(
+		&Config{
+			Image:     GetTestImage(runtime).Id,
+			Cmd:       []string{"/bin/sleep", "1"},
+			OpenStdin: true,
+		},
+	)
 	if err != nil {
 		t.Fatal(err)
 	}
+	defer runtime.Destroy(container)
 
-	body, err := postContainersWait(srv, r, req, nil)
-	if err != nil {
+	if err := container.Start(); err != nil {
 		t.Fatal(err)
 	}
 
-	if body == nil {
-		t.Fatalf("Body expected, received: nil\n")
-	}
+	setTimeout(t, "Wait timed out", 3*time.Second, func() {
+		body, err := postContainersWait(srv, nil, nil, nil)
+		if err != nil {
+			t.Fatal(err)
+		}
+		apiWait := &ApiWait{}
+		if err := json.Unmarshal(body, apiWait); err != nil {
+			t.Fatal(err)
+		}
+		if apiWait.StatusCode != 0 {
+			t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.StatusCode)
+		}
+	})
 
-	if r.Code != http.StatusOK {
-		t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
+	if container.State.Running {
+		t.Fatalf("The container should be stopped after wait")
 	}
 }
 
+func TestPostContainersAttach(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
+}
+
 // FIXME: Test deleting runnign container
 // FIXME: Test deleting container with volume
 // FIXME: Test deleting volume in use by other container
@@ -567,25 +726,7 @@ func TestDeleteContainers(t *testing.T) {
 	}
 }
 
-func testContainerChanges(t *testing.T, srv *Server, id string) {
-
-	r := httptest.NewRecorder()
-
-	req, err := http.NewRequest("GET", "/containers/"+id+"/changes", nil)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	body, err := getContainersChanges(srv, r, req, nil)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	if body == nil {
-		t.Fatalf("Body expected, received: nil\n")
-	}
-
-	if r.Code != http.StatusOK {
-		t.Fatalf("%d OK expected, received %d\n", http.StatusNoContent, r.Code)
-	}
+func TestDeleteImages(t *testing.T) {
+	//FIXME: Implement this test
+	t.Log("Test on implemented")
 }