Skip to content

Commit

Permalink
testing: fix image tag and inspect using image id instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa authored and andrestc committed Dec 7, 2017
1 parent f3d2516 commit df05870
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
23 changes: 11 additions & 12 deletions testing/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,21 +1021,19 @@ 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")
newTag := r.URL.Query().Get("tag")
if newTag != "" {
newRepo += ":" + newTag
}
s.imgIDs[newRepo] = s.imgIDs[name]
s.imgIDs[newRepo] = id
w.WriteHeader(http.StatusCreated)
}

Expand Down Expand Up @@ -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)
Expand Down
78 changes: 78 additions & 0 deletions testing/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()
Expand Down

0 comments on commit df05870

Please sign in to comment.