github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/volume/service/service_linux_test.go (about) 1 package service 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/Prakhar-Agarwal-byte/moby/pkg/idtools" 10 "github.com/Prakhar-Agarwal-byte/moby/volume" 11 volumedrivers "github.com/Prakhar-Agarwal-byte/moby/volume/drivers" 12 "github.com/Prakhar-Agarwal-byte/moby/volume/local" 13 "github.com/Prakhar-Agarwal-byte/moby/volume/service/opts" 14 "github.com/Prakhar-Agarwal-byte/moby/volume/testutils" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestLocalVolumeSize(t *testing.T) { 20 t.Parallel() 21 22 ds := volumedrivers.NewStore(nil) 23 dir, err := os.MkdirTemp("", t.Name()) 24 assert.NilError(t, err) 25 defer os.RemoveAll(dir) 26 27 l, err := local.New(dir, idtools.Identity{UID: os.Getuid(), GID: os.Getegid()}) 28 assert.NilError(t, err) 29 assert.Assert(t, ds.Register(l, volume.DefaultDriverName)) 30 assert.Assert(t, ds.Register(testutils.NewFakeDriver("fake"), "fake")) 31 32 service, cleanup := newTestService(t, ds) 33 defer cleanup() 34 35 ctx := context.Background() 36 v1, err := service.Create(ctx, "test1", volume.DefaultDriverName, opts.WithCreateReference("foo")) 37 assert.NilError(t, err) 38 v2, err := service.Create(ctx, "test2", volume.DefaultDriverName) 39 assert.NilError(t, err) 40 _, err = service.Create(ctx, "test3", "fake") 41 assert.NilError(t, err) 42 43 data := make([]byte, 1024) 44 err = os.WriteFile(filepath.Join(v1.Mountpoint, "data"), data, 0o644) 45 assert.NilError(t, err) 46 err = os.WriteFile(filepath.Join(v2.Mountpoint, "data"), data[:1], 0o644) 47 assert.NilError(t, err) 48 49 ls, err := service.LocalVolumesSize(ctx) 50 assert.NilError(t, err) 51 assert.Assert(t, is.Len(ls, 2)) 52 53 for _, v := range ls { 54 switch v.Name { 55 case "test1": 56 assert.Assert(t, is.Equal(v.UsageData.Size, int64(len(data)))) 57 assert.Assert(t, is.Equal(v.UsageData.RefCount, int64(1))) 58 case "test2": 59 assert.Assert(t, is.Equal(v.UsageData.Size, int64(len(data[:1])))) 60 assert.Assert(t, is.Equal(v.UsageData.RefCount, int64(0))) 61 default: 62 t.Fatalf("got unexpected volume: %+v", v) 63 } 64 } 65 }