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 Aug 20, 2024
1 parent 9eaf180 commit a69f333
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,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(), ProtobufMatcher{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 +180,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(), ProtobufMatcher{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 +351,7 @@ func TestGetDriverName(t *testing.T) {
}

// Setup expectation
identityServer.EXPECT().GetPluginInfo(gomock.Any(), in).Return(out, injectedErr).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), ProtobufMatcher{in}).Return(out, injectedErr).Times(1)

name, err := GetDriverName(csiConn.conn, timeout)
if test.expectError && err == nil {
Expand Down Expand Up @@ -450,9 +450,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(), ProtobufMatcher{&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 @@ -4558,9 +4558,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(), ProtobufMatcher{&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 +6659,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(), ProtobufMatcher{&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 +6838,14 @@ func TestProvisionWithMigration(t *testing.T) {
expectParams[translatedKey] = "foo"
}
controllerServer.EXPECT().CreateVolume(gomock.Any(),
&csi.CreateVolumeRequest{
ProtobufMatcher{&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 +6997,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{
ProtobufMatcher{&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 Expand Up @@ -7046,3 +7046,26 @@ func createFakeCSIPV(volumeHandle string) *v1.PersistentVolume {
},
}
}

type ProtobufMatcher struct {
// Protobuf messages implement String() function that renders the message as "protobuf style string".
// `name:"test-testi" capacity_range:{required_bytes:100000} parameters:{key:"fstype" value:"ext3"} parameters:{key:"translated" value:"foo"}`
msg fmt.Stringer
}

func (p ProtobufMatcher) Matches(x interface{}) bool {
otherMsg, ok := x.(fmt.Stringer)
if !ok {
return false
}
// Using explicit variables to put a breakpoint here and see them easily in a debugger
myText := p.msg.String()
otherText := otherMsg.String()
return myText == otherText
}

func (p ProtobufMatcher) String() string {
// This is used in error prints when two messages don't match.
// Return the same text Matches() used for comparison.
return p.msg.String()
}

0 comments on commit a69f333

Please sign in to comment.