github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/moby/volume/local/local_linux_test.go (about) 1 // +build linux 2 3 package local // import "github.com/docker/docker/volume/local" 4 5 import ( 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/docker/docker/pkg/idtools" 12 "github.com/docker/docker/quota" 13 "gotest.tools/v3/assert" 14 is "gotest.tools/v3/assert/cmp" 15 ) 16 17 const quotaSize = 1024 * 1024 18 const quotaSizeLiteral = "1M" 19 20 func TestQuota(t *testing.T) { 21 if msg, ok := quota.CanTestQuota(); !ok { 22 t.Skip(msg) 23 } 24 25 // get sparse xfs test image 26 imageFileName, err := quota.PrepareQuotaTestImage(t) 27 if err != nil { 28 t.Fatal(err) 29 } 30 defer os.Remove(imageFileName) 31 32 t.Run("testVolWithQuota", quota.WrapMountTest(imageFileName, true, testVolWithQuota)) 33 t.Run("testVolQuotaUnsupported", quota.WrapMountTest(imageFileName, false, testVolQuotaUnsupported)) 34 } 35 36 func testVolWithQuota(t *testing.T, mountPoint, backingFsDev, testDir string) { 37 r, err := New(testDir, idtools.Identity{UID: os.Geteuid(), GID: os.Getegid()}) 38 if err != nil { 39 t.Fatal(err) 40 } 41 assert.Assert(t, r.quotaCtl != nil) 42 43 vol, err := r.Create("testing", map[string]string{"size": quotaSizeLiteral}) 44 if err != nil { 45 t.Fatal(err) 46 } 47 48 dir, err := vol.Mount("1234") 49 if err != nil { 50 t.Fatal(err) 51 } 52 defer func() { 53 if err := vol.Unmount("1234"); err != nil { 54 t.Fatal(err) 55 } 56 }() 57 58 testfile := filepath.Join(dir, "testfile") 59 60 // test writing file smaller than quota 61 assert.NilError(t, ioutil.WriteFile(testfile, make([]byte, quotaSize/2), 0644)) 62 assert.NilError(t, os.Remove(testfile)) 63 64 // test writing fiel larger than quota 65 err = ioutil.WriteFile(testfile, make([]byte, quotaSize+1), 0644) 66 assert.ErrorContains(t, err, "") 67 if _, err := os.Stat(testfile); err == nil { 68 assert.NilError(t, os.Remove(testfile)) 69 } 70 } 71 72 func testVolQuotaUnsupported(t *testing.T, mountPoint, backingFsDev, testDir string) { 73 r, err := New(testDir, idtools.Identity{UID: os.Geteuid(), GID: os.Getegid()}) 74 if err != nil { 75 t.Fatal(err) 76 } 77 assert.Assert(t, is.Nil(r.quotaCtl)) 78 79 _, err = r.Create("testing", map[string]string{"size": quotaSizeLiteral}) 80 assert.ErrorContains(t, err, "no quota support") 81 82 vol, err := r.Create("testing", nil) 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 // this could happen if someone moves volumes from storage with 88 // quota support to some place without 89 lv, ok := vol.(*localVolume) 90 assert.Assert(t, ok) 91 lv.opts = &optsConfig{ 92 Quota: quota.Quota{Size: quotaSize}, 93 } 94 95 _, err = vol.Mount("1234") 96 assert.ErrorContains(t, err, "no quota support") 97 }