2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2017-09-03 00:25:36 -04:00
|
|
|
// +build linux
|
|
|
|
|
2020-08-09 16:04:43 -04:00
|
|
|
package quota // import "github.com/docker/docker/quota"
|
2017-09-03 00:25:36 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-09-03 00:25:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 10MB
|
|
|
|
const testQuotaSize = 10 * 1024 * 1024
|
|
|
|
|
|
|
|
func TestBlockDev(t *testing.T) {
|
2020-08-10 12:00:09 -04:00
|
|
|
if msg, ok := CanTestQuota(); !ok {
|
|
|
|
t.Skip(msg)
|
2017-09-03 00:25:36 -04:00
|
|
|
}
|
|
|
|
|
2020-08-10 12:00:09 -04:00
|
|
|
// get sparse xfs test image
|
|
|
|
imageFileName, err := PrepareQuotaTestImage(t)
|
2017-09-03 00:25:36 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(imageFileName)
|
|
|
|
|
2020-08-10 12:00:09 -04: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
|
|
|
}
|
|
|
|
|
2020-08-10 12:00:09 -04:00
|
|
|
func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
2017-09-03 00:25:36 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-10 12:00:09 -04:00
|
|
|
func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
2017-09-03 00:25:36 -04:00
|
|
|
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 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")
|
2021-08-24 06:10:50 -04:00
|
|
|
assert.NilError(t, os.WriteFile(smallerThanQuotaFile, make([]byte, testQuotaSize/2), 0644))
|
2018-03-13 15:28:34 -04:00
|
|
|
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")
|
2021-08-24 06:10:50 -04:00
|
|
|
err := os.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
|
|
|
}
|