@@ -18,6 +18,7 @@ package volumesnapshot
1818
1919import (
2020 "fmt"
21+ "strconv"
2122
2223 storagesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2324 v1 "k8s.io/api/core/v1"
@@ -35,17 +36,44 @@ import (
3536 . "github.com/onsi/gomega"
3637)
3738
39+ var _ = Describe ("getSnapshotName" , func () {
40+ It ("should return only the backup name when the role is PVCRolePgData" , func () {
41+ name , err := getSnapshotName ("backup123" , utils .PVCRolePgData )
42+ Expect (err ).NotTo (HaveOccurred ())
43+ Expect (name ).To (Equal ("backup123" ))
44+ })
45+
46+ It ("should return only the backup name when the role is an empty string" , func () {
47+ name , err := getSnapshotName ("backup123" , "" )
48+ Expect (err ).NotTo (HaveOccurred ())
49+ Expect (name ).To (Equal ("backup123" ))
50+ })
51+
52+ It ("should append '-wal' to the backup name when the role is PVCRolePgWal" , func () {
53+ name , err := getSnapshotName ("backup123" , utils .PVCRolePgWal )
54+ Expect (err ).NotTo (HaveOccurred ())
55+ Expect (name ).To (Equal ("backup123-wal" ))
56+ })
57+
58+ It ("should return an error for unhandled PVCRole types" , func () {
59+ _ , err := getSnapshotName ("backup123" , "UNKNOWN_ROLE" )
60+ Expect (err ).To (HaveOccurred ())
61+ Expect (err .Error ()).To (Equal ("unhandled PVCRole type: UNKNOWN_ROLE" ))
62+ })
63+ })
64+
3865var _ = Describe ("Volumesnapshot reconciler" , func () {
3966 const (
4067 namespace = "test-namespace"
4168 clusterName = "clusterName"
4269 backupName = "theBakcup"
4370 )
4471 var (
45- cluster * apiv1.Cluster
46- targetPod * v1.Pod
47- pvcs []v1.PersistentVolumeClaim
48- backup * apiv1.Backup
72+ cluster * apiv1.Cluster
73+ targetPod * v1.Pod
74+ pvcs []v1.PersistentVolumeClaim
75+ backup * apiv1.Backup
76+ backupCounter = 1
4977 )
5078
5179 BeforeEach (func () {
@@ -75,21 +103,28 @@ var _ = Describe("Volumesnapshot reconciler", func() {
75103 ObjectMeta : metav1.ObjectMeta {
76104 Name : clusterName + "-2" ,
77105 Namespace : namespace ,
106+ Labels : map [string ]string {
107+ utils .PvcRoleLabelName : string (utils .PVCRolePgData ),
108+ },
78109 },
79110 },
80111 {
81112 ObjectMeta : metav1.ObjectMeta {
82113 Name : clusterName + "-2-wal" ,
83114 Namespace : namespace ,
115+ Labels : map [string ]string {
116+ utils .PvcRoleLabelName : string (utils .PVCRolePgWal ),
117+ },
84118 },
85119 },
86120 }
87121 backup = & apiv1.Backup {
88122 ObjectMeta : metav1.ObjectMeta {
89123 Namespace : namespace ,
90- Name : backupName ,
124+ Name : backupName + strconv . Itoa ( backupCounter ) ,
91125 },
92126 }
127+ backupCounter ++
93128 })
94129
95130 It ("should fence the target pod when there are no volumesnapshots" , func (ctx SpecContext ) {
@@ -128,18 +163,18 @@ var _ = Describe("Volumesnapshot reconciler", func() {
128163 {
129164 ObjectMeta : metav1.ObjectMeta {
130165 Namespace : namespace ,
131- Name : clusterName + "-2" ,
166+ Name : backup . Name ,
132167 Labels : map [string ]string {
133- utils .BackupNameLabelName : backupName ,
168+ utils .BackupNameLabelName : backup . Name ,
134169 },
135170 },
136171 },
137172 {
138173 ObjectMeta : metav1.ObjectMeta {
139174 Namespace : namespace ,
140- Name : clusterName + "-2 -wal" ,
175+ Name : backup . Name + "-wal" ,
141176 Labels : map [string ]string {
142- utils .BackupNameLabelName : backupName ,
177+ utils .BackupNameLabelName : backup . Name ,
143178 },
144179 },
145180 },
@@ -178,9 +213,9 @@ var _ = Describe("Volumesnapshot reconciler", func() {
178213 {
179214 ObjectMeta : metav1.ObjectMeta {
180215 Namespace : namespace ,
181- Name : clusterName + "-2" ,
216+ Name : backup . Name ,
182217 Labels : map [string ]string {
183- utils .BackupNameLabelName : backupName ,
218+ utils .BackupNameLabelName : backup . Name ,
184219 },
185220 },
186221 Status : & storagesnapshotv1.VolumeSnapshotStatus {
@@ -191,9 +226,9 @@ var _ = Describe("Volumesnapshot reconciler", func() {
191226 {
192227 ObjectMeta : metav1.ObjectMeta {
193228 Namespace : namespace ,
194- Name : clusterName + "-2 -wal" ,
229+ Name : backup . Name + "-wal" ,
195230 Labels : map [string ]string {
196- utils .BackupNameLabelName : backupName ,
231+ utils .BackupNameLabelName : backup . Name ,
197232 },
198233 },
199234 Status : & storagesnapshotv1.VolumeSnapshotStatus {
0 commit comments