Skip to content

Commit

Permalink
Fix imports and failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dubrovina authored and Dean-Coakley committed Mar 4, 2020
1 parent 37b79d7 commit d0acb21
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ spec:
- path: "/canned-defaults"
action:
return:
code:
type:
body: |
line1
line2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ spec:
action:
return:
code: 301
type: "text/html"
body:
type: "anything will do"
body: "Variables must be enclosed in curly brackets: $request_uri"
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ spec:
- path: "/default-redirect"
action:
redirect:
code:
url: "${scheme}://${host}${request_uri}?arg=${http_x_forwarded_proto}"
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ spec:
- path: "/default-redirect"
action:
redirect:
code:
code: 204
url: "${nginx_version}"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ spec:
- path: "/custom-redirect"
action:
redirect:
code:
url: "http://demo.nginx.com"
- path: "/default-redirect"
action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ spec:
action:
return:
code: 301
type: "text/html"
body:
type: "anything will do"
body: "Variables must be inclosed in curly brackets: $request_uri"
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ spec:
- path: "/backends/canned-defaults"
action:
return:
code:
type:
body: |
line1
line2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ spec:
- path: "/backends/default-redirect"
action:
redirect:
code:
code: 204
url: "${nginx_version}"
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ spec:
- path: "/backends/custom-redirect"
action:
redirect:
code:
url: "http://demo.nginx.com"
- path: "/backends/default-redirect"
action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ spec:
- path: "/backends/default-redirect"
action:
redirect:
code:
url: "${scheme}://${host}${request_uri}?arg=${http_x_forwarded_proto}"
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ spec:
statusMatch: "invalid"
slow-start: "1.5m"
queue:
size: 0
timeout: "hour"
subroutes:
- path: "/backends/backend1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ spec:
- name: backend1
service: backend1-svc
port: 80
tls:
enable:
- name: backend3
service: backend3-svc
port: 80
tls:
enable: False
subroutes:
- path: "/backends/backend1"
action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ spec:
- name: backend2
service: backend2-svc
port: 80
tls:
enable:
subroutes:
- path: "/backend2"
action:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ spec:
- name: backend2
service: backend2-svc
port: 80
tls:
enable:
- name: backend1
service: backend1-svc
port: 80
tls:
enable: False
routes:
- path: "/backend1"
action:
Expand Down
31 changes: 18 additions & 13 deletions tests/suite/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
ApiextensionsV1beta1Api, AppsV1Api
from kubernetes.client.rest import ApiException

from suite.custom_resources_utils import create_crd_from_yaml, create_virtual_server_from_yaml, \
delete_virtual_server, create_v_s_route_from_yaml, delete_v_s_route, delete_crd
from suite.custom_resources_utils import create_virtual_server_from_yaml, \
delete_virtual_server, create_v_s_route_from_yaml, delete_v_s_route, create_crd_from_yaml, delete_crd
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, \
patch_rbac, create_example_app, wait_until_all_pods_are_ready, delete_common_app, \
ensure_connection_to_public_endpoint, create_service_with_name, create_deployment_with_name, delete_deployment, \
delete_service, replace_configmap_from_yaml, delete_testing_namespaces, \
create_ingress_controller, delete_ingress_controller, configure_rbac, cleanup_rbac, \
create_service_from_yaml, get_service_node_ports, wait_for_public_ip, \
create_configmap_from_yaml, create_secret_from_yaml, \
get_first_vs_host_from_yaml, get_paths_from_vs_yaml, get_paths_from_vsr_yaml, get_name_from_yaml, \
get_route_namespace_from_vs_yaml
delete_service, replace_configmap_from_yaml, delete_testing_namespaces
from suite.resources_utils import create_ingress_controller, delete_ingress_controller, configure_rbac, cleanup_rbac
from suite.resources_utils import create_service_from_yaml, get_service_node_ports, wait_for_public_ip
from suite.resources_utils import create_configmap_from_yaml, create_secret_from_yaml
from suite.yaml_utils import get_first_vs_host_from_yaml, get_paths_from_vs_yaml, get_paths_from_vsr_yaml, \
get_route_namespace_from_vs_yaml, get_name_from_yaml

from settings import ALLOWED_SERVICE_TYPES, ALLOWED_IC_TYPES, DEPLOYMENTS, TEST_DATA, ALLOWED_DEPLOYMENT_TYPES

