github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/quota/projectquota_test.go (about)

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