Browse code

check tag's validity before building.

When user passes an invalid tag to `docker build`
(i.e. `docker build -t abcd:A0123456789B0123456789C0123456789 .`), check the
tag first and terminate-early so user can specify the tag again

Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh <dqminh89@gmail.com> (github: dqminh)

Daniel, Dao Quang Minh authored on 2014/09/29 18:52:13
Showing 4 changed files
... ...
@@ -26,6 +26,7 @@ import (
26 26
 	"github.com/docker/docker/archive"
27 27
 	"github.com/docker/docker/dockerversion"
28 28
 	"github.com/docker/docker/engine"
29
+	"github.com/docker/docker/graph"
29 30
 	"github.com/docker/docker/nat"
30 31
 	"github.com/docker/docker/opts"
31 32
 	"github.com/docker/docker/pkg/log"
... ...
@@ -174,10 +175,15 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
174 174
 
175 175
 	//Check if the given image name can be resolved
176 176
 	if *tag != "" {
177
-		repository, _ := parsers.ParseRepositoryTag(*tag)
177
+		repository, tag := parsers.ParseRepositoryTag(*tag)
178 178
 		if _, _, err := registry.ResolveRepositoryName(repository); err != nil {
179 179
 			return err
180 180
 		}
181
+		if len(tag) > 0 {
182
+			if err := graph.ValidateTagName(tag); err != nil {
183
+				return err
184
+			}
185
+		}
181 186
 	}
182 187
 
183 188
 	v.Set("t", *tag)
... ...
@@ -209,7 +209,7 @@ func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
209 209
 	if err := validateRepoName(repoName); err != nil {
210 210
 		return err
211 211
 	}
212
-	if err := validateTagName(tag); err != nil {
212
+	if err := ValidateTagName(tag); err != nil {
213 213
 		return err
214 214
 	}
215 215
 	if err := store.reload(); err != nil {
... ...
@@ -285,7 +285,7 @@ func validateRepoName(name string) error {
285 285
 }
286 286
 
287 287
 // Validate the name of a tag
288
-func validateTagName(name string) error {
288
+func ValidateTagName(name string) error {
289 289
 	if name == "" {
290 290
 		return fmt.Errorf("Tag name can't be empty")
291 291
 	}
... ...
@@ -118,7 +118,7 @@ func TestLookupImage(t *testing.T) {
118 118
 func TestValidTagName(t *testing.T) {
119 119
 	validTags := []string{"9", "foo", "foo-test", "bar.baz.boo"}
120 120
 	for _, tag := range validTags {
121
-		if err := validateTagName(tag); err != nil {
121
+		if err := ValidateTagName(tag); err != nil {
122 122
 			t.Errorf("'%s' should've been a valid tag", tag)
123 123
 		}
124 124
 	}
... ...
@@ -127,7 +127,7 @@ func TestValidTagName(t *testing.T) {
127 127
 func TestInvalidTagName(t *testing.T) {
128 128
 	validTags := []string{"-9", ".foo", "-test", ".", "-"}
129 129
 	for _, tag := range validTags {
130
-		if err := validateTagName(tag); err == nil {
130
+		if err := ValidateTagName(tag); err == nil {
131 131
 			t.Errorf("'%s' shouldn't have been a valid tag", tag)
132 132
 		}
133 133
 	}
... ...
@@ -2237,3 +2237,16 @@ func TestBuildOnBuildOutput(t *testing.T) {
2237 2237
 
2238 2238
 	logDone("build - onbuild output")
2239 2239
 }
2240
+
2241
+func TestBuildInvalidTag(t *testing.T) {
2242
+	name := "abcd:A0123456789B0123456789C0123456789"
2243
+	defer deleteImages(name)
2244
+	_, out, err := buildImageWithOut(name, "FROM scratch\nMAINTAINER quux\n", true)
2245
+	// if the error doesnt check for illegal tag name, or the image is built
2246
+	// then this should fail
2247
+	if !strings.Contains(err.Error(), "Illegal tag name") ||
2248
+		strings.Contains(out, "Sending build context to Docker daemon") {
2249
+		t.Fatalf("failed to stop before building. Error: %s, Output: %s", err, out)
2250
+	}
2251
+	logDone("build - invalid tag")
2252
+}