container_create_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/container"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestContainerCreateError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. // 404 doesn't automatically means an unknown image
  23. client = &Client{
  24. client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
  25. }
  26. _, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
  27. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  28. }
  29. func TestContainerCreateImageNotFound(t *testing.T) {
  30. client := &Client{
  31. client: newMockClient(errorMock(http.StatusNotFound, "No such image")),
  32. }
  33. _, err := client.ContainerCreate(context.Background(), &container.Config{Image: "unknown_image"}, nil, nil, nil, "unknown")
  34. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  35. }
  36. func TestContainerCreateWithName(t *testing.T) {
  37. expectedURL := "/containers/create"
  38. client := &Client{
  39. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  40. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  41. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  42. }
  43. name := req.URL.Query().Get("name")
  44. if name != "container_name" {
  45. return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
  46. }
  47. b, err := json.Marshal(container.CreateResponse{
  48. ID: "container_id",
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &http.Response{
  54. StatusCode: http.StatusOK,
  55. Body: io.NopCloser(bytes.NewReader(b)),
  56. }, nil
  57. }),
  58. }
  59. r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name")
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if r.ID != "container_id" {
  64. t.Fatalf("expected `container_id`, got %s", r.ID)
  65. }
  66. }
  67. // TestContainerCreateAutoRemove validates that a client using API 1.24 always disables AutoRemove. When using API 1.25
  68. // or up, AutoRemove should not be disabled.
  69. func TestContainerCreateAutoRemove(t *testing.T) {
  70. autoRemoveValidator := func(expectedValue bool) func(req *http.Request) (*http.Response, error) {
  71. return func(req *http.Request) (*http.Response, error) {
  72. var config configWrapper
  73. if err := json.NewDecoder(req.Body).Decode(&config); err != nil {
  74. return nil, err
  75. }
  76. if config.HostConfig.AutoRemove != expectedValue {
  77. return nil, fmt.Errorf("expected AutoRemove to be %v, got %v", expectedValue, config.HostConfig.AutoRemove)
  78. }
  79. b, err := json.Marshal(container.CreateResponse{
  80. ID: "container_id",
  81. })
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &http.Response{
  86. StatusCode: http.StatusOK,
  87. Body: io.NopCloser(bytes.NewReader(b)),
  88. }, nil
  89. }
  90. }
  91. client := &Client{
  92. client: newMockClient(autoRemoveValidator(false)),
  93. version: "1.24",
  94. }
  95. if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
  96. t.Fatal(err)
  97. }
  98. client = &Client{
  99. client: newMockClient(autoRemoveValidator(true)),
  100. version: "1.25",
  101. }
  102. if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
  103. t.Fatal(err)
  104. }
  105. }