Skip to content

Commit

Permalink
fix NodeStageVolume tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RomanBednar committed May 2, 2022
1 parent 082db37 commit c244d1f
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions pkg/gce-pd-csi-driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ package gceGCEDriver

import (
"context"
"fmt"
"io/ioutil"
"k8s.io/utils/exec"
testingexec "k8s.io/utils/exec/testing"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -61,6 +64,15 @@ func getTestBlockingGCEDriver(t *testing.T, readyToExecute chan chan struct{}) *
return gceDriver
}

func makeFakeCmd(fakeCmd *testingexec.FakeCmd, cmd string, args ...string) testingexec.FakeCommandAction {
c := cmd
a := args
return func(cmd string, args ...string) exec.Cmd {
command := testingexec.InitFakeCmd(fakeCmd, c, a...)
return command
}
}

func TestNodeGetVolumeStats(t *testing.T) {
gceDriver := getTestGCEDriver(t)
ns := gceDriver.ns
Expand Down Expand Up @@ -349,8 +361,6 @@ func TestNodeUnpublishVolume(t *testing.T) {
}

func TestNodeStageVolume(t *testing.T) {
gceDriver := getTestGCEDriver(t)
ns := gceDriver.ns
volumeID := "project/test001/zones/c1/disks/testDisk"
blockCap := &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
Expand Down Expand Up @@ -434,6 +444,61 @@ func TestNodeStageVolume(t *testing.T) {
}
for _, tc := range testCases {
t.Logf("Test case: %s", tc.name)
actionList := []testingexec.FakeCommandAction{
makeFakeCmd(
&testingexec.FakeCmd{
CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
},
},
},
"blkid",
),
makeFakeCmd(
&testingexec.FakeCmd{
CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte("1"), nil, nil
},
},
},
"blockdev",
),
makeFakeCmd(
&testingexec.FakeCmd{
CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte("1"), nil, nil
},
},
},
"blockdev",
),
makeFakeCmd(
&testingexec.FakeCmd{
CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte(fmt.Sprintf("DEVNAME=/dev/sdb\nTYPE=ext4")), nil, nil
},
},
},
"blkid",
),
makeFakeCmd(
&testingexec.FakeCmd{
CombinedOutputScript: []testingexec.FakeAction{
func() ([]byte, []byte, error) {
return []byte(fmt.Sprintf("block size: 1\nblock count: 1")), nil, nil
},
},
},
"dumpe2fs",
),
}
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(&testingexec.FakeExec{CommandScript: actionList})
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
ns := gceDriver.ns
_, err := ns.NodeStageVolume(context.Background(), tc.req)
if err != nil {
serverError, ok := status.FromError(err)
Expand Down

0 comments on commit c244d1f

Please sign in to comment.