Browse code

Reimplementing builder tests for Dockerfile outside context as a unit test

Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>

Tomasz Kopczynski authored on 2016/05/30 01:56:49
Showing 3 changed files
... ...
@@ -53,3 +53,50 @@ func TestEmptyDockerfile(t *testing.T) {
53 53
 		t.Fatalf("Wrong error message. Should be \"%s\". Got \"%s\"", "The Dockerfile (Dockerfile) cannot be empty", err.Error())
54 54
 	}
55 55
 }
56
+
57
+func TestDockerfileOutsideTheBuildContext(t *testing.T) {
58
+	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
59
+	defer cleanup()
60
+
61
+	tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
62
+
63
+	if err != nil {
64
+		t.Fatalf("Error when creating tar stream: %s", err)
65
+	}
66
+
67
+	defer func() {
68
+		if err = tarStream.Close(); err != nil {
69
+			t.Fatalf("Error when closing tar stream: %s", err)
70
+		}
71
+	}()
72
+
73
+	context, err := builder.MakeTarSumContext(tarStream)
74
+
75
+	if err != nil {
76
+		t.Fatalf("Error when creating tar context: %s", err)
77
+	}
78
+
79
+	defer func() {
80
+		if err = context.Close(); err != nil {
81
+			t.Fatalf("Error when closing tar context: %s", err)
82
+		}
83
+	}()
84
+
85
+	options := &types.ImageBuildOptions{
86
+		Dockerfile: "../../Dockerfile",
87
+	}
88
+
89
+	b := &Builder{options: options, context: context}
90
+
91
+	err = b.readDockerfile()
92
+
93
+	if err == nil {
94
+		t.Fatalf("No error when executing test for Dockerfile outside the build context")
95
+	}
96
+
97
+	expectedError := "Forbidden path outside the build context"
98
+
99
+	if !strings.Contains(err.Error(), expectedError) {
100
+		t.Fatalf("Wrong error message. Should be \"%s\". Got \"%s\"", expectedError, err.Error())
101
+	}
102
+}
... ...
@@ -11,39 +11,6 @@ import (
11 11
 	"github.com/go-check/check"
12 12
 )
13 13
 
14
-func (s *DockerSuite) TestBuildApiDockerfilePath(c *check.C) {
15
-	// Test to make sure we stop people from trying to leave the
16
-	// build context when specifying the path to the dockerfile
17
-	buffer := new(bytes.Buffer)
18
-	tw := tar.NewWriter(buffer)
19
-	defer tw.Close()
20
-
21
-	dockerfile := []byte("FROM busybox")
22
-	err := tw.WriteHeader(&tar.Header{
23
-		Name: "Dockerfile",
24
-		Size: int64(len(dockerfile)),
25
-	})
26
-	//failed to write tar file header
27
-	c.Assert(err, checker.IsNil)
28
-
29
-	_, err = tw.Write(dockerfile)
30
-	// failed to write tar file content
31
-	c.Assert(err, checker.IsNil)
32
-
33
-	// failed to close tar archive
34
-	c.Assert(tw.Close(), checker.IsNil)
35
-
36
-	res, body, err := sockRequestRaw("POST", "/build?dockerfile=../Dockerfile", buffer, "application/x-tar")
37
-	c.Assert(err, checker.IsNil)
38
-	c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
39
-
40
-	out, err := readBody(body)
41
-	c.Assert(err, checker.IsNil)
42
-
43
-	// Didn't complain about leaving build context
44
-	c.Assert(string(out), checker.Contains, "Forbidden path outside the build context")
45
-}
46
-
47 14
 func (s *DockerSuite) TestBuildApiDockerFileRemote(c *check.C) {
48 15
 	testRequires(c, NotUserNamespace)
49 16
 	testRequires(c, DaemonIsLinux)
... ...
@@ -2988,32 +2988,6 @@ func (s *DockerSuite) TestBuildEntrypointRunCleanup(c *check.C) {
2988 2988
 	}
2989 2989
 }
2990 2990
 
2991
-func (s *DockerSuite) TestBuildForbiddenContextPath(c *check.C) {
2992
-	name := "testbuildforbidpath"
2993
-	ctx, err := fakeContext(`FROM `+minimalBaseImage()+`
2994
-        ADD ../../ test/
2995
-        `,
2996
-		map[string]string{
2997
-			"test.txt":  "test1",
2998
-			"other.txt": "other",
2999
-		})
3000
-	if err != nil {
3001
-		c.Fatal(err)
3002
-	}
3003
-	defer ctx.Close()
3004
-
3005
-	expected := "Forbidden path outside the build context: ../../ "
3006
-
3007
-	if daemonPlatform == "windows" {
3008
-		expected = "Forbidden path outside the build context: ..\\..\\ "
3009
-	}
3010
-
3011
-	if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
3012
-		c.Fatalf("Wrong error: (should contain \"%s\") got:\n%v", expected, err)
3013
-	}
3014
-
3015
-}
3016
-
3017 2991
 func (s *DockerSuite) TestBuildAddFileNotFound(c *check.C) {
3018 2992
 	name := "testbuildaddnotfound"
3019 2993
 	expected := "foo: no such file or directory"