1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273 |
- package archive
- import (
- "archive/tar"
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
- "runtime"
- "strings"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- var tmp string
- func init() {
- tmp = "/tmp/"
- if runtime.GOOS == "windows" {
- tmp = os.Getenv("TEMP") + `\`
- }
- }
- func TestIsArchiveNilHeader(t *testing.T) {
- out := IsArchive(nil)
- if out {
- t.Fatalf("isArchive should return false as nil is not a valid archive header")
- }
- }
- func TestIsArchiveInvalidHeader(t *testing.T) {
- header := []byte{0x00, 0x01, 0x02}
- out := IsArchive(header)
- if out {
- t.Fatalf("isArchive should return false as %s is not a valid archive header", header)
- }
- }
- func TestIsArchiveBzip2(t *testing.T) {
- header := []byte{0x42, 0x5A, 0x68}
- out := IsArchive(header)
- if !out {
- t.Fatalf("isArchive should return true as %s is a bz2 header", header)
- }
- }
- func TestIsArchive7zip(t *testing.T) {
- header := []byte{0x50, 0x4b, 0x03, 0x04}
- out := IsArchive(header)
- if out {
- t.Fatalf("isArchive should return false as %s is a 7z header and it is not supported", header)
- }
- }
- func TestIsArchivePathDir(t *testing.T) {
- cmd := exec.Command("sh", "-c", "mkdir -p /tmp/archivedir")
- output, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatalf("Fail to create an archive file for test : %s.", output)
- }
- if IsArchivePath(tmp + "archivedir") {
- t.Fatalf("Incorrectly recognised directory as an archive")
- }
- }
- func TestIsArchivePathInvalidFile(t *testing.T) {
- cmd := exec.Command("sh", "-c", "dd if=/dev/zero bs=1024 count=1 of=/tmp/archive && gzip --stdout /tmp/archive > /tmp/archive.gz")
- output, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatalf("Fail to create an archive file for test : %s.", output)
- }
- if IsArchivePath(tmp + "archive") {
- t.Fatalf("Incorrectly recognised invalid tar path as archive")
- }
- if IsArchivePath(tmp + "archive.gz") {
- t.Fatalf("Incorrectly recognised invalid compressed tar path as archive")
- }
- }
- func TestIsArchivePathTar(t *testing.T) {
- var whichTar string
- if runtime.GOOS == "solaris" {
- whichTar = "gtar"
- } else {
- whichTar = "tar"
- }
- cmdStr := fmt.Sprintf("touch /tmp/archivedata && %s -cf /tmp/archive /tmp/archivedata && gzip --stdout /tmp/archive > /tmp/archive.gz", whichTar)
- cmd := exec.Command("sh", "-c", cmdStr)
- output, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatalf("Fail to create an archive file for test : %s.", output)
- }
- if !IsArchivePath(tmp + "/archive") {
- t.Fatalf("Did not recognise valid tar path as archive")
- }
- if !IsArchivePath(tmp + "archive.gz") {
- t.Fatalf("Did not recognise valid compressed tar path as archive")
- }
- }
- func testDecompressStream(t *testing.T, ext, compressCommand string) {
- cmd := exec.Command("sh", "-c",
- fmt.Sprintf("touch /tmp/archive && %s /tmp/archive", compressCommand))
- output, err := cmd.CombinedOutput()
- if err != nil {
- t.Fatalf("Failed to create an archive file for test : %s.", output)
- }
- filename := "archive." + ext
- archive, err := os.Open(tmp + filename)
- if err != nil {
- t.Fatalf("Failed to open file %s: %v", filename, err)
- }
- defer archive.Close()
- r, err := DecompressStream(archive)
- if err != nil {
- t.Fatalf("Failed to decompress %s: %v", filename, err)
- }
- if _, err = ioutil.ReadAll(r); err != nil {
- t.Fatalf("Failed to read the decompressed stream: %v ", err)
- }
- if err = r.Close(); err != nil {
- t.Fatalf("Failed to close the decompressed stream: %v ", err)
- }
- }
- func TestDecompressStreamGzip(t *testing.T) {
- testDecompressStream(t, "gz", "gzip -f")
- }
- func TestDecompressStreamBzip2(t *testing.T) {
- testDecompressStream(t, "bz2", "bzip2 -f")
- }
- func TestDecompressStreamXz(t *testing.T) {
- if runtime.GOOS == "windows" {
- t.Skip("Xz not present in msys2")
- }
- testDecompressStream(t, "xz", "xz -f")
- }
- func TestCompressStreamXzUnsupported(t *testing.T) {
- dest, err := os.Create(tmp + "dest")
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- defer dest.Close()
- _, err = CompressStream(dest, Xz)
- if err == nil {
- t.Fatalf("Should fail as xz is unsupported for compression format.")
- }
- }
- func TestCompressStreamBzip2Unsupported(t *testing.T) {
- dest, err := os.Create(tmp + "dest")
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- defer dest.Close()
- _, err = CompressStream(dest, Xz)
- if err == nil {
- t.Fatalf("Should fail as xz is unsupported for compression format.")
- }
- }
- func TestCompressStreamInvalid(t *testing.T) {
- dest, err := os.Create(tmp + "dest")
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- defer dest.Close()
- _, err = CompressStream(dest, -1)
- if err == nil {
- t.Fatalf("Should fail as xz is unsupported for compression format.")
- }
- }
- func TestExtensionInvalid(t *testing.T) {
- compression := Compression(-1)
- output := compression.Extension()
- if output != "" {
- t.Fatalf("The extension of an invalid compression should be an empty string.")
- }
- }
- func TestExtensionUncompressed(t *testing.T) {
- compression := Uncompressed
- output := compression.Extension()
- if output != "tar" {
- t.Fatalf("The extension of an uncompressed archive should be 'tar'.")
- }
- }
- func TestExtensionBzip2(t *testing.T) {
- compression := Bzip2
- output := compression.Extension()
- if output != "tar.bz2" {
- t.Fatalf("The extension of a bzip2 archive should be 'tar.bz2'")
- }
- }
- func TestExtensionGzip(t *testing.T) {
- compression := Gzip
- output := compression.Extension()
- if output != "tar.gz" {
- t.Fatalf("The extension of a bzip2 archive should be 'tar.gz'")
- }
- }
- func TestExtensionXz(t *testing.T) {
- compression := Xz
- output := compression.Extension()
- if output != "tar.xz" {
- t.Fatalf("The extension of a bzip2 archive should be 'tar.xz'")
- }
- }
- func TestCmdStreamLargeStderr(t *testing.T) {
- cmd := exec.Command("sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
- out, _, err := cmdStream(cmd, nil)
- if err != nil {
- t.Fatalf("Failed to start command: %s", err)
- }
- errCh := make(chan error)
- go func() {
- _, err := io.Copy(ioutil.Discard, out)
- errCh <- err
- }()
- select {
- case err := <-errCh:
- if err != nil {
- t.Fatalf("Command should not have failed (err=%.100s...)", err)
- }
- case <-time.After(5 * time.Second):
- t.Fatalf("Command did not complete in 5 seconds; probable deadlock")
- }
- }
- func TestCmdStreamBad(t *testing.T) {
- // TODO Windows: Figure out why this is failing in CI but not locally
- if runtime.GOOS == "windows" {
- t.Skip("Failing on Windows CI machines")
- }
- badCmd := exec.Command("sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
- out, _, err := cmdStream(badCmd, nil)
- if err != nil {
- t.Fatalf("Failed to start command: %s", err)
- }
- 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" {
- t.Fatalf("Wrong error value (%s)", err)
- } 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("sh", "-c", "echo hello; exit 0")
- out, _, err := cmdStream(cmd, nil)
- 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)
- }
- }
- func TestUntarPathWithInvalidDest(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tempFolder)
- invalidDestFolder := filepath.Join(tempFolder, "invalidDest")
- // Create a src file
- srcFile := filepath.Join(tempFolder, "src")
- tarFile := filepath.Join(tempFolder, "src.tar")
- os.Create(srcFile)
- os.Create(invalidDestFolder) // being a file (not dir) should cause an error
- // Translate back to Unix semantics as next exec.Command is run under sh
- srcFileU := srcFile
- tarFileU := tarFile
- if runtime.GOOS == "windows" {
- tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
- srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
- }
- cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
- _, err = cmd.CombinedOutput()
- if err != nil {
- t.Fatal(err)
- }
- err = UntarPath(tarFile, invalidDestFolder)
- if err == nil {
- t.Fatalf("UntarPath with invalid destination path should throw an error.")
- }
- }
- func TestUntarPathWithInvalidSrc(t *testing.T) {
- dest, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- defer os.RemoveAll(dest)
- err = UntarPath("/invalid/path", dest)
- if err == nil {
- t.Fatalf("UntarPath with invalid src path should throw an error.")
- }
- }
- func TestUntarPath(t *testing.T) {
- tmpFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpFolder)
- srcFile := filepath.Join(tmpFolder, "src")
- tarFile := filepath.Join(tmpFolder, "src.tar")
- os.Create(filepath.Join(tmpFolder, "src"))
- destFolder := filepath.Join(tmpFolder, "dest")
- err = os.MkdirAll(destFolder, 0740)
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- // Translate back to Unix semantics as next exec.Command is run under sh
- srcFileU := srcFile
- tarFileU := tarFile
- if runtime.GOOS == "windows" {
- tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
- srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
- }
- cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
- _, err = cmd.CombinedOutput()
- if err != nil {
- t.Fatal(err)
- }
- err = UntarPath(tarFile, destFolder)
- if err != nil {
- t.Fatalf("UntarPath shouldn't throw an error, %s.", err)
- }
- expectedFile := filepath.Join(destFolder, srcFileU)
- _, err = os.Stat(expectedFile)
- if err != nil {
- t.Fatalf("Destination folder should contain the source file but did not.")
- }
- }
- // Do the same test as above but with the destination as file, it should fail
- func TestUntarPathWithDestinationFile(t *testing.T) {
- tmpFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpFolder)
- srcFile := filepath.Join(tmpFolder, "src")
- tarFile := filepath.Join(tmpFolder, "src.tar")
- os.Create(filepath.Join(tmpFolder, "src"))
- // Translate back to Unix semantics as next exec.Command is run under sh
- srcFileU := srcFile
- tarFileU := tarFile
- if runtime.GOOS == "windows" {
- tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
- srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
- }
- cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
- _, err = cmd.CombinedOutput()
- if err != nil {
- t.Fatal(err)
- }
- destFile := filepath.Join(tmpFolder, "dest")
- _, err = os.Create(destFile)
- if err != nil {
- t.Fatalf("Fail to create the destination file")
- }
- err = UntarPath(tarFile, destFile)
- if err == nil {
- t.Fatalf("UntarPath should throw an error if the destination if a file")
- }
- }
- // Do the same test as above but with the destination folder already exists
- // and the destination file is a directory
- // It's working, see https://github.com/docker/docker/issues/10040
- func TestUntarPathWithDestinationSrcFileAsFolder(t *testing.T) {
- tmpFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpFolder)
- srcFile := filepath.Join(tmpFolder, "src")
- tarFile := filepath.Join(tmpFolder, "src.tar")
- os.Create(srcFile)
- // Translate back to Unix semantics as next exec.Command is run under sh
- srcFileU := srcFile
- tarFileU := tarFile
- if runtime.GOOS == "windows" {
- tarFileU = "/tmp/" + filepath.Base(filepath.Dir(tarFile)) + "/src.tar"
- srcFileU = "/tmp/" + filepath.Base(filepath.Dir(srcFile)) + "/src"
- }
- cmd := exec.Command("sh", "-c", "tar cf "+tarFileU+" "+srcFileU)
- _, err = cmd.CombinedOutput()
- if err != nil {
- t.Fatal(err)
- }
- destFolder := filepath.Join(tmpFolder, "dest")
- err = os.MkdirAll(destFolder, 0740)
- if err != nil {
- t.Fatalf("Fail to create the destination folder")
- }
- // Let's create a folder that will has the same path as the extracted file (from tar)
- destSrcFileAsFolder := filepath.Join(destFolder, srcFileU)
- err = os.MkdirAll(destSrcFileAsFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = UntarPath(tarFile, destFolder)
- if err != nil {
- t.Fatalf("UntarPath should throw not throw an error if the extracted file already exists and is a folder")
- }
- }
- func TestCopyWithTarInvalidSrc(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(nil)
- }
- destFolder := filepath.Join(tempFolder, "dest")
- invalidSrc := filepath.Join(tempFolder, "doesnotexists")
- err = os.MkdirAll(destFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = CopyWithTar(invalidSrc, destFolder)
- if err == nil {
- t.Fatalf("archiver.CopyWithTar with invalid src path should throw an error.")
- }
- }
- func TestCopyWithTarInexistentDestWillCreateIt(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(nil)
- }
- srcFolder := filepath.Join(tempFolder, "src")
- inexistentDestFolder := filepath.Join(tempFolder, "doesnotexists")
- err = os.MkdirAll(srcFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = CopyWithTar(srcFolder, inexistentDestFolder)
- if err != nil {
- t.Fatalf("CopyWithTar with an inexistent folder shouldn't fail.")
- }
- _, err = os.Stat(inexistentDestFolder)
- if err != nil {
- t.Fatalf("CopyWithTar with an inexistent folder should create it.")
- }
- }
- // Test CopyWithTar with a file as src
- func TestCopyWithTarSrcFile(t *testing.T) {
- folder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(folder)
- dest := filepath.Join(folder, "dest")
- srcFolder := filepath.Join(folder, "src")
- src := filepath.Join(folder, filepath.Join("src", "src"))
- err = os.MkdirAll(srcFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = os.MkdirAll(dest, 0740)
- if err != nil {
- t.Fatal(err)
- }
- ioutil.WriteFile(src, []byte("content"), 0777)
- err = CopyWithTar(src, dest)
- if err != nil {
- t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
- }
- _, err = os.Stat(dest)
- // FIXME Check the content
- if err != nil {
- t.Fatalf("Destination file should be the same as the source.")
- }
- }
- // Test CopyWithTar with a folder as src
- func TestCopyWithTarSrcFolder(t *testing.T) {
- folder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(folder)
- dest := filepath.Join(folder, "dest")
- src := filepath.Join(folder, filepath.Join("src", "folder"))
- err = os.MkdirAll(src, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = os.MkdirAll(dest, 0740)
- if err != nil {
- t.Fatal(err)
- }
- ioutil.WriteFile(filepath.Join(src, "file"), []byte("content"), 0777)
- err = CopyWithTar(src, dest)
- if err != nil {
- t.Fatalf("archiver.CopyWithTar shouldn't throw an error, %s.", err)
- }
- _, err = os.Stat(dest)
- // FIXME Check the content (the file inside)
- if err != nil {
- t.Fatalf("Destination folder should contain the source file but did not.")
- }
- }
- func TestCopyFileWithTarInvalidSrc(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tempFolder)
- destFolder := filepath.Join(tempFolder, "dest")
- err = os.MkdirAll(destFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- invalidFile := filepath.Join(tempFolder, "doesnotexists")
- err = CopyFileWithTar(invalidFile, destFolder)
- if err == nil {
- t.Fatalf("archiver.CopyWithTar with invalid src path should throw an error.")
- }
- }
- func TestCopyFileWithTarInexistentDestWillCreateIt(t *testing.T) {
- tempFolder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(nil)
- }
- defer os.RemoveAll(tempFolder)
- srcFile := filepath.Join(tempFolder, "src")
- inexistentDestFolder := filepath.Join(tempFolder, "doesnotexists")
- _, err = os.Create(srcFile)
- if err != nil {
- t.Fatal(err)
- }
- err = CopyFileWithTar(srcFile, inexistentDestFolder)
- if err != nil {
- t.Fatalf("CopyWithTar with an inexistent folder shouldn't fail.")
- }
- _, err = os.Stat(inexistentDestFolder)
- if err != nil {
- t.Fatalf("CopyWithTar with an inexistent folder should create it.")
- }
- // FIXME Test the src file and content
- }
- func TestCopyFileWithTarSrcFolder(t *testing.T) {
- folder, err := ioutil.TempDir("", "docker-archive-copyfilewithtar-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(folder)
- dest := filepath.Join(folder, "dest")
- src := filepath.Join(folder, "srcfolder")
- err = os.MkdirAll(src, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = os.MkdirAll(dest, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = CopyFileWithTar(src, dest)
- if err == nil {
- t.Fatalf("CopyFileWithTar should throw an error with a folder.")
- }
- }
- func TestCopyFileWithTarSrcFile(t *testing.T) {
- folder, err := ioutil.TempDir("", "docker-archive-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(folder)
- dest := filepath.Join(folder, "dest")
- srcFolder := filepath.Join(folder, "src")
- src := filepath.Join(folder, filepath.Join("src", "src"))
- err = os.MkdirAll(srcFolder, 0740)
- if err != nil {
- t.Fatal(err)
- }
- err = os.MkdirAll(dest, 0740)
- if err != nil {
- t.Fatal(err)
- }
- ioutil.WriteFile(src, []byte("content"), 0777)
- err = CopyWithTar(src, dest+"/")
- if err != nil {
- t.Fatalf("archiver.CopyFileWithTar shouldn't throw an error, %s.", err)
- }
- _, err = os.Stat(dest)
- if err != nil {
- t.Fatalf("Destination folder should contain the source file but did not.")
- }
- }
- func TestTarFiles(t *testing.T) {
- // TODO Windows: Figure out how to port this test.
- if runtime.GOOS == "windows" {
- t.Skip("Failing on Windows")
- }
- // try without hardlinks
- if err := checkNoChanges(1000, false); err != nil {
- t.Fatal(err)
- }
- // try with hardlinks
- if err := checkNoChanges(1000, true); err != nil {
- t.Fatal(err)
- }
- }
- func checkNoChanges(fileNum int, hardlinks bool) error {
- srcDir, err := ioutil.TempDir("", "docker-test-srcDir")
- if err != nil {
- return err
- }
- defer os.RemoveAll(srcDir)
- destDir, err := ioutil.TempDir("", "docker-test-destDir")
- if err != nil {
- return err
- }
- defer os.RemoveAll(destDir)
- _, err = prepareUntarSourceDirectory(fileNum, srcDir, hardlinks)
- if err != nil {
- return err
- }
- err = TarUntar(srcDir, destDir)
- if err != nil {
- return err
- }
- changes, err := ChangesDirs(destDir, srcDir)
- if err != nil {
- return err
- }
- if len(changes) > 0 {
- return fmt.Errorf("with %d files and %v hardlinks: expected 0 changes, got %d", fileNum, hardlinks, len(changes))
- }
- return nil
- }
- func tarUntar(t *testing.T, origin string, options *TarOptions) ([]Change, error) {
- archive, err := TarWithOptions(origin, options)
- if err != nil {
- t.Fatal(err)
- }
- defer archive.Close()
- buf := make([]byte, 10)
- if _, err := archive.Read(buf); err != nil {
- return nil, err
- }
- wrap := io.MultiReader(bytes.NewReader(buf), archive)
- detectedCompression := DetectCompression(buf)
- compression := options.Compression
- if detectedCompression.Extension() != compression.Extension() {
- return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
- }
- tmp, err := ioutil.TempDir("", "docker-test-untar")
- if err != nil {
- return nil, err
- }
- defer os.RemoveAll(tmp)
- if err := Untar(wrap, tmp, nil); err != nil {
- return nil, err
- }
- if _, err := os.Stat(tmp); err != nil {
- return nil, err
- }
- return ChangesDirs(origin, tmp)
- }
- func TestTarUntar(t *testing.T) {
- // TODO Windows: Figure out how to fix this test.
- if runtime.GOOS == "windows" {
- t.Skip("Failing on Windows")
- }
- origin, err := ioutil.TempDir("", "docker-test-untar-origin")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(origin)
- if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
- t.Fatal(err)
- }
- if err := ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
- t.Fatal(err)
- }
- if err := ioutil.WriteFile(filepath.Join(origin, "3"), []byte("will be ignored"), 0700); err != nil {
- t.Fatal(err)
- }
- for _, c := range []Compression{
- Uncompressed,
- Gzip,
- } {
- changes, err := tarUntar(t, origin, &TarOptions{
- Compression: c,
- ExcludePatterns: []string{"3"},
- })
- if err != nil {
- t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
- }
- if len(changes) != 1 || changes[0].Path != "/3" {
- t.Fatalf("Unexpected differences after tarUntar: %v", changes)
- }
- }
- }
- func TestTarWithOptions(t *testing.T) {
- // TODO Windows: Figure out how to fix this test.
- if runtime.GOOS == "windows" {
- t.Skip("Failing on Windows")
- }
- origin, err := ioutil.TempDir("", "docker-test-untar-origin")
- if err != nil {
- t.Fatal(err)
- }
- if _, err := ioutil.TempDir(origin, "folder"); err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(origin)
- if err := ioutil.WriteFile(filepath.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
- t.Fatal(err)
- }
- if err := ioutil.WriteFile(filepath.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
- t.Fatal(err)
- }
- cases := []struct {
- opts *TarOptions
- numChanges int
- }{
- {&TarOptions{IncludeFiles: []string{"1"}}, 2},
- {&TarOptions{ExcludePatterns: []string{"2"}}, 1},
- {&TarOptions{ExcludePatterns: []string{"1", "folder*"}}, 2},
- {&TarOptions{IncludeFiles: []string{"1", "1"}}, 2},
- {&TarOptions{IncludeFiles: []string{"1"}, RebaseNames: map[string]string{"1": "test"}}, 4},
- }
- 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)
- }
- }
- }
- // 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}
- tmpDir, err := ioutil.TempDir("", "docker-test-archive-pax-test")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
- err = createTarFile(filepath.Join(tmpDir, "pax_global_header"), tmpDir, &hdr, nil, true, nil, false)
- if err != nil {
- t.Fatal(err)
- }
- }
- // 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)
- }
- defer f.Close()
- 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 {
- t.Fatalf("%s not found in the archive", "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm")
- }
- }
- func prepareUntarSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) {
- fileData := []byte("fooo")
- for n := 0; n < numberOfFiles; n++ {
- fileName := fmt.Sprintf("file-%d", n)
- if err := ioutil.WriteFile(filepath.Join(targetPath, fileName), fileData, 0700); err != nil {
- return 0, err
- }
- if makeLinks {
- if err := os.Link(filepath.Join(targetPath, fileName), filepath.Join(targetPath, fileName+"-link")); 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 := filepath.Join(tempDir, "dest")
- n, err := prepareUntarSourceDirectory(100, origin, false)
- if err != nil {
- b.Fatal(err)
- }
- defer os.RemoveAll(origin)
- defer os.RemoveAll(tempDir)
- b.ResetTimer()
- b.SetBytes(int64(n))
- for n := 0; n < b.N; n++ {
- err := TarUntar(origin, target)
- if err != nil {
- b.Fatal(err)
- }
- os.RemoveAll(target)
- }
- }
- func BenchmarkTarUntarWithLinks(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 := filepath.Join(tempDir, "dest")
- n, err := prepareUntarSourceDirectory(100, origin, true)
- if err != nil {
- b.Fatal(err)
- }
- defer os.RemoveAll(origin)
- defer os.RemoveAll(tempDir)
- b.ResetTimer()
- b.SetBytes(int64(n))
- for n := 0; n < b.N; n++ {
- err := TarUntar(origin, target)
- if err != nil {
- b.Fatal(err)
- }
- os.RemoveAll(target)
- }
- }
- func TestUntarInvalidFilenames(t *testing.T) {
- // TODO Windows: Figure out how to fix this test.
- if runtime.GOOS == "windows" {
- t.Skip("Passes but hits breakoutError: platform and architecture is not supported")
- }
- for i, headers := range [][]*tar.Header{
- {
- {
- Name: "../victim/dotdot",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- {
- {
- // Note the leading slash
- Name: "/../victim/slash-dotdot",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- } {
- if err := testBreakout("untar", "docker-TestUntarInvalidFilenames", headers); err != nil {
- t.Fatalf("i=%d. %v", i, err)
- }
- }
- }
- func TestUntarHardlinkToSymlink(t *testing.T) {
- // TODO Windows. There may be a way of running this, but turning off for now
- if runtime.GOOS == "windows" {
- t.Skip("hardlinks on Windows")
- }
- for i, headers := range [][]*tar.Header{
- {
- {
- Name: "symlink1",
- Typeflag: tar.TypeSymlink,
- Linkname: "regfile",
- Mode: 0644,
- },
- {
- Name: "symlink2",
- Typeflag: tar.TypeLink,
- Linkname: "symlink1",
- Mode: 0644,
- },
- {
- Name: "regfile",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- } {
- if err := testBreakout("untar", "docker-TestUntarHardlinkToSymlink", headers); err != nil {
- t.Fatalf("i=%d. %v", i, err)
- }
- }
- }
- func TestUntarInvalidHardlink(t *testing.T) {
- // TODO Windows. There may be a way of running this, but turning off for now
- if runtime.GOOS == "windows" {
- t.Skip("hardlinks on Windows")
- }
- for i, headers := range [][]*tar.Header{
- { // try reading victim/hello (../)
- {
- Name: "dotdot",
- Typeflag: tar.TypeLink,
- Linkname: "../victim/hello",
- Mode: 0644,
- },
- },
- { // try reading victim/hello (/../)
- {
- Name: "slash-dotdot",
- Typeflag: tar.TypeLink,
- // Note the leading slash
- Linkname: "/../victim/hello",
- Mode: 0644,
- },
- },
- { // try writing victim/file
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeLink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "loophole-victim/file",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- { // try reading victim/hello (hardlink, symlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeLink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "symlink",
- Typeflag: tar.TypeSymlink,
- Linkname: "loophole-victim/hello",
- Mode: 0644,
- },
- },
- { // Try reading victim/hello (hardlink, hardlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeLink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "hardlink",
- Typeflag: tar.TypeLink,
- Linkname: "loophole-victim/hello",
- Mode: 0644,
- },
- },
- { // Try removing victim directory (hardlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeLink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- } {
- if err := testBreakout("untar", "docker-TestUntarInvalidHardlink", headers); err != nil {
- t.Fatalf("i=%d. %v", i, err)
- }
- }
- }
- func TestUntarInvalidSymlink(t *testing.T) {
- // TODO Windows. There may be a way of running this, but turning off for now
- if runtime.GOOS == "windows" {
- t.Skip("hardlinks on Windows")
- }
- for i, headers := range [][]*tar.Header{
- { // try reading victim/hello (../)
- {
- Name: "dotdot",
- Typeflag: tar.TypeSymlink,
- Linkname: "../victim/hello",
- Mode: 0644,
- },
- },
- { // try reading victim/hello (/../)
- {
- Name: "slash-dotdot",
- Typeflag: tar.TypeSymlink,
- // Note the leading slash
- Linkname: "/../victim/hello",
- Mode: 0644,
- },
- },
- { // try writing victim/file
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeSymlink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "loophole-victim/file",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- { // try reading victim/hello (symlink, symlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeSymlink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "symlink",
- Typeflag: tar.TypeSymlink,
- Linkname: "loophole-victim/hello",
- Mode: 0644,
- },
- },
- { // try reading victim/hello (symlink, hardlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeSymlink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "hardlink",
- Typeflag: tar.TypeLink,
- Linkname: "loophole-victim/hello",
- Mode: 0644,
- },
- },
- { // try removing victim directory (symlink)
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeSymlink,
- Linkname: "../victim",
- Mode: 0755,
- },
- {
- Name: "loophole-victim",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- { // try writing to victim/newdir/newfile with a symlink in the path
- {
- // this header needs to be before the next one, or else there is an error
- Name: "dir/loophole",
- Typeflag: tar.TypeSymlink,
- Linkname: "../../victim",
- Mode: 0755,
- },
- {
- Name: "dir/loophole/newdir/newfile",
- Typeflag: tar.TypeReg,
- Mode: 0644,
- },
- },
- } {
- if err := testBreakout("untar", "docker-TestUntarInvalidSymlink", headers); err != nil {
- t.Fatalf("i=%d. %v", i, err)
- }
- }
- }
- func TestTempArchiveCloseMultipleTimes(t *testing.T) {
- reader := ioutil.NopCloser(strings.NewReader("hello"))
- tempArchive, err := NewTempArchive(reader, "")
- buf := make([]byte, 10)
- n, err := tempArchive.Read(buf)
- if n != 5 {
- t.Fatalf("Expected to read 5 bytes. Read %d instead", n)
- }
- for i := 0; i < 3; i++ {
- if err = tempArchive.Close(); err != nil {
- t.Fatalf("i=%d. Unexpected error closing temp archive: %v", i, err)
- }
- }
- }
- func TestReplaceFileTarWrapper(t *testing.T) {
- filesInArchive := 20
- testcases := []struct {
- doc string
- filename string
- modifier TarModifierFunc
- expected string
- fileCount int
- }{
- {
- doc: "Modifier creates a new file",
- filename: "newfile",
- modifier: createModifier(t),
- expected: "the new content",
- fileCount: filesInArchive + 1,
- },
- {
- doc: "Modifier replaces a file",
- filename: "file-2",
- modifier: createOrReplaceModifier,
- expected: "the new content",
- fileCount: filesInArchive,
- },
- {
- doc: "Modifier replaces the last file",
- filename: fmt.Sprintf("file-%d", filesInArchive-1),
- modifier: createOrReplaceModifier,
- expected: "the new content",
- fileCount: filesInArchive,
- },
- {
- doc: "Modifier appends to a file",
- filename: "file-3",
- modifier: appendModifier,
- expected: "fooo\nnext line",
- fileCount: filesInArchive,
- },
- }
- for _, testcase := range testcases {
- sourceArchive, cleanup := buildSourceArchive(t, filesInArchive)
- defer cleanup()
- resultArchive := ReplaceFileTarWrapper(
- sourceArchive,
- map[string]TarModifierFunc{testcase.filename: testcase.modifier})
- actual := readFileFromArchive(t, resultArchive, testcase.filename, testcase.fileCount, testcase.doc)
- assert.Equal(t, testcase.expected, actual, testcase.doc)
- }
- }
- func buildSourceArchive(t *testing.T, numberOfFiles int) (io.ReadCloser, func()) {
- srcDir, err := ioutil.TempDir("", "docker-test-srcDir")
- require.NoError(t, err)
- _, err = prepareUntarSourceDirectory(numberOfFiles, srcDir, false)
- require.NoError(t, err)
- sourceArchive, err := TarWithOptions(srcDir, &TarOptions{})
- require.NoError(t, err)
- return sourceArchive, func() {
- os.RemoveAll(srcDir)
- sourceArchive.Close()
- }
- }
- func createOrReplaceModifier(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
- return &tar.Header{
- Mode: 0600,
- Typeflag: tar.TypeReg,
- }, []byte("the new content"), nil
- }
- func createModifier(t *testing.T) TarModifierFunc {
- return func(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
- assert.Nil(t, content)
- return createOrReplaceModifier(path, header, content)
- }
- }
- func appendModifier(path string, header *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
- buffer := bytes.Buffer{}
- if content != nil {
- if _, err := buffer.ReadFrom(content); err != nil {
- return nil, nil, err
- }
- }
- buffer.WriteString("\nnext line")
- return &tar.Header{Mode: 0600, Typeflag: tar.TypeReg}, buffer.Bytes(), nil
- }
- func readFileFromArchive(t *testing.T, archive io.ReadCloser, name string, expectedCount int, doc string) string {
- destDir, err := ioutil.TempDir("", "docker-test-destDir")
- require.NoError(t, err)
- defer os.RemoveAll(destDir)
- err = Untar(archive, destDir, nil)
- require.NoError(t, err)
- files, _ := ioutil.ReadDir(destDir)
- assert.Len(t, files, expectedCount, doc)
- content, err := ioutil.ReadFile(filepath.Join(destDir, name))
- assert.NoError(t, err)
- return string(content)
- }
|