Skip to content

Commit

Permalink
Refactor code of CommonApp methods
Browse files Browse the repository at this point in the history
Extend methods to create/delete any kind of items in app.yaml (not just Service&Deployment)
  • Loading branch information
tellet committed Aug 19, 2019
1 parent df8c9bc commit 6852528
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 47 deletions.
4 changes: 2 additions & 2 deletions tests/suite/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ def virtual_server_setup(request, kube_apis, crd_ingress_controller, ingress_con
test_namespace)
vs_host = get_first_vs_host_from_yaml(vs_source)
vs_paths = get_paths_from_vs_yaml(vs_source)
common_app = create_example_app(kube_apis, request.param['app_type'], test_namespace)
create_example_app(kube_apis, request.param['app_type'], test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)

def fin():
print("Clean up Virtual Server Example:")
delete_virtual_server(kube_apis.custom_objects, vs_name, test_namespace)
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, request.param['app_type'], test_namespace)

request.addfinalizer(fin)

Expand Down
61 changes: 26 additions & 35 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from kubernetes.stream import stream
from kubernetes import client
from settings import TEST_DATA, RECONFIGURATION_DELAY, DEPLOYMENTS
from suite.yaml_utils import get_names_from_yaml


class RBACAuthorization:
Expand Down Expand Up @@ -599,6 +598,24 @@ def replace_configmap(v1: CoreV1Api, name, namespace, body) -> None:
print("ConfigMap replaced")


def delete_configmap(v1: CoreV1Api, name, namespace) -> None:
"""
Delete a ConfigMap.
:param v1: CoreV1Api
:param name: ConfigMap name
:param namespace: namespace name
:return:
"""
print(f"Delete a ConfigMap: {name}")
delete_options = client.V1DeleteOptions()
delete_options.grace_period_seconds = 0
delete_options.propagation_policy = 'Foreground'
v1.delete_namespaced_config_map(name, namespace, delete_options)
ensure_item_removal(v1.read_namespaced_config_map, name, namespace)
print(f"ConfigMap was removed with name '{name}'")


