Skip to content

Commit

Permalink
Update unit tests to new gRPC
Browse files Browse the repository at this point in the history
gomock cannot compare protobuf messages due to mismatches in private
fields. Use a custom matcher for protobuf messages.
  • Loading branch information
jsafrane committed Sep 19, 2024
1 parent 1bc306d commit 1416604
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/v5/utils"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestGetPluginName(t *testing.T) {
in := &csi.GetPluginInfoRequest{}
out := test.output[0]

identityServer.EXPECT().GetPluginInfo(gomock.Any(), in).Return(out, nil).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), utils.Protobuf(in)).Return(out, nil).Times(1)
oldName, err := GetDriverName(csiConn.conn, timeout)
if err != nil {
t.Errorf("test %q: Failed to get driver's name", test.name)
Expand All @@ -180,7 +181,7 @@ func TestGetPluginName(t *testing.T) {
}

out = test.output[1]
identityServer.EXPECT().GetPluginInfo(gomock.Any(), in).Return(out, nil).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), utils.Protobuf(in)).Return(out, nil).Times(1)
newName, err := GetDriverName(csiConn.conn, timeout)
if err != nil {
t.Errorf("test %s: Failed to get driver's name", test.name)
Expand Down Expand Up @@ -351,7 +352,7 @@ func TestGetDriverName(t *testing.T) {
}

// Setup expectation
identityServer.EXPECT().GetPluginInfo(gomock.Any(), in).Return(out, injectedErr).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), utils.Protobuf(in)).Return(out, injectedErr).Times(1)

name, err := GetDriverName(csiConn.conn, timeout)
if test.expectError && err == nil {
Expand Down Expand Up @@ -450,9 +451,9 @@ func TestCreateDriverReturnsInvalidCapacityDuringProvision(t *testing.T) {
// Set up Mocks
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, nil).Times(1)
// Since capacity returned by driver is invalid, we expect the provision call to clean up the volume
controllerServer.EXPECT().DeleteVolume(gomock.Any(), &csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), utils.Protobuf(&csi.DeleteVolumeRequest{
VolumeId: "test-volume-id",
}).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
})).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)

// Call provision
_, _, err = csiProvisioner.Provision(context.Background(), opts)
Expand Down Expand Up @@ -2279,7 +2280,7 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) {
},
expectErr: true,
expectState: controller.ProvisioningNoChange,
expectNoProvision: true, // not owner yet
expectNoProvision: true, // notowner yet
expectSelectedNode: nodeFoo.Name, // changed by ShouldProvision
},
"distributed immediate, allowed topologies not okay": {
Expand Down Expand Up @@ -4558,9 +4559,9 @@ func TestProvisionFromSnapshot(t *testing.T) {
if tc.notPopulated {
out.Volume.ContentSource = nil
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, nil).Times(1)
controllerServer.EXPECT().DeleteVolume(gomock.Any(), &csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), utils.Protobuf(&csi.DeleteVolumeRequest{
VolumeId: "test-volume-id",
}).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
})).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
} else {
snapshotSource := csi.VolumeContentSource_Snapshot{
Snapshot: &csi.VolumeContentSource_SnapshotSource{
Expand Down Expand Up @@ -6659,9 +6660,9 @@ func TestProvisionFromPVC(t *testing.T) {
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, nil).Times(1)
// if the volume created is less than the requested size,
// deletevolume will be called
controllerServer.EXPECT().DeleteVolume(gomock.Any(), &csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), utils.Protobuf(&csi.DeleteVolumeRequest{
VolumeId: "test-volume-id",
}).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
})).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
}

_, _, _, claimLister, _, _ := listers(clientSet)
Expand Down Expand Up @@ -6838,14 +6839,14 @@ func TestProvisionWithMigration(t *testing.T) {
expectParams[translatedKey] = "foo"
}
controllerServer.EXPECT().CreateVolume(gomock.Any(),
&csi.CreateVolumeRequest{
utils.Protobuf(&csi.CreateVolumeRequest{
Name: "test-testi",
Parameters: expectParams,
VolumeCapabilities: nil,
CapacityRange: &csi.CapacityRange{
RequiredBytes: int64(requestBytes),
},
}).Return(
})).Return(
&csi.CreateVolumeResponse{
Volume: &csi.Volume{
CapacityBytes: requestBytes,
Expand Down Expand Up @@ -6997,9 +6998,9 @@ func TestDeleteMigration(t *testing.T) {
// We assert that the Delete is called on the driver with either the
// normal or the translated handle
controllerServer.EXPECT().DeleteVolume(gomock.Any(),
&csi.DeleteVolumeRequest{
utils.Protobuf(&csi.DeleteVolumeRequest{
VolumeId: volID,
}).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)
})).Return(&csi.DeleteVolumeResponse{}, nil).Times(1)

// Run Delete
err = csiProvisioner.Delete(context.Background(), tc.pv)
Expand Down

0 comments on commit 1416604

Please sign in to comment.