Skip to content

Commit

Permalink
Merge pull request #8276 from dqminh/8262-validate-build-tag
Browse files Browse the repository at this point in the history
Fix #8262: check tag's validity before building.
  • Loading branch information
vieux committed Sep 29, 2014
2 parents 74dbea8 + 8833d80 commit 22bd54b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
8 changes: 7 additions & 1 deletion api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/docker/docker/archive"
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/engine"
"github.com/docker/docker/graph"
"github.com/docker/docker/nat"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/log"
Expand Down Expand Up @@ -174,10 +175,15 @@ func (cli *DockerCli) CmdBuild(args ...string) error {

//Check if the given image name can be resolved
if *tag != "" {
repository, _ := parsers.ParseRepositoryTag(*tag)
repository, tag := parsers.ParseRepositoryTag(*tag)
if _, _, err := registry.ResolveRepositoryName(repository); err != nil {
return err
}
if len(tag) > 0 {
if err := graph.ValidateTagName(tag); err != nil {
return err
}
}
}

v.Set("t", *tag)
Expand Down
4 changes: 2 additions & 2 deletions graph/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
if err := validateRepoName(repoName); err != nil {
return err
}
if err := validateTagName(tag); err != nil {
if err := ValidateTagName(tag); err != nil {
return err
}
if err := store.reload(); err != nil {
Expand Down Expand Up @@ -285,7 +285,7 @@ func validateRepoName(name string) error {
}

// Validate the name of a tag
func validateTagName(name string) error {
func ValidateTagName(name string) error {
if name == "" {
return fmt.Errorf("Tag name can't be empty")
}
Expand Down
4 changes: 2 additions & 2 deletions graph/tags_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestLookupImage(t *testing.T) {
func TestValidTagName(t *testing.T) {
validTags := []string{"9", "foo", "foo-test", "bar.baz.boo"}
for _, tag := range validTags {
if err := validateTagName(tag); err != nil {
if err := ValidateTagName(tag); err != nil {
t.Errorf("'%s' should've been a valid tag", tag)
}
}
Expand All @@ -127,7 +127,7 @@ func TestValidTagName(t *testing.T) {
func TestInvalidTagName(t *testing.T) {
validTags := []string{"-9", ".foo", "-test", ".", "-"}
for _, tag := range validTags {
if err := validateTagName(tag); err == nil {
if err := ValidateTagName(tag); err == nil {
t.Errorf("'%s' shouldn't have been a valid tag", tag)
}
}
Expand Down
13 changes: 13 additions & 0 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2237,3 +2237,16 @@ func TestBuildOnBuildOutput(t *testing.T) {

logDone("build - onbuild output")
}

func TestBuildInvalidTag(t *testing.T) {
name := "abcd:A0123456789B0123456789C0123456789"
defer deleteImages(name)
_, out, err := buildImageWithOut(name, "FROM scratch\nMAINTAINER quux\n", true)
// if the error doesnt check for illegal tag name, or the image is built
// then this should fail
if !strings.Contains(err.Error(), "Illegal tag name") ||
strings.Contains(out, "Sending build context to Docker daemon") {
t.Fatalf("failed to stop before building. Error: %s, Output: %s", err, out)
}
logDone("build - invalid tag")
}

0 comments on commit 22bd54b

Please sign in to comment.