123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- package docker
- import (
- "bytes"
- "encoding/json"
- "github.com/dotcloud/docker/auth"
- "net/http"
- "net/http/httptest"
- "testing"
- )
- func TestAuth(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- r := httptest.NewRecorder()
- authConfig := &auth.AuthConfig{
- Username: "utest",
- Password: "utest",
- Email: "utest@yopmail.com",
- }
- authConfigJson, err := json.Marshal(authConfig)
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("POST", "/auth", bytes.NewReader(authConfigJson))
- if err != nil {
- t.Fatal(err)
- }
- body, err := postAuth(srv, r, req, nil)
- if err != nil {
- t.Fatal(err)
- }
- if body == nil {
- t.Fatalf("No body received\n")
- }
- if r.Code != http.StatusOK {
- t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
- }
- authConfig = &auth.AuthConfig{}
- req, err = http.NewRequest("GET", "/auth", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err = getAuth(srv, nil, req, nil)
- 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) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- body, err := getVersion(srv, nil, nil, nil)
- if err != nil {
- t.Fatal(err)
- }
- 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 TestContainersExport(t *testing.T) {
- //FIXME: Implement this test
- }
- func TestGetImages(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- // FIXME: Do more tests with filter
- req, err := http.NewRequest("GET", "/images?quiet=0&all=0", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := getImages(srv, nil, req, nil)
- if err != nil {
- t.Fatal(err)
- }
- images := []ApiImages{}
- err = json.Unmarshal(body, &images)
- if err != nil {
- t.Fatal(err)
- }
- if len(images) != 1 {
- t.Errorf("Excepted 1 image, %d found", len(images))
- }
- if images[0].Repository != "docker-ut" {
- t.Errorf("Excepted image docker-ut, %s found", images[0].Repository)
- }
- }
- func TestInfo(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- body, err := getInfo(srv, nil, nil, nil)
- if err != nil {
- t.Fatal(err)
- }
- infos := &ApiInfo{}
- err = json.Unmarshal(body, infos)
- if err != nil {
- t.Fatal(err)
- }
- if infos.Version != VERSION {
- t.Errorf("Excepted version %s, %s found", VERSION, infos.Version)
- }
- }
- func TestHistory(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- body, err := getImagesHistory(srv, nil, nil, map[string]string{"name": unitTestImageName})
- if err != nil {
- t.Fatal(err)
- }
- history := []ApiHistory{}
- err = json.Unmarshal(body, &history)
- if err != nil {
- t.Fatal(err)
- }
- if len(history) != 1 {
- t.Errorf("Excepted 1 line, %d found", len(history))
- }
- }
- func TestImagesSearch(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- req, err := http.NewRequest("GET", "/images/search?term=redis", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := getImagesSearch(srv, nil, req, nil)
- if err != nil {
- t.Fatal(err)
- }
- 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 TestGetImage(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- body, err := getImagesByName(srv, nil, nil, map[string]string{"name": unitTestImageName})
- if err != nil {
- t.Fatal(err)
- }
- 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")
- }
- }
- func TestCreateListStartStopRestartKillWaitDelete(t *testing.T) {
- runtime, err := newTestRuntime()
- if err != nil {
- t.Fatal(err)
- }
- defer nuke(runtime)
- srv := &Server{runtime: runtime}
- containers := testListContainers(t, srv, -1)
- for _, container := range containers {
- testDeleteContainer(t, srv, container.Id)
- }
- testCreateContainer(t, srv)
- id := testListContainers(t, srv, 1)[0].Id
- testContainerStart(t, srv, id)
- testContainerStop(t, srv, id)
- testContainerRestart(t, srv, id)
- testContainerKill(t, srv, id)
- testContainerWait(t, srv, id)
- testDeleteContainer(t, srv, id)
- testListContainers(t, srv, 0)
- }
- func testCreateContainer(t *testing.T, srv *Server) {
- r := httptest.NewRecorder()
- config, _, err := ParseRun([]string{unitTestImageName, "touch test"}, nil)
- if err != nil {
- t.Fatal(err)
- }
- configJson, err := json.Marshal(config)
- if err != nil {
- t.Fatal(err)
- }
- req, err := http.NewRequest("POST", "/containers", bytes.NewReader(configJson))
- if err != nil {
- t.Fatal(err)
- }
- body, err := postContainersCreate(srv, r, req, nil)
- if err != nil {
- t.Fatal(err)
- }
- if body == nil {
- t.Fatalf("Body expected, received: nil\n")
- }
- if r.Code != http.StatusCreated {
- t.Fatalf("%d Created expected, received %d\n", http.StatusNoContent, r.Code)
- }
- }
- func testListContainers(t *testing.T, srv *Server, expected int) []ApiContainers {
- r := httptest.NewRecorder()
- req, err := http.NewRequest("GET", "/containers?quiet=1&all=1", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := getContainersPs(srv, r, req, nil)
- if err != nil {
- t.Fatal(err)
- }
- var outs []ApiContainers
- err = json.Unmarshal(body, &outs)
- if err != nil {
- t.Fatal(err)
- }
- if expected >= 0 && len(outs) != expected {
- t.Errorf("Excepted %d container, %d found", expected, len(outs))
- }
- return outs
- }
- func testContainerStart(t *testing.T, srv *Server, id string) {
- r := httptest.NewRecorder()
- req, err := http.NewRequest("POST", "/containers/"+id+"/start", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := postContainersStart(srv, r, req, nil)
- 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)
- }
- }
- func testContainerRestart(t *testing.T, srv *Server, id string) {
- r := httptest.NewRecorder()
- req, err := http.NewRequest("POST", "/containers/"+id+"/restart?t=1", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := postContainersRestart(srv, r, req, nil)
- 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)
- }
- }
- func testContainerStop(t *testing.T, srv *Server, id string) {
- r := httptest.NewRecorder()
- req, err := http.NewRequest("POST", "/containers/"+id+"/stop?t=1", nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := postContainersStop(srv, r, req, nil)
- 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)
- }
- }
- 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)
- 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)
- }
- }
- 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")
- if err != nil {
- t.Fatal(err)
- }
- body, err := postContainersWait(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 testDeleteContainer(t *testing.T, srv *Server, id string) {
- r := httptest.NewRecorder()
- req, err := http.NewRequest("DELETE", "/containers/"+id, nil)
- if err != nil {
- t.Fatal(err)
- }
- body, err := deleteContainers(srv, r, req, nil)
- 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)
- }
- }
- 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)
- }
- }
|