2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2017-09-03 04:25:36 +00:00
|
|
|
|
2020-08-09 20:04:43 +00:00
|
|
|
package quota // import "github.com/docker/docker/quota"
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-09-03 04:25:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 10MB
|
|
|
|
const testQuotaSize = 10 * 1024 * 1024
|
|
|
|
|
|
|
|
func TestBlockDev(t *testing.T) {
|
2020-08-10 16:00:09 +00:00
|
|
|
if msg, ok := CanTestQuota(); !ok {
|
|
|
|
t.Skip(msg)
|
2017-09-03 04:25:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 16:00:09 +00:00
|
|
|
// get sparse xfs test image
|
|
|
|
imageFileName, err := PrepareQuotaTestImage(t)
|
2017-09-03 04:25:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(imageFileName)
|
|
|
|
|
2020-08-10 16:00:09 +00: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 04:25:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 16:00:09 +00:00
|
|
|
func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
2017-09-03 04:25:36 +00:00
|
|
|
hasSupport, err := hasQuotaSupport(backingFsDev)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !hasSupport)
|
2017-09-03 04:25:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 16:00:09 +00:00
|
|
|
func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
2017-09-03 04:25:36 +00:00
|
|
|
hasSupport, err := hasQuotaSupport(backingFsDev)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, hasSupport)
|
2017-09-03 04:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSmallerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 04:25:36 +00:00
|
|
|
smallerThanQuotaFile := filepath.Join(testSubDir, "smaller-than-quota")
|
2022-01-20 13:13:30 +00:00
|
|
|
assert.NilError(t, os.WriteFile(smallerThanQuotaFile, make([]byte, testQuotaSize/2), 0o644))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, os.Remove(smallerThanQuotaFile))
|
2017-09-03 04:25:36 +00: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 19:28:34 +00:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
biggerThanQuotaFile := filepath.Join(testSubDir, "bigger-than-quota")
|
2022-01-20 13:13:30 +00:00
|
|
|
err := os.WriteFile(biggerThanQuotaFile, make([]byte, testQuotaSize+1), 0o644)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""))
|
2017-09-03 04:25:36 +00:00
|
|
|
if err == io.ErrShortWrite {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, os.Remove(biggerThanQuotaFile))
|
2017-09-03 04:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRetrieveQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
|
|
|
// Validate that we can retrieve quota
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
2017-09-03 04:25:36 +00:00
|
|
|
|
|
|
|
var q Quota
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, ctrl.GetQuota(testSubDir, &q))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.Check(t, is.Equal(uint64(testQuotaSize), q.Size))
|
2017-09-03 04:25:36 +00:00
|
|
|
}
|