Skip to content

Commit

Permalink
Add test for TransportServer load balancing config
Browse files Browse the repository at this point in the history
  • Loading branch information
soneillf5 committed Apr 19, 2021
1 parent 00d9097 commit 61410b4
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 6 deletions.
3 changes: 0 additions & 3 deletions tests/data/transport-server-status/standard/dns.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ spec:
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 5353
name: dns
protocol: UDP
- containerPort: 5353
name: dns-tcp
protocol: TCP
Expand Down
35 changes: 35 additions & 0 deletions tests/data/transport-server-tcp-load-balance/more-replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
spec:
replicas: 3
selector:
matchLabels:
app: coredns
template:
metadata:
labels:
app: coredns
spec:
containers:
- name: coredns
image: coredns/coredns:1.6.7
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 5353
name: dns-tcp
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
35 changes: 35 additions & 0 deletions tests/data/transport-server-tcp-load-balance/standard/dns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
spec:
replicas: 2
selector:
matchLabels:
app: coredns
template:
metadata:
labels:
app: coredns
spec:
containers:
- name: coredns
image: coredns/coredns:1.6.7
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 5353
name: dns-tcp
protocol: TCP
securityContext:
readOnlyRootFilesystem: true
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
15 changes: 12 additions & 3 deletions tests/suite/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
delete_service,
replace_configmap_from_yaml,
delete_testing_namespaces,
get_first_pod_name,
)
from suite.resources_utils import (
create_ingress_controller,
Expand Down Expand Up @@ -742,18 +743,22 @@ class TransportServerSetup:
namespace (str):
"""

def __init__(self, name, namespace):
def __init__(self, name, namespace, ingress_name, ingress_pod_name, ic_namespace):
self.name = name
self.namespace = namespace
self.ingress_name = ingress_name
self.ingress_pod_name = ingress_pod_name
self.ic_namespace = ic_namespace


@pytest.fixture(scope="class")
def transport_server_setup(
request, kube_apis, test_namespace
request, kube_apis, ingress_controller_prerequisites, test_namespace
) -> TransportServerSetup:
"""
Prepare Transport Server Example.
:param ingress_controller_prerequisites:
:param request: internal pytest fixture to parametrize this method
:param kube_apis: client apis
:param test_namespace:
Expand Down Expand Up @@ -785,7 +790,11 @@ def fin():

request.addfinalizer(fin)

return TransportServerSetup(ts_resource['metadata']['name'], test_namespace)
ingress_name = "nginx-ingress"
ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace)
ic_namespace = ingress_controller_prerequisites.namespace

return TransportServerSetup(ts_resource['metadata']['name'], test_namespace, ingress_name, ic_pod_name, ic_namespace)


@pytest.fixture(scope="class")
Expand Down
45 changes: 45 additions & 0 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ def create_deployment_from_yaml(apps_v1_api: AppsV1Api, namespace, yaml_manifest
return create_deployment(apps_v1_api, namespace, dep)


def patch_deployment_from_yaml(apps_v1_api: AppsV1Api, namespace, yaml_manifest) -> str:
"""
Create a deployment based on yaml file.
:param apps_v1_api: AppsV1Api
:param namespace: namespace name
:param yaml_manifest: absolute path to file
:return: str
"""
print(f"Load {yaml_manifest}")
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)
return patch_deployment(apps_v1_api, namespace, dep)


def patch_deployment(apps_v1_api: AppsV1Api, namespace, body) -> str:
"""
Create a deployment based on a dict.
:param apps_v1_api: AppsV1Api
:param namespace: namespace name
:param body: dict
:return: str
"""
print("Patch a deployment:")
apps_v1_api.patch_namespaced_deployment(body['metadata']['name'], namespace, body)
print(f"Deployment patched with name '{body['metadata']['name']}'")
return body['metadata']['name']


def create_deployment(apps_v1_api: AppsV1Api, namespace, body) -> str:
"""
Create a deployment based on a dict.
Expand Down Expand Up @@ -750,6 +780,21 @@ def get_ingress_nginx_template_conf(v1: CoreV1Api, ingress_namespace, ingress_na
return get_file_contents(v1, file_path, pod_name, pod_namespace)


def get_ingress_nginx_template_ts_conf(v1: CoreV1Api, resource_namespace, resource_name, ingress_name, ingress_namespace) -> str:
"""
Get contents of /etc/nginx/conf.d/{namespace}-{ingress_name}.conf in the pod.
:param v1: CoreV1Api
:param resource_namespace:
:param resource_name:
:param ingress_name:
:param ingress_namespace:
:return: str
"""
file_path = f"/etc/nginx/stream-conf.d/ts_{resource_namespace}_{resource_name}.conf"
return get_file_contents(v1, file_path, ingress_name, ingress_namespace)


def create_example_app(kube_apis, app_type, namespace) -> None:
"""
Create a backend application.
Expand Down
64 changes: 64 additions & 0 deletions tests/suite/test_transport_server_tcp_load_balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import pytest
import re
from settings import TEST_DATA
from suite.resources_utils import get_ingress_nginx_template_ts_conf
from suite.resources_utils import (
wait_before_test,
patch_deployment_from_yaml
)


@pytest.mark.ts
@pytest.mark.parametrize(
"crd_ingress_controller, transport_server_setup",
[
(
{
"type": "complete",
"extra_args":
[
"-enable-custom-resources",
"-global-configuration=nginx-ingress/nginx-configuration",
"-enable-leader-election=false"
]
},
{"example": "transport-server-status", "app_type": "simple"},
)
],
indirect=True,
)
class TestTransportServerTcpLoadBalance:

def restore_service(self, kube_apis, transport_server_setup) -> None:
"""
Function to revert a TransportServer resource to a valid state.
"""
dns_file = f"{TEST_DATA}/transport-server-tcp-load-balance/standard/dns.yaml"
patch_deployment_from_yaml(kube_apis.apps_v1_api, transport_server_setup.namespace, dns_file)

@pytest.mark.sean
def test_number_of_replicas(
self, kube_apis, crd_ingress_controller, transport_server_setup, ingress_controller_prerequisites
):
"""
The load balancing of TCP should result in 3 servers to match the 3 replicas of a service.
"""
dns_file = f"{TEST_DATA}/transport-server-tcp-load-balance/more-replicas.yaml"
patch_deployment_from_yaml(kube_apis.apps_v1_api, transport_server_setup.namespace, dns_file)

wait_before_test()

result_conf = get_ingress_nginx_template_ts_conf(
kube_apis.v1,
transport_server_setup.namespace,
transport_server_setup.name,
transport_server_setup.ingress_pod_name,
ingress_controller_prerequisites.namespace
)

pattern = 'server.*max_fails=1 fail_timeout=10s;'
num_servers = len(re.findall(pattern, result_conf))

assert num_servers is 3

self.restore_service(kube_apis, transport_server_setup)

0 comments on commit 61410b4

Please sign in to comment.