github.com/rawahars/moby@v24.0.4+incompatible/quota/projectquota_test.go (about)

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