Browse code

Merge pull request #7140 from proppy/more-addtar-tests

integration-cli: add more tests for BuildAddTar

Tibor Vass authored on 2014/07/29 00:58:29
Showing 5 changed files
1 1
deleted file mode 100644
... ...
@@ -1,3 +0,0 @@
1
-FROM busybox
2
-ADD test.tar /test.tar
3
-RUN cat /test.tar/test/foo
4 1
deleted file mode 100644
5 2
Binary files a/integration-cli/build_tests/TestBuildAddTar/1/test.tar and /dev/null differ
6 3
deleted file mode 100644
... ...
@@ -1,3 +0,0 @@
1
-FROM busybox
2
-ADD test.tar /
3
-RUN cat /test/foo
4 1
deleted file mode 100644
5 2
Binary files a/integration-cli/build_tests/TestBuildAddTar/2/test.tar and /dev/null differ
... ...
@@ -1,7 +1,9 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"archive/tar"
4 5
 	"fmt"
6
+	"io/ioutil"
5 7
 	"os"
6 8
 	"os/exec"
7 9
 	"path/filepath"
... ...
@@ -1770,34 +1772,55 @@ RUN [ "$(cat /testfile)" = 'test!' ]`
1770 1770
 func TestBuildAddTar(t *testing.T) {
1771 1771
 	name := "testbuildaddtar"
1772 1772
 	defer deleteImages(name)
1773
-	checkOutput := func(out string) {
1774
-		n := -1
1775
-		x := ""
1776
-		for i, line := range strings.Split(out, "\n") {
1777
-			if strings.HasPrefix(line, "Step 2") {
1778
-				n = i + 2
1779
-				x = line[strings.Index(line, "cat ")+4:]
1780
-			}
1781
-			if i == n {
1782
-				if line != "Hi" {
1783
-					t.Fatalf("Could not find contents of %s (expected 'Hi' got '%s'", x, line)
1784
-				}
1785
-				n = -2
1786
-			}
1773
+
1774
+	ctx := func() *FakeContext {
1775
+		dockerfile := `
1776
+FROM busybox
1777
+ADD test.tar /
1778
+RUN cat /test/foo | grep Hi
1779
+ADD test.tar /test.tar
1780
+RUN cat /test.tar/test/foo | grep Hi
1781
+ADD test.tar /unlikely-to-exist
1782
+RUN cat /unlikely-to-exist/test/foo | grep Hi
1783
+ADD test.tar /unlikely-to-exist-trailing-slash/
1784
+RUN cat /unlikely-to-exist-trailing-slash/test/foo | grep Hi
1785
+RUN mkdir /existing-directory
1786
+ADD test.tar /existing-directory
1787
+RUN cat /existing-directory/test/foo | grep Hi
1788
+ADD test.tar /existing-directory-trailing-slash/
1789
+RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
1790
+		tmpDir, err := ioutil.TempDir("", "fake-context")
1791
+		testTar, err := os.Create(filepath.Join(tmpDir, "test.tar"))
1792
+		if err != nil {
1793
+			t.Fatalf("failed to create test.tar archive: %v", err)
1787 1794
 		}
1788
-		if n > -2 {
1789
-			t.Fatalf("Could not find contents of %s in build output", x)
1795
+		defer testTar.Close()
1796
+
1797
+		tw := tar.NewWriter(testTar)
1798
+
1799
+		if err := tw.WriteHeader(&tar.Header{
1800
+			Name: "test/foo",
1801
+			Size: 2,
1802
+		}); err != nil {
1803
+			t.Fatalf("failed to write tar file header: %v", err)
1804
+		}
1805
+		if _, err := tw.Write([]byte("Hi")); err != nil {
1806
+			t.Fatalf("failed to write tar file content: %v", err)
1807
+		}
1808
+		if err := tw.Close(); err != nil {
1809
+			t.Fatalf("failed to close tar archive: %v", err)
1790 1810
 		}
1791
-	}
1792 1811
 
1793
-	for _, n := range []string{"1", "2"} {
1794
-		buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildAddTar", n)
1795
-		buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
1796
-		buildCmd.Dir = buildDirectory
1797
-		out, _, err := runCommandWithOutput(buildCmd)
1798
-		errorOut(err, t, fmt.Sprintf("build failed to complete for TestBuildAddTar/%s: %v", n, err))
1799
-		checkOutput(out)
1812
+		if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
1813
+			t.Fatalf("failed to open destination dockerfile: %v", err)
1814
+		}
1815
+		return &FakeContext{Dir: tmpDir}
1816
+	}()
1817
+
1818
+	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1819
+		t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
1800 1820
 	}
1821
+
1801 1822
 	logDone("build - ADD tar")
1802 1823
 }
1803 1824