Skip to content

Commit b2ab687

Browse files
authored
Merge pull request #66 from emmaaroche/e2e-manual-approval-tests
test: add E2E tests for manual APIKey approval and rejection workflows
2 parents 93f23ec + 564d25b commit b2ab687

3 files changed

Lines changed: 432 additions & 155 deletions

File tree

test/e2e/automatic_approval_test.go

Lines changed: 7 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -38,65 +38,7 @@ var _ = Describe("Automatic Approval", Ordered, func() {
3838
)
3939

4040
AfterEach(func() {
41-
specReport := CurrentSpecReport()
42-
if specReport.Failed() {
43-
By("Fetching controller manager pod name")
44-
cmd := exec.Command("kubectl", "get",
45-
"pods", "-l", "control-plane=controller-manager",
46-
"-o", "go-template={{ range .items }}"+
47-
"{{ if not .metadata.deletionTimestamp }}"+
48-
"{{ .metadata.name }}"+
49-
"{{ \"\\n\" }}{{ end }}{{ end }}",
50-
"-n", controllerNamespace,
51-
)
52-
podOutput, err := utils.Run(cmd)
53-
if err != nil {
54-
_, _ = fmt.Fprintf(GinkgoWriter, "Failed to get controller pod name: %s\n", err)
55-
return
56-
}
57-
podNames := utils.GetNonEmptyLines(podOutput)
58-
if len(podNames) == 0 {
59-
_, _ = fmt.Fprintf(GinkgoWriter, "No controller pod found\n")
60-
return
61-
}
62-
controllerPodName := podNames[0]
63-
64-
By("Fetching controller manager pod logs")
65-
cmd = exec.Command("kubectl", "logs", controllerPodName, "-n", controllerNamespace)
66-
controllerLogs, err := utils.Run(cmd)
67-
if err == nil {
68-
_, _ = fmt.Fprintf(GinkgoWriter, "Controller logs:\n%s\n", controllerLogs)
69-
} else {
70-
_, _ = fmt.Fprintf(GinkgoWriter, "Failed to get Controller logs: %s\n", err)
71-
}
72-
73-
By("Fetching Kubernetes events in owner namespace")
74-
cmd = exec.Command("kubectl", "get", "events", "-n", ownerNamespace, "--sort-by=.lastTimestamp")
75-
eventsOutput, err := utils.Run(cmd)
76-
if err == nil {
77-
_, _ = fmt.Fprintf(GinkgoWriter, "Events in %s:\n%s\n", ownerNamespace, eventsOutput)
78-
} else {
79-
_, _ = fmt.Fprintf(GinkgoWriter, "Failed to get events in %s: %s\n", ownerNamespace, err)
80-
}
81-
82-
By("Fetching Kubernetes events in consumer namespace")
83-
cmd = exec.Command("kubectl", "get", "events", "-n", consumerNamespace, "--sort-by=.lastTimestamp")
84-
eventsOutput, err = utils.Run(cmd)
85-
if err == nil {
86-
_, _ = fmt.Fprintf(GinkgoWriter, "Events in %s:\n%s\n", consumerNamespace, eventsOutput)
87-
} else {
88-
_, _ = fmt.Fprintf(GinkgoWriter, "Failed to get events in %s: %s\n", consumerNamespace, err)
89-
}
90-
91-
By("Fetching controller manager pod description")
92-
cmd = exec.Command("kubectl", "describe", "pod", controllerPodName, "-n", controllerNamespace)
93-
podDescription, err := utils.Run(cmd)
94-
if err == nil {
95-
_, _ = fmt.Fprintf(GinkgoWriter, "Pod description:\n%s\n", podDescription)
96-
} else {
97-
_, _ = fmt.Fprintf(GinkgoWriter, "Failed to describe controller pod: %s\n", err)
98-
}
99-
}
41+
LogDebugInfoOnFailure(ownerNamespace, consumerNamespace, controllerNamespace)
10042
})
10143

10244
BeforeAll(func() {
@@ -108,104 +50,14 @@ var _ = Describe("Automatic Approval", Ordered, func() {
10850
})
10951

11052
AfterAll(func() {
111-
By("cleaning up kuadrant namespace")
112-
cmd := exec.Command("kubectl", "delete", "ns", kuadrantNamespace, "--wait=false")
113-
_, _ = utils.Run(cmd)
114-
115-
By("cleaning up owner namespace")
116-
cmd = exec.Command("kubectl", "delete", "ns", ownerNamespace, "--wait=false")
117-
_, _ = utils.Run(cmd)
118-
119-
By("cleaning up consumer namespace")
120-
cmd = exec.Command("kubectl", "delete", "ns", consumerNamespace, "--wait=false")
121-
_, _ = utils.Run(cmd)
53+
CleanupNamespaces(ownerNamespace, consumerNamespace, kuadrantNamespace)
12254
})
12355

12456
Context("APIKey with automatic approval mode", func() {
12557
It("should create APIKeyRequest, automatic approval, and approve the APIKey", func() {
126-
By("creating an HTTPRoute as a reference target")
127-
httpRouteYAML := fmt.Sprintf(`
128-
apiVersion: gateway.networking.k8s.io/v1
129-
kind: HTTPRoute
130-
metadata:
131-
name: test-route
132-
namespace: %s
133-
spec:
134-
parentRefs:
135-
- name: test-gateway
136-
rules:
137-
- matches:
138-
- path:
139-
type: PathPrefix
140-
value: /api
141-
backendRefs:
142-
- name: test-service
143-
port: 8080
144-
`, ownerNamespace)
145-
146-
cmd := exec.Command("kubectl", "apply", "-f", "-")
147-
cmd.Stdin = utils.StringReader(httpRouteYAML)
148-
_, err := utils.Run(cmd)
149-
Expect(err).NotTo(HaveOccurred(), "Failed to create HTTPRoute")
150-
151-
By("creating an AuthPolicy with API key authentication")
152-
authPolicyYAML := fmt.Sprintf(`
153-
apiVersion: kuadrant.io/v1
154-
kind: AuthPolicy
155-
metadata:
156-
name: test-auth-policy
157-
namespace: %s
158-
spec:
159-
targetRef:
160-
group: gateway.networking.k8s.io
161-
kind: HTTPRoute
162-
name: test-route
163-
rules:
164-
authentication:
165-
"api-key":
166-
apiKey:
167-
selector:
168-
matchLabels:
169-
kuadrant.io/apikeys: "true"
170-
credentials:
171-
authorizationHeader:
172-
prefix: "API-KEY"
173-
`, ownerNamespace)
174-
175-
cmd = exec.Command("kubectl", "apply", "-f", "-")
176-
cmd.Stdin = utils.StringReader(authPolicyYAML)
177-
_, err = utils.Run(cmd)
178-
Expect(err).NotTo(HaveOccurred(), "Failed to create AuthPolicy")
179-
180-
By("updating AuthPolicy status to Accepted and Enforced")
181-
authPolicyStatusPatch := `{
182-
"status": {
183-
"conditions": [
184-
{
185-
"type": "Accepted",
186-
"status": "True",
187-
"reason": "Accepted",
188-
"message": "AuthPolicy has been accepted",
189-
"lastTransitionTime": "2024-01-01T00:00:00Z"
190-
},
191-
{
192-
"type": "Enforced",
193-
"status": "True",
194-
"reason": "Enforced",
195-
"message": "AuthPolicy has been successfully enforced",
196-
"lastTransitionTime": "2024-01-01T00:00:00Z"
197-
}
198-
]
199-
}
200-
}`
201-
202-
cmd = exec.Command("kubectl", "patch", "authpolicy", "test-auth-policy",
203-
"-n", ownerNamespace,
204-
"--type=merge",
205-
"--subresource=status",
206-
"-p", authPolicyStatusPatch)
207-
_, err = utils.Run(cmd)
208-
Expect(err).NotTo(HaveOccurred(), "Failed to update AuthPolicy status")
58+
By("creating HTTPRoute and AuthPolicy")
59+
CreateHTTPRoute(ownerNamespace)
60+
CreateAuthPolicy(ownerNamespace)
20961

21062
By("creating an APIProduct with automatic approval mode")
21163
apiProductYAML := fmt.Sprintf(`
@@ -225,9 +77,9 @@ spec:
22577
name: test-route
22678
`, apiProductName, ownerNamespace)
22779

228-
cmd = exec.Command("kubectl", "apply", "-f", "-")
80+
cmd := exec.Command("kubectl", "apply", "-f", "-")
22981
cmd.Stdin = utils.StringReader(apiProductYAML)
230-
_, err = utils.Run(cmd)
82+
_, err := utils.Run(cmd)
23183
Expect(err).NotTo(HaveOccurred(), "Failed to create APIProduct")
23284

23385
By("verifying APIProduct was created")

0 commit comments

Comments
 (0)