container_exec_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/errdefs"
  13. )
  14. func TestContainerExecCreateError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. _, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{})
  19. if !errdefs.IsSystem(err) {
  20. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  21. }
  22. }
  23. func TestContainerExecCreate(t *testing.T) {
  24. expectedURL := "/containers/container_id/exec"
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  28. return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
  29. }
  30. if req.Method != http.MethodPost {
  31. return nil, fmt.Errorf("expected POST method, got %s", req.Method)
  32. }
  33. // FIXME validate the content is the given ExecConfig ?
  34. if err := req.ParseForm(); err != nil {
  35. return nil, err
  36. }
  37. execConfig := &types.ExecConfig{}
  38. if err := json.NewDecoder(req.Body).Decode(execConfig); err != nil {
  39. return nil, err
  40. }
  41. if execConfig.User != "user" {
  42. return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
  43. }
  44. b, err := json.Marshal(types.IDResponse{
  45. ID: "exec_id",
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &http.Response{
  51. StatusCode: http.StatusOK,
  52. Body: io.NopCloser(bytes.NewReader(b)),
  53. }, nil
  54. }),
  55. }
  56. r, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{
  57. User: "user",
  58. })
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if r.ID != "exec_id" {
  63. t.Fatalf("expected `exec_id`, got %s", r.ID)
  64. }
  65. }
  66. func TestContainerExecStartError(t *testing.T) {
  67. client := &Client{
  68. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  69. }
  70. err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{})
  71. if !errdefs.IsSystem(err) {
  72. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  73. }
  74. }
  75. func TestContainerExecStart(t *testing.T) {
  76. expectedURL := "/exec/exec_id/start"
  77. client := &Client{
  78. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  79. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  80. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  81. }
  82. if err := req.ParseForm(); err != nil {
  83. return nil, err
  84. }
  85. execStartCheck := &types.ExecStartCheck{}
  86. if err := json.NewDecoder(req.Body).Decode(execStartCheck); err != nil {
  87. return nil, err
  88. }
  89. if execStartCheck.Tty || !execStartCheck.Detach {
  90. return nil, fmt.Errorf("expected execStartCheck{Detach:true,Tty:false}, got %v", execStartCheck)
  91. }
  92. return &http.Response{
  93. StatusCode: http.StatusOK,
  94. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  95. }, nil
  96. }),
  97. }
  98. err := client.ContainerExecStart(context.Background(), "exec_id", types.ExecStartCheck{
  99. Detach: true,
  100. Tty: false,
  101. })
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. }
  106. func TestContainerExecInspectError(t *testing.T) {
  107. client := &Client{
  108. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  109. }
  110. _, err := client.ContainerExecInspect(context.Background(), "nothing")
  111. if !errdefs.IsSystem(err) {
  112. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  113. }
  114. }
  115. func TestContainerExecInspect(t *testing.T) {
  116. expectedURL := "/exec/exec_id/json"
  117. client := &Client{
  118. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  119. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  120. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  121. }
  122. b, err := json.Marshal(types.ContainerExecInspect{
  123. ExecID: "exec_id",
  124. ContainerID: "container_id",
  125. })
  126. if err != nil {
  127. return nil, err
  128. }
  129. return &http.Response{
  130. StatusCode: http.StatusOK,
  131. Body: io.NopCloser(bytes.NewReader(b)),
  132. }, nil
  133. }),
  134. }
  135. inspect, err := client.ContainerExecInspect(context.Background(), "exec_id")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if inspect.ExecID != "exec_id" {
  140. t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID)
  141. }
  142. if inspect.ContainerID != "container_id" {
  143. t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID)
  144. }
  145. }