Skip to content

Commit

Permalink
Merge pull request #186 from blackpiglet/remove_hardcode_testing
Browse files Browse the repository at this point in the history
Replace hardcode testing setup with added builder methods.
  • Loading branch information
sseago authored Jun 30, 2023
2 parents aa8c2b7 + 6b19c6c commit fc74662
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 61 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.2
// TODO: need to use velero v1.12.0 after releasing.
github.com/vmware-tanzu/velero v0.0.0-20230620063525-6f3adcf7282b
github.com/vmware-tanzu/velero v0.0.0-20230630063030-84eca51d22c7
k8s.io/api v0.25.6
k8s.io/apimachinery v0.25.6
k8s.io/client-go v0.25.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
github.com/tg123/go-htpasswd v1.2.1 h1:i4wfsX1KvvkyoMiHZzjS0VzbAPWfxzI8INcZAKtutoU=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/vmware-tanzu/velero v0.0.0-20230620063525-6f3adcf7282b h1:mrsiuap4DAaF5ukPvJrFAiELcuHmLF2QVswJpIyVSP8=
github.com/vmware-tanzu/velero v0.0.0-20230620063525-6f3adcf7282b/go.mod h1:URz7drKyNV58T6MuVdDH/iATs3/DorJI+IEVPZwdHZE=
github.com/vmware-tanzu/velero v0.0.0-20230630063030-84eca51d22c7 h1:w/KPz4EQwQaaXes4gEgjrFrvygVSjjUaDQnm7VggtZ8=
github.com/vmware-tanzu/velero v0.0.0-20230630063030-84eca51d22c7/go.mod h1:URz7drKyNV58T6MuVdDH/iATs3/DorJI+IEVPZwdHZE=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down
64 changes: 12 additions & 52 deletions internal/restore/pvc_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,14 @@ func TestExecute(t *testing.T) {
expectedErr: "Failed to get Volumesnapshot velero/testVS to restore PVC velero/testPVC: volumesnapshots.snapshot.storage.k8s.io \"testVS\" not found",
},
{
name: "Restore from VolumeSnapshot",
backup: builder.ForBackup("velero", "testBackup").Result(),
restore: builder.ForRestore("velero", "testRestore").Backup("testBackup").Result(),
pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotLabel, "testVS")).Result(),
name: "Restore from VolumeSnapshot",
backup: builder.ForBackup("velero", "testBackup").Result(),
restore: builder.ForRestore("velero", "testRestore").Backup("testBackup").Result(),
pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotLabel, "testVS")).
RequestResource(map[corev1api.ResourceName]resource.Quantity{corev1api.ResourceStorage: resource.MustParse("10Gi")}).
DataSource(&corev1api.TypedLocalObjectReference{APIGroup: &snapshotv1api.SchemeGroupVersion.Group, Kind: util.VolumeSnapshotKindName, Name: "testVS"}).
DataSourceRef(&corev1api.TypedLocalObjectReference{APIGroup: &snapshotv1api.SchemeGroupVersion.Group, Kind: util.VolumeSnapshotKindName, Name: "testVS"}).
Result(),
vs: builder.ForVolumeSnapshot("velero", "testVS").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi")).Result(),
expectedPVC: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations("velero.io/volume-snapshot-name", "testVS")).Result(),
},
Expand All @@ -575,6 +579,10 @@ func TestExecute(t *testing.T) {
pvc: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations(util.VolumeSnapshotRestoreSize, "10Gi")).Result(),
dataUploadResult: builder.ForConfigMap("velero", "testCM").Data("uid", "{}").ObjectMeta(builder.WithLabels(velerov1api.RestoreUIDLabel, "uid", util.PVCNamespaceNameLabel, "velero.testPVC")).Result(),
expectedPVC: builder.ForPersistentVolumeClaim("velero", "testPVC").ObjectMeta(builder.WithAnnotations("velero.io/vsi-volumesnapshot-restore-size", "10Gi")).Result(),
expectedDataDownload: builder.ForDataDownload("velero", "").TargetVolume(velerov2alpha1.TargetVolumeSpec{PVC: "testPVC", Namespace: "velero"}).
ObjectMeta(builder.WithOwnerReference([]metav1.OwnerReference{{APIVersion: velerov1api.SchemeGroupVersion.String(), Kind: "Restore", Name: "testRestore", UID: "uid", Controller: boolptr.True()}}),
builder.WithLabelsMap(map[string]string{util.AsyncOperationIDLabel: "dd-uid.", velerov1api.RestoreNameLabel: "testRestore", velerov1api.RestoreUIDLabel: "uid"}),
builder.WithGenerateName("testRestore-")).Result(),
},
}

