|
| 1 | +package volumesnapshot |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/appscode/go/strings" |
| 9 | + type_util "github.com/appscode/go/types" |
| 10 | + crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1" |
| 11 | + vsfake "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/fake" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/runtime" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + "stash.appscode.dev/stash/apis" |
| 17 | + "stash.appscode.dev/stash/apis/stash/v1alpha1" |
| 18 | + "stash.appscode.dev/stash/apis/stash/v1beta1" |
| 19 | +) |
| 20 | + |
| 21 | +type snapInfo struct { |
| 22 | + name string |
| 23 | + creationTime string |
| 24 | + pvcName string |
| 25 | +} |
| 26 | + |
| 27 | +type testInfo struct { |
| 28 | + description string |
| 29 | + policy v1alpha1.RetentionPolicy |
| 30 | + hostBackupStats []v1beta1.HostBackupStats |
| 31 | + expectedSnapshots []string |
| 32 | +} |
| 33 | + |
| 34 | +const testNamespace = "vs-retention-policy-test" |
| 35 | + |
| 36 | +func TestCleanupSnapshots(t *testing.T) { |
| 37 | + |
| 38 | + snapMeta := []snapInfo{ |
| 39 | + {name: "snap-1", creationTime: "2019-12-10T05:36:07Z", pvcName: "pvc-1"}, |
| 40 | + {name: "snap-2", creationTime: "2019-11-10T05:36:07Z", pvcName: "pvc-1"}, |
| 41 | + {name: "snap-3", creationTime: "2019-10-10T05:36:07Z", pvcName: "pvc-1"}, |
| 42 | + {name: "snap-4", creationTime: "2019-10-10T05:30:07Z", pvcName: "pvc-1"}, |
| 43 | + {name: "snap-5", creationTime: "2019-10-10T05:15:07Z", pvcName: "pvc-1"}, |
| 44 | + {name: "snap-6", creationTime: "2018-10-11T05:36:07Z", pvcName: "pvc-1"}, |
| 45 | + {name: "snap-7", creationTime: "2018-10-10T05:36:07Z", pvcName: "pvc-1"}, |
| 46 | + {name: "snap-8", creationTime: "2017-12-10T05:36:07Z", pvcName: "pvc-1"}, |
| 47 | + {name: "snap-9", creationTime: "2017-10-10T05:36:07Z", pvcName: "pvc-1"}, |
| 48 | + {name: "snap-10", creationTime: "2019-12-10T05:36:07Z", pvcName: "pvc-2"}, |
| 49 | + {name: "snap-11", creationTime: "2019-10-10T05:36:07Z", pvcName: "pvc-2"}, |
| 50 | + {name: "snap-12", creationTime: "2019-10-10T05:36:07Z", pvcName: "pvc-2"}, |
| 51 | + {name: "snap-13", creationTime: "2019-10-09T05:30:07Z", pvcName: "pvc-2"}, |
| 52 | + } |
| 53 | + |
| 54 | + testCases := []testInfo{ |
| 55 | + { |
| 56 | + description: "No Policy", |
| 57 | + policy: v1alpha1.RetentionPolicy{}, |
| 58 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 59 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-4", "snap-5", "snap-6", "snap-7", "snap-8", "snap-9", "snap-10", "snap-11", "snap-12", "snap-13"}, // should keep all snapshots |
| 60 | + }, |
| 61 | + { |
| 62 | + description: "KeepLast", |
| 63 | + policy: v1alpha1.RetentionPolicy{KeepLast: 3}, |
| 64 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 65 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-10", "snap-11", "snap-12"}, // should keep last 3 snapshots of claim 1 and last 3 snapshots of claim 2 |
| 66 | + }, |
| 67 | + { |
| 68 | + description: "KeepHourly", |
| 69 | + policy: v1alpha1.RetentionPolicy{KeepHourly: 3}, |
| 70 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 71 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-10", "snap-11", "snap-13"}, |
| 72 | + }, |
| 73 | + { |
| 74 | + description: "KeepDaily", |
| 75 | + policy: v1alpha1.RetentionPolicy{KeepDaily: 3}, |
| 76 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 77 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-10", "snap-11", "snap-13"}, |
| 78 | + }, |
| 79 | + { |
| 80 | + description: "KeepMonthly", |
| 81 | + policy: v1alpha1.RetentionPolicy{KeepMonthly: 3}, |
| 82 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 83 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-10", "snap-11"}, |
| 84 | + }, |
| 85 | + { |
| 86 | + description: "KeepYearly", |
| 87 | + policy: v1alpha1.RetentionPolicy{KeepYearly: 3}, |
| 88 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 89 | + expectedSnapshots: []string{"snap-1", "snap-6", "snap-8", "snap-10"}, |
| 90 | + }, |
| 91 | + { |
| 92 | + description: "KeepLast & KeepDaily", |
| 93 | + policy: v1alpha1.RetentionPolicy{KeepLast: 3, KeepDaily: 3}, |
| 94 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 95 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-10", "snap-11", "snap-12", "snap-13"}, |
| 96 | + }, |
| 97 | + { |
| 98 | + description: "KeepWeekly & KeepMonthly", |
| 99 | + policy: v1alpha1.RetentionPolicy{KeepWeekly: 2, KeepMonthly: 2}, |
| 100 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 101 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-10", "snap-11"}, |
| 102 | + }, |
| 103 | + { |
| 104 | + description: "KeepWeekly & KeepMonthly & KeepYearly", |
| 105 | + policy: v1alpha1.RetentionPolicy{KeepWeekly: 2, KeepMonthly: 3, KeepYearly: 4}, |
| 106 | + hostBackupStats: []v1beta1.HostBackupStats{{Hostname: "pvc-1"}, {Hostname: "pvc-2"}}, |
| 107 | + expectedSnapshots: []string{"snap-1", "snap-2", "snap-3", "snap-6", "snap-8", "snap-10", "snap-11"}, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, test := range testCases { |
| 112 | + t.Run(test.description, func(t *testing.T) { |
| 113 | + volumeSnasphots, err := getVolumeSnapshots(snapMeta) |
| 114 | + if err != nil { |
| 115 | + t.Errorf("Failed to generate VolumeSnasphots. Reason: %v", err) |
| 116 | + return |
| 117 | + } |
| 118 | + vsClient := vsfake.NewSimpleClientset(volumeSnasphots...) |
| 119 | + err = CleanupSnapshots(test.policy, test.hostBackupStats, testNamespace, vsClient) |
| 120 | + if err != nil { |
| 121 | + t.Errorf("Failed to cleanup VolumeSnapshots. Reason: %v", err) |
| 122 | + return |
| 123 | + } |
| 124 | + vsList, err := vsClient.SnapshotV1alpha1().VolumeSnapshots(testNamespace).List(metav1.ListOptions{}) |
| 125 | + if err != nil { |
| 126 | + t.Errorf("Failed to list remaining VolumeSnapshots. Reason: %v", err) |
| 127 | + return |
| 128 | + } |
| 129 | + if len(test.expectedSnapshots) != len(vsList.Items) { |
| 130 | + var remainingSnapshots []string |
| 131 | + for i := range vsList.Items { |
| 132 | + remainingSnapshots = append(remainingSnapshots, vsList.Items[i].Name) |
| 133 | + } |
| 134 | + |
| 135 | + t.Errorf("Remaining VolumeSnapshot number did not match with expected number."+ |
| 136 | + "\nExpected: %d"+ |
| 137 | + "\nFound: %d"+ |
| 138 | + "\nExpected Snapshots: %q"+ |
| 139 | + "\nRemaining Snapshots: %q", len(test.expectedSnapshots), len(vsList.Items), test.expectedSnapshots, remainingSnapshots) |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + for _, vs := range vsList.Items { |
| 144 | + if !strings.Contains(test.expectedSnapshots, vs.Name) { |
| 145 | + t.Errorf("VolumeSnapshot %s should be deleted according to retention-policy: %s.", vs.Name, test.description) |
| 146 | + } |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +func getVolumeSnapshots(snapMetas []snapInfo) ([]runtime.Object, error) { |
| 154 | + snapshots := make([]runtime.Object, 0) |
| 155 | + for i := range snapMetas { |
| 156 | + snapshot, err := newSnapshot(snapMetas[i]) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + snapshots = append(snapshots, snapshot) |
| 162 | + } |
| 163 | + return snapshots, nil |
| 164 | +} |
| 165 | + |
| 166 | +func newSnapshot(snapMeta snapInfo) (*crdv1.VolumeSnapshot, error) { |
| 167 | + creationTimestamp, err := time.Parse(time.RFC3339, snapMeta.creationTime) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + |
| 172 | + return &crdv1.VolumeSnapshot{ |
| 173 | + ObjectMeta: metav1.ObjectMeta{ |
| 174 | + Name: snapMeta.name, |
| 175 | + Namespace: testNamespace, |
| 176 | + UID: types.UID(snapMeta.name), |
| 177 | + ResourceVersion: "1", |
| 178 | + SelfLink: "/apis/snapshot.storage.k8s.io/v1alpha1/namespaces/" + testNamespace + "/volumesnapshots/" + snapMeta.name, |
| 179 | + CreationTimestamp: metav1.Time{Time: creationTimestamp}, |
| 180 | + }, |
| 181 | + Spec: crdv1.VolumeSnapshotSpec{ |
| 182 | + VolumeSnapshotClassName: type_util.StringP("standard"), |
| 183 | + SnapshotContentName: fmt.Sprintf("snapshot-content-%s", snapMeta.name), |
| 184 | + Source: &v1.TypedLocalObjectReference{ |
| 185 | + Name: snapMeta.pvcName, |
| 186 | + Kind: apis.KindPersistentVolumeClaim, |
| 187 | + }, |
| 188 | + }, |
| 189 | + Status: crdv1.VolumeSnapshotStatus{ |
| 190 | + ReadyToUse: true, |
| 191 | + Error: nil, |
| 192 | + }, |
| 193 | + }, nil |
| 194 | +} |
0 commit comments