From df0587029c84fb4b7dc88444ea63deb665cfedef Mon Sep 17 00:00:00 2001 From: Cezar Sa Espinola Date: Thu, 7 Dec 2017 15:19:15 -0200 Subject: [PATCH] testing: fix image tag and inspect using image id instead of name --- testing/server.go | 23 ++++++------- testing/server_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/testing/server.go b/testing/server.go index 4a91a2db..f4b274f0 100644 --- a/testing/server.go +++ b/testing/server.go @@ -1021,13 +1021,11 @@ func (s *DockerServer) pushImage(w http.ResponseWriter, r *http.Request) { func (s *DockerServer) tagImage(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] - s.iMut.RLock() - if _, ok := s.imgIDs[name]; !ok { - s.iMut.RUnlock() + id, err := s.findImage(name) + if err != nil { http.Error(w, "No such image", http.StatusNotFound) return } - s.iMut.RUnlock() s.iMut.Lock() defer s.iMut.Unlock() newRepo := r.URL.Query().Get("repo") @@ -1035,7 +1033,7 @@ func (s *DockerServer) tagImage(w http.ResponseWriter, r *http.Request) { if newTag != "" { newRepo += ":" + newTag } - s.imgIDs[newRepo] = s.imgIDs[name] + s.imgIDs[newRepo] = id w.WriteHeader(http.StatusCreated) } @@ -1075,13 +1073,14 @@ func (s *DockerServer) inspectImage(w http.ResponseWriter, r *http.Request) { s.iMut.RLock() defer s.iMut.RUnlock() if id, ok := s.imgIDs[name]; ok { - for _, img := range s.images { - if img.ID == id { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(img) - return - } + name = id + } + for _, img := range s.images { + if img.ID == name { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(img) + return } } http.Error(w, "not found", http.StatusNotFound) diff --git a/testing/server_test.go b/testing/server_test.go index 1ff26153..cba22eb8 100644 --- a/testing/server_test.go +++ b/testing/server_test.go @@ -1408,6 +1408,21 @@ func TestTagImageWithRepoAndTag(t *testing.T) { } } +func TestTagImageWithID(t *testing.T) { + t.Parallel() + server := DockerServer{images: []docker.Image{{ID: "myimgid"}}, imgIDs: make(map[string]string)} + server.buildMuxer() + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("POST", "/images/myimgid/tag?repo=tsuru/new-python", nil) + server.ServeHTTP(recorder, request) + if recorder.Code != http.StatusCreated { + t.Errorf("TagImage: wrong status. Want %d. Got %d.", http.StatusCreated, recorder.Code) + } + if server.imgIDs["tsuru/new-python"] != "myimgid" { + t.Errorf("TagImage: did not tag the image") + } +} + func TestTagImageNotFound(t *testing.T) { t.Parallel() server := DockerServer{} @@ -1420,6 +1435,69 @@ func TestTagImageNotFound(t *testing.T) { } } +func TestInspectImage(t *testing.T) { + t.Parallel() + server := DockerServer{ + imgIDs: map[string]string{"tsuru/python": "a123"}, + images: []docker.Image{{ID: "a123", Author: "me"}}, + } + server.buildMuxer() + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/images/tsuru/python/json", nil) + server.ServeHTTP(recorder, request) + if recorder.Code != http.StatusOK { + t.Errorf("InspectImage: wrong status. Want %d. Got %d.", http.StatusOK, recorder.Code) + } + var img docker.Image + err := json.NewDecoder(recorder.Body).Decode(&img) + if err != nil { + t.Fatal(err) + } + expected := docker.Image{ + ID: "a123", + Author: "me", + } + if !reflect.DeepEqual(img, expected) { + t.Errorf("InspectImage: wrong image returned, expected %#v, got: %#v", expected, img) + } +} + +func TestInspectImageWithID(t *testing.T) { + t.Parallel() + server := DockerServer{images: []docker.Image{{ID: "myimgid", Author: "me"}}, imgIDs: make(map[string]string)} + server.buildMuxer() + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/images/myimgid/json", nil) + server.ServeHTTP(recorder, request) + if recorder.Code != http.StatusOK { + t.Errorf("InspectImage: wrong status. Want %d. Got %d.", http.StatusOK, recorder.Code) + } + var img docker.Image + err := json.NewDecoder(recorder.Body).Decode(&img) + if err != nil { + t.Fatal(err) + } + expected := docker.Image{ + ID: "myimgid", + Author: "me", + } + if !reflect.DeepEqual(img, expected) { + t.Errorf("InspectImage: wrong image returned, expected %#v, got: %#v", expected, img) + } +} + +func TestInspectImageNotFound(t *testing.T) { + t.Parallel() + server := DockerServer{} + server.buildMuxer() + recorder := httptest.NewRecorder() + request, _ := http.NewRequest("GET", "/images/tsuru/python/json", nil) + server.ServeHTTP(recorder, request) + if recorder.Code != http.StatusNotFound { + t.Errorf("InspectImage: wrong status. Want %d. Got %d.", http.StatusNotFound, recorder.Code) + } +} + func addContainers(server *DockerServer, n int) { server.cMut.Lock() defer server.cMut.Unlock()