execin_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package integration
  2. import (
  3. "os"
  4. "os/exec"
  5. "strings"
  6. "sync"
  7. "testing"
  8. "github.com/docker/libcontainer"
  9. "github.com/docker/libcontainer/namespaces"
  10. )
  11. func TestExecIn(t *testing.T) {
  12. if testing.Short() {
  13. return
  14. }
  15. rootfs, err := newRootFs()
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. defer remove(rootfs)
  20. config := newTemplateConfig(rootfs)
  21. if err := writeConfig(config); err != nil {
  22. t.Fatalf("failed to write config %s", err)
  23. }
  24. containerCmd, statePath, containerErr := startLongRunningContainer(config)
  25. defer func() {
  26. // kill the container
  27. if containerCmd.Process != nil {
  28. containerCmd.Process.Kill()
  29. }
  30. if err := <-containerErr; err != nil {
  31. t.Fatal(err)
  32. }
  33. }()
  34. // start the exec process
  35. state, err := libcontainer.GetState(statePath)
  36. if err != nil {
  37. t.Fatalf("failed to get state %s", err)
  38. }
  39. buffers := newStdBuffers()
  40. execErr := make(chan error, 1)
  41. go func() {
  42. _, err := namespaces.ExecIn(config, state, []string{"ps"},
  43. os.Args[0], "exec", buffers.Stdin, buffers.Stdout, buffers.Stderr,
  44. "", nil)
  45. execErr <- err
  46. }()
  47. if err := <-execErr; err != nil {
  48. t.Fatalf("exec finished with error %s", err)
  49. }
  50. out := buffers.Stdout.String()
  51. if !strings.Contains(out, "sleep 10") || !strings.Contains(out, "ps") {
  52. t.Fatalf("unexpected running process, output %q", out)
  53. }
  54. }
  55. func TestExecInRlimit(t *testing.T) {
  56. if testing.Short() {
  57. return
  58. }
  59. rootfs, err := newRootFs()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. defer remove(rootfs)
  64. config := newTemplateConfig(rootfs)
  65. if err := writeConfig(config); err != nil {
  66. t.Fatalf("failed to write config %s", err)
  67. }
  68. containerCmd, statePath, containerErr := startLongRunningContainer(config)
  69. defer func() {
  70. // kill the container
  71. if containerCmd.Process != nil {
  72. containerCmd.Process.Kill()
  73. }
  74. if err := <-containerErr; err != nil {
  75. t.Fatal(err)
  76. }
  77. }()
  78. // start the exec process
  79. state, err := libcontainer.GetState(statePath)
  80. if err != nil {
  81. t.Fatalf("failed to get state %s", err)
  82. }
  83. buffers := newStdBuffers()
  84. execErr := make(chan error, 1)
  85. go func() {
  86. _, err := namespaces.ExecIn(config, state, []string{"/bin/sh", "-c", "ulimit -n"},
  87. os.Args[0], "exec", buffers.Stdin, buffers.Stdout, buffers.Stderr,
  88. "", nil)
  89. execErr <- err
  90. }()
  91. if err := <-execErr; err != nil {
  92. t.Fatalf("exec finished with error %s", err)
  93. }
  94. out := buffers.Stdout.String()
  95. if limit := strings.TrimSpace(out); limit != "1024" {
  96. t.Fatalf("expected rlimit to be 1024, got %s", limit)
  97. }
  98. }
  99. // start a long-running container so we have time to inspect execin processes
  100. func startLongRunningContainer(config *libcontainer.Config) (*exec.Cmd, string, chan error) {
  101. containerErr := make(chan error, 1)
  102. containerCmd := &exec.Cmd{}
  103. var statePath string
  104. createCmd := func(container *libcontainer.Config, console, dataPath, init string,
  105. pipe *os.File, args []string) *exec.Cmd {
  106. containerCmd = namespaces.DefaultCreateCommand(container, console, dataPath, init, pipe, args)
  107. statePath = dataPath
  108. return containerCmd
  109. }
  110. var containerStart sync.WaitGroup
  111. containerStart.Add(1)
  112. go func() {
  113. buffers := newStdBuffers()
  114. _, err := namespaces.Exec(config,
  115. buffers.Stdin, buffers.Stdout, buffers.Stderr,
  116. "", config.RootFs, []string{"sleep", "10"},
  117. createCmd, containerStart.Done)
  118. containerErr <- err
  119. }()
  120. containerStart.Wait()
  121. return containerCmd, statePath, containerErr
  122. }