1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove ListenAndServe from unit tests

This commit is contained in:
Guillaume J. Charmes 2013-05-09 15:47:06 -07:00
parent c423a790d6
commit b2b59ddb10

View file

@ -1,240 +1,282 @@
package docker package docker
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/auth"
"net/http"
"net/http/httptest"
"testing" "testing"
) )
func init() { // func init() {
// Make it our Store root // // Make it our Store root
runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false) // runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
// Create the "Server" // // Create the "Server"
srv := &Server{ // srv := &Server{
runtime: runtime, // runtime: runtime,
} // }
go ListenAndServe("0.0.0.0:4243", srv, false) // go ListenAndServe("0.0.0.0:4243", srv, false)
// }
}
func TestAuth(t *testing.T) { func TestAuth(t *testing.T) {
var out auth.AuthConfig runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
out.Username = "utest" srv := &Server{runtime: runtime}
out.Password = "utest"
out.Email = "utest@yopmail.com"
_, _, err := call("POST", "/auth", out) r := httptest.NewRecorder()
authConfig := &auth.AuthConfig{
Username: "utest",
Password: "utest",
Email: "utest@yopmail.com",
}
authConfigJson, err := json.Marshal(authConfig)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
out.Username = "" req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
out.Password = ""
out.Email = ""
body, _, err := call("GET", "/auth", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = json.Unmarshal(body, &out) body, err := postAuth(srv, r, req)
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)
}
authConfig = &auth.AuthConfig{}
req, err = http.NewRequest("GET", "/auth", nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if out.Username != "utest" { body, err = getAuth(srv, nil, req)
t.Errorf("Expected username to be utest, %s found", out.Username) if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(body, authConfig)
if err != nil {
t.Fatal(err)
}
if authConfig.Username != "utest" {
t.Errorf("Expected username to be utest, %s found", authConfig.Username)
} }
} }
func TestVersion(t *testing.T) { func TestVersion(t *testing.T) {
body, _, err := call("GET", "/version", nil) runtime, err := newTestRuntime()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var out ApiVersion defer nuke(runtime)
err = json.Unmarshal(body, &out)
srv := &Server{runtime: runtime}
body, err := getVersion(srv, nil, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if out.Version != VERSION {
t.Errorf("Excepted version %s, %s found", VERSION, out.Version) v := &ApiVersion{}
err = json.Unmarshal(body, v)
if err != nil {
t.Fatal(err)
}
if v.Version != VERSION {
t.Errorf("Excepted version %s, %s found", VERSION, v.Version)
} }
} }
func TestImages(t *testing.T) { // func TestImages(t *testing.T) {
body, _, err := call("GET", "/images?quiet=0&all=0", nil) // body, _, err := call("GET", "/images?quiet=0&all=0", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var outs []ApiImages // var outs []ApiImages
err = json.Unmarshal(body, &outs) // err = json.Unmarshal(body, &outs)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if len(outs) != 1 { // if len(outs) != 1 {
t.Errorf("Excepted 1 image, %d found", len(outs)) // t.Errorf("Excepted 1 image, %d found", len(outs))
} // }
if outs[0].Repository != "docker-ut" { // if outs[0].Repository != "docker-ut" {
t.Errorf("Excepted image docker-ut, %s found", outs[0].Repository) // t.Errorf("Excepted image docker-ut, %s found", outs[0].Repository)
} // }
} // }
func TestInfo(t *testing.T) { // func TestInfo(t *testing.T) {
body, _, err := call("GET", "/info", nil) // body, _, err := call("GET", "/info", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var out ApiInfo // var out ApiInfo
err = json.Unmarshal(body, &out) // err = json.Unmarshal(body, &out)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if out.Version != VERSION { // if out.Version != VERSION {
t.Errorf("Excepted version %s, %s found", VERSION, out.Version) // t.Errorf("Excepted version %s, %s found", VERSION, out.Version)
} // }
} // }
func TestHistory(t *testing.T) { // func TestHistory(t *testing.T) {
body, _, err := call("GET", "/images/"+unitTestImageName+"/history", nil) // body, _, err := call("GET", "/images/"+unitTestImageName+"/history", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var outs []ApiHistory // var outs []ApiHistory
err = json.Unmarshal(body, &outs) // err = json.Unmarshal(body, &outs)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if len(outs) != 1 { // if len(outs) != 1 {
t.Errorf("Excepted 1 line, %d found", len(outs)) // t.Errorf("Excepted 1 line, %d found", len(outs))
} // }
} // }
func TestImagesSearch(t *testing.T) { // func TestImagesSearch(t *testing.T) {
body, _, err := call("GET", "/images/search?term=redis", nil) // body, _, err := call("GET", "/images/search?term=redis", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var outs []ApiSearch // var outs []ApiSearch
err = json.Unmarshal(body, &outs) // err = json.Unmarshal(body, &outs)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if len(outs) < 2 { // if len(outs) < 2 {
t.Errorf("Excepted at least 2 lines, %d found", len(outs)) // t.Errorf("Excepted at least 2 lines, %d found", len(outs))
} // }
} // }
func TestGetImage(t *testing.T) { // func TestGetImage(t *testing.T) {
obj, _, err := call("GET", "/images/"+unitTestImageName+"/json", nil) // obj, _, err := call("GET", "/images/"+unitTestImageName+"/json", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var out Image // var out Image
err = json.Unmarshal(obj, &out) // err = json.Unmarshal(obj, &out)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if out.Comment != "Imported from http://get.docker.io/images/busybox" { // if out.Comment != "Imported from http://get.docker.io/images/busybox" {
t.Errorf("Error inspecting image") // t.Errorf("Error inspecting image")
} // }
} // }
func TestCreateListStartStopRestartKillWaitDelete(t *testing.T) { // func TestCreateListStartStopRestartKillWaitDelete(t *testing.T) {
containers := testListContainers(t, -1) // containers := testListContainers(t, -1)
for _, container := range containers { // for _, container := range containers {
testDeleteContainer(t, container.Id) // testDeleteContainer(t, container.Id)
} // }
testCreateContainer(t) // testCreateContainer(t)
id := testListContainers(t, 1)[0].Id // id := testListContainers(t, 1)[0].Id
testContainerStart(t, id) // testContainerStart(t, id)
testContainerStop(t, id) // testContainerStop(t, id)
testContainerRestart(t, id) // testContainerRestart(t, id)
testContainerKill(t, id) // testContainerKill(t, id)
testContainerWait(t, id) // testContainerWait(t, id)
testDeleteContainer(t, id) // testDeleteContainer(t, id)
testListContainers(t, 0) // testListContainers(t, 0)
} // }
func testCreateContainer(t *testing.T) { // func testCreateContainer(t *testing.T) {
config, _, err := ParseRun([]string{unitTestImageName, "touch test"}, nil) // config, _, err := ParseRun([]string{unitTestImageName, "touch test"}, nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
_, _, err = call("POST", "/containers", *config) // _, _, err = call("POST", "/containers", *config)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testListContainers(t *testing.T, expected int) []ApiContainers { // func testListContainers(t *testing.T, expected int) []ApiContainers {
body, _, err := call("GET", "/containers?quiet=1&all=1", nil) // body, _, err := call("GET", "/containers?quiet=1&all=1", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
var outs []ApiContainers // var outs []ApiContainers
err = json.Unmarshal(body, &outs) // err = json.Unmarshal(body, &outs)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
if expected >= 0 && len(outs) != expected { // if expected >= 0 && len(outs) != expected {
t.Errorf("Excepted %d container, %d found", expected, len(outs)) // t.Errorf("Excepted %d container, %d found", expected, len(outs))
} // }
return outs // return outs
} // }
func testContainerStart(t *testing.T, id string) { // func testContainerStart(t *testing.T, id string) {
_, _, err := call("POST", "/containers/"+id+"/start", nil) // _, _, err := call("POST", "/containers/"+id+"/start", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testContainerRestart(t *testing.T, id string) { // func testContainerRestart(t *testing.T, id string) {
_, _, err := call("POST", "/containers/"+id+"/restart?t=1", nil) // _, _, err := call("POST", "/containers/"+id+"/restart?t=1", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testContainerStop(t *testing.T, id string) { // func testContainerStop(t *testing.T, id string) {
_, _, err := call("POST", "/containers/"+id+"/stop?t=1", nil) // _, _, err := call("POST", "/containers/"+id+"/stop?t=1", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testContainerKill(t *testing.T, id string) { // func testContainerKill(t *testing.T, id string) {
_, _, err := call("POST", "/containers/"+id+"/kill", nil) // _, _, err := call("POST", "/containers/"+id+"/kill", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testContainerWait(t *testing.T, id string) { // func testContainerWait(t *testing.T, id string) {
_, _, err := call("POST", "/containers/"+id+"/wait", nil) // _, _, err := call("POST", "/containers/"+id+"/wait", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testDeleteContainer(t *testing.T, id string) { // func testDeleteContainer(t *testing.T, id string) {
_, _, err := call("DELETE", "/containers/"+id, nil) // _, _, err := call("DELETE", "/containers/"+id, nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }
func testContainerChanges(t *testing.T, id string) { // func testContainerChanges(t *testing.T, id string) {
_, _, err := call("GET", "/containers/"+id+"/changes", nil) // _, _, err := call("GET", "/containers/"+id+"/changes", nil)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
} // }