@@ -12,6 +12,7 @@ package resource_test
1212import (
1313 b64 "encoding/base64"
1414
15+ "gopkg.in/ini.v1"
1516 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1718
@@ -44,41 +45,58 @@ var _ = Describe("AdminSecret", func() {
4445 })
4546
4647 Context ("Build with defaults" , func () {
47- BeforeEach (func () {
48+ It ("creates the necessary admin secret" , func () {
49+ var username []byte
50+ var password []byte
51+ var ok bool
52+
4853 obj , err := adminSecretBuilder .Build ()
4954 Expect (err ).NotTo (HaveOccurred ())
5055 secret = obj .(* corev1.Secret )
51- })
5256
53- It ("creates the secret with correct name and namespace" , func () {
54- Expect (secret .Name ).To (Equal (instance .ChildResourceName ("admin" )))
55- Expect (secret .Namespace ).To (Equal ("a namespace" ))
56- })
57-
58- It ("creates a 'opaque' secret " , func () {
59- Expect (secret .Type ).To (Equal (corev1 .SecretTypeOpaque ))
60- })
61-
62- It ("creates a rabbitmq username that is base64 encoded and 24 characters in length" , func () {
63- username , ok := secret .Data ["username" ]
64- Expect (ok ).NotTo (BeFalse ())
65- decodedUsername , err := b64 .URLEncoding .DecodeString (string (username ))
66- Expect (err ).NotTo (HaveOccurred ())
67- Expect (len (decodedUsername )).To (Equal (24 ))
68-
69- })
70-
71- It ("creates a rabbitmq password that is base64 encoded and 24 characters in length" , func () {
72- password , ok := secret .Data ["password" ]
73- Expect (ok ).NotTo (BeFalse ())
74- decodedPassword , err := b64 .URLEncoding .DecodeString (string (password ))
75- Expect (err ).NotTo (HaveOccurred ())
76- Expect (len (decodedPassword )).To (Equal (24 ))
57+ By ("creating the secret with correct name and namespace" , func () {
58+ Expect (secret .Name ).To (Equal (instance .ChildResourceName ("admin" )))
59+ Expect (secret .Namespace ).To (Equal ("a namespace" ))
60+ })
61+
62+ By ("creating a 'opaque' secret " , func () {
63+ Expect (secret .Type ).To (Equal (corev1 .SecretTypeOpaque ))
64+ })
65+
66+ By ("creating a rabbitmq username that is base64 encoded and 24 characters in length" , func () {
67+ username , ok = secret .Data ["username" ]
68+ Expect (ok ).NotTo (BeFalse (), "Failed to find a key \" username\" in the generated Secret" )
69+ decodedUsername , err := b64 .URLEncoding .DecodeString (string (username ))
70+ Expect (err ).NotTo (HaveOccurred ())
71+ Expect (len (decodedUsername )).To (Equal (24 ))
72+ })
73+
74+ By ("creating a rabbitmq password that is base64 encoded and 24 characters in length" , func () {
75+ password , ok = secret .Data ["password" ]
76+ Expect (ok ).NotTo (BeFalse (), "Failed to find a key \" password\" in the generated Secret" )
77+ decodedPassword , err := b64 .URLEncoding .DecodeString (string (password ))
78+ Expect (err ).NotTo (HaveOccurred ())
79+ Expect (len (decodedPassword )).To (Equal (24 ))
80+ })
81+
82+ By ("creating a default_user.conf file that contains the correct sysctl config format to be parsed by RabbitMQ" , func () {
83+ defaultUserConf , ok := secret .Data ["default_user.conf" ]
84+ Expect (ok ).NotTo (BeFalse (), "Failed to find a key \" default_user.conf\" in the generated Secret" )
85+
86+ cfg , err := ini .Load (defaultUserConf )
87+ Expect (err ).NotTo (HaveOccurred ())
88+
89+ Expect (cfg .Section ("" ).HasKey ("default_user" )).To (BeTrue ())
90+ Expect (cfg .Section ("" ).HasKey ("default_pass" )).To (BeTrue ())
91+
92+ Expect (cfg .Section ("" ).Key ("default_user" ).Value ()).To (Equal (string (username )))
93+ Expect (cfg .Section ("" ).Key ("default_pass" ).Value ()).To (Equal (string (password )))
94+ })
7795 })
7896 })
7997
8098 Context ("Update with instance labels" , func () {
81- BeforeEach ( func () {
99+ It ( "Updates the secret" , func () {
82100 instance = rabbitmqv1beta1.RabbitmqCluster {
83101 ObjectMeta : metav1.ObjectMeta {
84102 Name : "rabbit-labelled" ,
@@ -102,26 +120,26 @@ var _ = Describe("AdminSecret", func() {
102120 }
103121 err := adminSecretBuilder .Update (secret )
104122 Expect (err ).NotTo (HaveOccurred ())
105- })
106123
107- It ( "adds new labels from the CR" , func () {
108- testLabels (secret .Labels )
109- })
124+ By ( "adding new labels from the CR" , func () {
125+ testLabels (secret .Labels )
126+ })
110127
111- It ( "restores the default labels" , func () {
112- labels := secret .Labels
113- Expect (labels ["app.kubernetes.io/name" ]).To (Equal (instance .Name ))
114- Expect (labels ["app.kubernetes.io/component" ]).To (Equal ("rabbitmq" ))
115- Expect (labels ["app.kubernetes.io/part-of" ]).To (Equal ("rabbitmq" ))
116- })
128+ By ( "restoring the default labels" , func () {
129+ labels := secret .Labels
130+ Expect (labels ["app.kubernetes.io/name" ]).To (Equal (instance .Name ))
131+ Expect (labels ["app.kubernetes.io/component" ]).To (Equal ("rabbitmq" ))
132+ Expect (labels ["app.kubernetes.io/part-of" ]).To (Equal ("rabbitmq" ))
133+ })
117134
118- It ("deletes the labels that are removed from the CR" , func () {
119- Expect (secret .Labels ).NotTo (HaveKey ("this-was-the-previous-label" ))
135+ By ("deleting the labels that are removed from the CR" , func () {
136+ Expect (secret .Labels ).NotTo (HaveKey ("this-was-the-previous-label" ))
137+ })
120138 })
121139 })
122140
123141 Context ("Update with instance annotations" , func () {
124- BeforeEach ( func () {
142+ It ( "updates the secret with the annotations" , func () {
125143 instance = rabbitmqv1beta1.RabbitmqCluster {
126144 ObjectMeta : metav1.ObjectMeta {
127145 Name : "rabbit-labelled" ,
@@ -150,19 +168,19 @@ var _ = Describe("AdminSecret", func() {
150168 }
151169 err := adminSecretBuilder .Update (secret )
152170 Expect (err ).NotTo (HaveOccurred ())
153- })
154-
155- It ("updates secret annotations on admin secret" , func () {
156- expectedAnnotations := map [string ]string {
157- "my-annotation" : "i-like-this" ,
158- "i-was-here-already" : "please-dont-delete-me" ,
159- "im-here-to-stay.kubernetes.io" : "for-a-while" ,
160- "kubernetes.io/name" : "should-stay" ,
161- "kubectl.kubernetes.io/name" : "should-stay" ,
162- "k8s.io/name" : "should-stay" ,
163- }
164171
165- Expect (secret .Annotations ).To (Equal (expectedAnnotations ))
172+ By ("updating secret annotations on admin secret" , func () {
173+ expectedAnnotations := map [string ]string {
174+ "my-annotation" : "i-like-this" ,
175+ "i-was-here-already" : "please-dont-delete-me" ,
176+ "im-here-to-stay.kubernetes.io" : "for-a-while" ,
177+ "kubernetes.io/name" : "should-stay" ,
178+ "kubectl.kubernetes.io/name" : "should-stay" ,
179+ "k8s.io/name" : "should-stay" ,
180+ }
181+
182+ Expect (secret .Annotations ).To (Equal (expectedAnnotations ))
183+ })
166184 })
167185 })
168186})
0 commit comments