@@ -19,6 +19,7 @@ package refreshinterval
1919import (
2020 "bytes"
2121 "os/exec"
22+ "strings"
2223
2324 corev1 "k8s.io/api/core/v1"
2425 rbacv1 "k8s.io/api/rbac/v1"
@@ -32,6 +33,8 @@ import (
3233 . "github.com/onsi/gomega"
3334)
3435
36+ const csiDriverNamespace = "cert-manager"
37+
3538var _ = framework .CasesDescribe ("RefreshInterval" , func () {
3639 f := framework .NewDefaultFramework ("RefreshInterval" )
3740
@@ -136,6 +139,129 @@ var _ = framework.CasesDescribe("RefreshInterval", func() {
136139 Expect (cmd .Run ()).To (Succeed ())
137140 Expect (buf .Len ()).To (BeNumerically (">" , 0 ), "expected certificate file to not be empty" )
138141
142+ By ("Verifying CSI driver logs show 1h refresh interval" )
143+ logBuf := new (bytes.Buffer )
144+ logCmd := exec .Command (f .Config ().KubectlBinPath , "logs" , "-n" + csiDriverNamespace , "-l" , "app=csi-driver-athenz" , "-c" , "csi-driver-athenz" , "--tail=100" )
145+ logCmd .Stdout = logBuf
146+ logCmd .Stderr = GinkgoWriter
147+ Expect (logCmd .Run ()).To (Succeed ())
148+ Expect (strings .Contains (logBuf .String (), "refreshInterval\" =\" 1h0m0s\" " )).To (BeTrue (), "expected logs to show 1h refresh interval" )
149+
150+ By ("Cleaning up resources" )
151+ Expect (f .Client ().Delete (f .Context (), & pod )).NotTo (HaveOccurred ())
152+ Expect (f .Client ().Delete (f .Context (), & rolebinding )).NotTo (HaveOccurred ())
153+ Expect (f .Client ().Delete (f .Context (), & role )).NotTo (HaveOccurred ())
154+ Expect (f .Client ().Delete (f .Context (), & serviceAccount )).NotTo (HaveOccurred ())
155+ })
156+
157+ It ("should issue certificate with default refresh interval (24h)" , func () {
158+ By ("Creating service account, role, and rolebinding" )
159+
160+ serviceAccount := corev1.ServiceAccount {
161+ ObjectMeta : metav1.ObjectMeta {
162+ Name : "athenz.default-refresh-test" ,
163+ Namespace : f .Namespace .Name ,
164+ },
165+ }
166+ Expect (f .Client ().Create (f .Context (), & serviceAccount )).NotTo (HaveOccurred ())
167+
168+ role := rbacv1.Role {
169+ ObjectMeta : metav1.ObjectMeta {
170+ Name : "default-refresh-test" ,
171+ Namespace : f .Namespace .Name ,
172+ },
173+ Rules : []rbacv1.PolicyRule {{
174+ Verbs : []string {"create" },
175+ APIGroups : []string {"cert-manager.io" },
176+ Resources : []string {"certificaterequests" },
177+ }},
178+ }
179+ Expect (f .Client ().Create (f .Context (), & role )).NotTo (HaveOccurred ())
180+
181+ rolebinding := rbacv1.RoleBinding {
182+ ObjectMeta : metav1.ObjectMeta {
183+ Name : "default-refresh-test" ,
184+ Namespace : f .Namespace .Name ,
185+ },
186+ RoleRef : rbacv1.RoleRef {
187+ APIGroup : "rbac.authorization.k8s.io" ,
188+ Kind : "Role" ,
189+ Name : role .Name ,
190+ },
191+ Subjects : []rbacv1.Subject {{
192+ Kind : "ServiceAccount" ,
193+ Name : serviceAccount .Name ,
194+ Namespace : f .Namespace .Name ,
195+ }},
196+ }
197+ Expect (f .Client ().Create (f .Context (), & rolebinding )).NotTo (HaveOccurred ())
198+
199+ By ("Creating pod without refresh-interval (should use default 24h)" )
200+ pod := corev1.Pod {
201+ ObjectMeta : metav1.ObjectMeta {
202+ Name : "default-refresh-interval-test" ,
203+ Namespace : f .Namespace .Name ,
204+ },
205+ Spec : corev1.PodSpec {
206+ Volumes : []corev1.Volume {{
207+ Name : "csi-driver-athenz" ,
208+ VolumeSource : corev1.VolumeSource {
209+ CSI : & corev1.CSIVolumeSource {
210+ Driver : "csi.cert-manager.athenz.io" ,
211+ ReadOnly : pointer .Bool (true ),
212+ // No refresh-interval specified - should use default 24h
213+ VolumeAttributes : map [string ]string {},
214+ },
215+ },
216+ }},
217+ ServiceAccountName : "athenz.default-refresh-test" ,
218+ Containers : []corev1.Container {
219+ {
220+ Name : "my-container" ,
221+ Image : "busybox" ,
222+ Command : []string {"sleep" , "10000" },
223+ VolumeMounts : []corev1.VolumeMount {
224+ {
225+ Name : "csi-driver-athenz" ,
226+ MountPath : "/var/run/secrets/athenz.io" ,
227+ },
228+ },
229+ },
230+ },
231+ },
232+ }
233+ Expect (f .Client ().Create (f .Context (), & pod )).NotTo (HaveOccurred ())
234+
235+ By ("Waiting for pod to become ready" )
236+ Eventually (func () bool {
237+ var p corev1.Pod
238+ Expect (f .Client ().Get (f .Context (), client.ObjectKey {Namespace : f .Namespace .Name , Name : pod .Name }, & p )).NotTo (HaveOccurred ())
239+
240+ for _ , c := range p .Status .Conditions {
241+ if c .Type == corev1 .PodReady {
242+ return c .Status == corev1 .ConditionTrue
243+ }
244+ }
245+
246+ return false
247+ }, "60s" , "1s" ).Should (BeTrue (), "expected pod to become ready in time" )
248+
249+ By ("Verifying certificate was issued" )
250+ buf := new (bytes.Buffer )
251+ cmd := exec .Command (f .Config ().KubectlBinPath , "exec" , "-n" + f .Namespace .Name , pod .Name , "-cmy-container" , "--" , "cat" , "/var/run/secrets/athenz.io/tls.crt" )
252+ cmd .Stdout = buf
253+ cmd .Stderr = GinkgoWriter
254+ Expect (cmd .Run ()).To (Succeed ())
255+ Expect (buf .Len ()).To (BeNumerically (">" , 0 ), "expected certificate file to not be empty" )
256+
257+ By ("Verifying CSI driver logs show 24h refresh interval (default)" )
258+ logBuf := new (bytes.Buffer )
259+ logCmd := exec .Command (f .Config ().KubectlBinPath , "logs" , "-n" + csiDriverNamespace , "-l" , "app=csi-driver-athenz" , "-c" , "csi-driver-athenz" , "--tail=100" )
260+ logCmd .Stdout = logBuf
261+ logCmd .Stderr = GinkgoWriter
262+ Expect (logCmd .Run ()).To (Succeed ())
263+ Expect (strings .Contains (logBuf .String (), "refreshInterval\" =\" 24h0m0s\" " )).To (BeTrue (), "expected logs to show 24h refresh interval (default)" )
264+
139265 By ("Cleaning up resources" )
140266 Expect (f .Client ().Delete (f .Context (), & pod )).NotTo (HaveOccurred ())
141267 Expect (f .Client ().Delete (f .Context (), & rolebinding )).NotTo (HaveOccurred ())
0 commit comments