2017-09-03 00:25:36 -04:00
|
|
|
// +build linux
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package quota // import "github.com/docker/docker/daemon/graphdriver/quota"
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/fs"
|
2017-09-03 00:25:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 10MB
|
|
|
|
const testQuotaSize = 10 * 1024 * 1024
|
|
|
|
const imageSize = 64 * 1024 * 1024
|
|
|
|
|
|
|
|
func TestBlockDev(t *testing.T) {
|
|
|
|
mkfs, err := exec.LookPath("mkfs.xfs")
|
|
|
|
if err != nil {
|
2017-12-29 13:17:46 -05:00
|
|
|
t.Skip("mkfs.xfs not found in PATH")
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a sparse image
|
|
|
|
imageFile, err := ioutil.TempFile("", "xfs-image")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
imageFileName := imageFile.Name()
|
|
|
|
defer os.Remove(imageFileName)
|
|
|
|
if _, err = imageFile.Seek(imageSize-1, 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err = imageFile.Write([]byte{0}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = imageFile.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The reason for disabling these options is sometimes people run with a newer userspace
|
|
|
|
// than kernelspace
|
|
|
|
out, err := exec.Command(mkfs, "-m", "crc=0,finobt=0", imageFileName).CombinedOutput()
|
|
|
|
if len(out) > 0 {
|
|
|
|
t.Log(string(out))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-12-29 13:17:46 -05:00
|
|
|
t.Run("testBlockDevQuotaDisabled", wrapMountTest(imageFileName, false, testBlockDevQuotaDisabled))
|
|
|
|
t.Run("testBlockDevQuotaEnabled", wrapMountTest(imageFileName, true, testBlockDevQuotaEnabled))
|
|
|
|
t.Run("testSmallerThanQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testSmallerThanQuota)))
|
|
|
|
t.Run("testBiggerThanQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testBiggerThanQuota)))
|
|
|
|
t.Run("testRetrieveQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testRetrieveQuota)))
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func wrapMountTest(imageFileName string, enableQuota bool, testFunc func(t *testing.T, mountPoint, backingFsDev string)) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
mountOptions := "loop"
|
|
|
|
|
|
|
|
if enableQuota {
|
|
|
|
mountOptions = mountOptions + ",prjquota"
|
|
|
|
}
|
|
|
|
|
2017-12-29 13:17:46 -05:00
|
|
|
mountPointDir := fs.NewDir(t, "xfs-mountPoint")
|
|
|
|
defer mountPointDir.Remove()
|
|
|
|
mountPoint := mountPointDir.Path()
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
out, err := exec.Command("mount", "-o", mountOptions, imageFileName, mountPoint).CombinedOutput()
|
|
|
|
if err != nil {
|
2017-12-29 13:17:46 -05:00
|
|
|
_, err := os.Stat("/proc/fs/xfs")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Skip("no /proc/fs/xfs")
|
|
|
|
}
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err, "mount failed: %s", out)
|
2017-12-29 13:17:46 -05:00
|
|
|
|
2017-09-03 00:25:36 -04:00
|
|
|
defer func() {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, unix.Unmount(mountPoint, 0))
|
2017-09-03 00:25:36 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
backingFsDev, err := makeBackingFsDev(mountPoint)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
testFunc(t, mountPoint, backingFsDev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev string) {
|
|
|
|
hasSupport, err := hasQuotaSupport(backingFsDev)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !hasSupport)
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev string) {
|
|
|
|
hasSupport, err := hasQuotaSupport(backingFsDev)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, hasSupport)
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func wrapQuotaTest(testFunc func(t *testing.T, ctrl *Control, mountPoint, testDir, testSubDir string)) func(t *testing.T, mountPoint, backingFsDev string) {
|
|
|
|
return func(t *testing.T, mountPoint, backingFsDev string) {
|
|
|
|
testDir, err := ioutil.TempDir(mountPoint, "per-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-03 00:25:36 -04:00
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
ctrl, err := NewControl(testDir)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
testSubDir, err := ioutil.TempDir(testDir, "quota-test")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-03 00:25:36 -04:00
|
|
|
testFunc(t, ctrl, mountPoint, testDir, testSubDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSmallerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 00:25:36 -04:00
|
|
|
smallerThanQuotaFile := filepath.Join(testSubDir, "smaller-than-quota")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, ioutil.WriteFile(smallerThanQuotaFile, make([]byte, testQuotaSize/2), 0644))
|
|
|
|
assert.NilError(t, os.Remove(smallerThanQuotaFile))
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testBiggerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
|
|
|
// Make sure the quota is being enforced
|
|
|
|
// TODO: When we implement this under EXT4, we need to shed CAP_SYS_RESOURCE, otherwise
|
|
|
|
// we're able to violate quota without issue
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
biggerThanQuotaFile := filepath.Join(testSubDir, "bigger-than-quota")
|
|
|
|
err := ioutil.WriteFile(biggerThanQuotaFile, make([]byte, testQuotaSize+1), 0644)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
2017-09-03 00:25:36 -04:00
|
|
|
if err == io.ErrShortWrite {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, os.Remove(biggerThanQuotaFile))
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRetrieveQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
|
|
|
// Validate that we can retrieve quota
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
var q Quota
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, ctrl.GetQuota(testSubDir, &q))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, is.Equal(uint64(testQuotaSize), q.Size))
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|