create_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package container
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/api/types/network"
  8. "github.com/docker/docker/integration/util/request"
  9. "github.com/docker/docker/internal/testutil"
  10. "github.com/gotestyourself/gotestyourself/skip"
  11. )
  12. func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
  13. defer setupTest(t)()
  14. client := request.NewAPIClient(t)
  15. testCases := []struct {
  16. doc string
  17. image string
  18. expectedError string
  19. }{
  20. {
  21. doc: "image and tag",
  22. image: "test456:v1",
  23. expectedError: "No such image: test456:v1",
  24. },
  25. {
  26. doc: "image no tag",
  27. image: "test456",
  28. expectedError: "No such image: test456",
  29. },
  30. {
  31. doc: "digest",
  32. image: "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
  33. expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
  34. },
  35. }
  36. for _, tc := range testCases {
  37. tc := tc
  38. t.Run(tc.doc, func(t *testing.T) {
  39. t.Parallel()
  40. _, err := client.ContainerCreate(context.Background(),
  41. &container.Config{Image: tc.image},
  42. &container.HostConfig{},
  43. &network.NetworkingConfig{},
  44. "foo",
  45. )
  46. testutil.ErrorContains(t, err, tc.expectedError)
  47. })
  48. }
  49. }
  50. func TestCreateWithInvalidEnv(t *testing.T) {
  51. defer setupTest(t)()
  52. client := request.NewAPIClient(t)
  53. testCases := []struct {
  54. env string
  55. expectedError string
  56. }{
  57. {
  58. env: "",
  59. expectedError: "invalid environment variable:",
  60. },
  61. {
  62. env: "=",
  63. expectedError: "invalid environment variable: =",
  64. },
  65. {
  66. env: "=foo",
  67. expectedError: "invalid environment variable: =foo",
  68. },
  69. }
  70. for index, tc := range testCases {
  71. tc := tc
  72. t.Run(strconv.Itoa(index), func(t *testing.T) {
  73. t.Parallel()
  74. _, err := client.ContainerCreate(context.Background(),
  75. &container.Config{
  76. Image: "busybox",
  77. Env: []string{tc.env},
  78. },
  79. &container.HostConfig{},
  80. &network.NetworkingConfig{},
  81. "foo",
  82. )
  83. testutil.ErrorContains(t, err, tc.expectedError)
  84. })
  85. }
  86. }
  87. // Test case for #30166 (target was not validated)
  88. func TestCreateTmpfsMountsTarget(t *testing.T) {
  89. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  90. defer setupTest(t)()
  91. client := request.NewAPIClient(t)
  92. testCases := []struct {
  93. target string
  94. expectedError string
  95. }{
  96. {
  97. target: ".",
  98. expectedError: "mount path must be absolute",
  99. },
  100. {
  101. target: "foo",
  102. expectedError: "mount path must be absolute",
  103. },
  104. {
  105. target: "/",
  106. expectedError: "destination can't be '/'",
  107. },
  108. {
  109. target: "//",
  110. expectedError: "destination can't be '/'",
  111. },
  112. }
  113. for _, tc := range testCases {
  114. _, err := client.ContainerCreate(context.Background(),
  115. &container.Config{
  116. Image: "busybox",
  117. },
  118. &container.HostConfig{
  119. Tmpfs: map[string]string{tc.target: ""},
  120. },
  121. &network.NetworkingConfig{},
  122. "",
  123. )
  124. testutil.ErrorContains(t, err, tc.expectedError)
  125. }
  126. }