github.com/MaximeAubanel/moby@v1.13.1/client/image_history_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "golang.org/x/net/context" 14 ) 15 16 func TestImageHistoryError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 _, err := client.ImageHistory(context.Background(), "nothing") 21 if err == nil || err.Error() != "Error response from daemon: Server error" { 22 t.Fatalf("expected a Server error, got %v", err) 23 } 24 } 25 26 func TestImageHistory(t *testing.T) { 27 expectedURL := "/images/image_id/history" 28 client := &Client{ 29 client: newMockClient(func(r *http.Request) (*http.Response, error) { 30 if !strings.HasPrefix(r.URL.Path, expectedURL) { 31 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) 32 } 33 b, err := json.Marshal([]types.ImageHistory{ 34 { 35 ID: "image_id1", 36 Tags: []string{"tag1", "tag2"}, 37 }, 38 { 39 ID: "image_id2", 40 Tags: []string{"tag1", "tag2"}, 41 }, 42 }) 43 if err != nil { 44 return nil, err 45 } 46 47 return &http.Response{ 48 StatusCode: http.StatusOK, 49 Body: ioutil.NopCloser(bytes.NewReader(b)), 50 }, nil 51 }), 52 } 53 imageHistories, err := client.ImageHistory(context.Background(), "image_id") 54 if err != nil { 55 t.Fatal(err) 56 } 57 if len(imageHistories) != 2 { 58 t.Fatalf("expected 2 containers, got %v", imageHistories) 59 } 60 }