archive_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package docker
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "testing"
  8. "time"
  9. )
  10. func TestCmdStreamLargeStderr(t *testing.T) {
  11. cmd := exec.Command("/bin/sh", "-c", "dd if=/dev/zero bs=1k count=1000 of=/dev/stderr; echo hello")
  12. out, err := CmdStream(cmd)
  13. if err != nil {
  14. t.Fatalf("Failed to start command: " + err.Error())
  15. }
  16. errCh := make(chan error)
  17. go func() {
  18. _, err := io.Copy(ioutil.Discard, out)
  19. errCh <- err
  20. }()
  21. select {
  22. case err := <-errCh:
  23. if err != nil {
  24. t.Fatalf("Command should not have failed (err=%s...)", err.Error()[:100])
  25. }
  26. case <-time.After(5 * time.Second):
  27. t.Fatalf("Command did not complete in 5 seconds; probable deadlock")
  28. }
  29. }
  30. func TestCmdStreamBad(t *testing.T) {
  31. badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
  32. out, err := CmdStream(badCmd)
  33. if err != nil {
  34. t.Fatalf("Failed to start command: " + err.Error())
  35. }
  36. if output, err := ioutil.ReadAll(out); err == nil {
  37. t.Fatalf("Command should have failed")
  38. } else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
  39. t.Fatalf("Wrong error value (%s)", err.Error())
  40. } else if s := string(output); s != "hello\n" {
  41. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  42. }
  43. }
  44. func TestCmdStreamGood(t *testing.T) {
  45. cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
  46. out, err := CmdStream(cmd)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if output, err := ioutil.ReadAll(out); err != nil {
  51. t.Fatalf("Command should not have failed (err=%s)", err)
  52. } else if s := string(output); s != "hello\n" {
  53. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  54. }
  55. }
  56. func TestTarUntar(t *testing.T) {
  57. archive, err := Tar(".", Uncompressed)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. tmp, err := ioutil.TempDir("", "docker-test-untar")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer os.RemoveAll(tmp)
  66. if err := Untar(archive, tmp); err != nil {
  67. t.Fatal(err)
  68. }
  69. if _, err := os.Stat(tmp); err != nil {
  70. t.Fatalf("Error stating %s: %s", tmp, err.Error())
  71. }
  72. }