2021-06-28 11:38:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
2021-11-26 06:38:21 +00:00
|
|
|
"context"
|
2023-06-05 16:18:29 +00:00
|
|
|
"errors"
|
2021-06-28 11:38:42 +00:00
|
|
|
"reflect"
|
2023-06-09 10:42:50 +00:00
|
|
|
"strconv"
|
2022-09-09 11:26:41 +00:00
|
|
|
"strings"
|
2021-06-28 11:38:42 +00:00
|
|
|
"testing"
|
2022-09-09 11:26:41 +00:00
|
|
|
"time"
|
2021-06-28 11:38:42 +00:00
|
|
|
|
2023-01-12 09:52:51 +00:00
|
|
|
corerbd "github.com/ceph/ceph-csi/internal/rbd"
|
|
|
|
|
2021-10-25 10:24:12 +00:00
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
2021-06-28 11:38:42 +00:00
|
|
|
"github.com/ceph/go-ceph/rbd/admin"
|
2023-06-09 10:42:50 +00:00
|
|
|
"github.com/csi-addons/spec/lib/go/replication"
|
2023-06-05 16:18:29 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-06-09 10:42:50 +00:00
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2022-09-09 11:26:41 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-06-28 11:38:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateSchedulingInterval(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
interval string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid interval in minutes",
|
|
|
|
"3m",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid interval in hour",
|
|
|
|
"22h",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid interval in days",
|
|
|
|
"13d",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid interval without number",
|
|
|
|
"d",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"invalid interval without (m|h|d) suffix",
|
|
|
|
"12",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2021-07-11 07:27:41 +00:00
|
|
|
tt := tt
|
2021-06-28 11:38:42 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-07-11 07:27:41 +00:00
|
|
|
t.Parallel()
|
2021-11-15 06:00:13 +00:00
|
|
|
err := validateSchedulingInterval(tt.interval)
|
2021-07-11 07:27:41 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("validateSchedulingInterval() error = %v, wantErr %v", err, tt.wantErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-28 11:38:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 06:00:13 +00:00
|
|
|
func TestValidateSchedulingDetails(t *testing.T) {
|
2021-07-11 07:27:41 +00:00
|
|
|
t.Parallel()
|
2021-11-26 06:38:21 +00:00
|
|
|
ctx := context.TODO()
|
2021-06-28 11:38:42 +00:00
|
|
|
tests := []struct {
|
2021-11-15 06:00:13 +00:00
|
|
|
name string
|
|
|
|
parameters map[string]string
|
|
|
|
wantErr bool
|
2021-06-28 11:38:42 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid parameters",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
schedulingStartTimeKey: "14:00:00-05:00",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid parameters when optional startTime is missing",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"when mirroring mode is journal",
|
|
|
|
map[string]string{
|
2021-11-26 06:38:21 +00:00
|
|
|
imageMirroringKey: string(imageMirrorModeJournal),
|
2021-06-28 11:38:42 +00:00
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
},
|
2021-11-26 06:38:21 +00:00
|
|
|
false,
|
2021-06-28 11:38:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"when startTime is specified without interval",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
schedulingStartTimeKey: "14:00:00-05:00",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"when no scheduling is specified",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2021-08-09 04:48:32 +00:00
|
|
|
{
|
|
|
|
"when no parameters and scheduling details are specified",
|
|
|
|
map[string]string{},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"when no mirroring mode is specified",
|
|
|
|
map[string]string{
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
schedulingStartTimeKey: "14:00:00-05:00",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2021-07-11 07:27:41 +00:00
|
|
|
tt := tt
|
2021-06-28 11:38:42 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-07-11 07:27:41 +00:00
|
|
|
t.Parallel()
|
2021-11-26 06:38:21 +00:00
|
|
|
err := validateSchedulingDetails(ctx, tt.parameters)
|
2021-07-11 07:27:41 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("getSchedulingDetails() error = %v, wantErr %v", err, tt.wantErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-28 11:38:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-15 06:00:13 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSchedulingDetails(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
parameters map[string]string
|
|
|
|
wantInterval admin.Interval
|
|
|
|
wantStartTime admin.StartTime
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid parameters",
|
|
|
|
map[string]string{
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
schedulingStartTimeKey: "14:00:00-05:00",
|
|
|
|
},
|
|
|
|
admin.Interval("1h"),
|
|
|
|
admin.StartTime("14:00:00-05:00"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"valid parameters when optional startTime is missing",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
},
|
|
|
|
admin.Interval("1h"),
|
|
|
|
admin.NoStartTime,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"when startTime is specified without interval",
|
|
|
|
map[string]string{
|
|
|
|
imageMirroringKey: string(imageMirrorModeSnapshot),
|
|
|
|
schedulingStartTimeKey: "14:00:00-05:00",
|
|
|
|
},
|
|
|
|
admin.NoInterval,
|
|
|
|
admin.StartTime("14:00:00-05:00"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"when no parameters and scheduling details are specified",
|
|
|
|
map[string]string{},
|
|
|
|
admin.NoInterval,
|
|
|
|
admin.NoStartTime,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
interval, startTime := getSchedulingDetails(tt.parameters)
|
2021-07-11 07:27:41 +00:00
|
|
|
if !reflect.DeepEqual(interval, tt.wantInterval) {
|
|
|
|
t.Errorf("getSchedulingDetails() interval = %v, want %v", interval, tt.wantInterval)
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
2021-07-11 07:27:41 +00:00
|
|
|
if !reflect.DeepEqual(startTime, tt.wantStartTime) {
|
|
|
|
t.Errorf("getSchedulingDetails() startTime = %v, want %v", startTime, tt.wantStartTime)
|
2021-06-28 11:38:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 10:24:12 +00:00
|
|
|
|
|
|
|
func TestCheckVolumeResyncStatus(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args librbd.SiteMirrorImageStatus
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2022-08-08 11:53:35 +00:00
|
|
|
{
|
|
|
|
name: "test when rbd mirror daemon is not running",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: false,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-10-25 10:24:12 +00:00
|
|
|
{
|
|
|
|
name: "test for unknown state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for error state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateError,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for syncing state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateSyncing,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for starting_replay state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStartingReplay,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for replaying state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateReplaying,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for stopping_replay state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStoppingReplay,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for stopped state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStopped,
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for invalid state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusState(100),
|
2022-08-08 11:53:35 +00:00
|
|
|
Up: true,
|
2021-10-25 10:24:12 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
ts := tt
|
|
|
|
t.Run(ts.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if err := checkVolumeResyncStatus(ts.args); (err != nil) != ts.wantErr {
|
|
|
|
t.Errorf("checkVolumeResyncStatus() error = %v, expect error = %v", err, ts.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-08 17:53:41 +00:00
|
|
|
|
|
|
|
func TestCheckRemoteSiteStatus(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args librbd.GlobalMirrorImageStatus
|
|
|
|
wantReady bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Test a single peer in sync",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test a single peer in sync, including a local instance",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MirrorUUID: "",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test a multiple peers in sync",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote1",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote2",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test no remote peers",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{},
|
|
|
|
},
|
|
|
|
wantReady: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test single peer not in sync",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote",
|
|
|
|
State: librbd.MirrorImageStatusStateReplaying,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test single peer not up",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test multiple peers, when first peer is not in sync",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote1",
|
|
|
|
State: librbd.MirrorImageStatusStateStoppingReplay,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote2",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Test multiple peers, when second peer is not up",
|
|
|
|
args: librbd.GlobalMirrorImageStatus{
|
|
|
|
SiteStatuses: []librbd.SiteMirrorImageStatus{
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote1",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
MirrorUUID: "remote2",
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
Up: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantReady: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
ts := tt
|
|
|
|
t.Run(ts.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
if ready := checkRemoteSiteStatus(context.TODO(), &ts.args); ready != ts.wantReady {
|
|
|
|
t.Errorf("checkRemoteSiteStatus() ready = %v, expect ready = %v", ready, ts.wantReady)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-09 11:26:41 +00:00
|
|
|
|
2023-06-09 10:42:50 +00:00
|
|
|
func TestValidateLastSyncInfo(t *testing.T) {
|
2022-09-09 11:26:41 +00:00
|
|
|
t.Parallel()
|
2023-06-09 10:42:50 +00:00
|
|
|
duration, err := time.ParseDuration(strconv.Itoa(int(56743)) + "s")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to parse duration)")
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:26:41 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
description string
|
2023-06-09 10:42:50 +00:00
|
|
|
info *replication.GetVolumeReplicationInfoResponse
|
2022-09-09 11:26:41 +00:00
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
2023-06-09 10:42:50 +00:00
|
|
|
name: "valid description",
|
2022-09-09 11:26:41 +00:00
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
2023-06-09 10:42:50 +00:00
|
|
|
description: `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,"last_snapshot_bytes":81920,"last_snapshot_sync_seconds":56743,"local_snapshot_timestamp":1684675261,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncTime: timestamppb.New(time.Unix(1684675261, 0)),
|
|
|
|
LastSyncDuration: durationpb.New(duration),
|
|
|
|
LastSyncBytes: 81920,
|
|
|
|
},
|
|
|
|
expectedErr: "",
|
2022-09-09 11:26:41 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-09 10:42:50 +00:00
|
|
|
name: "empty description",
|
|
|
|
description: "",
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncTime: nil,
|
|
|
|
LastSyncDuration: nil,
|
|
|
|
LastSyncBytes: 0,
|
|
|
|
},
|
|
|
|
expectedErr: corerbd.ErrLastSyncTimeNotFound.Error(),
|
2022-09-09 11:26:41 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-09 10:42:50 +00:00
|
|
|
name: "description without last_snapshot_bytes",
|
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
|
|
|
description: `replaying, {"bytes_per_second":0.0,"last_snapshot_sync_seconds":56743,"local_snapshot_timestamp":1684675261,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: durationpb.New(duration),
|
|
|
|
LastSyncTime: timestamppb.New(time.Unix(1684675261, 0)),
|
|
|
|
LastSyncBytes: 0,
|
|
|
|
},
|
|
|
|
expectedErr: "",
|
2022-09-09 11:26:41 +00:00
|
|
|
},
|
|
|
|
{
|
2023-06-09 10:42:50 +00:00
|
|
|
name: "description without local_snapshot_time",
|
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
|
|
|
description: `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,"last_snapshot_bytes":81920,"last_snapshot_sync_seconds":56743,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: nil,
|
|
|
|
LastSyncTime: nil,
|
|
|
|
LastSyncBytes: 0,
|
|
|
|
},
|
|
|
|
expectedErr: corerbd.ErrLastSyncTimeNotFound.Error(),
|
2022-09-09 11:26:41 +00:00
|
|
|
},
|
2022-09-14 11:09:35 +00:00
|
|
|
{
|
2023-06-09 10:42:50 +00:00
|
|
|
name: "description without last_snapshot_sync_seconds",
|
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
|
|
|
description: `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,"last_snapshot_bytes":81920,"local_snapshot_timestamp":1684675261,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: nil,
|
|
|
|
LastSyncTime: timestamppb.New(time.Unix(1684675261, 0)),
|
|
|
|
LastSyncBytes: 81920,
|
|
|
|
},
|
|
|
|
expectedErr: "",
|
|
|
|
},
|
2023-06-23 08:43:59 +00:00
|
|
|
{
|
|
|
|
name: "description with last_snapshot_sync_seconds = 0",
|
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
|
|
|
description: `replaying, {"bytes_per_second":0.0,"bytes_per_snapshot":81920.0,"last_snapshot_sync_seconds":0,
|
|
|
|
"last_snapshot_bytes":81920,"local_snapshot_timestamp":1684675261,"remote_snapshot_timestamp":1684675261,"replay_state":"idle"}`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: durationpb.New(time.Duration(0)),
|
|
|
|
LastSyncTime: timestamppb.New(time.Unix(1684675261, 0)),
|
|
|
|
LastSyncBytes: 81920,
|
|
|
|
},
|
|
|
|
expectedErr: "",
|
|
|
|
},
|
2023-06-09 10:42:50 +00:00
|
|
|
{
|
|
|
|
name: "description with invalid JSON",
|
|
|
|
//nolint:lll // sample output cannot be split into multiple lines.
|
|
|
|
description: `replaying,{"bytes_per_second":0.0,"last_snapshot_bytes":81920","bytes_per_snapshot":149504.0","remote_snapshot_timestamp":1662655501`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: nil,
|
|
|
|
LastSyncTime: nil,
|
|
|
|
LastSyncBytes: 0,
|
|
|
|
},
|
|
|
|
expectedErr: "failed to unmarshal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description with no JSON",
|
|
|
|
description: `replaying`,
|
|
|
|
info: &replication.GetVolumeReplicationInfoResponse{
|
|
|
|
LastSyncDuration: nil,
|
|
|
|
LastSyncTime: nil,
|
|
|
|
LastSyncBytes: 0,
|
|
|
|
},
|
|
|
|
expectedErr: corerbd.ErrLastSyncTimeNotFound.Error(),
|
2022-09-14 11:09:35 +00:00
|
|
|
},
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-06-09 10:42:50 +00:00
|
|
|
teststruct, err := getLastSyncInfo(tt.description)
|
2022-09-09 11:26:41 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), tt.expectedErr) {
|
|
|
|
// returned error
|
2023-06-09 10:42:50 +00:00
|
|
|
t.Errorf("getLastSyncInfo() returned error, expected: %v, got: %v",
|
2022-09-09 11:26:41 +00:00
|
|
|
tt.expectedErr, err)
|
|
|
|
}
|
2023-06-09 10:42:50 +00:00
|
|
|
if teststruct != nil {
|
|
|
|
if teststruct.LastSyncTime.GetSeconds() != tt.info.LastSyncTime.GetSeconds() {
|
|
|
|
t.Errorf("name: %v, getLastSyncInfo() %v, expected %v", tt.name, teststruct.LastSyncTime, tt.info.LastSyncTime)
|
|
|
|
}
|
|
|
|
if tt.info.LastSyncDuration == nil && teststruct.LastSyncDuration != nil {
|
|
|
|
t.Errorf("name: %v, getLastSyncInfo() %v, expected %v", tt.name, teststruct.LastSyncDuration,
|
|
|
|
tt.info.LastSyncDuration)
|
|
|
|
}
|
|
|
|
if teststruct.LastSyncDuration.GetSeconds() != tt.info.LastSyncDuration.GetSeconds() {
|
|
|
|
t.Errorf("name: %v, getLastSyncInfo() %v, expected %v", tt.name, teststruct.LastSyncDuration,
|
|
|
|
tt.info.LastSyncDuration)
|
|
|
|
}
|
|
|
|
if teststruct.LastSyncBytes != tt.info.LastSyncBytes {
|
|
|
|
t.Errorf("name: %v, getLastSyncInfo() %v, expected %v", tt.name, teststruct.LastSyncBytes, tt.info.LastSyncBytes)
|
|
|
|
}
|
2022-09-09 11:26:41 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-06-05 16:18:29 +00:00
|
|
|
|
|
|
|
func TestGetGRPCError(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "FetchingLocalStateFailed",
|
|
|
|
err: corerbd.ErrFetchingLocalState,
|
|
|
|
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingLocalState.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ResyncImageFailed",
|
|
|
|
err: corerbd.ErrResyncImageFailed,
|
|
|
|
expectedErr: status.Error(codes.Internal, corerbd.ErrResyncImageFailed.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DisableImageMirroringFailed",
|
|
|
|
err: corerbd.ErrDisableImageMirroringFailed,
|
|
|
|
expectedErr: status.Error(codes.Internal, corerbd.ErrDisableImageMirroringFailed.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FetchingMirroringInfoFailed",
|
|
|
|
err: corerbd.ErrFetchingMirroringInfo,
|
|
|
|
expectedErr: status.Error(codes.Internal, corerbd.ErrFetchingMirroringInfo.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InvalidArgument",
|
|
|
|
err: corerbd.ErrInvalidArgument,
|
|
|
|
expectedErr: status.Error(codes.InvalidArgument, corerbd.ErrInvalidArgument.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Aborted",
|
|
|
|
err: corerbd.ErrAborted,
|
|
|
|
expectedErr: status.Error(codes.Aborted, corerbd.ErrAborted.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FailedPrecondition",
|
|
|
|
err: corerbd.ErrFailedPrecondition,
|
|
|
|
expectedErr: status.Error(codes.FailedPrecondition, corerbd.ErrFailedPrecondition.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Unavailable",
|
|
|
|
err: corerbd.ErrUnavailable,
|
|
|
|
expectedErr: status.Error(codes.Unavailable, corerbd.ErrUnavailable.Error()),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InvalidError",
|
|
|
|
err: errors.New("some error"),
|
|
|
|
expectedErr: status.Error(codes.Unknown, "some error"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NilError",
|
|
|
|
err: nil,
|
|
|
|
expectedErr: status.Error(codes.OK, "ok string"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
result := getGRPCError(tt.err)
|
|
|
|
assert.Equal(t, tt.expectedErr, result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|