Skip to content

Commit

Permalink
Add tests for upstreams options in VS/VSR
Browse files Browse the repository at this point in the history
  • Loading branch information
tellet committed Jul 11, 2019
1 parent bc2326e commit f04246d
Show file tree
Hide file tree
Showing 17 changed files with 851 additions and 161 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ def pytest_runtest_makereport(item) -> None:
if rep.when == "call" and rep.failed:
pod_namespace = item.funcargs['ingress_controller_prerequisites'].namespace
pod_name = get_first_pod_name(item.funcargs['kube_apis'].v1, pod_namespace)
print("===================== IC Logs Start =====================")
print("\n===================== IC Logs Start =====================")
print(item.funcargs['kube_apis'].v1.read_namespaced_pod_log(pod_name, pod_namespace))
print("===================== IC Logs End =====================")
print("\n===================== IC Logs End =====================")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
namespace: nginx-ingress
data:
lb-method: "round_robin"
max-fails: "3"
fail-timeout: "33s"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
lb-method: "} exit;"
max-fails: -10
fail-timeout: "20 seconds"
- name: backend3
service: backend3-svc
port: 80
lb-method: "123"
max-fails: -10
fail-timeout: "s"
subroutes:
- path: "/backends/backend1"
upstream: backend1
- path: "/backends/backend3"
upstream: backend3
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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
lb-method: "random two least_connn"
max-fails: -10
fail-timeout: "20 seconds"
subroutes:
- path: "/backend2"
upstream: backend2
13 changes: 13 additions & 0 deletions tests/data/virtual-server-route-upstream-options/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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
namespace: nginx-ingress
data:
lb-method: "round_robin"
max-fails: "3"
fail-timeout: "33s"
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServer
metadata:
name: virtual-server
spec:
host: virtual-server.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
- name: backend1
service: backend1-svc
port: 80
routes:
- path: "/backend1"
upstream: backend1
- path: "/backend2"
upstream: backend2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: k8s.nginx.org/v1alpha1
kind: VirtualServer
metadata:
name: virtual-server
spec:
host: virtual-server.example.com
upstreams:
- name: backend2
service: backend2-svc
port: 80
lb-method: "} exit;"
max-fails: -10
fail-timeout: "20 seconds"
- name: backend1
service: backend1-svc
port: 80
lb-method: "123"
max-fails: -10
fail-timeout: "s"
routes:
- path: "/backend1"
upstream: backend1
- path: "/backend2"
upstream: backend2
47 changes: 47 additions & 0 deletions tests/suite/custom_resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_
print(f"VirtualServer updated with name '{dep['metadata']['name']}'")


def patch_virtual_server(custom_objects: CustomObjectsApi, name, namespace, body) -> str:
"""
Update a VirtualServer based on a dict.
:param custom_objects: CustomObjectsApi
:param body: dict
:param namespace:
:return: str
"""
print("Update a VirtualServer:")
custom_objects.patch_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualservers", name, body)
print(f"VirtualServer updated with a name '{body['metadata']['name']}'")
return body['metadata']['name']


def patch_v_s_route_from_yaml(custom_objects: CustomObjectsApi, name, yaml_manifest, namespace) -> None:
"""
Update a VirtualServerRoute based on yaml manifest
Expand Down Expand Up @@ -154,6 +169,21 @@ def create_v_s_route_from_yaml(custom_objects: CustomObjectsApi, yaml_manifest,
return dep['metadata']['name']


def patch_v_s_route(custom_objects: CustomObjectsApi, name, namespace, body) -> str:
"""
Update a VirtualServerRoute based on a dict.
:param custom_objects: CustomObjectsApi
:param body: dict
:param namespace:
:return: str
"""
print("Update a VirtualServerRoute:")
custom_objects.patch_namespaced_custom_object("k8s.nginx.org", "v1alpha1", namespace, "virtualserverroutes", name, body)
print(f"VirtualServerRoute updated with a name '{body['metadata']['name']}'")
return body['metadata']['name']


def delete_v_s_route(custom_objects: CustomObjectsApi, name, namespace) -> None:
"""
Delete a VirtualServerRoute.
Expand All @@ -170,3 +200,20 @@ def delete_v_s_route(custom_objects: CustomObjectsApi, name, namespace) -> None:
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}'")


def generate_item_with_upstream_option(yaml_manifest, option, value) -> dict:
"""
Generate a VS/VSR item with an upstream option.
Update all the upstreams in VS/VSR
:param yaml_manifest: an absolute path to a file
:param option:
:param value:
:return: dict
"""
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)
for upstream in dep['spec']['upstreams']:
upstream[option] = value
return dep
Loading

0 comments on commit f04246d

Please sign in to comment.