github.com/nginxinc/kubernetes-ingress@v1.12.5/tests/suite/test_v_s_route_canned_responses.py (about) 1 import pytest 2 import json 3 import requests 4 from kubernetes.client.rest import ApiException 5 6 from settings import TEST_DATA 7 from suite.custom_assertions import assert_event_and_get_count, wait_and_assert_status_code, \ 8 assert_event_count_increased, assert_event_starts_with_text_and_contains_errors 9 from suite.custom_resources_utils import get_vs_nginx_template_conf, patch_v_s_route_from_yaml 10 from suite.resources_utils import get_first_pod_name, get_events, wait_before_test 11 12 13 @pytest.mark.vsr 14 @pytest.mark.parametrize('crd_ingress_controller, v_s_route_setup', 15 [({"type": "complete", "extra_args": [f"-enable-custom-resources"]}, 16 {"example": "virtual-server-route-canned-responses"})], 17 indirect=True) 18 class TestVSRCannedResponses: 19 def test_config(self, kube_apis, ingress_controller_prerequisites, crd_ingress_controller, v_s_route_setup): 20 wait_before_test(1) 21 ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace) 22 config = get_vs_nginx_template_conf(kube_apis.v1, 23 v_s_route_setup.namespace, 24 v_s_route_setup.vs_name, 25 ic_pod_name, 26 ingress_controller_prerequisites.namespace) 27 assert "error_page 418 =407" in config and "error_page 418 =200" in config 28 29 def test_custom_canned_response(self, kube_apis, crd_ingress_controller, v_s_route_setup): 30 req_host = f"{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}" 31 req_url = f"http://{req_host}{v_s_route_setup.route_m.paths[0]}?arg1=arg" 32 wait_and_assert_status_code(407, req_url, v_s_route_setup.vs_host) 33 resp = requests.get(req_url, headers={"host": v_s_route_setup.vs_host}) 34 resp_content = json.loads(resp.content) 35 assert resp.headers['content-type'] == 'application/json' \ 36 and resp_content['host'] == v_s_route_setup.vs_host \ 37 and resp_content['request_time'] != "" \ 38 and resp_content['pid'] != "" \ 39 and resp_content['server_protocol'] == "HTTP/1.1" \ 40 and resp_content['connections_active'] != "" \ 41 and resp_content['connections_writing'] != "" \ 42 and resp_content['request_uri'] == f"{v_s_route_setup.route_m.paths[0]}?arg1=arg" \ 43 and resp_content['remote_addr'] != "" \ 44 and resp_content['remote_port'] != "" \ 45 and resp_content['server_addr'] != "" \ 46 and resp_content['request_method'] == "GET" \ 47 and resp_content['scheme'] == "http" \ 48 and resp_content['request_length'] != "" \ 49 and resp_content['nginx_version'] != "" \ 50 and resp_content['connection'] != "" \ 51 and resp_content['time_local'] != "" \ 52 and resp_content['server_port'] != "" \ 53 and resp_content['server_name'] == v_s_route_setup.vs_host \ 54 and resp_content['connections_waiting'] != "" \ 55 and resp_content['request_body'] == "" \ 56 and resp_content['args'] == "arg1=arg" \ 57 and resp_content['time_iso8601'] != "" \ 58 and resp_content['connections_reading'] != "" 59 60 def test_default_canned_response(self, kube_apis, crd_ingress_controller, v_s_route_setup): 61 req_host = f"{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}" 62 req_url = f"http://{req_host}{v_s_route_setup.route_m.paths[1]}" 63 wait_and_assert_status_code(200, req_url, v_s_route_setup.vs_host) 64 resp = requests.get(req_url, headers={"host": v_s_route_setup.vs_host}) 65 resp_content = resp.content.decode('utf-8') 66 assert resp.headers['content-type'] == 'text/plain' and resp_content == "line1\nline2\nline3" 67 68 def test_update(self, kube_apis, crd_ingress_controller, v_s_route_setup): 69 req_host = f"{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}" 70 req_url_1 = f"http://{req_host}{v_s_route_setup.route_m.paths[0]}" 71 req_url_2 = f"http://{req_host}{v_s_route_setup.route_m.paths[1]}" 72 wait_before_test(1) 73 vs_name = f"{v_s_route_setup.namespace}/{v_s_route_setup.vs_name}" 74 vsr_name = f"{v_s_route_setup.namespace}/{v_s_route_setup.route_m.name}" 75 vsr_event_text = f"Configuration for {vsr_name} was added or updated" 76 vs_event_text = f"Configuration for {vs_name} was added or updated" 77 events_ns = get_events(kube_apis.v1, v_s_route_setup.namespace) 78 initial_count_vs = assert_event_and_get_count(vs_event_text, events_ns) 79 initial_count_vsr = assert_event_and_get_count(vsr_event_text, events_ns) 80 vsr_src = f"{TEST_DATA}/virtual-server-route-canned-responses/route-multiple-updated.yaml" 81 patch_v_s_route_from_yaml(kube_apis.custom_objects, 82 v_s_route_setup.route_m.name, vsr_src, v_s_route_setup.namespace) 83 wait_and_assert_status_code(501, req_url_1, v_s_route_setup.vs_host) 84 resp = requests.get(req_url_1, headers={"host": v_s_route_setup.vs_host}) 85 resp_content = resp.content.decode('utf-8') 86 assert resp.headers['content-type'] == 'some/type' and resp_content == "{}" 87 88 wait_and_assert_status_code(201, req_url_2, v_s_route_setup.vs_host) 89 resp = requests.get(req_url_2, headers={"host": v_s_route_setup.vs_host}) 90 resp_content = resp.content.decode('utf-8') 91 assert resp.headers['content-type'] == 'user-type' and resp_content == "line1\nline2" 92 93 new_events_ns = get_events(kube_apis.v1, v_s_route_setup.namespace) 94 assert_event_count_increased(vs_event_text, initial_count_vs, new_events_ns) 95 assert_event_count_increased(vsr_event_text, initial_count_vsr, new_events_ns) 96 97 def test_validation_flow(self, kube_apis, crd_ingress_controller, v_s_route_setup): 98 invalid_fields = [ 99 "spec.subroutes[0].action.return.code", "spec.subroutes[0].action.return.body" 100 ] 101 req_host = f"{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}" 102 req_url = f"http://{req_host}{v_s_route_setup.route_s.paths[0]}" 103 text = f"{v_s_route_setup.namespace}/{v_s_route_setup.route_m.name}" 104 vsr_m_event_text = f"VirtualServerRoute {text} was rejected with error:" 105 vsr_src = f"{TEST_DATA}/virtual-server-route-canned-responses/route-multiple-invalid.yaml" 106 patch_v_s_route_from_yaml(kube_apis.custom_objects, 107 v_s_route_setup.route_m.name, vsr_src, v_s_route_setup.namespace) 108 wait_before_test(1) 109 110 wait_and_assert_status_code(404, req_url, v_s_route_setup.vs_host) 111 events = get_events(kube_apis.v1, v_s_route_setup.route_m.namespace) 112 assert_event_starts_with_text_and_contains_errors(vsr_m_event_text, events, invalid_fields) 113 114 def test_openapi_validation_flow(self, kube_apis, ingress_controller_prerequisites, 115 crd_ingress_controller, v_s_route_setup): 116 ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace) 117 config_old = get_vs_nginx_template_conf(kube_apis.v1, 118 v_s_route_setup.namespace, 119 v_s_route_setup.vs_name, 120 ic_pod_name, 121 ingress_controller_prerequisites.namespace) 122 vsr_src = f"{TEST_DATA}/virtual-server-route-canned-responses/route-multiple-invalid-openapi.yaml" 123 try: 124 patch_v_s_route_from_yaml(kube_apis.custom_objects, 125 v_s_route_setup.route_m.name, vsr_src, v_s_route_setup.namespace) 126 except ApiException as ex: 127 assert ex.status == 422 \ 128 and "spec.subroutes.action.return.type" in ex.body \ 129 and "spec.subroutes.action.return.body" in ex.body \ 130 and "spec.subroutes.action.return.code" in ex.body 131 except Exception as ex: 132 pytest.fail(f"An unexpected exception is raised: {ex}") 133 else: 134 pytest.fail("Expected an exception but there was none") 135 136 wait_before_test(1) 137 config_new = get_vs_nginx_template_conf(kube_apis.v1, 138 v_s_route_setup.namespace, 139 v_s_route_setup.vs_name, 140 ic_pod_name, 141 ingress_controller_prerequisites.namespace) 142 assert config_old == config_new, "Expected: config doesn't change"