...
1import sys
2import time
3
4import pytest
5
6from tests.integration.utils import get_code_with_retry, install_ambassador
7from tests.kubeutils import apply_kube_artifacts, delete_kube_artifacts
8from tests.manifests import qotm_manifests
9from tests.runutils import run_and_assert
10
11
12class WattTesting:
13 def manifests(self):
14 pass
15
16 def apply_manifests(self):
17 pass
18
19 def create_listeners(self, namespace):
20 manifest = f"""
21---
22apiVersion: getambassador.io/v3alpha1
23kind: Listener
24metadata:
25 name: listener-8080
26spec:
27 port: 8080
28 protocol: HTTP
29 securityModel: INSECURE
30 hostBinding:
31 namespace:
32 from: SELF
33"""
34
35 apply_kube_artifacts(namespace=namespace, artifacts=manifest)
36
37 def apply_qotm_endpoint_manifests(self, namespace):
38 qotm_resolver = f"""
39apiVersion: getambassador.io/v3alpha1
40kind: KubernetesEndpointResolver
41metadata:
42 name: qotm-resolver
43 namespace: {namespace}
44"""
45
46 apply_kube_artifacts(namespace=namespace, artifacts=qotm_resolver)
47 self.create_qotm_mapping(namespace=namespace)
48
49 def create_qotm_mapping(self, namespace):
50 qotm_mapping = f"""
51---
52apiVersion: getambassador.io/v3alpha1
53kind: Mapping
54metadata:
55 name: qotm-mapping
56 namespace: {namespace}
57spec:
58 hostname: "*"
59 prefix: /qotm/
60 service: qotm.{namespace}
61 resolver: qotm-resolver
62 load_balancer:
63 policy: round_robin
64 """
65
66 apply_kube_artifacts(namespace=namespace, artifacts=qotm_mapping)
67
68 def delete_qotm_mapping(self, namespace):
69 qotm_mapping = f"""
70---
71apiVersion: getambassador.io/v3alpha1
72kind: Mapping
73metadata:
74 name: qotm-mapping
75 namespace: {namespace}
76spec:
77 hostname: "*"
78 prefix: /qotm/
79 service: qotm.{namespace}
80 resolver: qotm-resolver
81 load_balancer:
82 policy: round_robin
83 """
84
85 delete_kube_artifacts(namespace=namespace, artifacts=qotm_mapping)
86
87 def test_rapid_additions_and_deletions(self):
88 namespace = "watt-rapid"
89
90 # Install Ambassador
91 install_ambassador(namespace=namespace)
92
93 # Set up our listener.
94 self.create_listeners(namespace)
95
96 # Install QOTM
97 apply_kube_artifacts(namespace=namespace, artifacts=qotm_manifests)
98
99 # Install QOTM Ambassador manifests
100 self.apply_qotm_endpoint_manifests(namespace=namespace)
101
102 # Now let's wait for ambassador and QOTM pods to become ready
103 run_and_assert(
104 [
105 "tools/bin/kubectl",
106 "wait",
107 "--timeout=90s",
108 "--for=condition=Ready",
109 "pod",
110 "-l",
111 "service=ambassador",
112 "-n",
113 namespace,
114 ]
115 )
116 run_and_assert(
117 [
118 "tools/bin/kubectl",
119 "wait",
120 "--timeout=90s",
121 "--for=condition=Ready",
122 "pod",
123 "-l",
124 "service=qotm",
125 "-n",
126 namespace,
127 ]
128 )
129
130 # Assume we can reach Ambassador through telepresence
131 qotm_host = "ambassador." + namespace
132 qotm_url = f"http://{qotm_host}/qotm/"
133
134 # Assert 200 OK at /qotm/ endpoint
135 qotm_ready = False
136
137 loop_limit = 60
138 while not qotm_ready:
139 assert loop_limit > 0, "QOTM is not ready yet, aborting..."
140 try:
141 qotm_http_code = get_code_with_retry(qotm_url)
142 assert qotm_http_code == 200, f"Expected 200 OK, got {qotm_http_code}"
143 print(f"{qotm_url} is ready")
144 qotm_ready = True
145
146 except Exception as e:
147 print(f"Error: {e}")
148 print(f"{qotm_url} not ready yet, trying again...")
149 time.sleep(1)
150 loop_limit -= 1
151
152 # Try to mess up Ambassador by applying and deleting QOTM mapping over and over
153 for i in range(10):
154 self.delete_qotm_mapping(namespace=namespace)
155 self.create_qotm_mapping(namespace=namespace)
156
157 # Let's give Ambassador a few seconds to register the changes...
158 time.sleep(5)
159
160 # Assert 200 OK at /qotm/ endpoint
161 qotm_http_code = get_code_with_retry(qotm_url)
162 assert qotm_http_code == 200, f"Expected 200 OK, got {qotm_http_code}"
163
164
165@pytest.mark.flaky(reruns=1, reruns_delay=10)
166def test_watt():
167 watt_test = WattTesting()
168 watt_test.test_rapid_additions_and_deletions()
169
170
171if __name__ == "__main__":
172 pytest.main(sys.argv)
View as plain text