Skip to content

Commit 302dd6c

Browse files
committed
Add unit tests
1 parent 9310f46 commit 302dd6c

File tree

2 files changed

+79
-25
lines changed

2 files changed

+79
-25
lines changed

pkg/gce-pd-csi-driver/cache.go

+29-25
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ import (
1515
)
1616

1717
const (
18-
cacheSuffix = "csi-fast"
19-
mainLvSuffix = "csi-main"
20-
raidedLocalSsdName = "csi-driver-data-cache"
21-
raidMode = "0"
22-
raidedLssdPrefix = "/dev/md/"
23-
maxAllowedChunks int64 = 1000000 // This is the max allowed chunks for LVM
24-
GiB int64 = 1024 * 1024 * 1024
25-
KiB int64 = 1024
18+
cacheSuffix = "csi-fast"
19+
mainLvSuffix = "csi-main"
20+
raidedLocalSsdName = "csi-driver-data-cache"
21+
raidMode = "0"
22+
raidedLssdPrefix = "/dev/md/"
23+
maxAllowedChunks int64 = 1000000 // This is the max allowed chunks for LVM
24+
GiB float64 = 1024 * 1024 * 1024
25+
KiB float64 = 1024
2626
)
2727

28-
var raidedLocalSsdPath = raidedLssdPrefix + raidedLocalSsdName
28+
var (
29+
maxChunkSize float64 = 1 * GiB // Max allowed chunk size as per LVM documentation
30+
minChunkSize float64 = 160 * KiB // This is randomly selected, we need a multiple of 32KiB, the default size would be too small for caching https://man7.org/linux/man-pages/man8/lvcreate.8.html (--chunksize)
31+
raidedLocalSsdPath = raidedLssdPrefix + raidedLocalSsdName
32+
)
2933

