archive_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. )
  13. func TestCmdStreamLargeStderr(t *testing.T) {
  14. cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
  15. out, err := CmdStream(cmd, nil, nil)
  16. if err != nil {
  17. t.Fatalf("Failed to start command: %s", err)
  18. }
  19. errCh := make(chan error)
  20. go func() {
  21. _, err := io.Copy(ioutil.Discard, out)
  22. errCh <- err
  23. }()
  24. select {
  25. case err := <-errCh:
  26. if err != nil {
  27. t.Fatalf("Command should not have failed (err=%.100s...)", err)
  28. }
  29. case <-time.After(5 * time.Second):
  30. t.Fatalf("Command did not complete in 5 seconds; probable deadlock")
  31. }
  32. }
  33. func TestCmdStreamBad(t *testing.T) {
  34. badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
  35. out, err := CmdStream(badCmd, nil, nil)
  36. if err != nil {
  37. t.Fatalf("Failed to start command: %s", err)
  38. }
  39. if output, err := ioutil.ReadAll(out); err == nil {
  40. t.Fatalf("Command should have failed")
  41. } else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
  42. t.Fatalf("Wrong error value (%s)", err)
  43. } else if s := string(output); s != "hello\n" {
  44. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  45. }
  46. }
  47. func TestCmdStreamGood(t *testing.T) {
  48. cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
  49. out, err := CmdStream(cmd, nil, nil)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if output, err := ioutil.ReadAll(out); err != nil {
  54. t.Fatalf("Command should not have failed (err=%s)", err)
  55. } else if s := string(output); s != "hello\n" {
  56. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  57. }
  58. }
  59. func tarUntar(t *testing.T, origin string, compression Compression) error {
  60. archive, err := Tar(origin, compression)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. buf := make([]byte, 10)
  65. if _, err := archive.Read(buf); err != nil {
  66. return err
  67. }
  68. archive = io.MultiReader(bytes.NewReader(buf), archive)
  69. detectedCompression := DetectCompression(buf)
  70. if detectedCompression.Extension() != compression.Extension() {
  71. return fmt.Errorf("Wrong compression detected. Actual compression: %s, found %s", compression.Extension(), detectedCompression.Extension())
  72. }
  73. tmp, err := ioutil.TempDir("", "docker-test-untar")
  74. if err != nil {
  75. return err
  76. }
  77. defer os.RemoveAll(tmp)
  78. if err := Untar(archive, tmp, nil); err != nil {
  79. return err
  80. }
  81. if _, err := os.Stat(tmp); err != nil {
  82. return err
  83. }
  84. return nil
  85. }
  86. func TestTarUntar(t *testing.T) {
  87. origin, err := ioutil.TempDir("", "docker-test-untar-origin")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(origin)
  92. if err := ioutil.WriteFile(path.Join(origin, "1"), []byte("hello world"), 0700); err != nil {
  93. t.Fatal(err)
  94. }
  95. if err := ioutil.WriteFile(path.Join(origin, "2"), []byte("welcome!"), 0700); err != nil {
  96. t.Fatal(err)
  97. }
  98. for _, c := range []Compression{
  99. Uncompressed,
  100. Gzip,
  101. Bzip2,
  102. Xz,
  103. } {
  104. if err := tarUntar(t, origin, c); err != nil {
  105. t.Fatalf("Error tar/untar for compression %s: %s", c.Extension(), err)
  106. }
  107. }
  108. }