github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/integration/system/disk_usage_test.go (about)

     1  package system // import "github.com/docker/docker/integration/system"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/integration/internal/container"
     9  	"github.com/docker/docker/testutil/daemon"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/skip"
    12  )
    13  
    14  func TestDiskUsage(t *testing.T) {
    15  	skip.If(t, testEnv.OSType == "windows") // d.Start fails on Windows with `protocol not available`
    16  
    17  	t.Parallel()
    18  
    19  	d := daemon.New(t)
    20  	defer d.Cleanup(t)
    21  	d.Start(t, "--iptables=false")
    22  	defer d.Stop(t)
    23  	client := d.NewClientT(t)
    24  
    25  	ctx := context.Background()
    26  
    27  	var stepDU types.DiskUsage
    28  	for _, step := range []struct {
    29  		doc  string
    30  		next func(t *testing.T, prev types.DiskUsage) types.DiskUsage
    31  	}{
    32  		{
    33  			doc: "empty",
    34  			next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
    35  				du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
    36  				assert.NilError(t, err)
    37  				assert.DeepEqual(t, du, types.DiskUsage{
    38  					Images:     []*types.ImageSummary{},
    39  					Containers: []*types.Container{},
    40  					Volumes:    []*types.Volume{},
    41  					BuildCache: []*types.BuildCache{},
    42  				})
    43  				return du
    44  			},
    45  		},
    46  		{
    47  			doc: "after LoadBusybox",
    48  			next: func(t *testing.T, _ types.DiskUsage) types.DiskUsage {
    49  				d.LoadBusybox(t)
    50  
    51  				du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
    52  				assert.NilError(t, err)
    53  				assert.Assert(t, du.LayersSize > 0)
    54  				assert.Equal(t, len(du.Images), 1)
    55  				assert.DeepEqual(t, du, types.DiskUsage{
    56  					LayersSize: du.LayersSize,
    57  					Images: []*types.ImageSummary{
    58  						{
    59  							Created:     du.Images[0].Created,
    60  							ID:          du.Images[0].ID,
    61  							RepoTags:    []string{"busybox:latest"},
    62  							Size:        du.LayersSize,
    63  							VirtualSize: du.LayersSize,
    64  						},
    65  					},
    66  					Containers: []*types.Container{},
    67  					Volumes:    []*types.Volume{},
    68  					BuildCache: []*types.BuildCache{},
    69  				})
    70  				return du
    71  			},
    72  		},
    73  		{
    74  			doc: "after container.Run",
    75  			next: func(t *testing.T, prev types.DiskUsage) types.DiskUsage {
    76  				cID := container.Run(ctx, t, client)
    77  
    78  				du, err := client.DiskUsage(ctx, types.DiskUsageOptions{})
    79  				assert.NilError(t, err)
    80  				assert.Equal(t, len(du.Containers), 1)
    81  				assert.Equal(t, len(du.Containers[0].Names), 1)
    82  				assert.Assert(t, du.Containers[0].Created >= prev.Images[0].Created)
    83  				assert.DeepEqual(t, du, types.DiskUsage{
    84  					LayersSize: prev.LayersSize,
    85  					Images: []*types.ImageSummary{
    86  						func() *types.ImageSummary {
    87  							sum := *prev.Images[0]
    88  							sum.Containers++
    89  							return &sum
    90  						}(),
    91  					},
    92  					Containers: []*types.Container{
    93  						{
    94  							ID:              cID,
    95  							Names:           du.Containers[0].Names,
    96  							Image:           "busybox",
    97  							ImageID:         prev.Images[0].ID,
    98  							Command:         du.Containers[0].Command, // not relevant for the test
    99  							Created:         du.Containers[0].Created,
   100  							Ports:           du.Containers[0].Ports, // not relevant for the test
   101  							SizeRootFs:      prev.Images[0].Size,
   102  							Labels:          du.Containers[0].Labels,          // not relevant for the test
   103  							State:           du.Containers[0].State,           // not relevant for the test
   104  							Status:          du.Containers[0].Status,          // not relevant for the test
   105  							HostConfig:      du.Containers[0].HostConfig,      // not relevant for the test
   106  							NetworkSettings: du.Containers[0].NetworkSettings, // not relevant for the test
   107  							Mounts:          du.Containers[0].Mounts,          // not relevant for the test
   108  						},
   109  					},
   110  					Volumes:    []*types.Volume{},
   111  					BuildCache: []*types.BuildCache{},
   112  				})
   113  				return du
   114  			},
   115  		},
   116  	} {
   117  		t.Run(step.doc, func(t *testing.T) {
   118  			stepDU = step.next(t, stepDU)
   119  
   120  			for _, tc := range []struct {
   121  				doc      string
   122  				options  types.DiskUsageOptions
   123  				expected types.DiskUsage
   124  			}{
   125  				{
   126  					doc: "container types",
   127  					options: types.DiskUsageOptions{
   128  						Types: []types.DiskUsageObject{
   129  							types.ContainerObject,
   130  						},
   131  					},
   132  					expected: types.DiskUsage{
   133  						Containers: stepDU.Containers,
   134  					},
   135  				},
   136  				{
   137  					doc: "image types",
   138  					options: types.DiskUsageOptions{
   139  						Types: []types.DiskUsageObject{
   140  							types.ImageObject,
   141  						},
   142  					},
   143  					expected: types.DiskUsage{
   144  						LayersSize: stepDU.LayersSize,
   145  						Images:     stepDU.Images,
   146  					},
   147  				},
   148  				{
   149  					doc: "volume types",
   150  					options: types.DiskUsageOptions{
   151  						Types: []types.DiskUsageObject{
   152  							types.VolumeObject,
   153  						},
   154  					},
   155  					expected: types.DiskUsage{
   156  						Volumes: stepDU.Volumes,
   157  					},
   158  				},
   159  				{
   160  					doc: "build-cache types",
   161  					options: types.DiskUsageOptions{
   162  						Types: []types.DiskUsageObject{
   163  							types.BuildCacheObject,
   164  						},
   165  					},
   166  					expected: types.DiskUsage{
   167  						BuildCache: stepDU.BuildCache,
   168  					},
   169  				},
   170  				{
   171  					doc: "container, volume types",
   172  					options: types.DiskUsageOptions{
   173  						Types: []types.DiskUsageObject{
   174  							types.ContainerObject,
   175  							types.VolumeObject,
   176  						},
   177  					},
   178  					expected: types.DiskUsage{
   179  						Containers: stepDU.Containers,
   180  						Volumes:    stepDU.Volumes,
   181  					},
   182  				},
   183  				{
   184  					doc: "image, build-cache types",
   185  					options: types.DiskUsageOptions{
   186  						Types: []types.DiskUsageObject{
   187  							types.ImageObject,
   188  							types.BuildCacheObject,
   189  						},
   190  					},
   191  					expected: types.DiskUsage{
   192  						LayersSize: stepDU.LayersSize,
   193  						Images:     stepDU.Images,
   194  						BuildCache: stepDU.BuildCache,
   195  					},
   196  				},
   197  				{
   198  					doc: "container, volume, build-cache types",
   199  					options: types.DiskUsageOptions{
   200  						Types: []types.DiskUsageObject{
   201  							types.ContainerObject,
   202  							types.VolumeObject,
   203  							types.BuildCacheObject,
   204  						},
   205  					},
   206  					expected: types.DiskUsage{
   207  						Containers: stepDU.Containers,
   208  						Volumes:    stepDU.Volumes,
   209  						BuildCache: stepDU.BuildCache,
   210  					},
   211  				},
   212  				{
   213  					doc: "image, volume, build-cache types",
   214  					options: types.DiskUsageOptions{
   215  						Types: []types.DiskUsageObject{
   216  							types.ImageObject,
   217  							types.VolumeObject,
   218  							types.BuildCacheObject,
   219  						},
   220  					},
   221  					expected: types.DiskUsage{
   222  						LayersSize: stepDU.LayersSize,
   223  						Images:     stepDU.Images,
   224  						Volumes:    stepDU.Volumes,
   225  						BuildCache: stepDU.BuildCache,
   226  					},
   227  				},
   228  				{
   229  					doc: "container, image, volume types",
   230  					options: types.DiskUsageOptions{
   231  						Types: []types.DiskUsageObject{
   232  							types.ContainerObject,
   233  							types.ImageObject,
   234  							types.VolumeObject,
   235  						},
   236  					},
   237  					expected: types.DiskUsage{
   238  						LayersSize: stepDU.LayersSize,
   239  						Containers: stepDU.Containers,
   240  						Images:     stepDU.Images,
   241  						Volumes:    stepDU.Volumes,
   242  					},
   243  				},
   244  				{
   245  					doc: "container, image, volume, build-cache types",
   246  					options: types.DiskUsageOptions{
   247  						Types: []types.DiskUsageObject{
   248  							types.ContainerObject,
   249  							types.ImageObject,
   250  							types.VolumeObject,
   251  							types.BuildCacheObject,
   252  						},
   253  					},
   254  					expected: types.DiskUsage{
   255  						LayersSize: stepDU.LayersSize,
   256  						Containers: stepDU.Containers,
   257  						Images:     stepDU.Images,
   258  						Volumes:    stepDU.Volumes,
   259  						BuildCache: stepDU.BuildCache,
   260  					},
   261  				},
   262  			} {
   263  				tc := tc
   264  				t.Run(tc.doc, func(t *testing.T) {
   265  					// TODO: Run in parallel once https://github.com/moby/moby/pull/42560 is merged.
   266  
   267  					du, err := client.DiskUsage(ctx, tc.options)
   268  					assert.NilError(t, err)
   269  					assert.DeepEqual(t, du, tc.expected)
   270  				})
   271  			}
   272  		})
   273  	}
   274  }