3034
func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId string) (string, error) {
3135
volumeId := req.GetVolumeId()
3236
volumeGroupName := getVolumeGroupName(nodeId)
3337
mainDevicePath := "/dev/" + volumeGroupName + "/" + getLvName(mainLvSuffix, volumeId)
3438
mainLvName := getLvName(mainLvSuffix, volumeId)
35-
klog.V(2).Infof("Volume group available on node %v ", volumeGroupName)
39+
klog.V(4).Infof("Volume group available on node %v ", volumeGroupName)
3640

3741
info, err := common.RunCommand("grep", raidedLocalSsdName, "ls", raidedLssdPrefix)
3842
if err != nil {
@@ -72,7 +76,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
7276
infoSlice := strings.Split(strings.TrimSpace(infoString), " ")
7377
vgNameForPv := strings.TrimSpace(infoSlice[(len(infoSlice) - 1)])
7478
if vgNameForPv == volumeGroupName {
75-
klog.V(2).Infof("Physical Volume(PV) already exists in the Volume Group %v", volumeGroupName)
79+
klog.V(4).Infof("Physical Volume(PV) already exists in the Volume Group %v", volumeGroupName)
7680
} else if vgNameForPv != "VG" && vgNameForPv != "" {
7781
info, err = common.RunCommand("" /* pipedCmd */, "" /* pipedCmdArg */, "vgchange", []string{"-an", vgNameForPv}...)
7882
if err != nil {
@@ -143,10 +147,10 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
143147
cacheLvName := getLvName(cacheSuffix, volumeId)
144148
if isCached {
145149
// Validate that cache is setup for required size
146-
klog.V(2).Infof("Assuming valid data cache size and mode, resizing cache is not supported")
150+
klog.V(4).Infof("Assuming valid data cache size and mode, resizing cache is not supported")
147151
} else {
148152
cacheSize := req.GetPublishContext()[common.ContexLocalSsdCacheSize]
149-
chunkSize, err := fetchChunkSize(cacheSize)
153+
chunkSize, err := fetchChunkSizeKiB(cacheSize)
150154
if err != nil {
151155
klog.Errorf("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q", cacheSize, err)
152156
return mainDevicePath, err
@@ -183,7 +187,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
183187
req.GetPublishContext()[common.ContextDataCacheMode],
184188
volumeGroupName + "/" + mainLvName,
185189
"--chunksize",
186-
chunkSize,
190+
chunkSize, // default unit is KiB
187191
"--force",
188192
"-y",
189193
}
@@ -283,7 +287,7 @@ func createVg(volumeGroupName string, devicePath string, raidedLocalSsds string)
283287
if err != nil {
284288
return fmt.Errorf("Volume group creation failed %w: %s", err, info)
285289
}
286-
klog.Infof("Volume group creation succeeded for %v", volumeGroupName)
290+
klog.V(4).Infof("Volume group creation succeeded for %v", volumeGroupName)
287291

288292
args = []string{}
289293
info, err = common.RunCommand("" /* pipedCmd */, "" /* pipedCmdArg */, "vgscan", args...)
@@ -310,9 +314,9 @@ func reduceVolumeGroup(volumeGroupName string, force bool) {
310314
func RaidLocalSsds() error {
311315
isAlreadyRaided, err := isRaided()
312316
if err != nil {
313-
klog.V(2).Infof("Errored while scanning for available LocalSSDs err:%v; continuing Raiding", err)
317+
klog.V(4).Infof("Errored while scanning for available LocalSSDs err:%v; continuing raiding", err)
314318
} else if isAlreadyRaided {
315-
klog.V(2).Infof("Local SSDs are already RAIDed, no further action needed here")
319+
klog.V(4).Infof("Local SSDs are already RAIDed, no further action needed here")
316320
return nil
317321
}
318322
diskList := []string{}
@@ -356,7 +360,7 @@ func RaidLocalSsds() error {
356360
// Validate if Raided successfully
357361
isAlreadyRaided, err = isRaided()
358362
if err != nil {
359-
klog.V(2).Infof("Errored while scanning for available raided LocalSSDs err:%v=", err)
363+
klog.V(4).Infof("Errored while scanning for available raided LocalSSDs err:%v=", err)
360364
}
361365
if !isAlreadyRaided {
362366
return fmt.Errorf("failed raiding, raided device not found on scanning")
@@ -397,17 +401,17 @@ func isCachingSetup(mainLvName string) (error, bool) {
397401
return nil, false
398402
}
399403

400-
func fetchChunkSize(cacheSize string) (string, error) {
404+
func fetchChunkSizeKiB(cacheSize string) (string, error) {
401405
var chunkSize float64
402-
var maxChunkSize int64 = 1 * GiB // Max allowed chunk size as per LVM documentation
403-
var minChunkSize int64 = 320 * KiB // This is randomly selected, we need a multiple of 32KiB, the default size would be too small for caching https://man7.org/linux/man-pages/man8/lvcreate.8.html (--chunksize)
406+
404407
cacheSizeInt, err := common.ConvertGiStringToInt64(cacheSize)
405408
if err != nil {
406409
return "0", err
407410
}
408411
// Chunksize should be divisible by 32Kib so we need (chunksize/32*1024)*32*1024
409-
chunkSize = float64(cacheSizeInt) / float64(maxAllowedChunks)
410-
chunkSize = math.Ceil(chunkSize/float64(32*KiB)) * float64(32*KiB)
411-
chunkSize = math.Min(math.Max(chunkSize, float64(minChunkSize)), float64(maxChunkSize))
412-
return strconv.FormatInt(int64(chunkSize), 10), nil
412+
chunkSize = (float64(cacheSizeInt) * GiB) / float64(maxAllowedChunks)
413+
chunkSize = math.Round(chunkSize/(32*KiB)) * (32 * KiB)
414+
chunkSize = math.Min(math.Max(chunkSize, minChunkSize), maxChunkSize) / KiB
415+
// default chunk size unit KiB
416+
return strconv.FormatInt(int64(chunkSize), 10) + "KiB", nil
413417
}

pkg/gce-pd-csi-driver/cache_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package gceGCEDriver
2+
3+
import (
4+
"testing"
5+
6+
"google.golang.org/grpc/codes"
7+
)
8+
9+
func TestFetchChunkSizeKiB(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
cacheSize string
13+
expChunkSize string
14+
expErrCode codes.Code
15+
}{
16+
{
17+
name: "chunk size is in the allowed range",
18+
cacheSize: "500Gi",
19+
expChunkSize: "512KiB", //range defined in fetchChunkSizeKiB
20+
},
21+
{
22+
name: "chunk size is set to the range ceil",
23+
cacheSize: "30000000Gi",
24+
expChunkSize: "1048576KiB", //range defined in fetchChunkSizeKiB - max 1GiB
25+
},
26+
{
27+
name: "chunk size is set to the allowed range floor",
28+
cacheSize: "10Gi",
29+
expChunkSize: "160KiB", //range defined in fetchChunkSizeKiB - min 160 KiB
30+
},
31+
{
32+
name: "cacheSize set to KiB also sets the chunk size to range floor",
33+
cacheSize: "100Ki",
34+
expChunkSize: "160KiB", //range defined in fetchChunkSizeKiB - min 160 KiB
35+
},
36+
// cacheSize is validated in storage class parameter so assuming invalid cacheSize (like negative, 0) would not be passed to the function
37+
}
38+
39+
for _, tc := range testCases {
40+
chunkSize, err := fetchChunkSizeKiB(tc.cacheSize)
41+
if err != nil {
42+
t.Errorf("Errored %s", err)
43+
}
44+
if chunkSize != tc.expChunkSize {
45+
t.Errorf("Got %s want %s", chunkSize, tc.expChunkSize)
46+
}
47+
48+
}
49+
50+
}

0 commit comments

Comments
 (0)