1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Update TestUpdatePidsLimit to be more atomic

Create a new container for each subtest, so that individual
subtests are self-contained, and there's no need to execute
them in the exact order, or resetting the container in between.

This makes the test slower (6.54s vs  3.43s), but reduced the
difference by using `network=host`, which made a substantial
difference (without `network=host`, the test took more than
twice as long: 13.96s).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-02-24 16:26:37 +01:00
parent ffa1728d4b
commit 1101568fa1
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 21 additions and 25 deletions

View file

@ -7,7 +7,6 @@ import (
"testing"
"time"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/integration/internal/container"
@ -114,8 +113,6 @@ func TestUpdatePidsLimit(t *testing.T) {
oldAPIclient := request.NewAPIClient(t, client.WithVersion("1.24"))
ctx := context.Background()
cID := container.Run(t, ctx, apiClient)
intPtr := func(i int64) *int64 {
return &i
}
@ -123,33 +120,28 @@ func TestUpdatePidsLimit(t *testing.T) {
for _, test := range []struct {
desc string
oldAPI bool
initial *int64
update *int64
expect int64
expectCg string
}{
{desc: "update from none", update: intPtr(32), expect: 32, expectCg: "32"},
{desc: "no change", update: nil, expectCg: "32"},
{desc: "update lower", update: intPtr(16), expect: 16, expectCg: "16"},
{desc: "update on old api ignores value", oldAPI: true, update: intPtr(10), expect: 16, expectCg: "16"},
{desc: "unset limit", update: intPtr(0), expect: 0, expectCg: "max"},
{desc: "reset", update: intPtr(32), expect: 32, expectCg: "32"},
{desc: "unset limit with minus one", update: intPtr(-1), expect: 0, expectCg: "max"},
{desc: "reset", update: intPtr(32), expect: 32, expectCg: "32"},
{desc: "unset limit with minus two", update: intPtr(-2), expect: 0, expectCg: "max"},
{desc: "no change", initial: intPtr(32), expect: 32, expectCg: "32"},
{desc: "update lower", initial: intPtr(32), update: intPtr(16), expect: 16, expectCg: "16"},
{desc: "update on old api ignores value", oldAPI: true, initial: intPtr(32), update: intPtr(16), expect: 32, expectCg: "32"},
{desc: "unset limit with zero", initial: intPtr(32), update: intPtr(0), expect: 0, expectCg: "max"},
{desc: "unset limit with minus one", initial: intPtr(32), update: intPtr(-1), expect: 0, expectCg: "max"},
{desc: "unset limit with minus two", initial: intPtr(32), update: intPtr(-2), expect: 0, expectCg: "max"},
} {
c := apiClient
if test.oldAPI {
c = oldAPIclient
}
var before types.ContainerJSON
if test.update == nil {
var err error
before, err = c.ContainerInspect(ctx, cID)
assert.NilError(t, err)
}
t.Run(test.desc, func(t *testing.T) {
// Using "network=host" to speed up creation (13.96s vs 6.54s)
cID := container.Run(t, ctx, apiClient, container.WithPidsLimit(test.initial), container.WithNetworkMode("host"))
_, err := c.ContainerUpdate(ctx, cID, containertypes.UpdateConfig{
Resources: containertypes.Resources{
PidsLimit: test.update,
@ -160,13 +152,7 @@ func TestUpdatePidsLimit(t *testing.T) {
inspect, err := c.ContainerInspect(ctx, cID)
assert.NilError(t, err)
assert.Assert(t, inspect.HostConfig.Resources.PidsLimit != nil)
if test.update == nil {
assert.Assert(t, before.HostConfig.Resources.PidsLimit != nil)
assert.Equal(t, *before.HostConfig.Resources.PidsLimit, *inspect.HostConfig.Resources.PidsLimit)
} else {
assert.Equal(t, *inspect.HostConfig.Resources.PidsLimit, test.expect)
}
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()

View file

@ -137,6 +137,16 @@ func WithAutoRemove(c *TestContainerConfig) {
c.HostConfig.AutoRemove = true
}
// WithPidsLimit sets the container's "pids-limit
func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
}
c.HostConfig.PidsLimit = limit
}
}
// WithRestartPolicy sets container's restart policy
func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
return func(c *TestContainerConfig) {