moby/integration/container/stats_test.go
Sebastiaan van Stijn 5e7eade1f7
integration: don't poll for containers to be running
container.Run() should be a synchronous operation in normal circumstances;
the container is created and started, so polling after that for the
container to be in the "running" state should not be needed.

This should also prevent issues when a container (for whatever reason)
exited immediately after starting; in that case we would continue
polling for it to be running (which likely would never happen).

Let's skip the polling; if the container is not in the expected state
(i.e. exited), tests should fail as well.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-09-08 23:05:43 +02:00

50 lines
1.4 KiB
Go

package container // import "github.com/docker/docker/integration/container"
import (
"encoding/json"
"io"
"reflect"
"testing"
"github.com/docker/docker/api/types"
"github.com/docker/docker/integration/internal/container"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
)
func TestStats(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
ctx := setupTest(t)
apiClient := testEnv.APIClient()
info, err := apiClient.Info(ctx)
assert.NilError(t, err)
cID := container.Run(ctx, t, apiClient)
resp, err := apiClient.ContainerStats(ctx, cID, false)
assert.NilError(t, err)
defer resp.Body.Close()
var v types.Stats
err = json.NewDecoder(resp.Body).Decode(&v)
assert.NilError(t, err)
assert.Check(t, is.Equal(int64(v.MemoryStats.Limit), info.MemTotal))
assert.Check(t, !reflect.DeepEqual(v.PreCPUStats, types.CPUStats{}))
err = json.NewDecoder(resp.Body).Decode(&v)
assert.Assert(t, is.ErrorContains(err, ""), io.EOF)
resp, err = apiClient.ContainerStatsOneShot(ctx, cID)
assert.NilError(t, err)
defer resp.Body.Close()
v = types.Stats{}
err = json.NewDecoder(resp.Body).Decode(&v)
assert.NilError(t, err)
assert.Check(t, is.Equal(int64(v.MemoryStats.Limit), info.MemTotal))
assert.Check(t, is.DeepEqual(v.PreCPUStats, types.CPUStats{}))
err = json.NewDecoder(resp.Body).Decode(&v)
assert.Assert(t, is.ErrorContains(err, ""), io.EOF)
}