Files
velero/pkg/cmd/cli/backup/create_test.go
Adnan Abdulhussein 6aa0215137 create backups from schedules using velero create backup (#1734)
* add --from-schedule to `velero backup create` to create backups from schedules

Signed-off-by: Adnan Abdulhussein <aadnan@vmware.com>
2019-08-23 13:03:51 -07:00

89 lines
2.8 KiB
Go

/*
Copyright 2019 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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/builder"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
)
const testNamespace = "velero"
func TestCreateOptions_BuildBackup(t *testing.T) {
o := NewCreateOptions()
o.Labels.Set("velero.io/test=true")
backup, err := o.BuildBackup(testNamespace)
assert.NoError(t, err)
assert.Equal(t, velerov1api.BackupSpec{
TTL: metav1.Duration{Duration: o.TTL},
IncludedNamespaces: []string(o.IncludeNamespaces),
SnapshotVolumes: o.SnapshotVolumes.Value,
IncludeClusterResources: o.IncludeClusterResources.Value,
}, backup.Spec)
assert.Equal(t, map[string]string{
"velero.io/test": "true",
}, backup.GetLabels())
}
func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
o := NewCreateOptions()
o.FromSchedule = "test"
o.client = fake.NewSimpleClientset()
t.Run("inexistant schedule", func(t *testing.T) {
_, err := o.BuildBackup(testNamespace)
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")).Result()
o.client.VeleroV1().Schedules(testNamespace).Create(schedule)
t.Run("existing schedule", func(t *testing.T) {
backup, err := o.BuildBackup(testNamespace)
assert.NoError(t, err)
assert.Equal(t, expectedBackupSpec, backup.Spec)
assert.Equal(t, map[string]string{
"velero.io/test": "true",
velerov1api.ScheduleNameLabel: "test",
}, backup.GetLabels())
})
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)
assert.NoError(t, err)
assert.Equal(t, expectedBackupSpec, backup.Spec)
assert.Equal(t, map[string]string{
"velero.io/test": "yes",
velerov1api.ScheduleNameLabel: "test",
"custom-label": "true",
}, backup.GetLabels())
})
}