Skip to content

Commit

Permalink
Implement tests for VSRoutes
Browse files Browse the repository at this point in the history
Refactor old code to get rid of unused common yamls
  • Loading branch information
tellet committed Jun 7, 2019
1 parent 0dff58c commit 56bff21
Show file tree
Hide file tree
Showing 19 changed files with 779 additions and 91 deletions.
13 changes: 0 additions & 13 deletions tests/data/common/backend2-svc.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions tests/data/common/backend2.yaml

This file was deleted.

18 changes: 18 additions & 0 deletions tests/data/virtual-server-route/route-multiple-updated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backends
spec:
host: virtual-server-route.example.com
upstreams:
- name: backend1
service: backend1-svc
port: 80
- name: backend3
service: backend3-svc
port: 80
subroutes:
- path: "/backends/updated-backend1"
upstream: backend1
- path: "/backends/updated-backend3"
upstream: backend3
18 changes: 18 additions & 0 deletions tests/data/virtual-server-route/route-multiple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backends
spec:
host: virtual-server-route.example.com
upstreams:
- name: backend1
service: backend1-svc
port: 80
- name: backend3
service: backend3-svc
port: 80
subroutes:
- path: "/backends/backend1"
upstream: backend1
- path: "/backends/backend3"
upstream: backend3
13 changes: 13 additions & 0 deletions tests/data/virtual-server-route/route-orphan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backend-orphan
spec:
host: virtual-server-route.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
subroutes:
- path: "/alone-backend2"
upstream: backend2
15 changes: 15 additions & 0 deletions tests/data/virtual-server-route/route-single-duplicate-path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backend2
spec:
host: virtual-server-route.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
subroutes:
- path: "/backend2"
upstream: backend2
- path: "/backend2"
upstream: backend2
13 changes: 13 additions & 0 deletions tests/data/virtual-server-route/route-single-invalid-host.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backend2
spec:
host: invalid-host.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
subroutes:
- path: "/backend2"
upstream: backend2
13 changes: 13 additions & 0 deletions tests/data/virtual-server-route/route-single.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServerRoute
metadata:
name: backend2
spec:
host: virtual-server-route.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
subroutes:
- path: "/backend2"
upstream: backend2
11 changes: 11 additions & 0 deletions tests/data/virtual-server-route/standard/virtual-server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServer
metadata:
name: virtual-server-route
spec:
host: virtual-server-route.example.com
routes:
- path: "/backends"
route: backends-namespace/backends
- path: "/backend2"
route: backend2-namespace/backend2
88 changes: 72 additions & 16 deletions tests/suite/custom_resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@
from suite.resources_utils import ensure_item_removal, get_file_contents


def create_crd_from_yaml(api_extensions_v1_beta1: ApiextensionsV1beta1Api, yaml_manifest) -> str:
def create_crds_from_yaml(api_extensions_v1_beta1: ApiextensionsV1beta1Api, yaml_manifest) -> []:
"""
Create a CRD based on yaml file.
:param api_extensions_v1_beta1: ApiextensionsV1beta1Api
:param yaml_manifest: an absolute path to file
:return: str
:return: []
"""
print("Create CRD:")
print("Create a CRD:")
names = []
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)

try:
api_extensions_v1_beta1.create_custom_resource_definition(dep)
except Exception as ex:
# https://github.com/kubernetes-client/python/issues/376
if ex.args[0] == 'Invalid value for `conditions`, must not be `None`':
print("There was an insignificant exception during the CRD creation. Continue...")
else:
pytest.fail(f"An unexpected exception {ex} occurred. Exiting...")
print(f"CRD created with name '{dep['metadata']['name']}'")
return dep['metadata']['name']
docs = yaml.safe_load_all(f)
for dep in docs:
try:
api_extensions_v1_beta1.create_custom_resource_definition(dep)
except Exception as ex:
# https://github.com/kubernetes-client/python/issues/376
if ex.args[0] == 'Invalid value for `conditions`, must not be `None`':
print("There was an insignificant exception during the CRD creation. Continue...")
else:
pytest.fail(f"An unexpected exception {ex} occurred. Exiting...")
names.append(dep['metadata']['name'])
print(f"CRD created with name '{dep['metadata']['name']}'")
return names


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

