Skip to content

Commit 5ec6f7f

Browse files
committed
Add unit tests
1 parent 81510ca commit 5ec6f7f

File tree

2 files changed

+79
-17
lines changed

2 files changed

+79
-17
lines changed

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

+22-17
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ import (
1717
)
1818

1919
const (
20-
cacheSuffix = "csi-fast"
21-
mainLvSuffix = "csi-main"
22-
raidedLocalSsdName = "csi-driver-data-cache"
23-
raidMode = "0"
24-
maxAllowedChunks int64 = 1000000 // This is the max allowed chunks for LVM
25-
GiB int64 = 1024 * 1024 * 1024
26-
KiB int64 = 1024
20+
cacheSuffix = "csi-fast"
21+
mainLvSuffix = "csi-main"
22+
raidedLocalSsdName = "csi-driver-data-cache"
23+
raidMode = "0"
24+
maxAllowedChunks int64 = 1000000 // This is the max allowed chunks for LVM
25+
GiB float64 = 1024 * 1024 * 1024
26+
KiB float64 = 1024
27+
)
28+
29+
var (
30+
maxChunkSize float64 = 1 * GiB // Max allowed chunk size as per LVM documentation
31+
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)
2732
)
2833

2934
func fetchRAIDedLocalSsdPath() (string, error) {
@@ -88,7 +93,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
8893
vgNameForPv := strings.TrimSpace(infoSlice[(len(infoSlice) - 1)])
8994
klog.V(4).Infof("Physical volume is part of Volume group: %v", vgNameForPv)
9095
if vgNameForPv == volumeGroupName {
91-
klog.V(4).Infof("Physical Volume(PV) already exists in the Volume Group")
96+
klog.V(4).Infof("Physical Volume(PV) already exists in the Volume Group %v", volumeGroupName)
9297
} else if vgNameForPv != "VG" && vgNameForPv != "" {
9398

9499
info, err = common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgchange", []string{"-an", vgNameForPv}...)
@@ -164,7 +169,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
164169
klog.V(4).Infof("Assuming valid data cache size and mode, resizing cache is not supported")
165170
} else {
166171
cacheSize := req.GetPublishContext()[common.ContextDataCacheSize]
167-
chunkSize, err := fetchChunkSize(cacheSize)
172+
chunkSize, err := fetchChunkSizeKiB(cacheSize)
168173
if err != nil {
169174
klog.Errorf("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q", cacheSize, err)
170175
return mainDevicePath, err
@@ -201,7 +206,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
201206
req.GetPublishContext()[common.ContextDataCacheMode],
202207
volumeGroupName + "/" + mainLvName,
203208
"--chunksize",
204-
chunkSize,
209+
chunkSize, // default unit is KiB
205210
"--force",
206211
"-y",
207212
}
@@ -528,17 +533,17 @@ func isCachingSetup(mainLvName string) (error, bool) {
528533
return nil, false
529534
}
530535

531-
func fetchChunkSize(cacheSize string) (string, error) {
536+
func fetchChunkSizeKiB(cacheSize string) (string, error) {
532537
var chunkSize float64
533-
var maxChunkSize int64 = 1 * GiB // Max allowed chunk size as per LVM documentation
534-
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)
538+
535539
cacheSizeInt, err := common.ConvertGiStringToInt64(cacheSize)
536540
if err != nil {
537541
return "0", err
538542
}
539543
// Chunksize should be divisible by 32Kib so we need (chunksize/32*1024)*32*1024
540-
chunkSize = float64(cacheSizeInt) / float64(maxAllowedChunks)
541-
chunkSize = math.Ceil(chunkSize/float64(32*KiB)) * float64(32*KiB)
542-
chunkSize = math.Min(math.Max(chunkSize, float64(minChunkSize)), float64(maxChunkSize))
543-
return strconv.FormatInt(int64(chunkSize), 10), nil
544+
chunkSize = (float64(cacheSizeInt) * GiB) / float64(maxAllowedChunks)
545+
chunkSize = math.Round(chunkSize/(32*KiB)) * (32 * KiB)
546+
chunkSize = math.Min(math.Max(chunkSize, minChunkSize), maxChunkSize) / KiB
547+
// default chunk size unit KiB
548+
return strconv.FormatInt(int64(chunkSize), 10) + "KiB", nil
544549
}

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

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

0 commit comments

Comments
 (0)