Skip to content

Commit 6b87175

Browse files
Add E2E test
1 parent 661b6fb commit 6b87175

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

internal/webhook/v1/webhook_suite_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (m *MockEngine) RemoveScopeRuleset(scope rules.Scope, rule rules.Rule, targ
7676
return args.Error(0)
7777
}
7878

79+
func (m *MockEngine) Reconcile(ctx context.Context, c client.Client, namespace string) error {
80+
args := m.Called(ctx, c, namespace)
81+
return args.Error(0)
82+
}
83+
7984
func TestAPIs(t *testing.T) {
8085
RegisterFailHandler(Fail)
8186

test/e2e/e2e_test.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
"strings"
2526
"time"
2627

2728
. "github.com/onsi/ginkgo/v2"
@@ -297,14 +298,52 @@ var _ = Describe("Manager", Ordered, func() {
297298

298299
// +kubebuilder:scaffold:e2e-webhooks-checks
299300

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+
})
308347
})
309348
})
310349

0 commit comments

Comments
 (0)