archive_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package docker
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "testing"
  8. )
  9. func TestCmdStreamLargeStderr(t *testing.T) {
  10. // This test checks for deadlock; thus, the main failure mode of this test is deadlocking.
  11. // FIXME implement a timeout to avoid blocking the whole test suite when this test fails
  12. cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
  13. out, err := CmdStream(cmd)
  14. if err != nil {
  15. t.Fatalf("Failed to start command: " + err.Error())
  16. }
  17. _, err = io.Copy(ioutil.Discard, out)
  18. if err != nil {
  19. t.Fatalf("Command should not have failed (err=%s...)", err.Error()[:100])
  20. }
  21. }
  22. func TestCmdStreamBad(t *testing.T) {
  23. badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
  24. out, err := CmdStream(badCmd)
  25. if err != nil {
  26. t.Fatalf("Failed to start command: " + err.Error())
  27. }
  28. if output, err := ioutil.ReadAll(out); err == nil {
  29. t.Fatalf("Command should have failed")
  30. } else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
  31. t.Fatalf("Wrong error value (%s)", err.Error())
  32. } else if s := string(output); s != "hello\n" {
  33. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  34. }
  35. }
  36. func TestCmdStreamGood(t *testing.T) {
  37. cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
  38. out, err := CmdStream(cmd)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if output, err := ioutil.ReadAll(out); err != nil {
  43. t.Fatalf("Command should not have failed (err=%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 TestTarUntar(t *testing.T) {
  49. archive, err := Tar(".", Uncompressed)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. tmp, err := ioutil.TempDir("", "docker-test-untar")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. defer os.RemoveAll(tmp)
  58. if err := Untar(archive, tmp); err != nil {
  59. t.Fatal(err)
  60. }
  61. if _, err := os.Stat(tmp); err != nil {
  62. t.Fatalf("Error stating %s: %s", tmp, err.Error())
  63. }
  64. }