Browse code

Get list of Dockerfile cmds from builder so we can be smarted in our tests

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/02/08 12:30:44
Showing 2 changed files
... ...
@@ -54,10 +54,12 @@ var replaceEnvAllowed = map[string]struct{}{
54 54
 	"user":    {},
55 55
 }
56 56
 
57
-var evaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
57
+// EvaluateTable is public so that we can get the list of Dockerfile
58
+// commands from within the test cases
59
+var EvaluateTable map[string]func(*Builder, []string, map[string]bool, string) error
58 60
 
59 61
 func init() {
60
-	evaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
62
+	EvaluateTable = map[string]func(*Builder, []string, map[string]bool, string) error{
61 63
 		"env":        env,
62 64
 		"maintainer": maintainer,
63 65
 		"add":        add,
... ...
@@ -224,7 +226,7 @@ func (b *Builder) readDockerfile(origFile string) error {
224 224
 // Child[Node, Node, Node] where Child is from parser.Node.Children and each
225 225
 // node comes from parser.Node.Next. This forms a "line" with a statement and
226 226
 // arguments and we process them in this normalized form by hitting
227
-// evaluateTable with the leaf nodes of the command and the Builder object.
227
+// EvaluateTable with the leaf nodes of the command and the Builder object.
228 228
 //
229 229
 // ONBUILD is a special case; in this case the parser will emit:
230 230
 // Child[Node, Child[Node, Node...]] where the first node is the literal
... ...
@@ -280,7 +282,7 @@ func (b *Builder) dispatch(stepN int, ast *parser.Node) error {
280 280
 
281 281
 	// XXX yes, we skip any cmds that are not valid; the parser should have
282 282
 	// picked these out already.
283
-	if f, ok := evaluateTable[cmd]; ok {
283
+	if f, ok := EvaluateTable[cmd]; ok {
284 284
 		return f(b, strList, attrs, original)
285 285
 	}
286 286
 
... ...
@@ -19,6 +19,7 @@ import (
19 19
 	"text/template"
20 20
 	"time"
21 21
 
22
+	"github.com/docker/docker/builder"
22 23
 	"github.com/docker/docker/pkg/archive"
23 24
 )
24 25
 
... ...
@@ -4726,9 +4727,9 @@ func TestBuildSpaces(t *testing.T) {
4726 4726
 
4727 4727
 	name := "testspaces"
4728 4728
 	defer deleteImages(name)
4729
-	ctx, err := fakeContext("FROM busybox\nRUN\n",
4729
+	ctx, err := fakeContext("FROM busybox\nCOPY\n",
4730 4730
 		map[string]string{
4731
-			"Dockerfile": "FROM busybox\nRUN\n",
4731
+			"Dockerfile": "FROM busybox\nCOPY\n",
4732 4732
 		})
4733 4733
 	if err != nil {
4734 4734
 		t.Fatal(err)
... ...
@@ -4739,7 +4740,7 @@ func TestBuildSpaces(t *testing.T) {
4739 4739
 		t.Fatal("Build 1 was supposed to fail, but didn't")
4740 4740
 	}
4741 4741
 
4742
-	ctx.Add("Dockerfile", "FROM busybox\nRUN    ")
4742
+	ctx.Add("Dockerfile", "FROM busybox\nCOPY    ")
4743 4743
 	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4744 4744
 		t.Fatal("Build 2 was supposed to fail, but didn't")
4745 4745
 	}
... ...
@@ -4753,7 +4754,7 @@ func TestBuildSpaces(t *testing.T) {
4753 4753
 		t.Fatalf("Build 2's error wasn't the same as build 1's\n1:%s\n2:%s", err1, err2)
4754 4754
 	}
4755 4755
 
4756
-	ctx.Add("Dockerfile", "FROM busybox\n   RUN")
4756
+	ctx.Add("Dockerfile", "FROM busybox\n   COPY")
4757 4757
 	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4758 4758
 		t.Fatal("Build 3 was supposed to fail, but didn't")
4759 4759
 	}
... ...
@@ -4767,7 +4768,7 @@ func TestBuildSpaces(t *testing.T) {
4767 4767
 		t.Fatalf("Build 3's error wasn't the same as build 1's\n1:%s\n3:%s", err1, err2)
4768 4768
 	}
4769 4769
 
4770
-	ctx.Add("Dockerfile", "FROM busybox\n   RUN    ")
4770
+	ctx.Add("Dockerfile", "FROM busybox\n   COPY    ")
4771 4771
 	if _, err2 = buildImageFromContext(name, ctx, false); err2 == nil {
4772 4772
 		t.Fatal("Build 4 was supposed to fail, but didn't")
4773 4773
 	}
... ...
@@ -4824,27 +4825,28 @@ func TestBuildVolumeFileExistsinContainer(t *testing.T) {
4824 4824
 }
4825 4825
 
4826 4826
 func TestBuildMissingArgs(t *testing.T) {
4827
-	// test to make sure these cmds w/o any args will generate an error
4828
-	// Notice some commands are missing because its ok for them to
4829
-	// not have any args - like: CMD, RUN, ENTRYPOINT
4830
-	cmds := []string{
4831
-		"ADD",
4832
-		"COPY",
4833
-		"ENV",
4834
-		"EXPOSE",
4835
-		"FROM",
4836
-		"MAINTAINER",
4837
-		"ONBUILD",
4838
-		"USER",
4839
-		"VOLUME",
4840
-		"WORKDIR",
4827
+	// Test to make sure that all Dockerfile commands (except the ones listed
4828
+	// in skipCmds) will generate an error if no args are provided.
4829
+	// Note: INSERT is deprecated so we exclude it because of that.
4830
+
4831
+	skipCmds := map[string]struct{}{
4832
+		"CMD":        {},
4833
+		"RUN":        {},
4834
+		"ENTRYPOINT": {},
4835
+		"INSERT":     {},
4841 4836
 	}
4842 4837
 
4843 4838
 	defer deleteAllContainers()
4844 4839
 
4845
-	for _, cmd := range cmds {
4840
+	for cmd := range builder.EvaluateTable {
4846 4841
 		var dockerfile string
4847 4842
 
4843
+		cmd = strings.ToUpper(cmd)
4844
+
4845
+		if _, ok := skipCmds[cmd]; ok {
4846
+			continue
4847
+		}
4848
+
4848 4849
 		if cmd == "FROM" {
4849 4850
 			dockerfile = cmd
4850 4851
 		} else {
... ...
@@ -4859,13 +4861,11 @@ func TestBuildMissingArgs(t *testing.T) {
4859 4859
 		defer ctx.Close()
4860 4860
 		var out string
4861 4861
 		if out, err = buildImageFromContext("args", ctx, true); err == nil {
4862
-			t.Fatalf("%s was supposed to fail:%s", cmd, out)
4862
+			t.Fatalf("%s was supposed to fail. Out:%s", cmd, out)
4863 4863
 		}
4864 4864
 		if !strings.Contains(err.Error(), cmd+" requires") {
4865 4865
 			t.Fatalf("%s returned the wrong type of error:%s", cmd, err)
4866 4866
 		}
4867
-
4868
-		ctx.Close()
4869 4867
 	}
4870 4868
 
4871 4869
 	logDone("build - verify missing args")