Expand Down Expand Up @@ -302,15 +302,18 @@ def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequi
"""
namespace = ingress_controller_prerequisites.namespace
name = "nginx-ingress"
crd_name = get_name_from_yaml(f"{DEPLOYMENTS}/common/common/vs-definition.yaml")
vs_crd_name = get_name_from_yaml(f"{DEPLOYMENTS}/common/vs-definition.yaml")
vsr_crd_name = get_name_from_yaml(f"{DEPLOYMENTS}/common/vsr-definition.yaml")

try:
print("------------------------- Update ClusterRole -----------------------------------")
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 -----------------------------------")
create_crd_from_yaml(kube_apis.api_extensions_v1_beta1, crd_name,
print("------------------------- Register CRDs -----------------------------------")
create_crd_from_yaml(kube_apis.api_extensions_v1_beta1, vs_crd_name,
f"{DEPLOYMENTS}/common/vs-definition.yaml")
create_crd_from_yaml(kube_apis.api_extensions_v1_beta1, vsr_crd_name,
f"{DEPLOYMENTS}/common/vsr-definition.yaml")
print("------------------------- Create IC -----------------------------------")
name = create_ingress_controller(kube_apis.v1, kube_apis.apps_v1_api, cli_arguments, namespace,
request.param.get('extra_args', None))
Expand All @@ -320,14 +323,16 @@ def crd_ingress_controller(cli_arguments, kube_apis, ingress_controller_prerequi
except ApiException as ex:
# Finalizer method doesn't start if fixture creation was incomplete, ensure clean up here
print(f"Failed to complete CRD IC fixture: {ex}\nClean up the cluster as much as possible.")
delete_crd(kube_apis.api_extensions_v1_beta1, crd_name)
delete_crd(kube_apis.api_extensions_v1_beta1, vs_crd_name)
delete_crd(kube_apis.api_extensions_v1_beta1, vsr_crd_name)
print("Restore the ClusterRole:")
patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml")
print("Remove the IC:")
delete_ingress_controller(kube_apis.apps_v1_api, name, cli_arguments['deployment-type'], namespace)

def fin():
delete_crd(kube_apis.api_extensions_v1_beta1, crd_name)
delete_crd(kube_apis.api_extensions_v1_beta1, vs_crd_name)
delete_crd(kube_apis.api_extensions_v1_beta1, vsr_crd_name)
print("Restore the ClusterRole:")
patch_rbac(kube_apis.rbac_v1_beta1, f"{DEPLOYMENTS}/rbac/rbac.yaml")
print("Remove the IC:")
Expand Down
33 changes: 33 additions & 0 deletions tests/suite/resources_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ def cleanup_rbac(rbac_v1_beta1: RbacAuthorizationV1beta1Api, rbac: RBACAuthoriza
rbac_v1_beta1.delete_cluster_role(rbac.role, delete_options)


def create_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 create_deployment(apps_v1_api, namespace, dep)


def create_deployment(apps_v1_api: AppsV1Api, namespace, body) -> str:
"""
Create a deployment based on a dict.
Expand Down Expand Up @@ -529,6 +544,24 @@ def create_namespace(v1: CoreV1Api, body) -> str:
return body['metadata']['name']


def create_namespace_with_name_from_yaml(v1: CoreV1Api, name, yaml_manifest) -> str:
"""
Create a namespace with a specific name based on a yaml manifest.
:param v1: CoreV1Api
:param name: name
:param yaml_manifest: an absolute path to file
:return: str
"""
print(f"Create a namespace with specific name:")
with open(yaml_manifest) as f:
dep = yaml.safe_load(f)
dep['metadata']['name'] = name
v1.create_namespace(dep)
print(f"Namespace created with name '{str(dep['metadata']['name'])}'")
return dep['metadata']['name']


