Skip to content

Commit 8ffb6e1

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 6ad0610 commit 8ffb6e1

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

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

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ package gceGCEDriver
1515

1616
import (
1717
"context"
18+
"fmt"
1819
"io/ioutil"
20+
"k8s.io/utils/exec"
21+
testingexec "k8s.io/utils/exec/testing"
1922
"os"
2023
"path/filepath"
2124
"testing"
@@ -61,6 +64,15 @@ func getTestBlockingGCEDriver(t *testing.T, readyToExecute chan chan struct{}) *
6164
return gceDriver
6265
}
6366

67+
func makeFakeCmd(fakeCmd *testingexec.FakeCmd, cmd string, args ...string) testingexec.FakeCommandAction {
68+
c := cmd
69+
a := args
70+
return func(cmd string, args ...string) exec.Cmd {
71+
command := testingexec.InitFakeCmd(fakeCmd, c, a...)
72+
return command
73+
}
74+
}
75+
6476
func TestNodeGetVolumeStats(t *testing.T) {
6577
gceDriver := getTestGCEDriver(t)
6678
ns := gceDriver.ns
@@ -349,8 +361,6 @@ func TestNodeUnpublishVolume(t *testing.T) {
349361
}
350362

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

0 commit comments

Comments
 (0)