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