archive_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package archive
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "testing"
  11. "time"
  12. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  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, options *TarOptions) ([]Change, error) {
  61. archive, err := TarWithOptions(origin, options)
  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 nil, err
  69. }
  70. wrap := io.MultiReader(bytes.NewReader(buf), archive)
  71. detectedCompression := DetectCompression(buf)
  72. compression := options.Compression
  73. if detectedCompression.Extension() != compression.Extension() {
  74. return nil, fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
  75. }
  76. tmp, err := ioutil.TempDir("", "docker-test-untar")
  77. if err != nil {
  78. return nil, err
  79. }
  80. defer os.RemoveAll(tmp)
  81. if err := Untar(wrap, tmp, nil); err != nil {
  82. return nil, err
  83. }
  84. if _, err := os.Stat(tmp); err != nil {
  85. return nil, err
  86. }
  87. return ChangesDirs(origin, tmp)
  88. }
  89. func TestTarUntar(t *testing.T) {
  90. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer os.RemoveAll(origin)
  95. if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
  96. t.Fatal(err)
  97. }
  98. if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
  99. t.Fatal(err)
  100. }
  101. if err := ioutil.WriteFile(path.Join(origin, "3"), []byte("will be ignored"), 0700); err != nil {
  102. t.Fatal(err)
  103. }
  104. for _, c := range []Compression{
  105. Uncompressed,
  106. Gzip,
  107. } {
  108. changes, err := tarUntar(t, origin, &TarOptions{
  109. Compression: c,
  110. Excludes: []string{"3"},
  111. })
  112. if err != nil {
  113. t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
  114. }
  115. if len(changes) != 1 || changes[0].Path != "/3" {
  116. t.Fatalf("Unexpected differences after tarUntar: %v", changes)
  117. }
  118. }
  119. }
  120. func TestTarWithOptions(t *testing.T) {
  121. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. defer os.RemoveAll(origin)
  126. if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
  127. t.Fatal(err)
  128. }
  129. if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
  130. t.Fatal(err)
  131. }
  132. cases := []struct {
  133. opts *TarOptions
  134. numChanges int
  135. }{
  136. {&TarOptions{Includes: []string{"1"}}, 1},
  137. {&TarOptions{Excludes: []string{"2"}}, 1},
  138. }
  139. for _, testCase := range cases {
  140. changes, err := tarUntar(t, origin, testCase.opts)
  141. if err != nil {
  142. t.Fatalf("Error tar/untar when testing inclusion/exclusion: %s", err)
  143. }
  144. if len(changes) != testCase.numChanges {
  145. t.Errorf("Expected %d changes, got %d for %+v:",
  146. testCase.numChanges, len(changes), testCase.opts)
  147. }
  148. }
  149. }
  150. // Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
  151. // use PAX Global Extended Headers.
  152. // Failing prevents the archives from being uncompressed during ADD
  153. func TestTypeXGlobalHeaderDoesNotFail(t *testing.T) {
  154. hdr := tar.Header{Typeflag: tar.TypeXGlobalHeader}
  155. err := createTarFile("pax_global_header", "some_dir", &hdr, nil, true)
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. }
  160. // Some tar have both GNU specific (huge uid) and Ustar specific (long name) things.
  161. // Not supposed to happen (should use PAX instead of Ustar for long name) but it does and it should still work.
  162. func TestUntarUstarGnuConflict(t *testing.T) {
  163. f, err := os.Open("testdata/broken.tar")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. found := false
  168. tr := tar.NewReader(f)
  169. // Iterate through the files in the archive.
  170. for {
  171. hdr, err := tr.Next()
  172. if err == io.EOF {
  173. // end of tar archive
  174. break
  175. }
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. if hdr.Name == "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm" {
  180. found = true
  181. break
  182. }
  183. }
  184. if !found {
  185. t.Fatalf("%s not found in the archive", "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm")
  186. }
  187. }
  188. func prepareUntarSourceDirectory(numberOfFiles int, targetPath string) (int, error) {
  189. fileData := []byte("fooo")
  190. for n := 0; n < numberOfFiles; n++ {
  191. fileName := fmt.Sprintf("file-%d", n)
  192. if err := ioutil.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil {
  193. return 0, err
  194. }
  195. }
  196. totalSize := numberOfFiles * len(fileData)
  197. return totalSize, nil
  198. }
  199. func BenchmarkTarUntar(b *testing.B) {
  200. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  201. if err != nil {
  202. b.Fatal(err)
  203. }
  204. tempDir, err := ioutil.TempDir("", "docker-test-untar-destination")
  205. if err != nil {
  206. b.Fatal(err)
  207. }
  208. target := path.Join(tempDir, "dest")
  209. n, err := prepareUntarSourceDirectory(100, origin)
  210. if err != nil {
  211. b.Fatal(err)
  212. }
  213. b.ResetTimer()
  214. b.SetBytes(int64(n))
  215. defer os.RemoveAll(origin)
  216. defer os.RemoveAll(tempDir)
  217. for n := 0; n < b.N; n++ {
  218. err := TarUntar(origin, target)
  219. if err != nil {
  220. b.Fatal(err)
  221. }
  222. os.RemoveAll(target)
  223. }
  224. }