Skip to content

Commit e829e37

Browse files
julianKatzcemakd
authored andcommitted
Made test device caches in node_test.go
1 parent cff9c64 commit e829e37

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
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/mount-utils"
3636
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
3737
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
38+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/linkcache"
3839
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
3940
)
4041

@@ -45,11 +46,13 @@ const (
4546
)
4647

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

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

5558
func getCustomTestGCEDriver(t *testing.T, mounter *mount.SafeFormatAndMount, deviceUtils deviceutils.DeviceUtils, metaService metadataservice.MetadataService, args *NodeServerArgs) *GCEDriver {
@@ -189,7 +192,9 @@ func TestNodeGetVolumeStats(t *testing.T) {
189192
}
190193

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

195200
req := &csi.NodeGetVolumeStatsRequest{
@@ -1246,7 +1251,9 @@ func TestNodeStageVolume(t *testing.T) {
12461251
))
12471252
}
12481253
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList, ExactOrder: true})
1249-
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
1254+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter, &NodeServerArgs{
1255+
DeviceCache: linkcache.TestDeviceCache(1*time.Minute, linkcache.TestNodeWithVolumes([]string{volumeID})),
1256+
})
12501257
ns := gceDriver.ns
12511258
ns.SysfsPath = tempDir + "/sys"
12521259
_, 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)