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