Skip to content

Commit 56bff21

Browse files
committed
Implement tests for VSRoutes
Refactor old code to get rid of unused common yamls
1 parent 0dff58c commit 56bff21

19 files changed

+779
-91
lines changed

tests/data/common/backend2-svc.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/data/common/backend2.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backends
5+
spec:
6+
host: virtual-server-route.example.com
7+
upstreams:
8+
- name: backend1
9+
service: backend1-svc
10+
port: 80
11+
- name: backend3
12+
service: backend3-svc
13+
port: 80
14+
subroutes:
15+
- path: "/backends/updated-backend1"
16+
upstream: backend1
17+
- path: "/backends/updated-backend3"
18+
upstream: backend3
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backends
5+
spec:
6+
host: virtual-server-route.example.com
7+
upstreams:
8+
- name: backend1
9+
service: backend1-svc
10+
port: 80
11+
- name: backend3
12+
service: backend3-svc
13+
port: 80
14+
subroutes:
15+
- path: "/backends/backend1"
16+
upstream: backend1
17+
- path: "/backends/backend3"
18+
upstream: backend3
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backend-orphan
5+
spec:
6+
host: virtual-server-route.example.com
7+
upstreams:
8+
- name: backend2
9+
service: backend2-svc
10+
port: 80
11+
subroutes:
12+
- path: "/alone-backend2"
13+
upstream: backend2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backend2
5+
spec:
6+
host: virtual-server-route.example.com
7+
upstreams:
8+
- name: backend2
9+
service: backend2-svc
10+
port: 80
11+
subroutes:
12+
- path: "/backend2"
13+
upstream: backend2
14+
- path: "/backend2"
15+
upstream: backend2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backend2
5+
spec:
6+
host: invalid-host.example.com
7+
upstreams:
8+
- name: backend2
9+
service: backend2-svc
10+
port: 80
11+
subroutes:
12+
- path: "/backend2"
13+
upstream: backend2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServerRoute
3+
metadata:
4+
name: backend2
5+
spec:
6+
host: virtual-server-route.example.com
7+
upstreams:
8+
- name: backend2
9+
service: backend2-svc
10+
port: 80
11+
subroutes:
12+
- path: "/backend2"
13+
upstream: backend2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: k8s.nginx.org/v1alpha1
2+
kind: VirtualServer
3+
metadata:
4+
name: virtual-server-route
5+
spec:
6+
host: virtual-server-route.example.com
7+
routes:
8+
- path: "/backends"
9+
route: backends-namespace/backends
10+
- path: "/backend2"
11+
route: backend2-namespace/backend2

tests/suite/custom_resources_utils.py

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,30 @@
88
from suite.resources_utils import ensure_item_removal, get_file_contents
99

1010

11-
def create_crd_from_yaml(api_extensions_v1_beta1: ApiextensionsV1beta1Api, yaml_manifest) -> str:
11+
def create_crds_from_yaml(api_extensions_v1_beta1: ApiextensionsV1beta1Api, yaml_manifest) -> []:
1212
"""
1313
Create a CRD based on yaml file.
1414
1515
:param api_extensions_v1_beta1: ApiextensionsV1beta1Api
1616
:param yaml_manifest: an absolute path to file
17-
:return: str
17+
:return: []
1818
"""
19-
print("Create CRD:")
19+
print("Create a CRD:")
20+
names = []
2021
with open(yaml_manifest) as f:
21-
dep = yaml.safe_load(f)
22-
23-
try:
24-
api_extensions_v1_beta1.create_custom_resource_definition(dep)
25-
except Exception as ex:
26-
# https://github.com/kubernetes-client/python/issues/376
27-
if ex.args[0] == 'Invalid value for `conditions`, must not be `None`':
28-
print("There was an insignificant exception during the CRD creation. Continue...")
29-
else:
30-
pytest.fail(f"An unexpected exception {ex} occurred. Exiting...")
31-
print(f"CRD created with name '{dep['metadata']['name']}'")
32-
return dep['metadata']['name']
22+
docs = yaml.safe_load_all(f)
23+
for dep in docs:
24+
try:
25+
api_extensions_v1_beta1.create_custom_resource_definition(dep)
26+
except Exception as ex:
27+
# https://github.com/kubernetes-client/python/issues/376
28+
if ex.args[0] == 'Invalid value for `conditions`, must not be `None`':
29+
print("There was an insignificant exception during the CRD creation. Continue...")
30+
else:
31+
pytest.fail(f"An unexpected exception {ex} occurred. Exiting...")
32+
names.append(dep['metadata']['name'])
33+
print(f"CRD created with name '{dep['metadata']['name']}'")
34+
return names
3335

