Skip to content

Commit

Permalink
Clean up the matcher
Browse files Browse the repository at this point in the history
There should be a function that returns the matcher.
  • Loading branch information
jsafrane committed Aug 22, 2024
1 parent 398f2c2 commit fe0cecf
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func TestGetPluginName(t *testing.T) {
in := &csi.GetPluginInfoRequest{}
out := test.output[0]

identityServer.EXPECT().GetPluginInfo(gomock.Any(), ProtobufMatcher{in}).Return(out, nil).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), 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 @@ -182,7 +182,7 @@ func TestGetPluginName(t *testing.T) {
}

out = test.output[1]
identityServer.EXPECT().GetPluginInfo(gomock.Any(), ProtobufMatcher{in}).Return(out, nil).Times(1)
identityServer.EXPECT().GetPluginInfo(gomock.Any(), 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 @@ -353,7 +353,7 @@ func TestGetDriverName(t *testing.T) {
}

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

name, err := GetDriverName(csiConn.conn, timeout)
if test.expectError && err == nil {
Expand Down Expand Up @@ -452,9 +452,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(), ProtobufMatcher{&csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), 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 @@ -4560,9 +4560,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(), ProtobufMatcher{&csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), 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 @@ -6661,9 +6661,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(), ProtobufMatcher{&csi.DeleteVolumeRequest{
controllerServer.EXPECT().DeleteVolume(gomock.Any(), 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 @@ -6840,14 +6840,14 @@ func TestProvisionWithMigration(t *testing.T) {
expectParams[translatedKey] = "foo"
}
controllerServer.EXPECT().CreateVolume(gomock.Any(),
ProtobufMatcher{&csi.CreateVolumeRequest{
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 @@ -6999,9 +6999,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(),
ProtobufMatcher{&csi.DeleteVolumeRequest{
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 Expand Up @@ -7049,21 +7049,25 @@ func createFakeCSIPV(volumeHandle string) *v1.PersistentVolume {
}
}

func Protobuf(msg proto.Message) gomock.Matcher {
return &ProtobufMatcher{msg}
}

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 proto.Message
}

func (p ProtobufMatcher) Matches(x interface{}) bool {
var _ gomock.Matcher = &ProtobufMatcher{}

func (p *ProtobufMatcher) Matches(x interface{}) bool {
otherMsg, ok := x.(proto.Message)
if !ok {
return false
}
return proto.Equal(p.msg, otherMsg)
}

func (p ProtobufMatcher) String() string {
func (p *ProtobufMatcher) String() string {
return prototext.Format(p.msg)

}

0 comments on commit fe0cecf

Please sign in to comment.