Skip to content

Commit 80c800d

Browse files
committed
fix NodeStageVolume tests
Unit tests were initiating test driver without a custom mounter which results in using FakeExec with DisableScripts set to true. This approach does not allow for any control over fake command return values. This causes the resize flow of mount-utils code to fail while parsing the fake command output. We can use custom mounter with predefined action list instead to have full control of the fake return values.
1 parent febd972 commit 80c800d

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

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

+67-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ package gceGCEDriver
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"io/ioutil"
22+
"k8s.io/utils/exec"
23+
testingexec "k8s.io/utils/exec/testing"
2124
"os"
2225
"path/filepath"
2326
"testing"
@@ -63,6 +66,15 @@ func getTestBlockingGCEDriver(t *testing.T, readyToExecute chan chan struct{}) *
6366
return gceDriver
6467
}
6568

69+
func makeFakeCmd(fakeCmd *testingexec.FakeCmd, cmd string, args ...string) testingexec.FakeCommandAction {
70+
c := cmd
71+
a := args
72+
return func(cmd string, args ...string) exec.Cmd {
73+
command := testingexec.InitFakeCmd(fakeCmd, c, a...)
74+
return command
75+
}
76+
}
77+
6678
func TestNodeGetVolumeStats(t *testing.T) {
6779
gceDriver := getTestGCEDriver(t)
6880
ns := gceDriver.ns
@@ -351,8 +363,6 @@ func TestNodeUnpublishVolume(t *testing.T) {
351363
}
352364

353365
func TestNodeStageVolume(t *testing.T) {
354-
gceDriver := getTestGCEDriver(t)
355-
ns := gceDriver.ns
356366
volumeID := "project/test001/zones/c1/disks/testDisk"
357367
blockCap := &csi.VolumeCapability_Block{
358368
Block: &csi.VolumeCapability_BlockVolume{},
@@ -436,6 +446,61 @@ func TestNodeStageVolume(t *testing.T) {
436446
}
437447
for _, tc := range testCases {
438448
t.Logf("Test case: %s", tc.name)
449+
actionList := []testingexec.FakeCommandAction{
450+
makeFakeCmd(
451+
&testingexec.FakeCmd{
452+
CombinedOutputScript: []testingexec.FakeAction{
453+
func() ([]byte, []byte, error) {
454+
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
455+
},
456+
},
457+
},
458+
"blkid",
459+
),
460+
makeFakeCmd(
461+
&testingexec.FakeCmd{
462+
CombinedOutputScript: []testingexec.FakeAction{
463+
func() ([]byte, []byte, error) {
464+
return []byte("1"), nil, nil
465+
},
466+
},
467+
},
468+
"blockdev",
469+
),
470+
makeFakeCmd(
471+
&testingexec.FakeCmd{
472+
CombinedOutputScript: []testingexec.FakeAction{
473+
func() ([]byte, []byte, error) {
474+
return []byte("1"), nil, nil
475+
},
476+
},
477+
},
478+
"blockdev",
479+
),
480+
makeFakeCmd(
481+
&testingexec.FakeCmd{
482+
CombinedOutputScript: []testingexec.FakeAction{
483+
func() ([]byte, []byte, error) {
484+
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
485+
},
486+
},
487+
},
488+
"blkid",
489+
),
490+
makeFakeCmd(
491+
&testingexec.FakeCmd{
492+
CombinedOutputScript: []testingexec.FakeAction{
493+
func() ([]byte, []byte, error) {
494+
return []byte(fmt.Sprintf("block size: 1\nblock count: 1")), nil, nil
495+
},
496+
},
497+
},
498+
"dumpe2fs",
499+
),
500+
}
501+
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList})
502+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
503+
ns := gceDriver.ns
439504
_, err := ns.NodeStageVolume(context.Background(), tc.req)
440505
if err != nil {
441506
serverError, ok := status.FromError(err)

0 commit comments

Comments
 (0)