From 3b9b03935e65c5d716ed1fe78831d6bab95aa0ba Mon Sep 17 00:00:00 2001 From: Itamar Holder Date: Wed, 1 May 2024 13:59:47 +0300 Subject: [PATCH] unit test: Use tmpfs noswap if supported Signed-off-by: Itamar Holder --- pkg/volume/emptydir/empty_dir_test.go | 56 +++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pkg/volume/emptydir/empty_dir_test.go b/pkg/volume/emptydir/empty_dir_test.go index ff2c199cd7e..9aa92fe1120 100644 --- a/pkg/volume/emptydir/empty_dir_test.go +++ b/pkg/volume/emptydir/empty_dir_test.go @@ -21,8 +21,10 @@ package emptydir import ( "fmt" + "k8s.io/kubernetes/pkg/kubelet/util/swap" "os" "path/filepath" + "strings" "testing" v1 "k8s.io/api/core/v1" @@ -1061,3 +1063,57 @@ func TestCalculateEmptyDirMemorySize(t *testing.T) { }) } } + +func TestTmpfsMountOptions(t *testing.T) { + subQuantity := resource.MustParse("123Ki") + + doesStringArrayContainSubstring := func(strSlice []string, substr string) bool { + for _, s := range strSlice { + if strings.Contains(s, substr) { + return true + } + } + return false + } + + testCases := map[string]struct { + tmpfsNoswapSupported bool + sizeLimit resource.Quantity + }{ + "default bahavior": {}, + "tmpfs noswap is supported": { + tmpfsNoswapSupported: true, + }, + "size limit is non-zero": { + sizeLimit: subQuantity, + }, + "tmpfs noswap is supported and size limit is non-zero": { + tmpfsNoswapSupported: true, + sizeLimit: subQuantity, + }, + } + + for testCaseName, testCase := range testCases { + t.Run(testCaseName, func(t *testing.T) { + emptyDirObj := emptyDir{ + sizeLimit: &testCase.sizeLimit, + } + + options := emptyDirObj.generateTmpfsMountOptions(testCase.tmpfsNoswapSupported) + + if testCase.tmpfsNoswapSupported && !doesStringArrayContainSubstring(options, swap.TmpfsNoswapOption) { + t.Errorf("tmpfs noswap option is expected when supported. options: %v", options) + } + if !testCase.tmpfsNoswapSupported && doesStringArrayContainSubstring(options, swap.TmpfsNoswapOption) { + t.Errorf("tmpfs noswap option is not expected when unsupported. options: %v", options) + } + + if testCase.sizeLimit.IsZero() && doesStringArrayContainSubstring(options, "size=") { + t.Errorf("size is not expected when is zero. options: %v", options) + } + if expectedOption := fmt.Sprintf("size=%d", testCase.sizeLimit.Value()); !testCase.sizeLimit.IsZero() && !doesStringArrayContainSubstring(options, expectedOption) { + t.Errorf("size option is not expected when is zero. options: %v", options) + } + }) + } +}