github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/system/disk_usage_test.go (about)

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