3436

3537
def delete_crd(api_extensions_v1_beta1: ApiextensionsV1beta1Api, name) -> None:
@@ -85,7 +87,7 @@ def delete_virtual_server(custom_objects: CustomObjectsApi, name, namespace) ->
8587

8688
def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_manifest, namespace) -> None:
8789
"""
88-
Update a VS based on yaml manifest
90+
Patch a VS based on yaml manifest
8991
9092
:param custom_objects: CustomObjectsApi
9193
:param name:
@@ -101,6 +103,24 @@ def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_
101103
print(f"VirtualServer updated with name '{dep['metadata']['name']}'")
102104

103105

106+
def patch_v_s_route_from_yaml(custom_objects: CustomObjectsApi, name, yaml_manifest, namespace) -> None:
107+
"""
108+
Update a VirtualServerRoute based on yaml manifest
109+
110+
:param custom_objects: CustomObjectsApi
111+
:param name:
112+
:param yaml_manifest: an absolute path to file
113+
:param namespace:
114+
:return:
115+
"""
116+
print(f"Update a VirtualServerRoute: {name}")
117+
with open(yaml_manifest) as f:
118+
dep = yaml.safe_load(f)
119+
120+
custom_objects.patch_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", name, dep)
121+
print(f"VirtualServerRoute updated with name '{dep['metadata']['name']}'")
122+
123+
104124
def get_vs_nginx_template_conf(v1: CoreV1Api, vs_namespace, vs_name, pod_name, pod_namespace) -> str:
105125
"""
106126
Get contents of /etc/nginx/conf.d/vs_{namespace}_{vs_name}.conf in the pod.
@@ -114,3 +134,39 @@ def get_vs_nginx_template_conf(v1: CoreV1Api, vs_namespace, vs_name, pod_name, p
114134
"""
115135
file_path = f"/etc/nginx/conf.d/vs_{vs_namespace}_{vs_name}.conf"
116136
return get_file_contents(v1, file_path, pod_name, pod_namespace)
137+
138+
139+
def create_v_s_route_from_yaml(custom_objects: CustomObjectsApi, yaml_manifest, namespace) -> str:
140+
"""
141+
Create a VirtualServerRoute based on a yaml file.
142+
143+
:param custom_objects: CustomObjectsApi
144+
:param yaml_manifest: an absolute path to a file
145+
:param namespace:
146+
:return: str
147+
"""
148+
print("Create a VirtualServerRoute:")
149+
with open(yaml_manifest) as f:
150+
dep = yaml.safe_load(f)
151+
152+
custom_objects.create_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", dep)
153+
print(f"VirtualServerRoute created with a name '{dep['metadata']['name']}'")
154+
return dep['metadata']['name']
155+
156+
157+
def delete_v_s_route(custom_objects: CustomObjectsApi, name, namespace) -> None:
158+
"""
159+
Delete a VirtualServerRoute.
160+
161+
:param custom_objects: CustomObjectsApi
162+
:param namespace: namespace
163+
:param name:
164+
:return:
165+
"""
166+
print(f"Delete a VirtualServerRoute: {name}")
167+
delete_options = client.V1DeleteOptions()
168+
custom_objects.delete_namespaced_custom_object("k8s.nginx.org",
169+
"v1alpha1", namespace, "virtualserverroutes", name, delete_options)
170+
ensure_item_removal(custom_objects.get_namespaced_custom_object,
171+
"k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", name)
172+
print(f"VirtualServerRoute was removed with the name '{name}'")

0 commit comments

Comments
 (0)