archive_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. func TestTarUntarFile(t *testing.T) {
  147. origin, err := ioutil.TempDir("", "docker-test-untar-origin-file")
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. defer os.RemoveAll(origin)
  152. if err := os.MkdirAll(path.Join(origin, "before"), 0700); err != nil {
  153. t.Fatal(err)
  154. }
  155. if err := os.MkdirAll(path.Join(origin, "after"), 0700); err != nil {
  156. t.Fatal(err)
  157. }
  158. if err := ioutil.WriteFile(path.Join(origin, "before", "file"), []byte("hello world"), 0700); err != nil {
  159. t.Fatal(err)
  160. }
  161. if err := ioutil.WriteFile(path.Join(origin, "after", "file2"), []byte("please overwrite me"), 0700); err != nil {
  162. t.Fatal(err)
  163. }
  164. tar, err := TarWithOptions(path.Join(origin, "before"), &TarOptions{Compression: Uncompressed, Includes: []string{"file"}})
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. if err := Untar(tar, path.Join(origin, "after", "file2"), nil); err != nil {
  169. t.Fatal(err)
  170. }
  171. catCmd := exec.Command("cat", path.Join(origin, "after", "file2"))
  172. out, err := CmdStream(catCmd, nil)
  173. if err != nil {
  174. t.Fatalf("Failed to start command: %s", err)
  175. }
  176. if output, err := ioutil.ReadAll(out); err != nil {
  177. t.Error(err)
  178. } else if string(output) != "hello world" {
  179. t.Fatalf("Expected 'hello world', got '%s'", output)
  180. }
  181. }
  182. // Some tar archives such as http://haproxy.1wt.eu/download/1.5/src/devel/haproxy-1.5-dev21.tar.gz
  183. // use PAX Global Extended Headers.
  184. // Failing prevents the archives from being uncompressed during ADD
  185. func TestTypeXGlobalHeaderDoesNotFail(t *testing.T) {
  186. hdr := tar.Header{Typeflag: tar.TypeXGlobalHeader}
  187. err := createTarFile("pax_global_header", "some_dir", &hdr, nil, true)
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. }
  192. // Some tar have both GNU specific (huge uid) and Ustar specific (long name) things.
  193. // Not supposed to happen (should use PAX instead of Ustar for long name) but it does and it should still work.
  194. func TestUntarUstarGnuConflict(t *testing.T) {
  195. f, err := os.Open("testdata/broken.tar")
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. found := false
  200. tr := tar.NewReader(f)
  201. // Iterate through the files in the archive.
  202. for {
  203. hdr, err := tr.Next()
  204. if err == io.EOF {
  205. // end of tar archive
  206. break
  207. }
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. if hdr.Name == "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm" {
  212. found = true
  213. break
  214. }
  215. }
  216. if !found {
  217. t.Fatalf("%s not found in the archive", "root/.cpanm/work/1395823785.24209/Plack-1.0030/blib/man3/Plack::Middleware::LighttpdScriptNameFix.3pm")
  218. }
  219. }