Skip to content

Commit aba456b

Browse files
committed
feat(test): add e2e test for mutation hook
1 parent 25988e2 commit aba456b

4 files changed

Lines changed: 63 additions & 11 deletions

File tree

e2e/smoke_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ var _ = Describe("Smoke End To End Tests - against OpenShift Cluster with Istio
4545

4646
InstallLocalOperator(namespace)
4747
DeployTestScenario(scenario, namespace)
48+
})
49+
50+
BeforeEach(func() {
4851
sessionName = GenerateSessionName()
4952
})
5053

@@ -230,6 +233,36 @@ var _ = Describe("Smoke End To End Tests - against OpenShift Cluster with Istio
230233
})
231234
})
232235

236+
Context("che deployment", func() {
237+
var tmpRemove func()
238+
239+
BeforeEach(func() {
240+
scenario = "scenario-che"
241+
tmpRemove = test.TemporaryEnvVars(
242+
"IKE_SESSION", sessionName,
243+
"IKE_ROUTE", "header:x-test-suite=smoke")
244+
})
245+
246+
AfterEach(func() {
247+
tmpRemove()
248+
})
249+
250+
It("should watch for changes in ratings service in specified namespace and serve it", func() {
251+
ChangeNamespace(namespace)
252+
EnsureAllPodsAreReady(namespace)
253+
EnsureProdRouteIsReachable(namespace, ContainSubstring("ratings-v1"))
254+
255+
// given the mutation hook has kicked in
256+
EnsureSessionRouteIsReachable(namespace, sessionName, ContainSubstring("che-workspace"))
257+
258+
// when the mutated deployment is cleaned up
259+
testshell.ExecuteInDir(tmpDir, "oc delete deployment che-workspace")
260+
261+
// then the session should no longer be available
262+
EnsureSessionRouteIsNotReachable(namespace, sessionName, ContainSubstring("ratings-v1"), Not(ContainSubstring("che-workspace")))
263+
EnsureProdRouteIsReachable(namespace, ContainSubstring("ratings-v1"))
264+
})
265+
})
233266
})
234267
})
235268

pkg/k8s/mutation/webhook.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ type data struct {
3232
}
3333

3434
func (d *data) IsIkeable() bool {
35-
//return d.Object.Labels["ike.target"] != ""
36-
return d.Object.Spec.Selector.MatchLabels["deployment"] == "workspace"
35+
return d.Object.Annotations["ike.target"] != ""
36+
//return d.Object.Spec.Selector.MatchLabels["deployment"] == "workspace"
3737
}
3838

3939
func (d *data) Target() string {
40-
t := d.Object.Labels["ike.target"]
40+
t := d.Object.Annotations["ike.target"]
4141
if t != "" {
4242
return t
4343
}
4444
return "preference-v1"
4545
}
4646

4747
func (d *data) Session() string {
48-
s := d.Object.Labels["ike.session"]
48+
s := d.Object.Annotations["ike.session"]
4949
if s != "" {
5050
return s
5151
}
@@ -58,7 +58,7 @@ func (d *data) Namespace() string {
5858
}
5959

6060
func (d *data) Route() string {
61-
r := d.Object.Labels["ike.route"]
61+
r := d.Object.Annotations["ike.route"]
6262
if r != "" {
6363
return r
6464
}
@@ -121,6 +121,7 @@ func (w *Webhook) Handle(ctx context.Context, req admission.Request) admission.R
121121
for k, v := range lables {
122122
logger().Info("Label added", "deployemnt", req.Name, k, v)
123123
deployment.Spec.Template.Labels[k] = v
124+
deployment.Spec.Selector.MatchLabels[k] = v // TODO: hmm, should we update the slector? In our test case scenario we have app=che-worksapce where the 'updated labels' become app=reviews
124125
}
125126

126127
targetHost := findGwHost(refStatus)

test/cmd/test-scenario/generator/scenarios.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generator
22

33
import (
44
"io"
5+
"os"
56
)
67

78
var (
@@ -80,7 +81,23 @@ func TestScenarioMutationHookChe(out io.Writer) {
8081
ForService(reviews, Call(HTTP(), ratings)),
8182
GatewayOnHost(GatewayHost),
8283
)
83-
Do(out, che, Deployment, WithAnnotations(map[string]string{"ike.target": "reviews-v1"}))
84+
Do(out, che, Deployment, WithAnnotations(map[string]string{
85+
"ike.target": "reviews-v1",
86+
"ike.session": os.Getenv("IKE_SESSION"),
87+
"ike.route": os.Getenv("IKE_SESSION")}))
88+
}
89+
90+
// TestScenarioMutationHookCheOnly is a basic test setup with a
91+
// few services calling each other in a chain. Similar to the original bookinfo example setup
92+
// and a single Deployment simulating a Che Deployment which should trigger the WebHook.
93+
// Using Deployment.
94+
func TestScenarioMutationHookCheOnly(out io.Writer) {
95+
che := Entry{"che-workspace", "Deployment", Namespace}
96+
97+
Do(out, che, Deployment, WithAnnotations(map[string]string{
98+
"ike.target": "reviews-v1",
99+
"ike.session": os.Getenv("IKE_SESSION"),
100+
"ike.route": os.Getenv("IKE_SESSION")}))
84101
}
85102

86103
// DemoScenario is a simple setup for demo purposes.

test/cmd/test-scenario/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ func main() {
2525
}
2626

2727
scenarios := map[string]func(io.Writer){
28-
"scenario-1": generator.TestScenario1HTTPThreeServicesInSequence,
29-
"scenario-1.1": generator.TestScenario1GRPCThreeServicesInSequence,
30-
"scenario-2": generator.TestScenario2ThreeServicesInSequenceDeploymentConfig,
31-
"scenario-che": generator.TestScenarioMutationHookChe,
32-
"demo": generator.DemoScenario,
28+
"scenario-1": generator.TestScenario1HTTPThreeServicesInSequence,
29+
"scenario-1.1": generator.TestScenario1GRPCThreeServicesInSequence,
30+
"scenario-2": generator.TestScenario2ThreeServicesInSequenceDeploymentConfig,
31+
"scenario-che": generator.TestScenarioMutationHookChe,
32+
"scenario-che-only": generator.TestScenarioMutationHookCheOnly,
33+
"demo": generator.DemoScenario,
3334
}
3435
scenario := os.Args[1]
3536
if f, ok := scenarios[scenario]; ok {

0 commit comments

Comments
 (0)