projectquota_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //go:build linux
  2. package quota // import "github.com/docker/docker/quota"
  3. import (
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "gotest.tools/v3/assert"
  9. is "gotest.tools/v3/assert/cmp"
  10. )
  11. // 10MB
  12. const testQuotaSize = 10 * 1024 * 1024
  13. func TestBlockDev(t *testing.T) {
  14. if msg, ok := CanTestQuota(); !ok {
  15. t.Skip(msg)
  16. }
  17. // get sparse xfs test image
  18. imageFileName, err := PrepareQuotaTestImage(t)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer os.Remove(imageFileName)
  23. t.Run("testBlockDevQuotaDisabled", WrapMountTest(imageFileName, false, testBlockDevQuotaDisabled))
  24. t.Run("testBlockDevQuotaEnabled", WrapMountTest(imageFileName, true, testBlockDevQuotaEnabled))
  25. t.Run("testSmallerThanQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testSmallerThanQuota)))
  26. t.Run("testBiggerThanQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testBiggerThanQuota)))
  27. t.Run("testRetrieveQuota", WrapMountTest(imageFileName, true, WrapQuotaTest(testRetrieveQuota)))
  28. }
  29. func testBlockDevQuotaDisabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
  30. hasSupport, err := hasQuotaSupport(backingFsDev)
  31. assert.NilError(t, err)
  32. assert.Check(t, !hasSupport)
  33. }
  34. func testBlockDevQuotaEnabled(t *testing.T, mountPoint, backingFsDev, testDir string) {
  35. hasSupport, err := hasQuotaSupport(backingFsDev)
  36. assert.NilError(t, err)
  37. assert.Check(t, hasSupport)
  38. }
  39. func testSmallerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
  40. assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
  41. smallerThanQuotaFile := filepath.Join(testSubDir, "smaller-than-quota")
  42. assert.NilError(t, os.WriteFile(smallerThanQuotaFile, make([]byte, testQuotaSize/2), 0o644))
  43. assert.NilError(t, os.Remove(smallerThanQuotaFile))
  44. }
  45. func testBiggerThanQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
  46. // Make sure the quota is being enforced
  47. // TODO: When we implement this under EXT4, we need to shed CAP_SYS_RESOURCE, otherwise
  48. // we're able to violate quota without issue
  49. assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
  50. biggerThanQuotaFile := filepath.Join(testSubDir, "bigger-than-quota")
  51. err := os.WriteFile(biggerThanQuotaFile, make([]byte, testQuotaSize+1), 0o644)
  52. assert.Assert(t, is.ErrorContains(err, ""))
  53. if err == io.ErrShortWrite {
  54. assert.NilError(t, os.Remove(biggerThanQuotaFile))
  55. }
  56. }
  57. func testRetrieveQuota(t *testing.T, ctrl *Control, homeDir, testDir, testSubDir string) {
  58. // Validate that we can retrieve quota
  59. assert.NilError(t, ctrl.SetQuota(testSubDir, Quota{testQuotaSize}))
  60. var q Quota
  61. assert.NilError(t, ctrl.GetQuota(testSubDir, &q))
  62. assert.Check(t, is.Equal(uint64(testQuotaSize), q.Size))
  63. }