docker_api_create_test.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/docker/docker/api/types/container"
  7. "github.com/docker/docker/integration-cli/checker"
  8. "github.com/docker/docker/integration-cli/request"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
  12. // test invalid Interval in Healthcheck: less than 0s
  13. name := "test1"
  14. config := map[string]interface{}{
  15. "Image": "busybox",
  16. "Healthcheck": map[string]interface{}{
  17. "Interval": -10 * time.Millisecond,
  18. "Timeout": time.Second,
  19. "Retries": int(1000),
  20. },
  21. }
  22. status, body, err := request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
  23. c.Assert(err, check.IsNil)
  24. c.Assert(status, check.Equals, http.StatusBadRequest)
  25. expected := fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration)
  26. c.Assert(getErrorMessage(c, body), checker.Contains, expected)
  27. // test invalid Interval in Healthcheck: larger than 0s but less than 1ms
  28. name = "test2"
  29. config = map[string]interface{}{
  30. "Image": "busybox",
  31. "Healthcheck": map[string]interface{}{
  32. "Interval": 500 * time.Microsecond,
  33. "Timeout": time.Second,
  34. "Retries": int(1000),
  35. },
  36. }
  37. status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
  38. c.Assert(err, check.IsNil)
  39. c.Assert(status, check.Equals, http.StatusBadRequest)
  40. c.Assert(getErrorMessage(c, body), checker.Contains, expected)
  41. // test invalid Timeout in Healthcheck: less than 1ms
  42. name = "test3"
  43. config = map[string]interface{}{
  44. "Image": "busybox",
  45. "Healthcheck": map[string]interface{}{
  46. "Interval": time.Second,
  47. "Timeout": -100 * time.Millisecond,
  48. "Retries": int(1000),
  49. },
  50. }
  51. status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
  52. c.Assert(err, check.IsNil)
  53. c.Assert(status, check.Equals, http.StatusBadRequest)
  54. expected = fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration)
  55. c.Assert(getErrorMessage(c, body), checker.Contains, expected)
  56. // test invalid Retries in Healthcheck: less than 0
  57. name = "test4"
  58. config = map[string]interface{}{
  59. "Image": "busybox",
  60. "Healthcheck": map[string]interface{}{
  61. "Interval": time.Second,
  62. "Timeout": time.Second,
  63. "Retries": int(-10),
  64. },
  65. }
  66. status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
  67. c.Assert(err, check.IsNil)
  68. c.Assert(status, check.Equals, http.StatusBadRequest)
  69. expected = "Retries in Healthcheck cannot be negative"
  70. c.Assert(getErrorMessage(c, body), checker.Contains, expected)
  71. // test invalid StartPeriod in Healthcheck: not 0 and less than 1ms
  72. name = "test3"
  73. config = map[string]interface{}{
  74. "Image": "busybox",
  75. "Healthcheck": map[string]interface{}{
  76. "Interval": time.Second,
  77. "Timeout": time.Second,
  78. "Retries": int(1000),
  79. "StartPeriod": 100 * time.Microsecond,
  80. },
  81. }
  82. status, body, err = request.SockRequest("POST", "/containers/create?name="+name, config, daemonHost())
  83. c.Assert(err, check.IsNil)
  84. c.Assert(status, check.Equals, http.StatusBadRequest)
  85. expected = fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration)
  86. c.Assert(getErrorMessage(c, body), checker.Contains, expected)
  87. }