Skip to content

Commit edb62c9

Browse files
committed
Made test device caches in node_test.go
1 parent e58b7e5 commit edb62c9

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/mount-utils"
3535
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
3636
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
37+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/linkcache"
3738
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
3839
)
3940

@@ -44,11 +45,13 @@ const (
4445
)
4546

4647
func getTestGCEDriver(t *testing.T) *GCEDriver {
47-
return getCustomTestGCEDriver(t, mountmanager.NewFakeSafeMounter(), deviceutils.NewFakeDeviceUtils(false), metadataservice.NewFakeService(), &NodeServerArgs{})
48+
return getCustomTestGCEDriver(t, mountmanager.NewFakeSafeMounter(), deviceutils.NewFakeDeviceUtils(false), metadataservice.NewFakeService(), &NodeServerArgs{
49+
DeviceCache: linkcache.TestDeviceCache(1*time.Minute, linkcache.TestNodeWithVolumes([]string{defaultVolumeID})),
50+
})
4851
}
4952

50-
func getTestGCEDriverWithCustomMounter(t *testing.T, mounter *mount.SafeFormatAndMount) *GCEDriver {
51-
return getCustomTestGCEDriver(t, mounter, deviceutils.NewFakeDeviceUtils(false), metadataservice.NewFakeService(), &NodeServerArgs{})
53+
func getTestGCEDriverWithCustomMounter(t *testing.T, mounter *mount.SafeFormatAndMount, args *NodeServerArgs) *GCEDriver {
54+
return getCustomTestGCEDriver(t, mounter, deviceutils.NewFakeDeviceUtils(false), metadataservice.NewFakeService(), args)
5255
}
5356

5457
func getCustomTestGCEDriver(t *testing.T, mounter *mount.SafeFormatAndMount, deviceUtils deviceutils.DeviceUtils, metaService metadataservice.MetadataService, args *NodeServerArgs) *GCEDriver {
@@ -188,7 +191,9 @@ func TestNodeGetVolumeStats(t *testing.T) {
188191
}
189192

190193
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList})
191-
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
194+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter, &NodeServerArgs{
195+
DeviceCache: linkcache.TestDeviceCache(1*time.Minute, linkcache.TestNodeWithVolumes([]string{tc.volumeID})),
196+
})
192197
ns := gceDriver.ns
193198

194199
req := &csi.NodeGetVolumeStatsRequest{
@@ -1142,7 +1147,9 @@ func TestNodeStageVolume(t *testing.T) {
11421147
))
11431148
}
11441149
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList, ExactOrder: true})
1145-
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
1150+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter, &NodeServerArgs{
1151+
DeviceCache: linkcache.TestDeviceCache(1*time.Minute, linkcache.TestNodeWithVolumes([]string{volumeID})),
1152+
})
11461153
ns := gceDriver.ns
11471154
ns.SysfsPath = tempDir + "/sys"
11481155
_, err := ns.NodeStageVolume(context.Background(), tc.req)

pkg/linkcache/devices_linux.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,27 @@ func NewDeviceCacheForNode(ctx context.Context, period time.Duration, nodeName s
2121
return nil, fmt.Errorf("failed to get node %s: %w", nodeName, err)
2222
}
2323

24-
return newDeviceCacheForNode(ctx, period, node)
24+
return newDeviceCacheForNode(period, node), nil
2525
}
2626

27-
func newDeviceCacheForNode(ctx context.Context, period time.Duration, node *v1.Node) (*DeviceCache, error) {
27+
func TestDeviceCache(period time.Duration, node *v1.Node) *DeviceCache {
28+
return newDeviceCacheForNode(period, node)
29+
}
30+
31+
func TestNodeWithVolumes(volumes []string) *v1.Node {
32+
volumesInUse := make([]v1.UniqueVolumeName, len(volumes))
33+
for i, volume := range volumes {
34+
volumesInUse[i] = v1.UniqueVolumeName("kubernetes.io/csi/pd.csi.storage.gke.io^" + volume)
35+
}
36+
37+
return &v1.Node{
38+
Status: v1.NodeStatus{
39+
VolumesInUse: volumesInUse,
40+
},
41+
}
42+
}
43+
44+
func newDeviceCacheForNode(period time.Duration, node *v1.Node) *DeviceCache {
2845
deviceCache := &DeviceCache{
2946
volumes: make(map[string]deviceMapping),
3047
period: period,
@@ -35,11 +52,23 @@ func newDeviceCacheForNode(ctx context.Context, period time.Duration, node *v1.N
3552
// of the string (after the last "/") and call AddVolume for that
3653
for _, volume := range node.Status.VolumesInUse {
3754
klog.Infof("Adding volume %s to cache", string(volume))
38-
volumeID := strings.Split(string(volume), "^")[1]
39-
deviceCache.AddVolume(volumeID)
55+
vID, err := pvNameFromVolumeID(string(volume))
56+
if err != nil {
57+
klog.Warningf("failure to retrieve name, skipping volume %q: %v", string(volume), err)
58+
continue
59+
}
60+
deviceCache.AddVolume(vID)
4061
}
4162

42-
return deviceCache, nil
63+
return deviceCache
64+
}
65+
66+
func pvNameFromVolumeID(volumeID string) (string, error) {
67+
tokens := strings.Split(volumeID, "^")
68+
if len(tokens) != 2 {
69+
return "", fmt.Errorf("invalid volume ID, split on `^` returns %d tokens, expected 2", len(tokens))
70+
}
71+
return tokens[1], nil
4372
}
4473

4574
// Run since it needs an infinite loop to keep itself up to date

0 commit comments

Comments
 (0)