|
@@ -1,7 +1,9 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "archive/tar"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "io/ioutil"
|
|
"os"
|
|
"os"
|
|
"os/exec"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
@@ -1770,34 +1772,55 @@ RUN [ "$(cat /testfile)" = 'test!' ]`
|
|
func TestBuildAddTar(t *testing.T) {
|
|
func TestBuildAddTar(t *testing.T) {
|
|
name := "testbuildaddtar"
|
|
name := "testbuildaddtar"
|
|
defer deleteImages(name)
|
|
defer deleteImages(name)
|
|
- checkOutput := func(out string) {
|
|
|
|
- n := -1
|
|
|
|
- x := ""
|
|
|
|
- for i, line := range strings.Split(out, "\n") {
|
|
|
|
- if strings.HasPrefix(line, "Step 2") {
|
|
|
|
- n = i + 2
|
|
|
|
- x = line[strings.Index(line, "cat ")+4:]
|
|
|
|
- }
|
|
|
|
- if i == n {
|
|
|
|
- if line != "Hi" {
|
|
|
|
- t.Fatalf("Could not find contents of %s (expected 'Hi' got '%s'", x, line)
|
|
|
|
- }
|
|
|
|
- n = -2
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ ctx := func() *FakeContext {
|
|
|
|
+ dockerfile := `
|
|
|
|
+FROM busybox
|
|
|
|
+ADD test.tar /
|
|
|
|
+RUN cat /test/foo | grep Hi
|
|
|
|
+ADD test.tar /test.tar
|
|
|
|
+RUN cat /test.tar/test/foo | grep Hi
|
|
|
|
+ADD test.tar /unlikely-to-exist
|
|
|
|
+RUN cat /unlikely-to-exist/test/foo | grep Hi
|
|
|
|
+ADD test.tar /unlikely-to-exist-trailing-slash/
|
|
|
|
+RUN cat /unlikely-to-exist-trailing-slash/test/foo | grep Hi
|
|
|
|
+RUN mkdir /existing-directory
|
|
|
|
+ADD test.tar /existing-directory
|
|
|
|
+RUN cat /existing-directory/test/foo | grep Hi
|
|
|
|
+ADD test.tar /existing-directory-trailing-slash/
|
|
|
|
+RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
|
|
|
|
+ tmpDir, err := ioutil.TempDir("", "fake-context")
|
|
|
|
+ testTar, err := os.Create(filepath.Join(tmpDir, "test.tar"))
|
|
|
|
+ if err != nil {
|
|
|
|
+ t.Fatalf("failed to create test.tar archive: %v", err)
|
|
}
|
|
}
|
|
- if n > -2 {
|
|
|
|
- t.Fatalf("Could not find contents of %s in build output", x)
|
|
|
|
|
|
+ defer testTar.Close()
|
|
|
|
+
|
|
|
|
+ tw := tar.NewWriter(testTar)
|
|
|
|
+
|
|
|
|
+ if err := tw.WriteHeader(&tar.Header{
|
|
|
|
+ Name: "test/foo",
|
|
|
|
+ Size: 2,
|
|
|
|
+ }); err != nil {
|
|
|
|
+ t.Fatalf("failed to write tar file header: %v", err)
|
|
|
|
+ }
|
|
|
|
+ if _, err := tw.Write([]byte("Hi")); err != nil {
|
|
|
|
+ t.Fatalf("failed to write tar file content: %v", err)
|
|
|
|
+ }
|
|
|
|
+ if err := tw.Close(); err != nil {
|
|
|
|
+ t.Fatalf("failed to close tar archive: %v", err)
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- for _, n := range []string{"1", "2"} {
|
|
|
|
- buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildAddTar", n)
|
|
|
|
- buildCmd := exec.Command(dockerBinary, "build", "-t", name, ".")
|
|
|
|
- buildCmd.Dir = buildDirectory
|
|
|
|
- out, _, err := runCommandWithOutput(buildCmd)
|
|
|
|
- errorOut(err, t, fmt.Sprintf("build failed to complete for TestBuildAddTar/%s: %v", n, err))
|
|
|
|
- checkOutput(out)
|
|
|
|
|
|
+ if err := ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
|
|
|
|
+ t.Fatalf("failed to open destination dockerfile: %v", err)
|
|
|
|
+ }
|
|
|
|
+ return &FakeContext{Dir: tmpDir}
|
|
|
|
+ }()
|
|
|
|
+
|
|
|
|
+ if _, err := buildImageFromContext(name, ctx, true); err != nil {
|
|
|
|
+ t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
|
|
}
|
|
}
|
|
|
|
+
|
|
logDone("build - ADD tar")
|
|
logDone("build - ADD tar")
|
|
}
|
|
}
|
|
|
|
|