def create_service_account(v1: CoreV1Api, namespace, body) -> None:
"""
Create a ServiceAccount based on a dict.
Expand Down
10 changes: 6 additions & 4 deletions tests/suite/test_v_s_route_canned_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from settings import TEST_DATA
from suite.custom_assertions import assert_event_and_get_count, wait_and_assert_status_code, \
assert_event, assert_event_count_increased
assert_event_count_increased, assert_event_starts_with_text_and_contains_errors
from suite.custom_resources_utils import get_vs_nginx_template_conf, patch_v_s_route_from_yaml
from suite.resources_utils import get_first_pod_name, get_events, wait_before_test

Expand Down Expand Up @@ -95,16 +95,18 @@ def test_update(self, kube_apis, crd_ingress_controller, v_s_route_setup):
assert_event_count_increased(vsr_event_text, initial_count_vsr, new_events_ns)

def test_validation_flow(self, kube_apis, crd_ingress_controller, v_s_route_setup):
invalid_fields = [
"spec.subroutes[0].action.return.code", "spec.subroutes[0].action.return.body"
]
req_host = f"{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}"
req_url = f"http://{req_host}{v_s_route_setup.route_s.paths[0]}"
text = f"{v_s_route_setup.namespace}/{v_s_route_setup.route_m.name}"
event_text = f"VirtualServerRoute {text} is invalid and was rejected: " \
f"spec.subroutes[0].action.return.body: Required value"
vsr_m_event_text = f"VirtualServerRoute {text} is invalid and was rejected: "
vsr_src = f"{TEST_DATA}/virtual-server-route-canned-responses/route-multiple-invalid.yaml"
patch_v_s_route_from_yaml(kube_apis.custom_objects,
v_s_route_setup.route_m.name, vsr_src, v_s_route_setup.namespace)
wait_before_test(1)

wait_and_assert_status_code(404, req_url, v_s_route_setup.vs_host)
events = get_events(kube_apis.v1, v_s_route_setup.route_m.namespace)
assert_event(event_text, events)
assert_event_starts_with_text_and_contains_errors(vsr_m_event_text, events, invalid_fields)
2 changes: 1 addition & 1 deletion tests/suite/test_v_s_route_focused_canary.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def __init__(self, namespace, vs_host, vs_name, route: VirtualServerRoute, backe
self.backends_url = backends_url


@pytest.mark.vsr
@pytest.fixture(scope="class")
def vsr_canary_setup(request, kube_apis,
ingress_controller_prerequisites, ingress_controller_endpoint) -> VSRAdvancedRoutingSetup:
Expand Down Expand Up @@ -109,6 +108,7 @@ def fin():
return VSRAdvancedRoutingSetup(ns_1, vs_host, vs_name, route, backends_url)


@pytest.mark.vsr
@pytest.mark.parametrize('crd_ingress_controller, vsr_canary_setup',
[({"type": "complete", "extra_args": [f"-enable-custom-resources"]},
{"example": "virtual-server-route-focused-canary"})],
Expand Down
15 changes: 10 additions & 5 deletions tests/suite/test_v_s_route_upstream_tls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import pytest
from kubernetes.client.rest import ApiException

from settings import TEST_DATA
from suite.custom_assertions import assert_event_and_get_count, assert_event_count_increased, assert_response_codes, \
Expand Down Expand Up @@ -84,16 +85,20 @@ def test_events_after_setup(self, kube_apis, ingress_controller_prerequisites,
assert_event(vsr_m_event_text, events_ns_m)
assert_event(vs_event_text, events_ns_m)

def test_invalid_value_rejection(self, kube_apis, ingress_controller_prerequisites,
def test_validation_flow(self, kube_apis, ingress_controller_prerequisites,
crd_ingress_controller,
v_s_route_setup, v_s_route_secure_app_setup):
ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace)
initial_events_ns_m = get_events(kube_apis.v1, v_s_route_setup.route_m.namespace)
initial_events_ns_s = get_events(kube_apis.v1, v_s_route_setup.route_s.namespace)
patch_v_s_route_from_yaml(kube_apis.custom_objects,
v_s_route_setup.route_s.name,
f"{TEST_DATA}/virtual-server-route-upstream-tls/route-single-invalid.yaml",
v_s_route_setup.route_s.namespace)
try:
patch_v_s_route_from_yaml(kube_apis.custom_objects,
v_s_route_setup.route_s.name,
f"{TEST_DATA}/virtual-server-route-upstream-tls/route-single-invalid.yaml",
v_s_route_setup.route_s.namespace)
except ApiException as ex:
assert ex.status == 422 and "spec.upstreams.tls.enable: Invalid value" in ex.body

wait_before_test(1)
config = get_vs_nginx_template_conf(kube_apis.v1,
v_s_route_setup.namespace,
Expand Down
2 changes: 1 addition & 1 deletion tests/suite/test_virtual_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_responses_after_rbac_misconfiguration_on_the_fly(self, kube_apis, crd_i

def test_responses_after_crd_removal_on_the_fly(self, kube_apis, crd_ingress_controller, virtual_server_setup):
print("\nStep 12: remove CRD and check")
crd_name = get_name_from_yaml(f"{DEPLOYMENTS}/common/common/vs-definition.yaml")
crd_name = get_name_from_yaml(f"{DEPLOYMENTS}/common/vs-definition.yaml")
delete_crd(kube_apis.api_extensions_v1_beta1, crd_name)
wait_and_assert_status_code(404, virtual_server_setup.backend_1_url, virtual_server_setup.vs_host)
wait_and_assert_status_code(404, virtual_server_setup.backend_2_url, virtual_server_setup.vs_host)
Expand Down
12 changes: 7 additions & 5 deletions tests/suite/test_virtual_server_canned_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import json

from settings import TEST_DATA
from suite.custom_assertions import wait_and_assert_status_code, assert_event, assert_event_and_get_count, \
assert_event_count_increased
from suite.custom_assertions import wait_and_assert_status_code, assert_event_and_get_count, \
assert_event_count_increased, assert_event_starts_with_text_and_contains_errors
from suite.custom_resources_utils import patch_virtual_server_from_yaml, get_vs_nginx_template_conf
from suite.resources_utils import get_first_pod_name, get_events, wait_before_test

Expand Down Expand Up @@ -83,14 +83,16 @@ def test_update(self, kube_apis, crd_ingress_controller, virtual_server_setup):
assert_event_count_increased(vs_event_text, initial_count, vs_events)

def test_validation_flow(self, kube_apis, crd_ingress_controller, virtual_server_setup):
invalid_fields = [
"spec.routes[0].action.return.code", "spec.routes[0].action.return.body"
]
text = f"{virtual_server_setup.namespace}/{virtual_server_setup.vs_name}"
event_text = f"VirtualServer {text} is invalid and was rejected: " \
f"spec.routes[0].action.return.body: Required value"
vs_event_text = f"VirtualServer {text} is invalid and was rejected: "
vs_src = f"{TEST_DATA}/virtual-server-canned-responses/virtual-server-invalid.yaml"
patch_virtual_server_from_yaml(kube_apis.custom_objects, virtual_server_setup.vs_name, vs_src,
virtual_server_setup.namespace)
wait_before_test(1)

wait_and_assert_status_code(404, virtual_server_setup.backend_1_url, virtual_server_setup.vs_host)
vs_events = get_events(kube_apis.v1, virtual_server_setup.namespace)
assert_event(event_text, vs_events)
assert_event_starts_with_text_and_contains_errors(vs_event_text, vs_events, invalid_fields)
15 changes: 10 additions & 5 deletions tests/suite/test_virtual_server_upstream_tls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import pytest
from kubernetes.client.rest import ApiException

from settings import TEST_DATA
from suite.custom_assertions import assert_event_and_get_count, assert_event_count_increased, assert_response_codes, \
Expand Down Expand Up @@ -39,14 +40,18 @@ def test_event_after_setup(self, kube_apis, ingress_controller_prerequisites,
events_vs = get_events(kube_apis.v1, virtual_server_setup.namespace)
assert_event(vs_event_text, events_vs)

def test_invalid_value_rejection(self, kube_apis, ingress_controller_prerequisites,
def test_validation_flow(self, kube_apis, ingress_controller_prerequisites,
crd_ingress_controller, virtual_server_setup):
ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace)
initial_events_vs = get_events(kube_apis.v1, virtual_server_setup.namespace)
patch_virtual_server_from_yaml(kube_apis.custom_objects,
virtual_server_setup.vs_name,
f"{TEST_DATA}/virtual-server-upstream-tls/virtual-server-invalid.yaml",
virtual_server_setup.namespace)
try:
patch_virtual_server_from_yaml(kube_apis.custom_objects,
virtual_server_setup.vs_name,
f"{TEST_DATA}/virtual-server-upstream-tls/virtual-server-invalid.yaml",
virtual_server_setup.namespace)
except ApiException as ex:
assert ex.status == 422 and "spec.upstreams.tls.enable: Invalid value" in ex.body

wait_before_test(1)
config = get_vs_nginx_template_conf(kube_apis.v1,
virtual_server_setup.namespace,
Expand Down
Loading

0 comments on commit d0acb21

Please sign in to comment.