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 (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-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{
|
|
|
|
imageMirroringKey: "journal",
|
|
|
|
schedulingIntervalKey: "1h",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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-15 06:00:13 +00:00
|
|
|
err := validateSchedulingDetails(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
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test for unknown state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateUnknown,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for error state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateError,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for syncing state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateSyncing,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for starting_replay state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStartingReplay,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for replaying state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateReplaying,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for stopping_replay state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStoppingReplay,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for stopped state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusStateStopped,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test for invalid state",
|
|
|
|
args: librbd.SiteMirrorImageStatus{
|
|
|
|
State: librbd.MirrorImageStatusState(100),
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|