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