mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-03 14:47:08 +00:00
Add UT for pkg/cmd/cli/backup (#6400)
Signed-off-by: danfengl <danfengl@vmware.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
)
|
||||
|
||||
func TestNewBackupCommand(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
// create command
|
||||
cmd := NewCommand(f)
|
||||
assert.Equal(t, "Work with backups", cmd.Short)
|
||||
}
|
||||
@@ -139,16 +139,16 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
|
||||
f := flags.VarPF(&o.SnapshotVolumes, "snapshot-volumes", "", "Take snapshots of PersistentVolumes as part of the backup. If the parameter is not set, it is treated as setting to 'true'.")
|
||||
// this allows the user to just specify "--snapshot-volumes" as shorthand for "--snapshot-volumes=true"
|
||||
// like a normal bool flag
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.SnapshotMoveData, "snapshot-move-data", "", "Specify whether snapshot data should be moved")
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.IncludeClusterResources, "include-cluster-resources", "", "Include cluster-scoped resources in the backup. Cannot work with include-cluster-scoped-resources, exclude-cluster-scoped-resources, include-namespace-scoped-resources and exclude-namespace-scoped-resources.")
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.DefaultVolumesToFsBackup, "default-volumes-to-fs-backup", "", "Use pod volume file system backup by default for volumes")
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
flags.StringVar(&o.ResPoliciesConfigmap, "resource-policies-configmap", "", "Reference to the resource policies configmap that backup using")
|
||||
flags.StringVar(&o.DataMover, "data-mover", "", "Specify the data mover to be used by the backup. If the parameter is not set or set as 'velero', the built-in data mover will be used")
|
||||
|
||||
@@ -18,18 +18,30 @@ package backup
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
|
||||
)
|
||||
|
||||
const testNamespace = "velero"
|
||||
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/fake"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
func TestCreateOptions_BuildBackup(t *testing.T) {
|
||||
o := NewCreateOptions()
|
||||
@@ -40,7 +52,7 @@ func TestCreateOptions_BuildBackup(t *testing.T) {
|
||||
o.ItemOperationTimeout = 20 * time.Minute
|
||||
assert.NoError(t, err)
|
||||
|
||||
backup, err := o.BuildBackup(testNamespace)
|
||||
backup, err := o.BuildBackup(cmdtest.VeleroNameSpace)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, velerov1api.BackupSpec{
|
||||
@@ -68,16 +80,16 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
|
||||
o.client = fake.NewSimpleClientset()
|
||||
|
||||
t.Run("inexistent schedule", func(t *testing.T) {
|
||||
_, err := o.BuildBackup(testNamespace)
|
||||
_, err := o.BuildBackup(cmdtest.VeleroNameSpace)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
expectedBackupSpec := builder.ForBackup("test", testNamespace).IncludedNamespaces("test").Result().Spec
|
||||
schedule := builder.ForSchedule(testNamespace, "test").Template(expectedBackupSpec).ObjectMeta(builder.WithLabels("velero.io/test", "true"), builder.WithAnnotations("velero.io/test", "true")).Result()
|
||||
o.client.VeleroV1().Schedules(testNamespace).Create(context.TODO(), schedule, metav1.CreateOptions{})
|
||||
expectedBackupSpec := builder.ForBackup("test", cmdtest.VeleroNameSpace).IncludedNamespaces("test").Result().Spec
|
||||
schedule := builder.ForSchedule(cmdtest.VeleroNameSpace, "test").Template(expectedBackupSpec).ObjectMeta(builder.WithLabels("velero.io/test", "true"), builder.WithAnnotations("velero.io/test", "true")).Result()
|
||||
o.client.VeleroV1().Schedules(cmdtest.VeleroNameSpace).Create(context.TODO(), schedule, metav1.CreateOptions{})
|
||||
|
||||
t.Run("existing schedule", func(t *testing.T) {
|
||||
backup, err := o.BuildBackup(testNamespace)
|
||||
backup, err := o.BuildBackup(cmdtest.VeleroNameSpace)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expectedBackupSpec, backup.Spec)
|
||||
@@ -92,7 +104,7 @@ func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
|
||||
|
||||
t.Run("command line labels take precedence over schedule labels", func(t *testing.T) {
|
||||
o.Labels.Set("velero.io/test=yes,custom-label=true")
|
||||
backup, err := o.BuildBackup(testNamespace)
|
||||
backup, err := o.BuildBackup(cmdtest.VeleroNameSpace)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expectedBackupSpec, backup.Spec)
|
||||
@@ -127,3 +139,235 @@ func TestCreateOptions_OrderedResources(t *testing.T) {
|
||||
assert.Equal(t, orderedResources, expectedMixedResources)
|
||||
|
||||
}
|
||||
|
||||
func TestCreateCommand(t *testing.T) {
|
||||
name := "nameToBeCreated"
|
||||
args := []string{name}
|
||||
|
||||
t.Run("create a backup create command with full options except fromSchedule and wait, then run by create option", func(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
// create command
|
||||
cmd := NewCreateCommand(f, "")
|
||||
assert.Equal(t, "Create a backup", cmd.Short)
|
||||
|
||||
includeNamespaces := "app1,app2"
|
||||
excludeNamespaces := "pod1,pod2,pod3"
|
||||
includeResources := "sc,sts"
|
||||
excludeResources := "job"
|
||||
includeClusterScopedResources := "pv,ComponentStatus"
|
||||
excludeClusterScopedResources := "MutatingWebhookConfiguration,APIService"
|
||||
includeNamespaceScopedResources := "Endpoints,Event,PodTemplate"
|
||||
excludeNamespaceScopedResources := "Secret,MultiClusterIngress"
|
||||
labels := "c=foo,b=woo"
|
||||
storageLocation := "bsl-name-1"
|
||||
snapshotLocations := "region=minio"
|
||||
selector := "a=pod"
|
||||
orderedResources := "pod=pod1,pod2,pod3"
|
||||
csiSnapshotTimeout := "8m30s"
|
||||
itemOperationTimeout := "99h1m6s"
|
||||
snapshotVolumes := "false"
|
||||
snapshotMoveData := "true"
|
||||
includeClusterResources := "true"
|
||||
defaultVolumesToFsBackup := "true"
|
||||
resPoliciesConfigmap := "cm-name-2"
|
||||
dataMover := "velero"
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewCreateOptions()
|
||||
o.BindFlags(flags)
|
||||
o.BindWait(flags)
|
||||
o.BindFromSchedule(flags)
|
||||
|
||||
flags.Parse([]string{"--include-namespaces", includeNamespaces})
|
||||
flags.Parse([]string{"--exclude-namespaces", excludeNamespaces})
|
||||
flags.Parse([]string{"--include-resources", includeResources})
|
||||
flags.Parse([]string{"--exclude-resources", excludeResources})
|
||||
flags.Parse([]string{"--include-cluster-scoped-resources", includeClusterScopedResources})
|
||||
flags.Parse([]string{"--exclude-cluster-scoped-resources", excludeClusterScopedResources})
|
||||
flags.Parse([]string{"--include-namespace-scoped-resources", includeNamespaceScopedResources})
|
||||
flags.Parse([]string{"--exclude-namespace-scoped-resources", excludeNamespaceScopedResources})
|
||||
flags.Parse([]string{"--labels", labels})
|
||||
flags.Parse([]string{"--storage-location", storageLocation})
|
||||
flags.Parse([]string{"--volume-snapshot-locations", snapshotLocations})
|
||||
flags.Parse([]string{"--selector", selector})
|
||||
flags.Parse([]string{"--ordered-resources", orderedResources})
|
||||
flags.Parse([]string{"--csi-snapshot-timeout", csiSnapshotTimeout})
|
||||
flags.Parse([]string{"--item-operation-timeout", itemOperationTimeout})
|
||||
flags.Parse([]string{fmt.Sprintf("--snapshot-volumes=%s", snapshotVolumes)})
|
||||
flags.Parse([]string{fmt.Sprintf("--snapshot-move-data=%s", snapshotMoveData)})
|
||||
flags.Parse([]string{"--include-cluster-resources", includeClusterResources})
|
||||
flags.Parse([]string{"--default-volumes-to-fs-backup", defaultVolumesToFsBackup})
|
||||
flags.Parse([]string{"--resource-policies-configmap", resPoliciesConfigmap})
|
||||
flags.Parse([]string{"--data-mover", dataMover})
|
||||
//flags.Parse([]string{"--wait"})
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(nil, nil)
|
||||
|
||||
//Complete
|
||||
e := o.Complete(args, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
//Validate
|
||||
e = o.Validate(cmd, args, f)
|
||||
assert.Contains(t, e.Error(), "include-resources, exclude-resources and include-cluster-resources are old filter parameters")
|
||||
assert.Contains(t, e.Error(), "include-cluster-scoped-resources, exclude-cluster-scoped-resources, include-namespace-scoped-resources and exclude-namespace-scoped-resources are new filter parameters.\nThey cannot be used together")
|
||||
|
||||
//cmd
|
||||
e = o.Run(cmd, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
//Execute
|
||||
cmd.SetArgs([]string{"bk-name-exe"})
|
||||
e = cmd.Execute()
|
||||
assert.NoError(t, e)
|
||||
|
||||
// verify all options are set as expected
|
||||
assert.Equal(t, name, o.Name)
|
||||
assert.Equal(t, includeNamespaces, o.IncludeNamespaces.String())
|
||||
assert.Equal(t, excludeNamespaces, o.ExcludeNamespaces.String())
|
||||
assert.Equal(t, includeResources, o.IncludeResources.String())
|
||||
assert.Equal(t, excludeResources, o.ExcludeResources.String())
|
||||
assert.Equal(t, includeClusterScopedResources, o.IncludeClusterScopedResources.String())
|
||||
assert.Equal(t, excludeClusterScopedResources, o.ExcludeClusterScopedResources.String())
|
||||
assert.Equal(t, includeNamespaceScopedResources, o.IncludeNamespaceScopedResources.String())
|
||||
assert.Equal(t, excludeNamespaceScopedResources, o.ExcludeNamespaceScopedResources.String())
|
||||
assert.Equal(t, true, test.CompareSlice(strings.Split(labels, ","), strings.Split(o.Labels.String(), ",")))
|
||||
assert.Equal(t, storageLocation, o.StorageLocation)
|
||||
assert.Equal(t, snapshotLocations, strings.Split(o.SnapshotLocations[0], ",")[0])
|
||||
assert.Equal(t, selector, o.Selector.String())
|
||||
assert.Equal(t, orderedResources, o.OrderedResources)
|
||||
assert.Equal(t, csiSnapshotTimeout, o.CSISnapshotTimeout.String())
|
||||
assert.Equal(t, itemOperationTimeout, o.ItemOperationTimeout.String())
|
||||
assert.Equal(t, snapshotVolumes, o.SnapshotVolumes.String())
|
||||
assert.Equal(t, snapshotMoveData, o.SnapshotMoveData.String())
|
||||
assert.Equal(t, includeClusterResources, o.IncludeClusterResources.String())
|
||||
assert.Equal(t, defaultVolumesToFsBackup, o.DefaultVolumesToFsBackup.String())
|
||||
assert.Equal(t, resPoliciesConfigmap, o.ResPoliciesConfigmap)
|
||||
assert.Equal(t, dataMover, o.DataMover)
|
||||
//assert.Equal(t, true, o.Wait)
|
||||
|
||||
// verify oldAndNewFilterParametersUsedTogether
|
||||
mix := o.oldAndNewFilterParametersUsedTogether()
|
||||
assert.Equal(t, true, mix)
|
||||
})
|
||||
t.Run("create a backup create command with specific storage-location setting", func(t *testing.T) {
|
||||
bsl := "bsl-1"
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
cmd := NewCreateCommand(f, "")
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
kbclient := clientfake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
bk := &velerov1api.Backup{}
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewCreateOptions()
|
||||
o.BindFlags(flags)
|
||||
o.BindWait(flags)
|
||||
o.BindFromSchedule(flags)
|
||||
flags.Parse([]string{"--include-namespaces", "ns-1"})
|
||||
flags.Parse([]string{"--storage-location", bsl})
|
||||
|
||||
// Complete
|
||||
e := o.Complete(args, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
// Validate
|
||||
e = o.Validate(cmd, args, f)
|
||||
assert.Contains(t, e.Error(), fmt.Sprintf("backupstoragelocations.velero.io \"%s\" not found", bsl))
|
||||
})
|
||||
|
||||
t.Run("create a backup create command with specific volume-snapshot-locations setting", func(t *testing.T) {
|
||||
vslName := "vsl-1"
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
cmd := NewCreateCommand(f, "")
|
||||
vsls := &velerov1mocks.VolumeSnapshotLocationInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
kbclient := clientfake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
vsl := &velerov1api.VolumeSnapshotLocation{}
|
||||
vsls.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(vsl, nil)
|
||||
veleroV1.On("VolumeSnapshotLocations", mock.Anything).Return(vsls, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewCreateOptions()
|
||||
o.BindFlags(flags)
|
||||
o.BindWait(flags)
|
||||
o.BindFromSchedule(flags)
|
||||
flags.Parse([]string{"--include-namespaces", "ns-1"})
|
||||
flags.Parse([]string{"--volume-snapshot-locations", vslName})
|
||||
|
||||
// Complete
|
||||
e := o.Complete(args, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
// Validate
|
||||
e = o.Validate(cmd, args, f)
|
||||
assert.NoError(t, e)
|
||||
})
|
||||
|
||||
t.Run("create the other create command with fromSchedule option for Run() other branches", func(t *testing.T) {
|
||||
f := &factorymocks.Factory{}
|
||||
c := NewCreateCommand(f, "")
|
||||
assert.Equal(t, "Create a backup", c.Short)
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewCreateOptions()
|
||||
o.BindFlags(flags)
|
||||
o.BindWait(flags)
|
||||
o.BindFromSchedule(flags)
|
||||
fromSchedule := "schedule-name-1"
|
||||
flags.Parse([]string{"--from-schedule", fromSchedule})
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
schedules := &velerov1mocks.ScheduleInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
kbclient := clientfake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
sd := &velerov1api.Schedule{}
|
||||
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
schedules.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(sd, nil)
|
||||
veleroV1.On("Schedules", mock.Anything).Return(schedules, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
e := o.Complete(args, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
e = o.Run(c, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
c.SetArgs([]string{"bk-1"})
|
||||
e = c.Execute()
|
||||
assert.NoError(t, e)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/cmd/cli"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestDeleteCommand(t *testing.T) {
|
||||
backupName := "backup-name-1"
|
||||
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
deleteBackupRequest := &velerov1mocks.DeleteBackupRequestInterface{}
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
dbr := &velerov1api.DeleteBackupRequest{}
|
||||
backups.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
deleteBackupRequest.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(dbr, nil)
|
||||
veleroV1.On("DeleteBackupRequests", mock.Anything).Return(deleteBackupRequest, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
|
||||
// create command
|
||||
c := NewDeleteCommand(f, "velero backup delete")
|
||||
c.SetArgs([]string{backupName})
|
||||
assert.Equal(t, "Delete backups", c.Short)
|
||||
|
||||
o := cli.NewDeleteOptions("backup")
|
||||
flags := new(flag.FlagSet)
|
||||
o.BindFlags(flags)
|
||||
flags.Parse([]string{"--confirm"})
|
||||
|
||||
args := []string{"bk1", "bk2"}
|
||||
|
||||
bk.Name = backupName
|
||||
e := o.Complete(f, args)
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
e = o.Validate(c, f, args)
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
e = Run(o)
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
e = c.Execute()
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestDeleteCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
stdout, _, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err == nil {
|
||||
assert.Contains(t, stdout, fmt.Sprintf("Request to delete backup \"%s\" submitted successfully.", backupName))
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backups by get()", err)
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"k8s.io/client-go/rest"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/features"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestNewDescribeCommand(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
clientConfig := rest.Config{}
|
||||
|
||||
deleteBackupRequest := &velerov1mocks.DeleteBackupRequestInterface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
bkList := &velerov1api.BackupList{}
|
||||
deleteBackupRequestList := &velerov1api.DeleteBackupRequestList{}
|
||||
podVolumeBackups := &velerov1mocks.PodVolumeBackupInterface{}
|
||||
podVolumeBackupList := &velerov1api.PodVolumeBackupList{}
|
||||
|
||||
backupName := "bk-describe-1"
|
||||
bk.Name = backupName
|
||||
|
||||
backups.On("List", mock.Anything, mock.Anything).Return(bkList, nil)
|
||||
backups.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
deleteBackupRequest.On("List", mock.Anything, mock.Anything).Return(deleteBackupRequestList, nil)
|
||||
veleroV1.On("DeleteBackupRequests", mock.Anything).Return(deleteBackupRequest, nil)
|
||||
podVolumeBackups.On("List", mock.Anything, mock.Anything).Return(podVolumeBackupList, nil)
|
||||
veleroV1.On("PodVolumeBackups", mock.Anything, mock.Anything).Return(podVolumeBackups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("ClientConfig").Return(&clientConfig, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(nil, nil)
|
||||
|
||||
// create command
|
||||
c := NewDescribeCommand(f, "velero backup describe")
|
||||
assert.Equal(t, "Describe backups", c.Short)
|
||||
|
||||
features.NewFeatureFlagSet("EnableCSI")
|
||||
defer features.NewFeatureFlagSet()
|
||||
|
||||
c.SetArgs([]string{"bk1"})
|
||||
e := c.Execute()
|
||||
assert.NoError(t, e)
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDescribeCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
stdout, _, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err == nil {
|
||||
assert.Contains(t, stdout, "Velero-Native Snapshots: <none included>")
|
||||
assert.Contains(t, stdout, fmt.Sprintf("Name: %s", backupName))
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backups by get()", err)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
)
|
||||
|
||||
func TestNewDownloadCommand(t *testing.T) {
|
||||
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
backups.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
// create command
|
||||
c := NewDownloadCommand(f)
|
||||
c.SetArgs([]string{"bk-to-be-download"})
|
||||
assert.Equal(t, "Download all Kubernetes manifests for a backup", c.Short)
|
||||
|
||||
// create a DownloadOptions with full options set and then run this backup command
|
||||
output := "path/to/download/bk.json"
|
||||
force := true
|
||||
timeout := "1m30s"
|
||||
insecureSkipTlsVerify := false
|
||||
cacert := "secret=YHJKKS"
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewDownloadOptions()
|
||||
o.BindFlags(flags)
|
||||
|
||||
flags.Parse([]string{"--output", output})
|
||||
flags.Parse([]string{"--force"})
|
||||
flags.Parse([]string{"--timeout", timeout})
|
||||
flags.Parse([]string{fmt.Sprintf("--insecure-skip-tls-verify=%s", strconv.FormatBool(insecureSkipTlsVerify))})
|
||||
flags.Parse([]string{"--cacert", cacert})
|
||||
|
||||
backupName := "backup-1"
|
||||
args := []string{backupName, "arg2"}
|
||||
|
||||
e := o.Complete(args)
|
||||
assert.NoError(t, e)
|
||||
|
||||
e = o.Validate(c, args, f)
|
||||
assert.NoError(t, e)
|
||||
|
||||
// verify all options are set as expected
|
||||
assert.Equal(t, output, o.Output)
|
||||
assert.Equal(t, force, o.Force)
|
||||
assert.Equal(t, timeout, o.Timeout.String())
|
||||
assert.Equal(t, insecureSkipTlsVerify, o.InsecureSkipTLSVerify)
|
||||
assert.Equal(t, cacert, o.caCertFile)
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
e = c.Execute()
|
||||
defer os.Remove("bk-to-be-download-data.tar.gz")
|
||||
assert.NoError(t, e)
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDownloadCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
_, stderr, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err != nil {
|
||||
assert.Contains(t, stderr, "download request download url timeout")
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backup delete successfully", err)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
)
|
||||
|
||||
func TestNewGetCommand(t *testing.T) {
|
||||
args := []string{"b1", "b2", "b3"}
|
||||
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
bkList := &velerov1api.BackupList{}
|
||||
|
||||
backups.On("List", mock.Anything, mock.Anything).Return(bkList, nil)
|
||||
backups.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
|
||||
// create command
|
||||
c := NewGetCommand(f, "velero backup get")
|
||||
assert.Equal(t, "Get backups", c.Short)
|
||||
|
||||
c.SetArgs(args)
|
||||
e := c.Execute()
|
||||
assert.NoError(t, e)
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
stdout, _, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err == nil {
|
||||
output := strings.Split(stdout, "\n")
|
||||
i := 0
|
||||
for _, line := range output {
|
||||
if strings.Contains(line, "New") {
|
||||
i++
|
||||
}
|
||||
}
|
||||
assert.Equal(t, len(args), i)
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backups by get()", err)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
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 backup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestNewLogsCommand(t *testing.T) {
|
||||
backupName := "bk-logs-1"
|
||||
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
bkList := &velerov1api.BackupList{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
backups.On("List", mock.Anything, mock.Anything).Return(bkList, nil)
|
||||
backups.On("Get", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
c := NewLogsCommand(f)
|
||||
assert.Equal(t, "Get backup logs", c.Short)
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
c.SetArgs([]string{backupName})
|
||||
e := c.Execute()
|
||||
assert.NoError(t, e)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewLogsCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
_, stderr, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err != nil {
|
||||
assert.Contains(t, stderr, fmt.Sprintf("Logs for backup \"%s\" are not available until it's finished processing", backupName))
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backup delete successfully", err)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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 backuplocation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
)
|
||||
|
||||
func TestNewCommand(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
// create command
|
||||
c := NewCommand(f)
|
||||
assert.Equal(t, "Work with backup storage locations", c.Short)
|
||||
}
|
||||
@@ -17,12 +17,28 @@ limitations under the License.
|
||||
package backuplocation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
veleroflag "github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
|
||||
"github.com/vmware-tanzu/velero/pkg/test"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
)
|
||||
|
||||
func TestBuildBackupStorageLocationSetsNamespace(t *testing.T) {
|
||||
@@ -87,3 +103,99 @@ func TestBuildBackupStorageLocationSetsLabels(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, map[string]string{"key": "value"}, bsl.Labels)
|
||||
}
|
||||
|
||||
func TestCreateCommand_Run(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
|
||||
// create command
|
||||
c := NewCreateCommand(f, "")
|
||||
assert.Equal(t, "Create a backup storage location", c.Short)
|
||||
|
||||
// create a CreateOptions with full options set and then run this backup command
|
||||
name := "bsl-name-to-be-created"
|
||||
provider := "aws"
|
||||
bucket := "velero123456"
|
||||
credential := veleroflag.NewMap()
|
||||
credential.Set("secret=a")
|
||||
|
||||
defaultBackupStorageLocation := true
|
||||
prefix := "builds"
|
||||
backupSyncPeriod := "1m30s"
|
||||
validationFrequency := "128h1m6s"
|
||||
bslConfig := veleroflag.NewMap()
|
||||
bslConfigStr := "region=minio"
|
||||
bslConfig.Set(bslConfigStr)
|
||||
|
||||
labels := "a=too,b=woo"
|
||||
caCertFile := "bsl-name-1"
|
||||
accessMode := "ReadWrite"
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewCreateOptions()
|
||||
o.BindFlags(flags)
|
||||
|
||||
flags.Parse([]string{"--provider", provider})
|
||||
flags.Parse([]string{"--bucket", bucket})
|
||||
flags.Parse([]string{"--credential", credential.String()})
|
||||
flags.Parse([]string{"--default"})
|
||||
flags.Parse([]string{"--prefix", prefix})
|
||||
flags.Parse([]string{"--backup-sync-period", backupSyncPeriod})
|
||||
flags.Parse([]string{"--validation-frequency", validationFrequency})
|
||||
flags.Parse([]string{"--config", bslConfigStr})
|
||||
flags.Parse([]string{"--labels", labels})
|
||||
flags.Parse([]string{"--cacert", caCertFile})
|
||||
flags.Parse([]string{"--access-mode", accessMode})
|
||||
|
||||
args := []string{name, "arg2"}
|
||||
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
o.Complete(args, f)
|
||||
e := o.Validate(c, args, f)
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
e = o.Run(c, f)
|
||||
assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", caCertFile))
|
||||
|
||||
// verify all options are set as expected
|
||||
assert.Equal(t, name, o.Name)
|
||||
assert.Equal(t, provider, o.Provider)
|
||||
assert.Equal(t, bucket, o.Bucket)
|
||||
assert.Equal(t, true, reflect.DeepEqual(credential, o.Credential))
|
||||
assert.Equal(t, defaultBackupStorageLocation, o.DefaultBackupStorageLocation)
|
||||
assert.Equal(t, prefix, o.Prefix)
|
||||
assert.Equal(t, backupSyncPeriod, o.BackupSyncPeriod.String())
|
||||
assert.Equal(t, validationFrequency, o.ValidationFrequency.String())
|
||||
assert.Equal(t, true, reflect.DeepEqual(bslConfig, o.Config))
|
||||
assert.Equal(t, true, test.CompareSlice(strings.Split(labels, ","), strings.Split(o.Labels.String(), ",")))
|
||||
assert.Equal(t, caCertFile, o.CACertFile)
|
||||
assert.Equal(t, accessMode, o.AccessMode.String())
|
||||
|
||||
// create the other create command without fromSchedule option for Run() other branches
|
||||
c = NewCreateCommand(f, "velero backup-location create")
|
||||
assert.Equal(t, "Create a backup storage location", c.Short)
|
||||
|
||||
o = NewCreateOptions()
|
||||
o.Labels.Set("velero.io/test=true")
|
||||
|
||||
args = []string{"backup-name-2", "arg2"}
|
||||
o.Complete(args, f)
|
||||
|
||||
e = o.Run(c, f)
|
||||
assert.NoError(t, e)
|
||||
c.SetArgs([]string{"bsl-1", "--provider=aws", "--bucket=bk1", "--default"})
|
||||
e = c.Execute()
|
||||
assert.NoError(t, e)
|
||||
}
|
||||
|
||||
@@ -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 backuplocation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/cmd/cli"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestNewDeleteCommand(t *testing.T) {
|
||||
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
client := &versionedmocks.Interface{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
// create command
|
||||
c := NewDeleteCommand(f, "velero backup-location delete")
|
||||
assert.Equal(t, "Delete backup storage locations", c.Short)
|
||||
|
||||
o := cli.NewDeleteOptions("backup")
|
||||
flags := new(flag.FlagSet)
|
||||
o.BindFlags(flags)
|
||||
flags.Parse([]string{"--confirm"})
|
||||
|
||||
args := []string{"bk-loc-1", "bk-loc-2"}
|
||||
e := o.Complete(f, args)
|
||||
assert.NoError(t, e)
|
||||
e = o.Validate(c, f, args)
|
||||
assert.NoError(t, e)
|
||||
c.SetArgs([]string{"bk-1", "--confirm"})
|
||||
e = c.Execute()
|
||||
assert.NoError(t, e)
|
||||
|
||||
e = Run(f, o)
|
||||
assert.NoError(t, e)
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewDeleteCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
stdout, _, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err == nil {
|
||||
assert.Contains(t, stdout, "No backup-locations found")
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backups by get()", err)
|
||||
|
||||
}
|
||||
func TestDeleteFunctions(t *testing.T) {
|
||||
//t.Run("create the other create command with fromSchedule option for Run() other branches", func(t *testing.T) {
|
||||
// create a factory
|
||||
f := &factorymocks.Factory{}
|
||||
client := &versionedmocks.Interface{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
bkList := velerov1api.BackupList{}
|
||||
bkrepoList := velerov1api.BackupRepositoryList{}
|
||||
t.Run("findAssociatedBackups", func(t *testing.T) {
|
||||
bkList, e := findAssociatedBackups(kbclient, "bk-loc-1", "ns1")
|
||||
assert.Equal(t, 0, len(bkList.Items))
|
||||
assert.NoError(t, e)
|
||||
})
|
||||
|
||||
t.Run("findAssociatedBackupRepos", func(t *testing.T) {
|
||||
bkrepoList, e := findAssociatedBackupRepos(kbclient, "bk-loc-1", "ns1")
|
||||
assert.Equal(t, 0, len(bkrepoList.Items))
|
||||
assert.NoError(t, e)
|
||||
})
|
||||
|
||||
t.Run("deleteBackups", func(t *testing.T) {
|
||||
bk := velerov1api.Backup{}
|
||||
bk.Name = "bk-name-last"
|
||||
bkList.Items = append(bkList.Items, bk)
|
||||
errList := deleteBackups(kbclient, bkList)
|
||||
assert.Equal(t, 1, len(errList))
|
||||
assert.Contains(t, errList[0].Error(), fmt.Sprintf("delete backup \"%s\" associated with deleted BSL: backups.velero.io \"%s\" not found", bk.Name, bk.Name))
|
||||
})
|
||||
t.Run("deleteBackupRepos", func(t *testing.T) {
|
||||
bkrepo := velerov1api.BackupRepository{}
|
||||
bkrepo.Name = "bk-repo-name-last"
|
||||
bkrepoList.Items = append(bkrepoList.Items, bkrepo)
|
||||
errList := deleteBackupRepos(kbclient, bkrepoList)
|
||||
assert.Equal(t, 1, len(errList))
|
||||
assert.Contains(t, errList[0].Error(), fmt.Sprintf("delete backup repository \"%s\" associated with deleted BSL: backuprepositories.velero.io \"%s\" not found", bkrepo.Name, bkrepo.Name))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
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 backuplocation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestNewGetCommand(t *testing.T) {
|
||||
bkList := []string{"b1", "b2"}
|
||||
|
||||
f := &factorymocks.Factory{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
// get command
|
||||
c := NewGetCommand(f, "velero backup-location get")
|
||||
assert.Equal(t, "Get backup storage locations", c.Short)
|
||||
|
||||
c.Execute()
|
||||
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
c.SetArgs([]string{"b1", "b2", "--default"})
|
||||
c.Execute()
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestNewGetCommand"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
_, stderr, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err != nil {
|
||||
assert.Contains(t, stderr, fmt.Sprintf("backupstoragelocations.velero.io \"%s\" not found", bkList[0]))
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backup delete successfully", err)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
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 backuplocation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
cmdtest "github.com/vmware-tanzu/velero/pkg/cmd/test"
|
||||
veleroflag "github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
|
||||
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
factorymocks "github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
versionedmocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/scheme"
|
||||
velerov1mocks "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1/mocks"
|
||||
veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec"
|
||||
)
|
||||
|
||||
func TestNewSetCommand(t *testing.T) {
|
||||
backupName := "arg2"
|
||||
// create a config for factory
|
||||
f := &factorymocks.Factory{}
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
// create command
|
||||
c := NewSetCommand(f, "")
|
||||
assert.Equal(t, "Set specific features for a backup storage location", c.Short)
|
||||
|
||||
// create a SetOptions with full options set and then run this backup command
|
||||
cacert := "a/b/c/ut-cert.ca"
|
||||
defaultBackupStorageLocation := true
|
||||
credential := veleroflag.NewMap()
|
||||
credential.Set("secret=a")
|
||||
|
||||
flags := new(flag.FlagSet)
|
||||
o := NewSetOptions()
|
||||
o.BindFlags(flags)
|
||||
|
||||
flags.Parse([]string{"--cacert", cacert})
|
||||
flags.Parse([]string{"--credential", credential.String()})
|
||||
flags.Parse([]string{"--default"})
|
||||
|
||||
args := []string{backupName}
|
||||
o.Complete(args, f)
|
||||
e := o.Validate(c, args, f)
|
||||
assert.Equal(t, e, nil)
|
||||
|
||||
e = o.Run(c, f)
|
||||
assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", cacert))
|
||||
|
||||
// verify all options are set as expected
|
||||
assert.Equal(t, backupName, o.Name)
|
||||
assert.Equal(t, cacert, o.CACertFile)
|
||||
assert.Equal(t, defaultBackupStorageLocation, o.DefaultBackupStorageLocation)
|
||||
assert.Equal(t, true, reflect.DeepEqual(credential, o.Credential))
|
||||
|
||||
assert.Contains(t, e.Error(), fmt.Sprintf("%s: no such file or directory", cacert))
|
||||
}
|
||||
|
||||
func TestSetCommand_Execute(t *testing.T) {
|
||||
bsl := "bsl-1"
|
||||
if os.Getenv(cmdtest.CaptureFlag) == "1" {
|
||||
// create a config for factory
|
||||
f := &factorymocks.Factory{}
|
||||
backups := &velerov1mocks.BackupInterface{}
|
||||
veleroV1 := &velerov1mocks.VeleroV1Interface{}
|
||||
client := &versionedmocks.Interface{}
|
||||
bk := &velerov1api.Backup{}
|
||||
kbclient := fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()
|
||||
|
||||
backups.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(bk, nil)
|
||||
veleroV1.On("Backups", mock.Anything).Return(backups, nil)
|
||||
client.On("VeleroV1").Return(veleroV1, nil)
|
||||
f.On("Client").Return(client, nil)
|
||||
f.On("Namespace").Return(mock.Anything)
|
||||
f.On("KubebuilderClient").Return(kbclient, nil)
|
||||
|
||||
// create command
|
||||
c := NewSetCommand(f, "velero backup-location set")
|
||||
c.SetArgs([]string{bsl})
|
||||
c.Execute()
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command(os.Args[0], []string{"-test.run=TestSetCommand_Execute"}...)
|
||||
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", cmdtest.CaptureFlag))
|
||||
_, stderr, err := veleroexec.RunCommand(cmd)
|
||||
|
||||
if err != nil {
|
||||
assert.Contains(t, stderr, fmt.Sprintf("backupstoragelocations.velero.io \"%s\" not found", bsl))
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want backup delete successfully", err)
|
||||
}
|
||||
@@ -125,18 +125,18 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
|
||||
f := flags.VarPF(&o.RestoreVolumes, "restore-volumes", "", "Whether to restore volumes from snapshots.")
|
||||
// this allows the user to just specify "--restore-volumes" as shorthand for "--restore-volumes=true"
|
||||
// like a normal bool flag
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.PreserveNodePorts, "preserve-nodeports", "", "Whether to preserve nodeports of Services when restoring.")
|
||||
// this allows the user to just specify "--preserve-nodeports" as shorthand for "--preserve-nodeports=true"
|
||||
// like a normal bool flag
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.IncludeClusterResources, "include-cluster-resources", "", "Include cluster-scoped resources in the restore.")
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
f = flags.VarPF(&o.AllowPartiallyFailed, "allow-partially-failed", "", "If using --from-schedule, whether to consider PartiallyFailed backups when looking for the most recent one. This flag has no effect if not using --from-schedule.")
|
||||
f.NoOptDefVal = "true"
|
||||
f.NoOptDefVal = cmd.TRUE
|
||||
|
||||
flags.BoolVarP(&o.Wait, "wait", "w", o.Wait, "Wait for the operation to complete.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user