mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
migrate integration tests from integration-cli/docker_api_create_test.go to integration/container
Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
This commit is contained in:
parent
cc7cda1968
commit
cd4d1cfc10
2 changed files with 87 additions and 136 deletions
|
@ -1,136 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/internal/test/request"
|
||||
"github.com/go-check/check"
|
||||
)
|
||||
|
||||
func (s *DockerSuite) TestAPICreateWithInvalidHealthcheckParams(c *check.C) {
|
||||
// test invalid Interval in Healthcheck: less than 0s
|
||||
name := "test1"
|
||||
config := map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": map[string]interface{}{
|
||||
"Interval": -10 * time.Millisecond,
|
||||
"Timeout": time.Second,
|
||||
"Retries": int(1000),
|
||||
},
|
||||
}
|
||||
|
||||
res, body, err := request.Post("/containers/create?name="+name, request.JSONBody(config))
|
||||
c.Assert(err, check.IsNil)
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
} else {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
buf, err := request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
expected := fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration)
|
||||
c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
|
||||
|
||||
// test invalid Interval in Healthcheck: larger than 0s but less than 1ms
|
||||
name = "test2"
|
||||
config = map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": map[string]interface{}{
|
||||
"Interval": 500 * time.Microsecond,
|
||||
"Timeout": time.Second,
|
||||
"Retries": int(1000),
|
||||
},
|
||||
}
|
||||
res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
buf, err = request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
} else {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
}
|
||||
c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
|
||||
|
||||
// test invalid Timeout in Healthcheck: less than 1ms
|
||||
name = "test3"
|
||||
config = map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": map[string]interface{}{
|
||||
"Interval": time.Second,
|
||||
"Timeout": -100 * time.Millisecond,
|
||||
"Retries": int(1000),
|
||||
},
|
||||
}
|
||||
res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
|
||||
c.Assert(err, check.IsNil)
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
} else {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
buf, err = request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
expected = fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration)
|
||||
c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
|
||||
|
||||
// test invalid Retries in Healthcheck: less than 0
|
||||
name = "test4"
|
||||
config = map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": map[string]interface{}{
|
||||
"Interval": time.Second,
|
||||
"Timeout": time.Second,
|
||||
"Retries": int(-10),
|
||||
},
|
||||
}
|
||||
res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
|
||||
c.Assert(err, check.IsNil)
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
} else {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
buf, err = request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
expected = "Retries in Healthcheck cannot be negative"
|
||||
c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
|
||||
|
||||
// test invalid StartPeriod in Healthcheck: not 0 and less than 1ms
|
||||
name = "test3"
|
||||
config = map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": map[string]interface{}{
|
||||
"Interval": time.Second,
|
||||
"Timeout": time.Second,
|
||||
"Retries": int(1000),
|
||||
"StartPeriod": 100 * time.Microsecond,
|
||||
},
|
||||
}
|
||||
res, body, err = request.Post("/containers/create?name="+name, request.JSONBody(config))
|
||||
c.Assert(err, check.IsNil)
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
||||
} else {
|
||||
c.Assert(res.StatusCode, check.Equals, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
buf, err = request.ReadBody(body)
|
||||
c.Assert(err, checker.IsNil)
|
||||
|
||||
expected = fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration)
|
||||
c.Assert(getErrorMessage(c, buf), checker.Contains, expected)
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -11,6 +12,7 @@ import (
|
|||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/versions"
|
||||
ctr "github.com/docker/docker/integration/internal/container"
|
||||
"github.com/docker/docker/internal/test/request"
|
||||
"github.com/docker/docker/oci"
|
||||
|
@ -301,3 +303,88 @@ func TestCreateWithCustomReadonlyPaths(t *testing.T) {
|
|||
checkInspect(t, ctx, name, tc.expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateWithInvalidHealthcheckParams(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
|
||||
testCases := []struct {
|
||||
doc string
|
||||
interval time.Duration
|
||||
timeout time.Duration
|
||||
retries int
|
||||
startPeriod time.Duration
|
||||
expectedErr string
|
||||
}{
|
||||
{
|
||||
doc: "test invalid Interval in Healthcheck: less than 0s",
|
||||
interval: -10 * time.Millisecond,
|
||||
timeout: time.Second,
|
||||
retries: 1000,
|
||||
expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
|
||||
},
|
||||
{
|
||||
doc: "test invalid Interval in Healthcheck: larger than 0s but less than 1ms",
|
||||
interval: 500 * time.Microsecond,
|
||||
timeout: time.Second,
|
||||
retries: 1000,
|
||||
expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
|
||||
},
|
||||
{
|
||||
doc: "test invalid Timeout in Healthcheck: less than 1ms",
|
||||
interval: time.Second,
|
||||
timeout: -100 * time.Millisecond,
|
||||
retries: 1000,
|
||||
expectedErr: fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration),
|
||||
},
|
||||
{
|
||||
doc: "test invalid Retries in Healthcheck: less than 0",
|
||||
interval: time.Second,
|
||||
timeout: time.Second,
|
||||
retries: -10,
|
||||
expectedErr: "Retries in Healthcheck cannot be negative",
|
||||
},
|
||||
{
|
||||
doc: "test invalid StartPeriod in Healthcheck: not 0 and less than 1ms",
|
||||
interval: time.Second,
|
||||
timeout: time.Second,
|
||||
retries: 1000,
|
||||
startPeriod: 100 * time.Microsecond,
|
||||
expectedErr: fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration),
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
i := i
|
||||
tc := tc
|
||||
t.Run(tc.doc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
healthCheck := map[string]interface{}{
|
||||
"Interval": tc.interval,
|
||||
"Timeout": tc.timeout,
|
||||
"Retries": tc.retries,
|
||||
}
|
||||
if tc.startPeriod != 0 {
|
||||
healthCheck["StartPeriod"] = tc.startPeriod
|
||||
}
|
||||
|
||||
config := map[string]interface{}{
|
||||
"Image": "busybox",
|
||||
"Healthcheck": healthCheck,
|
||||
}
|
||||
|
||||
res, body, err := request.Post("/containers/create?name="+fmt.Sprintf("test_%d_", i)+t.Name(), request.JSONBody(config))
|
||||
assert.NilError(t, err)
|
||||
|
||||
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
|
||||
assert.Check(t, is.Equal(http.StatusInternalServerError, res.StatusCode))
|
||||
} else {
|
||||
assert.Check(t, is.Equal(http.StatusBadRequest, res.StatusCode))
|
||||
}
|
||||
|
||||
buf, err := request.ReadBody(body)
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Check(t, is.Contains(string(buf), tc.expectedErr))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue