github.com/nginxinc/kubernetes-ingress@v1.12.5/tests/suite/test_watch_namespace.py (about)

     1  import requests
     2  import pytest
     3  
     4  from suite.resources_utils import ensure_connection_to_public_endpoint, create_items_from_yaml, create_example_app, \
     5      wait_until_all_pods_are_ready, ensure_response_from_backend, create_namespace_with_name_from_yaml, delete_namespace
     6  from suite.yaml_utils import get_first_ingress_host_from_yaml
     7  from settings import TEST_DATA
     8  
     9  
    10  class BackendSetup:
    11      """
    12      Encapsulate the example details.
    13  
    14      Attributes:
    15          req_url (str):
    16          ingress_hosts (dict):
    17      """
    18  
    19      def __init__(self, req_url, ingress_hosts):
    20          self.req_url = req_url
    21          self.ingress_hosts = ingress_hosts
    22  
    23  
    24  @pytest.fixture(scope="class")
    25  def backend_setup(request, kube_apis, ingress_controller_endpoint) -> BackendSetup:
    26      """
    27      Create 2 namespaces and deploy simple applications in them.
    28  
    29      :param request: pytest fixture
    30      :param kube_apis: client apis
    31      :param ingress_controller_endpoint: public endpoint
    32      :return: BackendSetup
    33      """
    34      watched_namespace = create_namespace_with_name_from_yaml(kube_apis.v1,
    35                                                               f"watched-ns", f"{TEST_DATA}/common/ns.yaml")
    36      foreign_namespace = create_namespace_with_name_from_yaml(kube_apis.v1,
    37                                                               f"foreign-ns", f"{TEST_DATA}/common/ns.yaml")
    38      ingress_hosts = {}
    39      for ns in [watched_namespace, foreign_namespace]:
    40          print(f"------------------------- Deploy the backend in {ns} -----------------------------------")
    41          create_example_app(kube_apis, "simple", ns)
    42          src_ing_yaml = f"{TEST_DATA}/watch-namespace/{ns}-ingress.yaml"
    43          create_items_from_yaml(kube_apis, src_ing_yaml, ns)
    44          ingress_host = get_first_ingress_host_from_yaml(src_ing_yaml)
    45          ingress_hosts[f"{ns}-ingress"] = ingress_host
    46          req_url = f"http://{ingress_controller_endpoint.public_ip}:{ingress_controller_endpoint.port}/backend1"
    47          wait_until_all_pods_are_ready(kube_apis.v1, ns)
    48          ensure_connection_to_public_endpoint(ingress_controller_endpoint.public_ip,
    49                                               ingress_controller_endpoint.port,
    50                                               ingress_controller_endpoint.port_ssl)
    51  
    52      def fin():
    53          print("Clean up:")
    54          delete_namespace(kube_apis.v1, watched_namespace)
    55          delete_namespace(kube_apis.v1, foreign_namespace)
    56  
    57      request.addfinalizer(fin)
    58  
    59      return BackendSetup(req_url, ingress_hosts)
    60  
    61  
    62  @pytest.mark.ingresses
    63  @pytest.mark.parametrize('ingress_controller, expected_responses',
    64                           [
    65                               pytest.param({"extra_args": ["-watch-namespace=watched-ns"]},
    66                                            {"watched-ns-ingress": 200, "foreign-ns-ingress": 404})
    67                            ],
    68                           indirect=["ingress_controller"])
    69  class TestWatchNamespace:
    70      def test_response_codes(self, ingress_controller, backend_setup, expected_responses):
    71          for ing in ["watched-ns-ingress", "foreign-ns-ingress"]:
    72              ensure_response_from_backend(backend_setup.req_url, backend_setup.ingress_hosts[ing])
    73              resp = requests.get(backend_setup.req_url, headers={"host": backend_setup.ingress_hosts[ing]})
    74              assert resp.status_code == expected_responses[ing],\
    75                  f"Expected: {expected_responses[ing]} response code for {backend_setup.ingress_hosts[ing]}"