archive_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package archive
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "testing"
  12. "time"
  13. )
  14. func TestCmdStreamLargeStderr(t *testing.T) {
  15. cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
  16. out, err := CmdStream(cmd, nil)
  17. if err != nil {
  18. t.Fatalf("Failed to start command: %s", err)
  19. }
  20. errCh := make(chan error)
  21. go func() {
  22. _, err := io.Copy(ioutil.Discard, out)
  23. errCh <- err
  24. }()
  25. select {
  26. case err := <-errCh:
  27. if err != nil {
  28. t.Fatalf("Command should not have failed (err=%.100s...)", err)
  29. }
  30. case <-time.After(5 * time.Second):
  31. t.Fatalf("Command did not complete in 5 seconds; probable deadlock")
  32. }
  33. }
  34. func TestCmdStreamBad(t *testing.T) {
  35. badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
  36. out, err := CmdStream(badCmd, nil)
  37. if err != nil {
  38. t.Fatalf("Failed to start command: %s", err)
  39. }
  40. if output, err := ioutil.ReadAll(out); err == nil {
  41. t.Fatalf("Command should have failed")
  42. } else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
  43. t.Fatalf("Wrong error value (%s)", err)
  44. } else if s := string(output); s != "hello\n" {
  45. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  46. }
  47. }
  48. func TestCmdStreamGood(t *testing.T) {
  49. cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
  50. out, err := CmdStream(cmd, nil)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if output, err := ioutil.ReadAll(out); err != nil {
  55. t.Fatalf("Command should not have failed (err=%s)", err)
  56. } else if s := string(output); s != "hello\n" {
  57. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  58. }
  59. }
  60. func tarUntar(t *testing.T, origin string, compression Compression) error {
  61. archive, err := Tar(origin, compression)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer archive.Close()
  66. buf := make([]byte, 10)
  67. if _, err := archive.Read(buf); err != nil {
  68. return err
  69. }
  70. wrap := io.MultiReader(bytes.NewReader(buf), archive)
  71. detectedCompression := DetectCompression(buf)
  72. if detectedCompression.Extension() != compression.Extension() {
  73. return fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
  74. }
  75. tmp, err := ioutil.TempDir("", "docker-test-untar")
  76. if err != nil {
  77. return err
  78. }
  79. defer os.RemoveAll(tmp)
  80. if err := Untar(wrap, tmp, nil); err != nil {
  81. return err
  82. }
  83. if _, err := os.Stat(tmp); err != nil {
  84. return err
  85. }
  86. changes, err := ChangesDirs(origin, tmp)
  87. if err != nil {
  88. return err
  89. }
  90. if len(changes) != 0 {
  91. t.Fatalf("Unexpected differences after tarUntar: %v", changes)
  92. }
  93. return nil
  94. }
  95. func TestTarUntar(t *testing.T) {
  96. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer os.RemoveAll(origin)
  101. if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
  102. t.Fatal(err)
  103. }
  104. if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
  105. t.Fatal(err)
  106. }
  107. for _, c := range []Compression{
  108. Uncompressed,
  109. Gzip,
  110. } {
  111. if err := tarUntar(t, origin, c); err != nil {
  112. t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
  113. }
  114. }
  115. }
  116. // Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
  117. // use PAX Global Extended Headers.
  118. // Failing prevents the archives from being uncompressed during ADD
  119. func TestTypeXGlobalHeaderDoesNotFail(t *testing.T) {
  120. hdr := tar.Header{Typeflag: tar.TypeXGlobalHeader}
  121. err := createTarFile("pax_global_header", "some_dir", &hdr, nil)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. }
  126. // Some tar have both GNU specific (huge uid) and Ustar specific (long name) things.
  127. // Not supposed to happen (should use PAX instead of Ustar for long name) but it does and it should still work.
  128. func TestUntarUstarGnuConflict(t *testing.T) {
  129. f, err := os.Open("testdata/broken.tar")
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. found := false
  134. tr := tar.NewReader(f)
  135. // Iterate through the files in the archive.
  136. for {
  137. hdr, err := tr.Next()
  138. if err == io.EOF {
  139. // end of tar archive
  140. break
  141. }
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. if hdr.Name == "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm" {
  146. found = true
  147. break
  148. }
  149. }
  150. if !found {
  151. t.Fatal("%s not found in the archive", "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm")
  152. }
  153. }