Signed-off-by: Timo Rothenpieler <timo@rothenpieler.org>
| ... | ... |
@@ -6,121 +6,47 @@ import ( |
| 6 | 6 |
"io" |
| 7 | 7 |
"io/ioutil" |
| 8 | 8 |
"os" |
| 9 |
- "os/exec" |
|
| 10 | 9 |
"path/filepath" |
| 11 | 10 |
"testing" |
| 12 | 11 |
|
| 13 |
- "golang.org/x/sys/unix" |
|
| 14 | 12 |
"gotest.tools/v3/assert" |
| 15 | 13 |
is "gotest.tools/v3/assert/cmp" |
| 16 |
- "gotest.tools/v3/fs" |
|
| 17 | 14 |
) |
| 18 | 15 |
|
| 19 | 16 |
// 10MB |
| 20 | 17 |
const testQuotaSize = 10 * 1024 * 1024 |
| 21 |
-const imageSize = 64 * 1024 * 1024 |
|
| 22 | 18 |
|
| 23 | 19 |
func TestBlockDev(t *testing.T) {
|
| 24 |
- mkfs, err := exec.LookPath("mkfs.xfs")
|
|
| 25 |
- if err != nil {
|
|
| 26 |
- t.Skip("mkfs.xfs not found in PATH")
|
|
| 20 |
+ if msg, ok := CanTestQuota(); !ok {
|
|
| 21 |
+ t.Skip(msg) |
|
| 27 | 22 |
} |
| 28 | 23 |
|
| 29 |
- // create a sparse image |
|
| 30 |
- imageFile, err := ioutil.TempFile("", "xfs-image")
|
|
| 24 |
+ // get sparse xfs test image |
|
| 25 |
+ imageFileName, err := PrepareQuotaTestImage(t) |
|
| 31 | 26 |
if err != nil {
|
| 32 | 27 |
t.Fatal(err) |
| 33 | 28 |
} |
| 34 |
- imageFileName := imageFile.Name() |
|
| 35 | 29 |
defer os.Remove(imageFileName) |
| 36 |
- if _, err = imageFile.Seek(imageSize-1, 0); err != nil {
|
|
| 37 |
- t.Fatal(err) |
|
| 38 |
- } |
|
| 39 |
- if _, err = imageFile.Write([]byte{0}); err != nil {
|
|
| 40 |
- t.Fatal(err) |
|
| 41 |
- } |
|
| 42 |
- if err = imageFile.Close(); err != nil {
|
|
| 43 |
- t.Fatal(err) |
|
| 44 |
- } |
|
| 45 |
- |
|
| 46 |
- // The reason for disabling these options is sometimes people run with a newer userspace |
|
| 47 |
- // than kernelspace |
|
| 48 |
- out, err := exec.Command(mkfs, "-m", "crc=0,finobt=0", imageFileName).CombinedOutput() |
|
| 49 |
- if len(out) > 0 {
|
|
| 50 |
- t.Log(string(out)) |
|
| 51 |
- } |
|
| 52 |
- if err != nil {
|
|
| 53 |
- t.Fatal(err) |
|
| 54 |
- } |
|
| 55 |
- |
|
| 56 |
- t.Run("testBlockDevQuotaDisabled", wrapMountTest(imageFileName, false, testBlockDevQuotaDisabled))
|
|
| 57 |
- t.Run("testBlockDevQuotaEnabled", wrapMountTest(imageFileName, true, testBlockDevQuotaEnabled))
|
|
| 58 |
- t.Run("testSmallerThanQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testSmallerThanQuota)))
|
|
| 59 |
- t.Run("testBiggerThanQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testBiggerThanQuota)))
|
|
| 60 |
- t.Run("testRetrieveQuota", wrapMountTest(imageFileName, true, wrapQuotaTest(testRetrieveQuota)))
|
|
| 61 |
-} |
|
| 62 | 30 |
|
| 63 |
-func wrapMountTest(imageFileName string, enableQuota bool, testFunc func(t *testing.T, mountPoint, backingFsDev string)) func(*testing.T) {
|
|
| 64 |
- return func(t *testing.T) {
|
|
| 65 |
- mountOptions := "loop" |
|
| 66 |
- |
|
| 67 |
- if enableQuota {
|
|
| 68 |
- mountOptions = mountOptions + ",prjquota" |
|
| 69 |
- } |
|
| 70 |
- |
|
| 71 |
- mountPointDir := fs.NewDir(t, "xfs-mountPoint") |
|
| 72 |
- defer mountPointDir.Remove() |
|
| 73 |
- mountPoint := mountPointDir.Path() |
|
| 74 |
- |
|
| 75 |
- out, err := exec.Command("mount", "-o", mountOptions, imageFileName, mountPoint).CombinedOutput()
|
|
| 76 |
- if err != nil {
|
|
| 77 |
- _, err := os.Stat("/proc/fs/xfs")
|
|
| 78 |
- if os.IsNotExist(err) {
|
|
| 79 |
- t.Skip("no /proc/fs/xfs")
|
|
| 80 |
- } |
|
| 81 |
- } |
|
| 82 |
- |
|
| 83 |
- assert.NilError(t, err, "mount failed: %s", out) |
|
| 84 |
- |
|
| 85 |
- defer func() {
|
|
| 86 |
- assert.NilError(t, unix.Unmount(mountPoint, 0)) |
|
| 87 |
- }() |
|
| 88 |
- |
|
| 89 |
- backingFsDev, err := makeBackingFsDev(mountPoint) |
|
| 90 |
- assert.NilError(t, err) |
|
| 91 |
- |
|
| 92 |
- testFunc(t, mountPoint, backingFsDev) |
|
| 93 |
- } |
|
| 31 |
+ t.Run("testBlockDevQuotaDisabled", WrapMountTest(imageFileName, false, testBlockDevQuotaDisabled))
|
|
| 32 |
+ t.Run("testBlockDevQuotaEnabled", WrapMountTest(imageFileName, true, testBlockDevQuotaEnabled))
|
|
| 33 |
+ t.Run("testSmallerThanQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testSmallerThanQuota)))
|
|
| 34 |
+ t.Run("testBiggerThanQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testBiggerThanQuota)))
|
|
| 35 |
+ t.Run("testRetrieveQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testRetrieveQuota)))
|
|
| 94 | 36 |
} |
| 95 | 37 |
|
| 96 |
-func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev string) {
|
|
| 38 |
+func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
|
| 97 | 39 |
hasSupport, err := hasQuotaSupport(backingFsDev) |
| 98 | 40 |
assert.NilError(t, err) |
| 99 | 41 |
assert.Check(t, !hasSupport) |
| 100 | 42 |
} |
| 101 | 43 |
|
| 102 |
-func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev string) {
|
|
| 44 |
+func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
|
| 103 | 45 |
hasSupport, err := hasQuotaSupport(backingFsDev) |
| 104 | 46 |
assert.NilError(t, err) |
| 105 | 47 |
assert.Check(t, hasSupport) |
| 106 | 48 |
} |
| 107 | 49 |
|
| 108 |
-func wrapQuotaTest(testFunc func(t *testing.T, ctrl *Control, mountPoint, testDir, testSubDir string)) func(t *testing.T, mountPoint, backingFsDev string) {
|
|
| 109 |
- return func(t *testing.T, mountPoint, backingFsDev string) {
|
|
| 110 |
- testDir, err := ioutil.TempDir(mountPoint, "per-test") |
|
| 111 |
- assert.NilError(t, err) |
|
| 112 |
- defer os.RemoveAll(testDir) |
|
| 113 |
- |
|
| 114 |
- ctrl, err := NewControl(testDir) |
|
| 115 |
- assert.NilError(t, err) |
|
| 116 |
- |
|
| 117 |
- testSubDir, err := ioutil.TempDir(testDir, "quota-test") |
|
| 118 |
- assert.NilError(t, err) |
|
| 119 |
- testFunc(t, ctrl, mountPoint, testDir, testSubDir) |
|
| 120 |
- } |
|
| 121 |
- |
|
| 122 |
-} |
|
| 123 |
- |
|
| 124 | 50 |
func testSmallerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
|
| 125 | 51 |
assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
|
| 126 | 52 |
smallerThanQuotaFile := filepath.Join(testSubDir, "smaller-than-quota") |
| 127 | 53 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,122 @@ |
| 0 |
+// +build linux |
|
| 1 |
+ |
|
| 2 |
+package quota // import "github.com/docker/docker/quota" |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "io/ioutil" |
|
| 6 |
+ "os" |
|
| 7 |
+ "os/exec" |
|
| 8 |
+ "testing" |
|
| 9 |
+ |
|
| 10 |
+ "golang.org/x/sys/unix" |
|
| 11 |
+ "gotest.tools/v3/assert" |
|
| 12 |
+ "gotest.tools/v3/fs" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+const imageSize = 64 * 1024 * 1024 |
|
| 16 |
+ |
|
| 17 |
+// CanTestQuota - checks if xfs prjquota can be tested |
|
| 18 |
+// returns a reason if not |
|
| 19 |
+func CanTestQuota() (string, bool) {
|
|
| 20 |
+ if os.Getuid() != 0 {
|
|
| 21 |
+ return "requires mounts", false |
|
| 22 |
+ } |
|
| 23 |
+ _, err := exec.LookPath("mkfs.xfs")
|
|
| 24 |
+ if err != nil {
|
|
| 25 |
+ return "mkfs.xfs not found in PATH", false |
|
| 26 |
+ } |
|
| 27 |
+ return "", true |
|
| 28 |
+} |
|
| 29 |
+ |
|
| 30 |
+// PrepareQuotaTestImage - prepares an xfs prjquota test image |
|
| 31 |
+// returns the path the the image on success |
|
| 32 |
+func PrepareQuotaTestImage(t *testing.T) (string, error) {
|
|
| 33 |
+ mkfs, err := exec.LookPath("mkfs.xfs")
|
|
| 34 |
+ if err != nil {
|
|
| 35 |
+ return "", err |
|
| 36 |
+ } |
|
| 37 |
+ |
|
| 38 |
+ // create a sparse image |
|
| 39 |
+ imageFile, err := ioutil.TempFile("", "xfs-image")
|
|
| 40 |
+ if err != nil {
|
|
| 41 |
+ return "", err |
|
| 42 |
+ } |
|
| 43 |
+ imageFileName := imageFile.Name() |
|
| 44 |
+ if _, err = imageFile.Seek(imageSize-1, 0); err != nil {
|
|
| 45 |
+ os.Remove(imageFileName) |
|
| 46 |
+ return "", err |
|
| 47 |
+ } |
|
| 48 |
+ if _, err = imageFile.Write([]byte{0}); err != nil {
|
|
| 49 |
+ os.Remove(imageFileName) |
|
| 50 |
+ return "", err |
|
| 51 |
+ } |
|
| 52 |
+ if err = imageFile.Close(); err != nil {
|
|
| 53 |
+ os.Remove(imageFileName) |
|
| 54 |
+ return "", err |
|
| 55 |
+ } |
|
| 56 |
+ |
|
| 57 |
+ // The reason for disabling these options is sometimes people run with a newer userspace |
|
| 58 |
+ // than kernelspace |
|
| 59 |
+ out, err := exec.Command(mkfs, "-m", "crc=0,finobt=0", imageFileName).CombinedOutput() |
|
| 60 |
+ if len(out) > 0 {
|
|
| 61 |
+ t.Log(string(out)) |
|
| 62 |
+ } |
|
| 63 |
+ if err != nil {
|
|
| 64 |
+ os.Remove(imageFileName) |
|
| 65 |
+ return "", err |
|
| 66 |
+ } |
|
| 67 |
+ |
|
| 68 |
+ return imageFileName, nil |
|
| 69 |
+} |
|
| 70 |
+ |
|
| 71 |
+// WrapMountTest - wraps a test function such that it has easy access to a mountPoint and testDir |
|
| 72 |
+// with guaranteed prjquota or guaranteed no prjquota support. |
|
| 73 |
+func WrapMountTest(imageFileName string, enableQuota bool, testFunc func(t *testing.T, mountPoint, backingFsDev, testDir string)) func(*testing.T) {
|
|
| 74 |
+ return func(t *testing.T) {
|
|
| 75 |
+ mountOptions := "loop" |
|
| 76 |
+ |
|
| 77 |
+ if enableQuota {
|
|
| 78 |
+ mountOptions = mountOptions + ",prjquota" |
|
| 79 |
+ } |
|
| 80 |
+ |
|
| 81 |
+ mountPointDir := fs.NewDir(t, "xfs-mountPoint") |
|
| 82 |
+ defer mountPointDir.Remove() |
|
| 83 |
+ mountPoint := mountPointDir.Path() |
|
| 84 |
+ |
|
| 85 |
+ out, err := exec.Command("mount", "-o", mountOptions, imageFileName, mountPoint).CombinedOutput()
|
|
| 86 |
+ if err != nil {
|
|
| 87 |
+ _, err := os.Stat("/proc/fs/xfs")
|
|
| 88 |
+ if os.IsNotExist(err) {
|
|
| 89 |
+ t.Skip("no /proc/fs/xfs")
|
|
| 90 |
+ } |
|
| 91 |
+ } |
|
| 92 |
+ |
|
| 93 |
+ assert.NilError(t, err, "mount failed: %s", out) |
|
| 94 |
+ |
|
| 95 |
+ defer func() {
|
|
| 96 |
+ assert.NilError(t, unix.Unmount(mountPoint, 0)) |
|
| 97 |
+ }() |
|
| 98 |
+ |
|
| 99 |
+ backingFsDev, err := makeBackingFsDev(mountPoint) |
|
| 100 |
+ assert.NilError(t, err) |
|
| 101 |
+ |
|
| 102 |
+ testDir, err := ioutil.TempDir(mountPoint, "per-test") |
|
| 103 |
+ assert.NilError(t, err) |
|
| 104 |
+ defer os.RemoveAll(testDir) |
|
| 105 |
+ |
|
| 106 |
+ testFunc(t, mountPoint, backingFsDev, testDir) |
|
| 107 |
+ } |
|
| 108 |
+} |
|
| 109 |
+ |
|
| 110 |
+// WrapQuotaTest - wraps a test function such that is has easy and guaranteed access to a quota Control |
|
| 111 |
+// instance with a quota test dir under its control. |
|
| 112 |
+func WrapQuotaTest(testFunc func(t *testing.T, ctrl *Control, mountPoint, testDir, testSubDir string)) func(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
|
| 113 |
+ return func(t *testing.T, mountPoint, backingFsDev, testDir string) {
|
|
| 114 |
+ ctrl, err := NewControl(testDir) |
|
| 115 |
+ assert.NilError(t, err) |
|
| 116 |
+ |
|
| 117 |
+ testSubDir, err := ioutil.TempDir(testDir, "quota-test") |
|
| 118 |
+ assert.NilError(t, err) |
|
| 119 |
+ testFunc(t, ctrl, mountPoint, testDir, testSubDir) |
|
| 120 |
+ } |
|
| 121 |
+} |