archive_test.go 6.4 KB

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