image_history_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/image"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  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. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestImageHistory(t *testing.T) {
  24. expectedURL := "/images/image_id/history"
  25. client := &Client{
  26. client: newMockClient(func(r *http.Request) (*http.Response, error) {
  27. if !strings.HasPrefix(r.URL.Path, expectedURL) {
  28. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
  29. }
  30. b, err := json.Marshal([]image.HistoryResponseItem{
  31. {
  32. ID: "image_id1",
  33. Tags: []string{"tag1", "tag2"},
  34. },
  35. {
  36. ID: "image_id2",
  37. Tags: []string{"tag1", "tag2"},
  38. },
  39. })
  40. if err != nil {
  41. return nil, err
  42. }
  43. return &http.Response{
  44. StatusCode: http.StatusOK,
  45. Body: io.NopCloser(bytes.NewReader(b)),
  46. }, nil
  47. }),
  48. }
  49. imageHistories, err := client.ImageHistory(context.Background(), "image_id")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if len(imageHistories) != 2 {
  54. t.Fatalf("expected 2 containers, got %v", imageHistories)
  55. }
  56. }