mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-07 13:55:20 +00:00
Add more UT for the CSI plugins.
Signed-off-by: Xun Jiang <blackpigletbruce@gmail.com>
This commit is contained in:
@@ -18,6 +18,7 @@ package csi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -40,6 +41,7 @@ import (
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
velerov2alpha1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/util/boolptr"
|
||||
@@ -365,3 +367,37 @@ func TestCancel(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPVCAppliesTo(t *testing.T) {
|
||||
p := pvcBackupItemAction{
|
||||
log: logrus.StandardLogger(),
|
||||
}
|
||||
selector, err := p.AppliesTo()
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(
|
||||
t,
|
||||
velero.ResourceSelector{
|
||||
IncludedResources: []string{"persistentvolumeclaims"},
|
||||
},
|
||||
selector,
|
||||
)
|
||||
}
|
||||
|
||||
func TestNewPVCBackupItemAction(t *testing.T) {
|
||||
logger := logrus.StandardLogger()
|
||||
crClient := velerotest.NewFakeControllerRuntimeClient(t)
|
||||
|
||||
f := &factorymocks.Factory{}
|
||||
f.On("KubebuilderClient").Return(nil, fmt.Errorf(""))
|
||||
plugin := NewPvcBackupItemAction(f)
|
||||
_, err := plugin(logger)
|
||||
require.Error(t, err)
|
||||
|
||||
f1 := &factorymocks.Factory{}
|
||||
f1.On("KubebuilderClient").Return(crClient, nil)
|
||||
plugin1 := NewPvcBackupItemAction(f1)
|
||||
_, err1 := plugin1(logger)
|
||||
require.NoError(t, err1)
|
||||
}
|
||||
|
||||
@@ -84,10 +84,15 @@ func (p *volumeSnapshotBackupItemAction) Execute(
|
||||
return nil, nil, "", nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
volumeSnapshotClassName := ""
|
||||
if vs.Spec.VolumeSnapshotClassName != nil {
|
||||
volumeSnapshotClassName = *vs.Spec.VolumeSnapshotClassName
|
||||
}
|
||||
|
||||
additionalItems := []velero.ResourceIdentifier{
|
||||
{
|
||||
GroupResource: kuberesource.VolumeSnapshotClasses,
|
||||
Name: *vs.Spec.VolumeSnapshotClassName,
|
||||
Name: volumeSnapshotClassName,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -128,6 +133,10 @@ func (p *volumeSnapshotBackupItemAction) Execute(
|
||||
WithField("Backup", fmt.Sprintf("%s/%s", backup.Namespace, backup.Name)).
|
||||
WithField("BackupPhase", backup.Status.Phase).Debugf("Clean VolumeSnapshots.")
|
||||
|
||||
if vsc == nil {
|
||||
vsc = &snapshotv1api.VolumeSnapshotContent{}
|
||||
}
|
||||
|
||||
csi.DeleteVolumeSnapshot(*vs, *vsc, backup, p.crClient, p.log)
|
||||
return item, nil, "", nil, nil
|
||||
}
|
||||
|
||||
312
pkg/backup/actions/csi/volumesnapshot_action_test.go
Normal file
312
pkg/backup/actions/csi/volumesnapshot_action_test.go
Normal file
@@ -0,0 +1,312 @@
|
||||
/*
|
||||
Copyright the Velero contributors.
|
||||
|
||||
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 csi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/kuberesource"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
func TestVSExecute(t *testing.T) {
|
||||
snapshotHandle := "handle"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
backup *velerov1api.Backup
|
||||
vs *snapshotv1api.VolumeSnapshot
|
||||
vsc *snapshotv1api.VolumeSnapshotContent
|
||||
expectedErr string
|
||||
expectedAdditionalItems []velero.ResourceIdentifier
|
||||
expectedItemToUpdate []velero.ResourceIdentifier
|
||||
}{
|
||||
{
|
||||
name: "VS not created by backup, has no status. Backup is finalizing",
|
||||
backup: builder.ForBackup("velero", "backup").
|
||||
Phase(velerov1api.BackupPhaseFinalizing).Result(),
|
||||
vs: builder.ForVolumeSnapshot("velero", "vs").
|
||||
VolumeSnapshotClass("class").Result(),
|
||||
expectedErr: "",
|
||||
},
|
||||
{
|
||||
name: "VS is not created by the backup, associated VSC not exists",
|
||||
backup: builder.ForBackup("velero", "backup").
|
||||
Phase(velerov1api.BackupPhaseInProgress).Result(),
|
||||
vs: builder.ForVolumeSnapshot("velero", "vs").
|
||||
VolumeSnapshotClass("class").Status().
|
||||
BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
expectedErr: `error getting volume snapshot content from API: volumesnapshotcontents.snapshot.storage.k8s.io "vsc" not found`,
|
||||
},
|
||||
{
|
||||
name: "Normal case",
|
||||
backup: builder.ForBackup("velero", "backup").
|
||||
Phase(velerov1api.BackupPhaseInProgress).Result(),
|
||||
vs: builder.ForVolumeSnapshot("velero", "vs").
|
||||
ObjectMeta(builder.WithLabels(
|
||||
velerov1api.BackupNameLabel, "backup")).
|
||||
VolumeSnapshotClass("class").Status().
|
||||
BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
vsc: builder.ForVolumeSnapshotContent("vsc").Status(
|
||||
&snapshotv1api.VolumeSnapshotContentStatus{
|
||||
SnapshotHandle: &snapshotHandle,
|
||||
},
|
||||
).Result(),
|
||||
expectedErr: "",
|
||||
expectedAdditionalItems: []velero.ResourceIdentifier{
|
||||
{
|
||||
GroupResource: kuberesource.VolumeSnapshotClasses,
|
||||
Name: "class",
|
||||
},
|
||||
{
|
||||
GroupResource: kuberesource.VolumeSnapshotContents,
|
||||
Name: "vsc",
|
||||
},
|
||||
},
|
||||
expectedItemToUpdate: []velero.ResourceIdentifier{
|
||||
{
|
||||
GroupResource: kuberesource.VolumeSnapshots,
|
||||
Namespace: "velero",
|
||||
Name: "vs",
|
||||
},
|
||||
{
|
||||
GroupResource: kuberesource.VolumeSnapshotContents,
|
||||
Name: "vsc",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(*testing.T) {
|
||||
vsBIA := volumeSnapshotBackupItemAction{
|
||||
log: logrus.New(),
|
||||
crClient: velerotest.NewFakeControllerRuntimeClient(t, tc.vs),
|
||||
}
|
||||
|
||||
item, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.vs)
|
||||
require.NoError(t, err)
|
||||
|
||||
if tc.vsc != nil {
|
||||
require.NoError(t, vsBIA.crClient.Create(context.TODO(), tc.vsc))
|
||||
}
|
||||
|
||||
_, additionalItems, _, itemToUpdate, err := vsBIA.Execute(&unstructured.UnstructuredList{Object: item}, tc.backup)
|
||||
if tc.expectedErr == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
require.Equal(t, tc.expectedErr, err.Error())
|
||||
}
|
||||
|
||||
require.ElementsMatch(t, tc.expectedAdditionalItems, additionalItems)
|
||||
require.ElementsMatch(t, tc.expectedItemToUpdate, itemToUpdate)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVSProgress(t *testing.T) {
|
||||
errorStr := "error"
|
||||
readyToUse := true
|
||||
tests := []struct {
|
||||
name string
|
||||
backup *velerov1api.Backup
|
||||
vs *snapshotv1api.VolumeSnapshot
|
||||
vsc *snapshotv1api.VolumeSnapshotContent
|
||||
operationID string
|
||||
expectedErr bool
|
||||
expectedProgress *velero.OperationProgress
|
||||
}{
|
||||
{
|
||||
name: "Empty OperationID",
|
||||
operationID: "",
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "OperationID doesn't have slash",
|
||||
operationID: "invalid",
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "OperationID doesn't have valid timestamp",
|
||||
operationID: "ns/name/invalid",
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "OperationID represents VS does not exist",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "VS status is nil",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "VS status has error",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Status().
|
||||
StatusError(snapshotv1api.VolumeSnapshotError{
|
||||
Message: &errorStr,
|
||||
}).Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "Fail to get VSC",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Status().
|
||||
ReadyToUse(true).BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: true,
|
||||
},
|
||||
{
|
||||
name: "VSC status is nil",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Status().
|
||||
ReadyToUse(true).BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
vsc: builder.ForVolumeSnapshotContent("vsc").Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: false,
|
||||
},
|
||||
{
|
||||
name: "VSC is ReadyToUse",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Status().
|
||||
ReadyToUse(true).BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
vsc: builder.ForVolumeSnapshotContent("vsc").
|
||||
Status(&snapshotv1api.VolumeSnapshotContentStatus{
|
||||
ReadyToUse: &readyToUse,
|
||||
}).Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: false,
|
||||
expectedProgress: &velero.OperationProgress{Completed: true},
|
||||
},
|
||||
{
|
||||
name: "VSC status has error",
|
||||
operationID: "ns/name/2024-04-11T18:49:00+08:00",
|
||||
vs: builder.ForVolumeSnapshot("ns", "name").Status().
|
||||
ReadyToUse(true).BoundVolumeSnapshotContentName("vsc").Result(),
|
||||
vsc: builder.ForVolumeSnapshotContent("vsc").
|
||||
Status(&snapshotv1api.VolumeSnapshotContentStatus{
|
||||
Error: &snapshotv1api.VolumeSnapshotError{
|
||||
Message: &errorStr,
|
||||
},
|
||||
}).Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectedErr: false,
|
||||
expectedProgress: &velero.OperationProgress{
|
||||
Completed: true,
|
||||
Err: "error",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(*testing.T) {
|
||||
crClient := velerotest.NewFakeControllerRuntimeClient(t)
|
||||
logger := logrus.New()
|
||||
|
||||
vsBIA := volumeSnapshotBackupItemAction{
|
||||
log: logger,
|
||||
crClient: crClient,
|
||||
}
|
||||
|
||||
if tc.vs != nil {
|
||||
err := crClient.Create(context.Background(), tc.vs)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if tc.vsc != nil {
|
||||
require.NoError(t, crClient.Create(context.TODO(), tc.vsc))
|
||||
}
|
||||
|
||||
progress, err := vsBIA.Progress(tc.operationID, tc.backup)
|
||||
if tc.expectedErr == false {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if tc.expectedProgress != nil {
|
||||
require.True(
|
||||
t,
|
||||
cmp.Equal(
|
||||
*tc.expectedProgress,
|
||||
progress,
|
||||
cmpopts.IgnoreFields(
|
||||
velero.OperationProgress{},
|
||||
"Started",
|
||||
"Updated",
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVSAppliesTo(t *testing.T) {
|
||||
p := volumeSnapshotBackupItemAction{
|
||||
log: logrus.StandardLogger(),
|
||||
}
|
||||
selector, err := p.AppliesTo()
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(
|
||||
t,
|
||||
velero.ResourceSelector{
|
||||
IncludedResources: []string{"volumesnapshots.snapshot.storage.k8s.io"},
|
||||
},
|
||||
selector,
|
||||
)
|
||||
}
|
||||
|
||||
func TestNewVolumeSnapshotBackupItemAction(t *testing.T) {
|
||||
logger := logrus.StandardLogger()
|
||||
crClient := velerotest.NewFakeControllerRuntimeClient(t)
|
||||
|
||||
f := &factorymocks.Factory{}
|
||||
f.On("KubebuilderClient").Return(nil, fmt.Errorf(""))
|
||||
plugin := NewVolumeSnapshotBackupItemAction(f)
|
||||
_, err := plugin(logger)
|
||||
require.Error(t, err)
|
||||
|
||||
f1 := &factorymocks.Factory{}
|
||||
f1.On("KubebuilderClient").Return(crClient, nil)
|
||||
plugin1 := NewVolumeSnapshotBackupItemAction(f1)
|
||||
_, err1 := plugin1(logger)
|
||||
require.NoError(t, err1)
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func (p *volumeSnapshotClassBackupItemAction) AppliesTo() (
|
||||
error,
|
||||
) {
|
||||
return velero.ResourceSelector{
|
||||
IncludedResources: []string{"volumesnapshotclass.snapshot.storage.k8s.io"},
|
||||
IncludedResources: []string{"volumesnapshotclasses.snapshot.storage.k8s.io"},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
113
pkg/backup/actions/csi/volumesnapshotclass_action_test.go
Normal file
113
pkg/backup/actions/csi/volumesnapshotclass_action_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
Copyright the Velero contributors.
|
||||
|
||||
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 csi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
"github.com/vmware-tanzu/velero/pkg/kuberesource"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
|
||||
)
|
||||
|
||||
func TestVSClassExecute(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
item runtime.Unstructured
|
||||
vsClass *snapshotv1api.VolumeSnapshotClass
|
||||
backup *velerov1api.Backup
|
||||
expectErr bool
|
||||
expectedItems []velero.ResourceIdentifier
|
||||
}{
|
||||
{
|
||||
name: "No Secret in the VS Class, no return additional items",
|
||||
vsClass: builder.ForVolumeSnapshotClass("test").Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
name: "Normal case, additional items should return",
|
||||
vsClass: builder.ForVolumeSnapshotClass("test").ObjectMeta(builder.WithAnnotationsMap(
|
||||
map[string]string{
|
||||
velerov1api.PrefixedListSecretNameAnnotation: "name",
|
||||
velerov1api.PrefixedListSecretNamespaceAnnotation: "namespace",
|
||||
},
|
||||
)).Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectErr: false,
|
||||
expectedItems: []velero.ResourceIdentifier{
|
||||
{
|
||||
GroupResource: kuberesource.Secrets,
|
||||
Namespace: "namespace",
|
||||
Name: "name",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
p, err := NewVolumeSnapshotClassBackupItemAction(logrus.StandardLogger())
|
||||
require.NoError(t, err)
|
||||
|
||||
action := p.(*volumeSnapshotClassBackupItemAction)
|
||||
|
||||
if test.vsClass != nil {
|
||||
vsMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(test.vsClass)
|
||||
require.NoError(t, err)
|
||||
test.item = &unstructured.Unstructured{Object: vsMap}
|
||||
}
|
||||
|
||||
_, additionalItems, _, _, err := action.Execute(
|
||||
test.item,
|
||||
test.backup,
|
||||
)
|
||||
|
||||
if test.expectErr == false {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if len(test.expectedItems) > 0 {
|
||||
require.Equal(t, test.expectedItems, additionalItems)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVSClassAppliesTo(t *testing.T) {
|
||||
p := volumeSnapshotClassBackupItemAction{
|
||||
log: logrus.StandardLogger(),
|
||||
}
|
||||
selector, err := p.AppliesTo()
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(
|
||||
t,
|
||||
velero.ResourceSelector{
|
||||
IncludedResources: []string{"volumesnapshotclasses.snapshot.storage.k8s.io"},
|
||||
},
|
||||
selector,
|
||||
)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ type volumeSnapshotContentBackupItemAction struct {
|
||||
// backup VolumeSnapshotContents.
|
||||
func (p *volumeSnapshotContentBackupItemAction) AppliesTo() (velero.ResourceSelector, error) {
|
||||
return velero.ResourceSelector{
|
||||
IncludedResources: []string{"volumesnapshotcontent.snapshot.storage.k8s.io"},
|
||||
IncludedResources: []string{"volumesnapshotcontents.snapshot.storage.k8s.io"},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
125
pkg/backup/actions/csi/volumesnapshotcontent_action_test.go
Normal file
125
pkg/backup/actions/csi/volumesnapshotcontent_action_test.go
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
Copyright the Velero contributors.
|
||||
|
||||
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 csi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v7/apis/volumesnapshot/v1"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
"github.com/vmware-tanzu/velero/pkg/kuberesource"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
func TestVSCExecute(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
item runtime.Unstructured
|
||||
vsc *snapshotv1api.VolumeSnapshotContent
|
||||
backup *velerov1api.Backup
|
||||
expectErr bool
|
||||
expectedItems []velero.ResourceIdentifier
|
||||
}{
|
||||
{
|
||||
name: "Invalid VolumeSnapshotClass",
|
||||
item: velerotest.UnstructuredOrDie(
|
||||
`
|
||||
{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"metadata": {
|
||||
"namespace": "ns",
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
`,
|
||||
),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Normal case, additional items should return",
|
||||
vsc: builder.ForVolumeSnapshotContent("test").ObjectMeta(builder.WithAnnotationsMap(
|
||||
map[string]string{
|
||||
velerov1api.PrefixedSecretNameAnnotation: "name",
|
||||
velerov1api.PrefixedSecretNamespaceAnnotation: "namespace",
|
||||
},
|
||||
)).Result(),
|
||||
backup: builder.ForBackup("velero", "backup").Result(),
|
||||
expectErr: false,
|
||||
expectedItems: []velero.ResourceIdentifier{
|
||||
{
|
||||
GroupResource: kuberesource.Secrets,
|
||||
Namespace: "namespace",
|
||||
Name: "name",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
p, err := NewVolumeSnapshotContentBackupItemAction(logrus.StandardLogger())
|
||||
require.NoError(t, err)
|
||||
|
||||
action := p.(*volumeSnapshotContentBackupItemAction)
|
||||
|
||||
if test.vsc != nil {
|
||||
vsMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(test.vsc)
|
||||
require.NoError(t, err)
|
||||
test.item = &unstructured.Unstructured{Object: vsMap}
|
||||
}
|
||||
|
||||
_, additionalItems, _, _, err := action.Execute(
|
||||
test.item,
|
||||
test.backup,
|
||||
)
|
||||
|
||||
if test.expectErr == false {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if len(test.expectedItems) > 0 {
|
||||
require.Equal(t, test.expectedItems, additionalItems)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestVSCAppliesTo(t *testing.T) {
|
||||
p := volumeSnapshotContentBackupItemAction{
|
||||
log: logrus.StandardLogger(),
|
||||
}
|
||||
selector, err := p.AppliesTo()
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(
|
||||
t,
|
||||
velero.ResourceSelector{
|
||||
IncludedResources: []string{"volumesnapshotcontents.snapshot.storage.k8s.io"},
|
||||
},
|
||||
selector,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user