@@ -55,4 +55,44 @@ var _ = Describe("Utils functions", func() {
5555 })
5656 })
5757 })
58+
59+ Context ("Calling GetLeaderElectionID" , func () {
60+ When ("operator's instance is not defined" , func () {
61+ It ("should return the default id" , func () {
62+ result := GetLeaderElectionID ("" )
63+ Expect (result ).To (Equal ("default-37a8eec1.managed-postgres-operator.hoppscale.com" ))
64+ Expect (len (result )).To (BeNumerically ("<=" , 63 ))
65+ })
66+ })
67+
68+ When ("operator's instance is defined and the name has a standard length" , func () {
69+ It ("should return the instance name with a unique id" , func () {
70+ result := GetLeaderElectionID ("foobar" )
71+ Expect (result ).To (Equal ("foobar-c3ab8ff1.managed-postgres-operator.hoppscale.com" ))
72+ Expect (len (result )).To (BeNumerically ("<=" , 63 ))
73+ })
74+ })
75+
76+ When ("operator's instance is defined and the name is too long" , func () {
77+ It ("should return the first few characters of the instance name and a unique ID" , func () {
78+ result := GetLeaderElectionID ("myverylonginstancename" )
79+ Expect (result ).To (Equal ("myverylonginst-3b8d6ea3.managed-postgres-operator.hoppscale.com" ))
80+ Expect (len (result )).To (BeNumerically ("<=" , 63 ))
81+ })
82+ })
83+
84+ When ("multiple instances begin with the same characters" , func () {
85+ It ("shouldn't cause a conflict" , func () {
86+ result1 := GetLeaderElectionID ("postgresinstance-dev" )
87+ result2 := GetLeaderElectionID ("postgresinstance-int" )
88+ result3 := GetLeaderElectionID ("postgresinstance-test" )
89+ result4 := GetLeaderElectionID ("postgresinstance-prod" )
90+
91+ Expect (result1 ).NotTo (BeElementOf ([]string {result2 , result3 , result4 }))
92+ Expect (result2 ).NotTo (BeElementOf ([]string {result1 , result3 , result4 }))
93+ Expect (result3 ).NotTo (BeElementOf ([]string {result1 , result2 , result4 }))
94+ Expect (result4 ).NotTo (BeElementOf ([]string {result1 , result2 , result3 }))
95+ })
96+ })
97+ })
5898})
0 commit comments