2013-10-31 19:57:45 -04:00
|
|
|
package archive
|
2013-03-11 08:42:36 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-14 13:47:49 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-03-29 07:42:17 -04:00
|
|
|
"io"
|
2013-03-11 08:42:36 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2013-06-14 13:47:49 -04:00
|
|
|
"path"
|
2013-03-11 08:42:36 -04:00
|
|
|
"testing"
|
2013-03-29 17:09:25 -04:00
|
|
|
"time"
|
2014-05-20 20:10:35 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
2013-03-11 08:42:36 -04:00
|
|
|
)
|
|
|
|
|
2013-03-29 07:42:17 -04:00
|
|
|
func TestCmdStreamLargeStderr(t *testing.T) {
|
|
|
|
cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
|
2014-01-17 05:09:39 -05:00
|
|
|
out, err := CmdStream(cmd, nil)
|
2013-03-29 07:42:17 -04:00
|
|
|
if err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
t.Fatalf("Failed to start command: %s", err)
|
2013-03-29 07:42:17 -04:00
|
|
|
}
|
2013-03-29 17:09:25 -04:00
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(ioutil.Discard, out)
|
|
|
|
errCh <- err
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
t.Fatalf("Command should not have failed (err=%.100s...)", err)
|
2013-03-29 17:09:25 -04:00
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("Command did not complete in 5 seconds; probable deadlock")
|
2013-03-29 07:42:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 08:42:36 -04:00
|
|
|
func TestCmdStreamBad(t *testing.T) {
|
|
|
|
badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
|
2014-01-17 05:09:39 -05:00
|
|
|
out, err := CmdStream(badCmd, nil)
|
2013-03-11 08:42:36 -04:00
|
|
|
if err != nil {
|
2013-07-04 17:28:49 -04:00
|
|
|
t.Fatalf("Failed to start command: %s", err)
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
if output, err := ioutil.ReadAll(out); err == nil {
|
|
|
|
t.Fatalf("Command should have failed")
|
|
|
|
} else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
|
2013-07-04 17:28:49 -04:00
|
|
|
t.Fatalf("Wrong error value (%s)", err)
|
2013-03-11 08:42:36 -04:00
|
|
|
} else if s := string(output); s != "hello\n" {
|
|
|
|
t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCmdStreamGood(t *testing.T) {
|
|
|
|
cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
|
2014-01-17 05:09:39 -05:00
|
|
|
out, err := CmdStream(cmd, nil)
|
2013-03-11 08:42:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if output, err := ioutil.ReadAll(out); err != nil {
|
|
|
|
t.Fatalf("Command should not have failed (err=%s)", err)
|
|
|
|
} else if s := string(output); s != "hello\n" {
|
|
|
|
t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 19:05:36 -05:00
|
|
|
func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error) {
|
|
|
|
archive, err := TarWithOptions(origin, options)
|
2013-03-11 08:42:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-02-14 06:41:46 -05:00
|
|
|
defer archive.Close()
|
2013-06-14 13:47:49 -04:00
|
|
|
|
|
|
|
buf := make([]byte, 10)
|
|
|
|
if _, err := archive.Read(buf); err != nil {
|
2014-02-13 19:05:36 -05:00
|
|
|
return nil, err
|
2013-06-14 13:47:49 -04:00
|
|
|
}
|
2014-02-14 06:41:46 -05:00
|
|
|
wrap := io.MultiReader(bytes.NewReader(buf), archive)
|
2013-06-14 13:47:49 -04:00
|
|
|
|
|
|
|
detectedCompression := DetectCompression(buf)
|
2014-02-13 19:05:36 -05:00
|
|
|
compression := options.Compression
|
2013-06-14 13:47:49 -04:00
|
|
|
if detectedCompression.Extension() != compression.Extension() {
|
2014-02-13 19:05:36 -05:00
|
|
|
return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
|
2013-06-14 13:47:49 -04:00
|
|
|
}
|
|
|
|
|
2013-03-11 08:42:36 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "docker-test-untar")
|
|
|
|
if err != nil {
|
2014-02-13 19:05:36 -05:00
|
|
|
return nil, err
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
2014-02-14 06:41:46 -05:00
|
|
|
if err := Untar(wrap, tmp, nil); err != nil {
|
2014-02-13 19:05:36 -05:00
|
|
|
return nil, err
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
if _, err := os.Stat(tmp); err != nil {
|
2014-02-13 19:05:36 -05:00
|
|
|
return nil, err
|
2013-06-14 13:47:49 -04:00
|
|
|
}
|
2014-01-20 06:07:34 -05:00
|
|
|
|
2014-02-13 19:05:36 -05:00
|
|
|
return ChangesDirs(origin, tmp)
|
2013-06-14 13:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTarUntar(t *testing.T) {
|
|
|
|
origin, err := ioutil.TempDir("", "docker-test-untar-origin")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(origin)
|
|
|
|
if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range []Compression{
|
|
|
|
Uncompressed,
|
|
|
|
Gzip,
|
|
|
|
} {
|
2014-02-13 19:05:36 -05:00
|
|
|
changes, err := tarUntar(t, origin, &TarOptions{
|
|
|
|
Compression: c,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2013-06-14 13:47:49 -04:00
|
|
|
t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
|
|
|
|
}
|
2014-02-13 19:05:36 -05:00
|
|
|
|
|
|
|
if len(changes) != 0 {
|
|
|
|
t.Fatalf("Unexpected differences after tarUntar: %v", changes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTarWithOptions(t *testing.T) {
|
|
|
|
origin, err := ioutil.TempDir("", "docker-test-untar-origin")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(origin)
|
|
|
|
if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
opts *TarOptions
|
|
|
|
numChanges int
|
|
|
|
}{
|
|
|
|
{&TarOptions{Includes: []string{"1"}}, 1},
|
|
|
|
{&TarOptions{Excludes: []string{"2"}}, 1},
|
|
|
|
}
|
|
|
|
for _, testCase := range cases {
|
|
|
|
changes, err := tarUntar(t, origin, testCase.opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error tar/untar when testing inclusion/exclusion: %s", err)
|
|
|
|
}
|
|
|
|
if len(changes) != testCase.numChanges {
|
|
|
|
t.Errorf("Expected %d changes, got %d for %+v:",
|
|
|
|
testCase.numChanges, len(changes), testCase.opts)
|
|
|
|
}
|
2013-03-11 08:42:36 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-31 08:48:06 -05:00
|
|
|
|
|
|
|
// Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
|
|
|
|
// use PAX Global Extended Headers.
|
|
|
|
// Failing prevents the archives from being uncompressed during ADD
|
|
|
|
func TestTypeXGlobalHeaderDoesNotFail(t *testing.T) {
|
|
|
|
hdr := tar.Header{Typeflag: tar.TypeXGlobalHeader}
|
2014-05-20 20:10:35 -04:00
|
|
|
err := createTarFile("pax_global_header", "some_dir", &hdr, nil, true)
|
2014-01-31 08:48:06 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 18:45:40 -04:00
|
|
|
|
|
|
|
// Some tar have both GNU specific (huge uid) and Ustar specific (long name) things.
|
|
|
|
// Not supposed to happen (should use PAX instead of Ustar for long name) but it does and it should still work.
|
|
|
|
func TestUntarUstarGnuConflict(t *testing.T) {
|
|
|
|
f, err := os.Open("testdata/broken.tar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
tr := tar.NewReader(f)
|
|
|
|
// Iterate through the files in the archive.
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if hdr.Name == "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2014-06-12 01:15:53 -04:00
|
|
|
t.Fatalf("%s not found in the archive", "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm")
|
2014-05-15 18:45:40 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-16 14:50:02 -04:00
|
|
|
|
|
|
|
func prepareUntarSourceDirectory(numberOfFiles int, targetPath string) (int, error) {
|
|
|
|
fileData := []byte("fooo")
|
|
|
|
for n := 0; n < numberOfFiles; n++ {
|
|
|
|
fileName := fmt.Sprintf("file-%d", n)
|
|
|
|
if err := ioutil.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalSize := numberOfFiles * len(fileData)
|
|
|
|
return totalSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTarUntar(b *testing.B) {
|
|
|
|
origin, err := ioutil.TempDir("", "docker-test-untar-origin")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
tempDir, err := ioutil.TempDir("", "docker-test-untar-destination")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
target := path.Join(tempDir, "dest")
|
|
|
|
n, err := prepareUntarSourceDirectory(100, origin)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(n))
|
|
|
|
defer os.RemoveAll(origin)
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
err := TarUntar(origin, target)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(target)
|
|
|
|
}
|
|
|
|
}
|