@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"os/exec"
24
24
"path/filepath"
25
+ "strings"
25
26
"time"
26
27
27
28
. "github.com/onsi/ginkgo/v2"
@@ -297,14 +298,52 @@ var _ = Describe("Manager", Ordered, func() {
297
298
298
299
// +kubebuilder:scaffold:e2e-webhooks-checks
299
300
300
- // TODO: Customize the e2e test suite with scenarios specific to your project.
301
- // Consider applying sample/CR(s) and check their status and/or verifying
302
- // the reconciliation by using the metrics, i.e.:
303
- // metricsOutput := getMetricsOutput()
304
- // Expect(metricsOutput).To(ContainSubstring(
305
- // fmt.Sprintf(`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
306
- // strings.ToLower(<Kind>),
307
- // ))
301
+ It ("should create ServiceAccount and roles when WorkloadServiceAccount is created" , func () {
302
+ wsaName := "test-wsa"
303
+ testNamespace := "default"
304
+
305
+ By ("creating a WorkloadServiceAccount using kubectl apply" )
306
+ cmd := exec .Command ("kubectl" , "apply" , "-f" , "-" )
307
+ cmd .Stdin = strings .NewReader (fmt .Sprintf (`
308
+ apiVersion: agent.octopus.com/v1beta1
309
+ kind: WorkloadServiceAccount
310
+ metadata:
311
+ name: %s
312
+ namespace: %s
313
+ spec:
314
+ scope:
315
+ projects: ["web-app", "mobile-app"]
316
+ environments: ["production", "staging"]
317
+ tenants: ["customer-a", "customer-b"]
318
+ steps: ["deploy-api", "deploy-frontend"]
319
+ spaces: ["default-space", "team-space"]
320
+ permissions:
321
+ permissions:
322
+ - apiGroups: [""]
323
+ resources: ["pods"]
324
+ verbs: ["get", "list"]
325
+ - apiGroups: ["apps"]
326
+ resources: ["deployments"]
327
+ verbs: ["get", "watch"]
328
+ ` , wsaName , testNamespace ))
329
+ _ , err := utils .Run (cmd )
330
+ Expect (err ).NotTo (HaveOccurred (), "Failed to create WorkloadServiceAccount" )
331
+
332
+ By ("waiting for the controller to reconcile and create ServiceAccount" )
333
+ verifyServiceAccountCreated := func (g Gomega ) {
334
+ cmd := exec .Command ("kubectl" , "get" , "serviceaccounts" , "-n" , testNamespace ,
335
+ "-l" , "agent.octopus.com/permissions=enabled" , "-o" , "jsonpath={.items[*].metadata.name}" )
336
+ output , err := utils .Run (cmd )
337
+ g .Expect (err ).NotTo (HaveOccurred (), "Failed to list ServiceAccounts" )
338
+ g .Expect (output ).NotTo (BeEmpty (), "Expected at least one ServiceAccount" )
339
+ g .Expect (output ).To (ContainSubstring ("octopus-sa-" ), "ServiceAccount should have octopus-sa- prefix" )
340
+ }
341
+ Eventually (verifyServiceAccountCreated , 2 * time .Minute ).Should (Succeed ())
342
+
343
+ By ("cleaning up test resources" )
344
+ cmd = exec .Command ("kubectl" , "delete" , "workloadserviceaccount" , wsaName , "-n" , testNamespace )
345
+ _ , _ = utils .Run (cmd )
346
+ })
308
347
})
309
348
})
310
349
0 commit comments