2021-06-23 13:26:54 +00:00
|
|
|
package system // import "github.com/docker/docker/integration/system"
|
|
|
|
|
|
|
|
import (
|
2023-09-29 23:00:52 +00:00
|
|
|
"strings"
|
2021-06-23 13:26:54 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2022-11-24 18:04:31 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2022-03-18 15:33:43 +00:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2021-06-23 13:26:54 +00:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2021-06-23 13:26:54 +00:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"gotest.tools/v3/assert"
|
2023-09-29 23:00:52 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2021-06-23 13:26:54 +00:00
|
|
|
"gotest.tools/v3/skip"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiskUsage(t *testing.T) {
|
2023-06-14 09:46:00 +00:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows") // d.Start fails on Windows with `protocol not available`
|
2021-06-23 13:26:54 +00:00
|
|
|
|
2024-03-18 11:11:34 +00:00
|
|
|
// TODO: If this helps, then fix the root cause.
|
|
|
|
// See: https://github.com/moby/moby/issues/47119
|
|
|
|
// t.Parallel()
|
2021-06-23 13:26:54 +00:00
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.StartSpan(baseContext, t)
|
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
d := daemon.New(t)
|
|
|
|
defer d.Cleanup(t)
|
|
|
|
d.Start(t, "--iptables=false")
|
|
|
|
defer d.Stop(t)
|
|
|
|
client := d.NewClientT(t)
|
|
|
|
|
|
|
|
var stepDU types.DiskUsage
|
|
|
|
for _, step := range []struct {
|
|
|
|
doc string
|
|
|
|
next func(t *testing.T, prev types.DiskUsage) types.DiskUsage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "empty",
|
|
|
|
next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
|
|
|
|
du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, du, types.DiskUsage{
|
2022-11-24 18:04:31 +00:00
|
|
|
Images: []*image.Summary{},
|
2021-06-23 13:26:54 +00:00
|
|
|
Containers: []*types.Container{},
|
2022-03-18 15:33:43 +00:00
|
|
|
Volumes: []*volume.Volume{},
|
2021-06-23 13:26:54 +00:00
|
|
|
BuildCache: []*types.BuildCache{},
|
|
|
|
})
|
|
|
|
return du
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "after LoadBusybox",
|
|
|
|
next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
|
2023-07-14 18:02:38 +00:00
|
|
|
d.LoadBusybox(ctx, t)
|
2021-06-23 13:26:54 +00:00
|
|
|
|
|
|
|
du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, du.LayersSize > 0)
|
|
|
|
assert.Equal(t, len(du.Images), 1)
|
2023-09-29 23:00:52 +00:00
|
|
|
assert.Equal(t, len(du.Images[0].RepoTags), 1)
|
|
|
|
assert.Check(t, is.Equal(du.Images[0].RepoTags[0], "busybox:latest"))
|
|
|
|
|
|
|
|
// Image size is layer size + content size, should be greater than total layer size
|
|
|
|
assert.Assert(t, du.Images[0].Size >= du.LayersSize)
|
|
|
|
|
|
|
|
// If size is greater, than content exists and should have a repodigest
|
|
|
|
if du.Images[0].Size > du.LayersSize {
|
|
|
|
assert.Equal(t, len(du.Images[0].RepoDigests), 1)
|
|
|
|
assert.Check(t, strings.HasPrefix(du.Images[0].RepoDigests[0], "busybox@"))
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
return du
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "after container.Run",
|
|
|
|
next: func(t *testing.T, prev types.DiskUsage) types.DiskUsage {
|
|
|
|
cID := container.Run(ctx, t, client)
|
|
|
|
|
|
|
|
du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, len(du.Containers), 1)
|
|
|
|
assert.Equal(t, len(du.Containers[0].Names), 1)
|
TestDiskUsage: don't panic if results don't match
This test is currently failing with containerd-integration, which should
be looked into, but let's start with preventing it from panicking, to make
the test-failures less noisy;
--- FAIL: TestDiskUsage/after_container.Run (0.26s)
panic: runtime error: index out of range [0] with length 0 [recovered]
panic: runtime error: index out of range [0] with length 0
goroutine 280 [running]:
testing.tRunner.func1.2({0xb07a00, 0x40002006a8})
/usr/local/go/src/testing/testing.go:1526 +0x1c8
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1529 +0x364
panic({0xb07a00, 0x40002006a8})
/usr/local/go/src/runtime/panic.go:884 +0x1f4
github.com/docker/docker/integration/system.TestDiskUsage.func3(0x0?, {0x0, {0x14ea4a8, 0x0, 0x0}, {0x14ea4a8, 0x0, 0x0}, {0x14ea4a8, 0x0, ...}, ...})
/go/src/github.com/docker/docker/integration/system/disk_usage_test.go:82 +0x7e4
github.com/docker/docker/integration/system.TestDiskUsage.func4(0x4000235c80?)
/go/src/github.com/docker/docker/integration/system/disk_usage_test.go:118 +0x8c
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-23 17:15:21 +00:00
|
|
|
assert.Assert(t, len(prev.Images) > 0)
|
2023-09-29 23:00:52 +00:00
|
|
|
assert.Check(t, du.Containers[0].Created >= prev.Images[0].Created)
|
|
|
|
|
|
|
|
// Additional container layer could add to the size
|
|
|
|
assert.Check(t, du.LayersSize >= prev.LayersSize)
|
|
|
|
|
|
|
|
assert.Equal(t, len(du.Images), 1)
|
|
|
|
assert.Equal(t, du.Images[0].Containers, prev.Images[0].Containers+1)
|
|
|
|
|
|
|
|
assert.Check(t, is.Equal(du.Containers[0].ID, cID))
|
|
|
|
assert.Check(t, is.Equal(du.Containers[0].Image, "busybox"))
|
|
|
|
assert.Check(t, is.Equal(du.Containers[0].ImageID, prev.Images[0].ID))
|
|
|
|
|
|
|
|
// The rootfs size should be equivalent to all the layers,
|
|
|
|
// previously used prev.Images[0].Size, which may differ from content data
|
|
|
|
assert.Check(t, is.Equal(du.Containers[0].SizeRootFs, du.LayersSize))
|
|
|
|
|
2021-06-23 13:26:54 +00:00
|
|
|
return du
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(step.doc, func(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.StartSpan(ctx, t)
|
2021-06-23 13:26:54 +00:00
|
|
|
stepDU = step.next(t, stepDU)
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
doc string
|
|
|
|
options types.DiskUsageOptions
|
|
|
|
expected types.DiskUsage
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "container types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ContainerObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
Containers: stepDU.Containers,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "image types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ImageObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
LayersSize: stepDU.LayersSize,
|
|
|
|
Images: stepDU.Images,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "volume types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.VolumeObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "build-cache types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.BuildCacheObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
BuildCache: stepDU.BuildCache,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "container, volume types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ContainerObject,
|
|
|
|
types.VolumeObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
Containers: stepDU.Containers,
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "image, build-cache types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ImageObject,
|
|
|
|
types.BuildCacheObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
LayersSize: stepDU.LayersSize,
|
|
|
|
Images: stepDU.Images,
|
|
|
|
BuildCache: stepDU.BuildCache,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "container, volume, build-cache types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ContainerObject,
|
|
|
|
types.VolumeObject,
|
|
|
|
types.BuildCacheObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
Containers: stepDU.Containers,
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
BuildCache: stepDU.BuildCache,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "image, volume, build-cache types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ImageObject,
|
|
|
|
types.VolumeObject,
|
|
|
|
types.BuildCacheObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
LayersSize: stepDU.LayersSize,
|
|
|
|
Images: stepDU.Images,
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
BuildCache: stepDU.BuildCache,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "container, image, volume types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ContainerObject,
|
|
|
|
types.ImageObject,
|
|
|
|
types.VolumeObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
LayersSize: stepDU.LayersSize,
|
|
|
|
Containers: stepDU.Containers,
|
|
|
|
Images: stepDU.Images,
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "container, image, volume, build-cache types",
|
|
|
|
options: types.DiskUsageOptions{
|
|
|
|
Types: []types.DiskUsageObject{
|
|
|
|
types.ContainerObject,
|
|
|
|
types.ImageObject,
|
|
|
|
types.VolumeObject,
|
|
|
|
types.BuildCacheObject,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: types.DiskUsage{
|
|
|
|
LayersSize: stepDU.LayersSize,
|
|
|
|
Containers: stepDU.Containers,
|
|
|
|
Images: stepDU.Images,
|
|
|
|
Volumes: stepDU.Volumes,
|
|
|
|
BuildCache: stepDU.BuildCache,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := testutil.StartSpan(ctx, t)
|
2021-06-23 13:26:54 +00:00
|
|
|
// TODO: Run in parallel once https://github.com/moby/moby/pull/42560 is merged.
|
|
|
|
|
|
|
|
du, err := client.DiskUsage(ctx, tc.options)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, du, tc.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|