@@ -31,6 +31,76 @@ import (
3131)
3232
3333var _ = Describe ("SnapshotPod Controller" , func () {
34+ Context ("When testing withRandomTag function" , func () {
35+ It ("should return original tag when appendSnappedSuffix is false" , func () {
36+ By ("Testing with appendSnappedSuffix = false" )
37+
38+ // Test case 1: empty tag
39+ result := withRandomTag ("" , false )
40+ Expect (result ).To (Equal ("" ))
41+
42+ // Test case 2: latest tag
43+ result = withRandomTag ("latest" , false )
44+ Expect (result ).To (Equal ("latest" ))
45+
46+ // Test case 3: custom tag
47+ result = withRandomTag ("v1.0.0" , false )
48+ Expect (result ).To (Equal ("v1.0.0" ))
49+
50+ // Test case 4: tag with special characters
51+ result = withRandomTag ("release-2024-01" , false )
52+ Expect (result ).To (Equal ("release-2024-01" ))
53+ })
54+
55+ It ("should append snapped suffix when appendSnappedSuffix is true" , func () {
56+ By ("Testing with appendSnappedSuffix = true" )
57+
58+ // Test case 1: empty tag
59+ result := withRandomTag ("" , true )
60+ Expect (result ).To (ContainSubstring ("snapped-" ))
61+ Expect (result ).To (MatchRegexp (`^snapped-\d+-[a-f0-9]{8}$` ))
62+
63+ // Test case 2: latest tag
64+ result = withRandomTag ("latest" , true )
65+ Expect (result ).To (ContainSubstring ("snapped-" ))
66+ Expect (result ).To (MatchRegexp (`^snapped-\d+-[a-f0-9]{8}$` ))
67+
68+ // Test case 3: custom tag
69+ result = withRandomTag ("v1.0.0" , true )
70+ Expect (result ).To (ContainSubstring ("v1.0.0-snapped-" ))
71+ Expect (result ).To (MatchRegexp (`^v1\.0\.0-snapped-\d+-[a-f0-9]{8}$` ))
72+
73+ // Test case 4: tag with special characters
74+ result = withRandomTag ("release-2024-01" , true )
75+ Expect (result ).To (ContainSubstring ("release-2024-01-snapped-" ))
76+ Expect (result ).To (MatchRegexp (`^release-2024-01-snapped-\d+-[a-f0-9]{8}$` ))
77+ })
78+
79+ It ("should not append suffix to already snapped tags" , func () {
80+ By ("Testing with tags that already contain 'snapped-'" )
81+
82+ // Test case 1: already snapped tag
83+ alreadySnapped := "v1.0.0-snapped-1704067200-abc12345"
84+ result := withRandomTag (alreadySnapped , true )
85+ Expect (result ).To (Equal (alreadySnapped ))
86+
87+ // Test case 2: already snapped tag with appendSnappedSuffix = false
88+ result = withRandomTag (alreadySnapped , false )
89+ Expect (result ).To (Equal (alreadySnapped ))
90+ })
91+
92+ It ("should generate unique suffixes for different calls" , func () {
93+ By ("Testing that multiple calls generate different suffixes" )
94+
95+ result1 := withRandomTag ("test" , true )
96+ result2 := withRandomTag ("test" , true )
97+
98+ Expect (result1 ).NotTo (Equal (result2 ))
99+ Expect (result1 ).To (ContainSubstring ("test-snapped-" ))
100+ Expect (result2 ).To (ContainSubstring ("test-snapped-" ))
101+ })
102+ })
103+
34104 Context ("When reconciling a resource" , func () {
35105 const resourceName = "test-resource"
36106
0 commit comments