Expand All @@ -588,54 +596,6 @@ func TestExecute(t *testing.T) {
}
input := new(velero.RestoreItemActionExecuteInput)

// TODO: after needed Velero builder methods are added, need to remove this block.
if tc.name == "Restore from VolumeSnapshot" {
requestMemory, _ := resource.ParseQuantity("10Gi")
tc.expectedPVC.Spec.Resources.Requests = make(map[corev1api.ResourceName]resource.Quantity)
tc.expectedPVC.Spec.Resources.Requests[corev1api.ResourceStorage] = requestMemory
tc.expectedPVC.Spec.DataSource = &corev1api.TypedLocalObjectReference{
APIGroup: &snapshotv1api.SchemeGroupVersion.Group,
Kind: util.VolumeSnapshotKindName,
Name: "testVS",
}
tc.expectedPVC.Spec.DataSourceRef = &corev1api.TypedLocalObjectReference{
APIGroup: &snapshotv1api.SchemeGroupVersion.Group,
Kind: util.VolumeSnapshotKindName,
Name: "testVS",
}
} else if tc.name == "Restore from DataUploadResult" {
tc.expectedDataDownload = &velerov2alpha1.DataDownload{
TypeMeta: metav1.TypeMeta{
Kind: "DataDownload",
APIVersion: velerov2alpha1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: tc.restore.Name + "-",
Namespace: "velero",
Labels: map[string]string{
util.AsyncOperationIDLabel: "dd-uid.",
velerov1api.RestoreNameLabel: tc.restore.Name,
velerov1api.RestoreUIDLabel: string(tc.restore.UID),
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: velerov1api.SchemeGroupVersion.String(),
Kind: "Restore",
Name: tc.restore.Name,
UID: tc.restore.UID,
Controller: boolptr.True(),
},
},
},
Spec: velerov2alpha1.DataDownloadSpec{
TargetVolume: velerov2alpha1.TargetVolumeSpec{
PVC: tc.pvc.Name,
Namespace: tc.pvc.Namespace,
},
},
}
}

if tc.pvc != nil {
pvcMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.pvc)
require.NoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1508,23 +1508,23 @@ func TestDeleteVolumeSnapshots(t *testing.T) {
{
name: "VS is ReadyToUse, and VS has corresponding VSC. VS should be deleted.",
vs: *builder.ForVolumeSnapshot("velero", "vs1").ObjectMeta(builder.WithLabels("testing-vs", "vs1")).Status().BoundVolumeSnapshotContentName("vsc1").Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status().Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status(&snapshotv1api.VolumeSnapshotContentStatus{}).Result(),
expectedVS: snapshotv1api.VolumeSnapshot{},
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentRetain).VolumeSnapshotRef("ns-", "name-").Status().Result(),
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentRetain).VolumeSnapshotRef("ns-", "name-").Result(),
},
{
name: "VS is ReadyToUse, and VS has corresponding VSC. Concurrent test.",
vs: *builder.ForVolumeSnapshot("velero", "vs1").ObjectMeta(builder.WithLabels("testing-vs", "vs1")).Status().BoundVolumeSnapshotContentName("vsc1").Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status().Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status(&snapshotv1api.VolumeSnapshotContentStatus{}).Result(),
expectedVS: snapshotv1api.VolumeSnapshot{},
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentRetain).VolumeSnapshotRef("ns-", "name-").Status().Result(),
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentRetain).VolumeSnapshotRef("ns-", "name-").Result(),
},
{
name: "VS status is nil. VSC should not be modified.",
vs: *builder.ForVolumeSnapshot("velero", "vs1").ObjectMeta(builder.WithLabels("testing-vs", "vs1")).Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status().Result(),
vsc: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status(&snapshotv1api.VolumeSnapshotContentStatus{}).Result(),
expectedVS: snapshotv1api.VolumeSnapshot{},
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Status().Result(),
expectedVSC: *builder.ForVolumeSnapshotContent("vsc1").DeletionPolicy(snapshotv1api.VolumeSnapshotContentDelete).Result(),
},
}

Expand Down

0 comments on commit fc74662

Please sign in to comment.