def delete_namespace(v1: CoreV1Api, namespace) -> None:
"""
Delete a namespace.
Expand Down Expand Up @@ -653,20 +670,7 @@ def get_ingress_nginx_template_conf(v1: CoreV1Api, ingress_namespace, ingress_na
return get_file_contents(v1, file_path, pod_name, pod_namespace)


class CommonApp:
"""
Encapsulate Common Application details.
Attributes:
services ([]): list of services names
deployments ([]): list of deployments names
"""
def __init__(self, services, deployments):
self.services = services
self.deployments = deployments


def create_example_app(kube_apis, app_type, namespace) -> CommonApp:
def create_example_app(kube_apis, app_type, namespace) -> None:
"""
Create a backend application.
Expand All @@ -675,36 +679,21 @@ 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
:return: CommonApp
:return:
"""
create_items_from_yaml(kube_apis, f"{TEST_DATA}/common/app/{app_type}/app.yaml", namespace)
all_names = get_names_from_yaml(f"{TEST_DATA}/common/app/{app_type}/app.yaml")
service_names = []
dep_names = []
for _ in all_names:
if "svc" in _:
service_names.append(_)
else:
dep_names.append(_)
return CommonApp(service_names, dep_names)


def delete_common_app(v1: CoreV1Api,
apps_v1_api: AppsV1Api,
common_app: CommonApp, namespace) -> None:
def delete_common_app(kube_apis, app_type, namespace) -> None:
"""
Delete a common simple application.
:param v1: CoreV1Api
:param apps_v1_api: AppsV1Api
:param common_app: CommonApp
:param kube_apis:
:param app_type:
:param namespace: namespace name
:return:
"""
for deployment in common_app.deployments:
delete_deployment(apps_v1_api, deployment, namespace)
for svc in common_app.services:
delete_service(v1, svc, namespace)
delete_items_from_yaml(kube_apis, f"{TEST_DATA}/common/app/{app_type}/app.yaml", namespace)


def delete_service(v1: CoreV1Api, name, namespace) -> None:
Expand Down Expand Up @@ -889,6 +878,8 @@ def delete_items_from_yaml(kube_apis, yaml_manifest, namespace) -> None:
delete_deployment(kube_apis.apps_v1_api, doc['metadata']['name'], namespace)
elif doc["kind"] == "DaemonSet":
delete_daemon_set(kube_apis.apps_v1_api, doc['metadata']['name'], namespace)
elif doc["kind"] == "ConfigMap":
delete_configmap(kube_apis.v1, doc['metadata']['name'], namespace)


def ensure_connection(request_url) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def annotations_setup(request,
test_namespace)
ingress_name = get_names_from_yaml(f"{TEST_DATA}/annotations/{request.param}/annotations-ingress.yaml")[0]
ingress_host = get_first_ingress_host_from_yaml(f"{TEST_DATA}/annotations/{request.param}/annotations-ingress.yaml")
common_app = create_example_app(kube_apis, "simple", test_namespace)
create_example_app(kube_apis, "simple", test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)
ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip,
ingress_controller_endpoint.port,
Expand All @@ -97,7 +97,7 @@ def fin():
ingress_controller_prerequisites.config_map['metadata']['name'],
ingress_controller_prerequisites.namespace,
f"{DEPLOYMENTS}/common/nginx-config.yaml")
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, "simple", test_namespace)
delete_items_from_yaml(kube_apis,
f"{TEST_DATA}/annotations/{request.param}/annotations-ingress.yaml",
test_namespace)
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_jwt_auth_mergeable.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def jwt_auth_setup(request, kube_apis, ingress_controller_endpoint, ingress_cont
print("------------------------- Deploy JWT Auth Mergeable Minions Example -----------------------------------")
create_items_from_yaml(kube_apis, f"{TEST_DATA}/jwt-auth-mergeable/mergeable/jwt-auth-ingress.yaml", test_namespace)
ingress_host = get_first_ingress_host_from_yaml(f"{TEST_DATA}/jwt-auth-mergeable/mergeable/jwt-auth-ingress.yaml")
common_app = create_example_app(kube_apis, "simple", test_namespace)
create_example_app(kube_apis, "simple", test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)
ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip,
ingress_controller_endpoint.port,
Expand All @@ -56,7 +56,7 @@ def fin():
delete_secret(kube_apis.v1, minion_secret_name, test_namespace)

print("Clean up the JWT Auth Mergeable Minions Application:")
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, "simple", test_namespace)
delete_items_from_yaml(kube_apis, f"{TEST_DATA}/jwt-auth-mergeable/mergeable/jwt-auth-ingress.yaml",
test_namespace)

Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_jwt_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def jwt_secrets_setup(request, kube_apis, ingress_controller_endpoint, ingress_c
print("------------------------- Deploy JWT Secrets Example -----------------------------------")
create_items_from_yaml(kube_apis, f"{TEST_DATA}/jwt-secrets/{request.param}/jwt-secrets-ingress.yaml", test_namespace)
ingress_host = get_first_ingress_host_from_yaml(f"{TEST_DATA}/jwt-secrets/{request.param}/jwt-secrets-ingress.yaml")
common_app = create_example_app(kube_apis, "simple", test_namespace)
create_example_app(kube_apis, "simple", test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)
ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip,
ingress_controller_endpoint.port,
ingress_controller_endpoint.port_ssl)

def fin():
print("Clean up the JWT Secrets Application:")
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, "simple", test_namespace)
delete_items_from_yaml(kube_apis, f"{TEST_DATA}/jwt-secrets/{request.param}/jwt-secrets-ingress.yaml",
test_namespace)

Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ def smoke_setup(request, kube_apis, ingress_controller_endpoint, ingress_control
secret_name = create_secret_from_yaml(kube_apis.v1, test_namespace, f"{TEST_DATA}/smoke/smoke-secret.yaml")
create_items_from_yaml(kube_apis, f"{TEST_DATA}/smoke/{request.param}/smoke-ingress.yaml", test_namespace)
ingress_host = get_first_ingress_host_from_yaml(f"{TEST_DATA}/smoke/{request.param}/smoke-ingress.yaml")
common_app = create_example_app(kube_apis, "simple", test_namespace)
create_example_app(kube_apis, "simple", test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)
ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip,
ingress_controller_endpoint.port,
ingress_controller_endpoint.port_ssl)

def fin():
print("Clean up the Smoke Application:")
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, "simple", test_namespace)
delete_items_from_yaml(kube_apis, f"{TEST_DATA}/smoke/{request.param}/smoke-ingress.yaml",
test_namespace)
delete_secret(kube_apis.v1, secret_name, test_namespace)
Expand Down
4 changes: 2 additions & 2 deletions tests/suite/test_wildcard_tls_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def wildcard_tls_secret_setup(request, kube_apis, ingress_controller_endpoint, t
create_items_from_yaml(kube_apis,
f"{TEST_DATA}/wildcard-tls-secret/{ing_type}/wildcard-secret-ingress.yaml", test_namespace)
host = get_first_ingress_host_from_yaml(f"{TEST_DATA}/wildcard-tls-secret/{ing_type}/wildcard-secret-ingress.yaml")
common_app = create_example_app(kube_apis, "simple", test_namespace)
create_example_app(kube_apis, "simple", test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)

def fin():
print("Clean up Wildcard-Tls-Secret-Example:")
delete_items_from_yaml(kube_apis,
f"{TEST_DATA}/wildcard-tls-secret/{ing_type}/wildcard-secret-ingress.yaml",
test_namespace)
delete_common_app(kube_apis.v1, kube_apis.apps_v1_api, common_app, test_namespace)
delete_common_app(kube_apis, "simple", test_namespace)

request.addfinalizer(fin)

Expand Down

0 comments on commit 6852528

Please sign in to comment.