def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_manifest, namespace) -> None:
"""
Update a VS based on yaml manifest
Patch a VS based on yaml manifest
:param custom_objects: CustomObjectsApi
:param name:
Expand All @@ -101,6 +103,24 @@ def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_
print(f"VirtualServer updated with name '{dep['metadata']['name']}'")


def patch_v_s_route_from_yaml(custom_objects: CustomObjectsApi, name, yaml_manifest, namespace) -> None:
"""
Update a VirtualServerRoute based on yaml manifest
:param custom_objects: CustomObjectsApi
:param name:
:param yaml_manifest: an absolute path to file
:param namespace:
:return:
"""
print(f"Update a VirtualServerRoute: {name}")
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)

custom_objects.patch_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", name, dep)
print(f"VirtualServerRoute updated with name '{dep['metadata']['name']}'")


def get_vs_nginx_template_conf(v1: CoreV1Api, vs_namespace, vs_name, pod_name, pod_namespace) -> str:
"""
Get contents of /etc/nginx/conf.d/vs_{namespace}_{vs_name}.conf in the pod.
Expand All @@ -114,3 +134,39 @@ def get_vs_nginx_template_conf(v1: CoreV1Api, vs_namespace, vs_name, pod_name, p
"""
file_path = f"/etc/nginx/conf.d/vs_{vs_namespace}_{vs_name}.conf"
return get_file_contents(v1, file_path, pod_name, pod_namespace)


def create_v_s_route_from_yaml(custom_objects: CustomObjectsApi, yaml_manifest, namespace) -> str:
"""
Create a VirtualServerRoute based on a yaml file.
:param custom_objects: CustomObjectsApi
:param yaml_manifest: an absolute path to a file
:param namespace:
:return: str
"""
print("Create a VirtualServerRoute:")
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)

custom_objects.create_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", dep)
print(f"VirtualServerRoute created with a name '{dep['metadata']['name']}'")
return dep['metadata']['name']


def delete_v_s_route(custom_objects: CustomObjectsApi, name, namespace) -> None:
"""
Delete a VirtualServerRoute.
:param custom_objects: CustomObjectsApi
:param namespace: namespace
:param name:
:return:
"""
print(f"Delete a VirtualServerRoute: {name}")
delete_options = client.V1DeleteOptions()
custom_objects.delete_namespaced_custom_object("k8s.nginx.org",
"v1alpha1", namespace, "virtualserverroutes", name, delete_options)
ensure_item_removal(custom_objects.get_namespaced_custom_object,
"k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", name)
print(f"VirtualServerRoute was removed with the name '{name}'")
13 changes: 7 additions & 6 deletions tests/suite/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from kubernetes.client import CoreV1Api, ExtensionsV1beta1Api, RbacAuthorizationV1beta1Api, CustomObjectsApi, \
ApiextensionsV1beta1Api

from suite.custom_resources_utils import create_crd_from_yaml, delete_crd, create_virtual_server_from_yaml, \
from suite.custom_resources_utils import create_crds_from_yaml, delete_crd, create_virtual_server_from_yaml, \
delete_virtual_server
from suite.kube_config_utils import ensure_context_in_config, get_current_context_name
from suite.resources_utils import create_namespace_with_name_from_yaml, delete_namespace, create_ns_and_sa_from_yaml, \
Expand Down Expand Up @@ -268,8 +268,8 @@ def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequi
if request.param['type'] == 'rbac-without-vs':
patch_rbac(kube_apis.rbac_v1_beta1, f"{TEST_DATA}/virtual-server/rbac-without-vs.yaml")
print("------------------------- Register CRD -----------------------------------")
crd_name = create_crd_from_yaml(kube_apis.api_extensions_v1_beta1,
f"{DEPLOYMENTS}/custom-resource-definitions/virtualserver.yaml")
crd_names = create_crds_from_yaml(kube_apis.api_extensions_v1_beta1,
f"{DEPLOYMENTS}/common/custom-resource-definitions.yaml")
print("------------------------- Create IC -----------------------------------")
name = create_ingress_controller(kube_apis.v1, kube_apis.extensions_v1_beta1, cli_arguments, namespace,
request.param.get('extra_args', None))
Expand All @@ -278,11 +278,12 @@ def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequi
ingress_controller_endpoint.port_ssl)

def fin():
print("Remove the CRD:")
delete_crd(kube_apis.api_extensions_v1_beta1, crd_name)
for crd_name in crd_names:
print("Remove the CRD:")
delete_crd(kube_apis.api_extensions_v1_beta1, crd_name)
print("Remove the IC:")
delete_ingress_controller(kube_apis.extensions_v1_beta1, name, cli_arguments['deployment-type'], namespace)
print("Restore ClusterRole:")
print("Restore the ClusterRole:")
patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml")

request.addfinalizer(fin)
Expand Down
58 changes: 36 additions & 22 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ def create_deployment(extensions_v1_beta1: ExtensionsV1beta1Api, namespace, body
return body['metadata']['name']


def create_deployment_with_name(extensions_v1_beta1: ExtensionsV1beta1Api, namespace, name) -> str:
"""
Create a deployment with a specific name based on common yaml file.
:param extensions_v1_beta1: ExtensionsV1beta1Api
:param namespace: namespace name
:param name:
:return: str
"""
print(f"Create a Deployment with a specific name")
with open(f"{TEST_DATA}/common/backend1.yaml") as f:
dep = yaml.safe_load(f)
dep['metadata']['name'] = name
dep['spec']['selector']['matchLabels']['app'] = name
dep['spec']['template']['metadata']['labels']['app'] = name
dep['spec']['template']['spec']['containers'][0]['name'] = name
return create_deployment(extensions_v1_beta1, namespace, dep)


def create_daemon_set(extensions_v1_beta1: ExtensionsV1beta1Api, namespace, body) -> str:
"""
Create a daemon-set based on a dict.
Expand Down Expand Up @@ -220,6 +239,23 @@ def create_service(v1: CoreV1Api, namespace, body) -> str:
return resp.metadata.name


def create_service_with_name(v1: CoreV1Api, namespace, name) -> str:
"""
Create a service with a specific name based on a common yaml manifest.
:param v1: CoreV1Api
:param namespace: namespace name
:param name: name
:return: str
"""
print(f"Create a Service with a specific name:")
with open(f"{TEST_DATA}/common/backend1-svc.yaml") as f:
dep = yaml.safe_load(f)
dep['metadata']['name'] = name
dep['spec']['selector']['app'] = name.replace("-svc", "")
return create_service(v1, namespace, dep)


def get_service_node_ports(v1: CoreV1Api, name, namespace) -> (str, str):
"""
Get service allocated node_ports.
Expand Down Expand Up @@ -596,27 +632,6 @@ def __init__(self, services, deployments):
self.deployments = deployments


def create_common_app(v1: CoreV1Api, extensions_v1_beta1: ExtensionsV1beta1Api, namespace) -> CommonApp:
"""
Create a simple backend application.
A simple application consists of 2 backend services.
:param v1: CoreV1Api
:param extensions_v1_beta1: ExtensionsV1beta1Api
:param namespace: namespace name
:return: CommonApp
"""
svc_one = create_service_from_yaml(v1, namespace, f"{TEST_DATA}/common/backend1-svc.yaml")
svc_two = create_service_from_yaml(v1, namespace, f"{TEST_DATA}/common/backend2-svc.yaml")
deployment_one = create_deployment_from_yaml(extensions_v1_beta1, namespace,
f"{TEST_DATA}/common/backend1.yaml")
deployment_two = create_deployment_from_yaml(extensions_v1_beta1, namespace,
f"{TEST_DATA}/common/backend2.yaml")
return CommonApp([svc_one, svc_two], [deployment_one, deployment_two])


def create_example_app(kube_apis, app_type, namespace) -> CommonApp:
"""
Create a backend application.
Expand All @@ -626,7 +641,6 @@ def create_example_app(kube_apis, app_type, namespace) -> CommonApp:
:param kube_apis: client apis
:param app_type: type of the application (simple|split)
:param namespace: namespace name
:param app_type: type of the application (simple|split)
:return: CommonApp
"""
create_items_from_yaml(kube_apis, f"{TEST_DATA}/common/app/{app_type}/app.yaml", namespace)
Expand Down
Loading

0 comments on commit 56bff21

Please sign in to comment.