Signed-off-by: wefine <wang.xiaoren@zte.com.cn>
wefine authored on 2017/01/04 00:02:58... | ... |
@@ -1,21 +1,23 @@ |
1 | 1 |
package client |
2 | 2 |
|
3 | 3 |
import ( |
4 |
- "errors" |
|
5 |
- "fmt" |
|
6 | 4 |
"net/url" |
7 | 5 |
|
8 |
- "golang.org/x/net/context" |
|
9 |
- |
|
10 | 6 |
distreference "github.com/docker/distribution/reference" |
11 | 7 |
"github.com/docker/docker/api/types/reference" |
8 |
+ "github.com/pkg/errors" |
|
9 |
+ "golang.org/x/net/context" |
|
12 | 10 |
) |
13 | 11 |
|
14 | 12 |
// ImageTag tags an image in the docker host |
15 |
-func (cli *Client) ImageTag(ctx context.Context, imageID, ref string) error { |
|
16 |
- distributionRef, err := distreference.ParseNamed(ref) |
|
13 |
+func (cli *Client) ImageTag(ctx context.Context, source, target string) error { |
|
14 |
+ if _, err := distreference.ParseNamed(source); err != nil { |
|
15 |
+ return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", source) |
|
16 |
+ } |
|
17 |
+ |
|
18 |
+ distributionRef, err := distreference.ParseNamed(target) |
|
17 | 19 |
if err != nil { |
18 |
- return fmt.Errorf("Error parsing reference: %q is not a valid repository/tag", ref) |
|
20 |
+ return errors.Wrapf(err, "Error parsing reference: %q is not a valid repository/tag", target) |
|
19 | 21 |
} |
20 | 22 |
|
21 | 23 |
if _, isCanonical := distributionRef.(distreference.Canonical); isCanonical { |
... | ... |
@@ -28,7 +30,7 @@ func (cli *Client) ImageTag(ctx context.Context, imageID, ref string) error { |
28 | 28 |
query.Set("repo", distributionRef.Name()) |
29 | 29 |
query.Set("tag", tag) |
30 | 30 |
|
31 |
- resp, err := cli.post(ctx, "/images/"+imageID+"/tag", query, nil, nil) |
|
31 |
+ resp, err := cli.post(ctx, "/images/"+source+"/tag", query, nil, nil) |
|
32 | 32 |
ensureReaderClosed(resp) |
33 | 33 |
return err |
34 | 34 |
} |
... | ... |
@@ -30,11 +30,22 @@ func TestImageTagInvalidReference(t *testing.T) { |
30 | 30 |
} |
31 | 31 |
|
32 | 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` { |
|
33 |
+ if err == nil || err.Error() != `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format` { |
|
34 | 34 |
t.Fatalf("expected ErrReferenceInvalidFormat, got %v", err) |
35 | 35 |
} |
36 | 36 |
} |
37 | 37 |
|
38 |
+func TestImageTagInvalidSourceImageName(t *testing.T) { |
|
39 |
+ client := &Client{ |
|
40 |
+ client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
41 |
+ } |
|
42 |
+ |
|
43 |
+ err := client.ImageTag(context.Background(), "invalid_source_image_name_", "repo:tag") |
|
44 |
+ if err == nil || err.Error() != "Error parsing reference: \"invalid_source_image_name_\" is not a valid repository/tag: invalid reference format" { |
|
45 |
+ t.Fatalf("expected Parsing Reference Error, got %v", err) |
|
46 |
+ } |
|
47 |
+} |
|
48 |
+ |
|
38 | 49 |
func TestImageTag(t *testing.T) { |
39 | 50 |
expectedURL := "/images/image_id/tag" |
40 | 51 |
tagCases := []struct { |