rbd: add unit test for checkHealthyPrimary

Removed the code in checkHealthyPrimary which
makes the ceph call, passing it as input now.
Added unit test for checkHealthyPrimary function

Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
(cherry picked from commit 8a47904e8f)
This commit is contained in:
Madhu Rajanna 2022-06-28 13:52:05 +05:30 committed by mergify[bot]
parent 0e67c8da24
commit 08de15cebe
2 changed files with 79 additions and 6 deletions

View File

@ -580,7 +580,12 @@ func (rs *ReplicationServer) PromoteVolume(ctx context.Context,
} }
} }
err = checkHealthyPrimary(ctx, rbdVol) mirrorStatus, err := rbdVol.getImageMirroringStatus()
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
err = checkHealthyPrimary(ctx, rbdVol, mirrorStatus)
if err != nil { if err != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Error(codes.Internal, err.Error())
} }
@ -617,11 +622,7 @@ func (rs *ReplicationServer) PromoteVolume(ctx context.Context,
// checkHealthyPrimary checks if the image is a healhty primary or not. // checkHealthyPrimary checks if the image is a healhty primary or not.
// healthy primary image will be in up+stopped state, for states other // healthy primary image will be in up+stopped state, for states other
// than this it returns an error message. // than this it returns an error message.
func checkHealthyPrimary(ctx context.Context, rbdVol *rbdVolume) error { func checkHealthyPrimary(ctx context.Context, rbdVol *rbdVolume, mirrorStatus *librbd.GlobalMirrorImageStatus) error {
mirrorStatus, err := rbdVol.getImageMirroringStatus()
if err != nil {
return err
}
localStatus, err := mirrorStatus.LocalStatus() localStatus, err := mirrorStatus.LocalStatus()
if err != nil { if err != nil {
// LocalStatus can fail if the local site status is not found in // LocalStatus can fail if the local site status is not found in

View File

@ -279,3 +279,75 @@ func TestCheckVolumeResyncStatus(t *testing.T) {
}) })
} }
} }
func Test_checkHealthyPrimary(t *testing.T) {
t.Parallel()
rbdVol := &rbdVolume{
rbdImage: rbdImage{
RbdImageName: "test",
Pool: "test-pool",
},
}
tests := []struct {
name string
ctx context.Context
rbdVol *rbdVolume
mirrorStatus *librbd.GlobalMirrorImageStatus
wantErr bool
}{
{
name: "when image is in up+stopped state",
ctx: context.TODO(),
rbdVol: rbdVol,
mirrorStatus: &librbd.GlobalMirrorImageStatus{
SiteStatuses: []librbd.SiteMirrorImageStatus{
{
MirrorUUID: "",
State: librbd.MirrorImageStatusStateStopped,
Up: true,
},
},
},
wantErr: false,
},
{
name: "when image is in up+error state",
ctx: context.TODO(),
rbdVol: rbdVol,
mirrorStatus: &librbd.GlobalMirrorImageStatus{
SiteStatuses: []librbd.SiteMirrorImageStatus{
{
MirrorUUID: "",
State: librbd.MirrorImageStatusStateError,
Up: false,
},
},
},
wantErr: true,
},
{
name: "when image is in up+replaying state",
ctx: context.TODO(),
rbdVol: rbdVol,
mirrorStatus: &librbd.GlobalMirrorImageStatus{
SiteStatuses: []librbd.SiteMirrorImageStatus{
{
MirrorUUID: "",
State: librbd.MirrorImageStatusStateReplaying,
Up: true,
},
},
},
wantErr: true,
},
}
for _, tt := range tests {
ts := tt
t.Run(ts.name, func(t *testing.T) {
t.Parallel()
if err := checkHealthyPrimary(ts.ctx, ts.rbdVol, ts.mirrorStatus); (err != nil) != ts.wantErr {
t.Errorf("checkHealthyPrimary() error = %v, wantErr %v", err, ts.wantErr)
}
})
}
}