image_tag_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "golang.org/x/net/context"
  10. )
  11. func TestImageTagError(t *testing.T) {
  12. client := &Client{
  13. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  14. }
  15. err := client.ImageTag(context.Background(), "image_id", "repo:tag")
  16. if err == nil || err.Error() != "Error response from daemon: Server error" {
  17. t.Fatalf("expected a Server Error, got %v", err)
  18. }
  19. }
  20. // Note: this is not testing all the InvalidReference as it's the reponsability
  21. // of distribution/reference package.
  22. func TestImageTagInvalidReference(t *testing.T) {
  23. client := &Client{
  24. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  25. }
  26. err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa")
  27. if err == nil || err.Error() != `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag` {
  28. t.Fatalf("expected ErrReferenceInvalidFormat, got %v", err)
  29. }
  30. }
  31. func TestImageTag(t *testing.T) {
  32. expectedURL := "/images/image_id/tag"
  33. tagCases := []struct {
  34. reference string
  35. expectedQueryParams map[string]string
  36. }{
  37. {
  38. reference: "repository:tag1",
  39. expectedQueryParams: map[string]string{
  40. "repo": "repository",
  41. "tag": "tag1",
  42. },
  43. }, {
  44. reference: "another_repository:latest",
  45. expectedQueryParams: map[string]string{
  46. "repo": "another_repository",
  47. "tag": "latest",
  48. },
  49. }, {
  50. reference: "another_repository",
  51. expectedQueryParams: map[string]string{
  52. "repo": "another_repository",
  53. "tag": "latest",
  54. },
  55. }, {
  56. reference: "test/another_repository",
  57. expectedQueryParams: map[string]string{
  58. "repo": "test/another_repository",
  59. "tag": "latest",
  60. },
  61. }, {
  62. reference: "test/another_repository:tag1",
  63. expectedQueryParams: map[string]string{
  64. "repo": "test/another_repository",
  65. "tag": "tag1",
  66. },
  67. }, {
  68. reference: "test/test/another_repository:tag1",
  69. expectedQueryParams: map[string]string{
  70. "repo": "test/test/another_repository",
  71. "tag": "tag1",
  72. },
  73. }, {
  74. reference: "test:5000/test/another_repository:tag1",
  75. expectedQueryParams: map[string]string{
  76. "repo": "test:5000/test/another_repository",
  77. "tag": "tag1",
  78. },
  79. }, {
  80. reference: "test:5000/test/another_repository",
  81. expectedQueryParams: map[string]string{
  82. "repo": "test:5000/test/another_repository",
  83. "tag": "latest",
  84. },
  85. },
  86. }
  87. for _, tagCase := range tagCases {
  88. client := &Client{
  89. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  90. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  91. return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
  92. }
  93. if req.Method != "POST" {
  94. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  95. }
  96. query := req.URL.Query()
  97. for key, expected := range tagCase.expectedQueryParams {
  98. actual := query.Get(key)
  99. if actual != expected {
  100. return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
  101. }
  102. }
  103. return &http.Response{
  104. StatusCode: http.StatusOK,
  105. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  106. }, nil
  107. }),
  108. }
  109. err := client.ImageTag(context.Background(), "image_id", tagCase.reference)
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. }
  114. }