archive_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package fs
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. )
  8. func TestCmdStreamBad(t *testing.T) {
  9. badCmd := exec.Command("/bin/sh", "-c", "echo hello; echo >&2 error couldn\\'t reverse the phase pulser; exit 1")
  10. out, err := CmdStream(badCmd)
  11. if err != nil {
  12. t.Fatalf("Failed to start command: " + err.Error())
  13. }
  14. if output, err := ioutil.ReadAll(out); err == nil {
  15. t.Fatalf("Command should have failed")
  16. } else if err.Error() != "exit status 1: error couldn't reverse the phase pulser\n" {
  17. t.Fatalf("Wrong error value (%s)", err.Error())
  18. } else if s := string(output); s != "hello\n" {
  19. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  20. }
  21. }
  22. func TestCmdStreamGood(t *testing.T) {
  23. cmd := exec.Command("/bin/sh", "-c", "echo hello; exit 0")
  24. out, err := CmdStream(cmd)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if output, err := ioutil.ReadAll(out); err != nil {
  29. t.Fatalf("Command should not have failed (err=%s)", err)
  30. } else if s := string(output); s != "hello\n" {
  31. t.Fatalf("Command output should be '%s', not '%s'", "hello\\n", output)
  32. }
  33. }
  34. func TestTarUntar(t *testing.T) {
  35. archive, err := Tar(".", Uncompressed)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. tmp, err := ioutil.TempDir("", "docker-test-untar")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer os.RemoveAll(tmp)
  44. if err := Untar(archive, tmp); err != nil {
  45. t.Fatal(err)
  46. }
  47. if _, err := os.Stat(tmp); err != nil {
  48. t.Fatalf("Error stating %s: %s", tmp, err.Error())
  49. }
  50. }