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

     1  import requests
     2  import pytest
     3  
     4  from suite.resources_utils import (
     5      ensure_connection_to_public_endpoint,
     6      create_items_from_yaml,
     7      create_example_app,
     8      delete_common_app,
     9      delete_items_from_yaml,
    10      wait_until_all_pods_are_ready,
    11      ensure_response_from_backend,
    12      wait_before_test,
    13  )
    14  from suite.yaml_utils import get_first_ingress_host_from_yaml
    15  from settings import TEST_DATA
    16  
    17  
    18  class BackendSetup:
    19      """
    20      Encapsulate the example details.
    21  
    22      Attributes:
    23          req_url (str):
    24          ingress_hosts (dict):
    25      """
    26  
    27      def __init__(self, req_url, ingress_hosts):
    28          self.req_url = req_url
    29          self.ingress_hosts = ingress_hosts
    30  
    31  
    32  ingresses_under_test = ["custom-class", "nginx-class", "no-class"]
    33  
    34  
    35  @pytest.fixture(scope="class")
    36  def backend_setup(request, kube_apis, ingress_controller_endpoint, test_namespace) -> BackendSetup:
    37      """
    38      Deploy simple application and all the Ingress resources under test in one namespace.
    39  
    40      :param request: pytest fixture
    41      :param kube_apis: client apis
    42      :param ingress_controller_endpoint: public endpoint
    43      :param test_namespace:
    44      :return: BackendSetup
    45      """
    46      print("------------------------- Deploy the backend -----------------------------------")
    47      create_example_app(kube_apis, "simple", test_namespace)
    48      req_url = f"http://{ingress_controller_endpoint.public_ip}:{ingress_controller_endpoint.port}/backend1"
    49      wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)
    50      ensure_connection_to_public_endpoint(
    51          ingress_controller_endpoint.public_ip,
    52          ingress_controller_endpoint.port,
    53          ingress_controller_endpoint.port_ssl,
    54      )
    55      print(
    56          "------------------------- Deploy ingresses under test -----------------------------------"
    57      )
    58      ingress_hosts = {}
    59      for item in ingresses_under_test:
    60          src_ing_yaml = f"{TEST_DATA}/ingress-class/{item}-ingress.yaml"
    61          create_items_from_yaml(kube_apis, src_ing_yaml, test_namespace)
    62          ingress_hosts[item] = get_first_ingress_host_from_yaml(src_ing_yaml)
    63      wait_before_test(2)
    64  
    65      def fin():
    66          print("Clean up:")
    67          delete_common_app(kube_apis, "simple", test_namespace)
    68          for item in ingresses_under_test:
    69              src_ing_yaml = f"{TEST_DATA}/ingress-class/{item}-ingress.yaml"
    70              delete_items_from_yaml(kube_apis, src_ing_yaml, test_namespace)
    71  
    72      request.addfinalizer(fin)
    73  
    74      return BackendSetup(req_url, ingress_hosts)
    75  
    76  
    77  @pytest.mark.ingresses
    78  class TestIngressClassArgs:
    79      @pytest.mark.parametrize(
    80          "ingress_controller, expected_responses",
    81          [
    82              pytest.param(
    83                  {"extra_args": ["-ingress-class=custom"]},
    84                  {"custom-class": 200, "nginx-class": 404, "no-class": 200},
    85                  id="custom-ingress-class",
    86              ),
    87              pytest.param(
    88                  {"extra_args": ["-use-ingress-class-only"]},
    89                  {"custom-class": 404, "nginx-class": 200, "no-class": 404},
    90                  id="use-ingress-class-only",
    91              ),
    92              pytest.param(
    93                  {"extra_args": ["-use-ingress-class-only", "-ingress-class=custom"]},
    94                  {"custom-class": 200, "nginx-class": 404, "no-class": 404},
    95                  id="both-args-set",
    96              ),
    97              pytest.param(
    98                  {"extra_args": None},
    99                  {"custom-class": 404, "nginx-class": 200, "no-class": 200},
   100                  id="no-args-set",
   101              ),
   102          ],
   103          indirect=["ingress_controller"],
   104      )
   105      def test_response_codes_117(
   106          self,
   107          ingress_controller,
   108          backend_setup,
   109          expected_responses,
   110          ingress_controller_prerequisites,
   111      ):
   112          """
   113          Checks for ingressClass behaviour in k8s < 1.18
   114          """
   115          if ingress_controller_prerequisites.minorVer < 18:
   116              for item in ingresses_under_test:
   117                  ensure_response_from_backend(
   118                      backend_setup.req_url, backend_setup.ingress_hosts[item]
   119                  )
   120                  resp = requests.get(
   121                      backend_setup.req_url, headers={"host": backend_setup.ingress_hosts[item]}
   122                  )
   123                  assert (
   124                      resp.status_code == expected_responses[item]
   125                  ), f"Expected: {expected_responses[item]} response code for {backend_setup.ingress_hosts[item]}"
   126          else:
   127              print(f"Skipping test because k8s version is >= 1.18")
   128  
   129      @pytest.mark.parametrize(
   130          "ingress_controller, expected_responses",
   131          [
   132              pytest.param(
   133                  {"extra_args": ["-ingress-class=custom"]},
   134                  {"custom-class": 200, "nginx-class": 404, "no-class": 404},
   135                  id="custom-ingress-class",
   136              ),
   137              pytest.param(
   138                  {"extra_args": ["-use-ingress-class-only"]},
   139                  {"custom-class": 404, "nginx-class": 200, "no-class": 404},
   140                  id="use-ingress-class-only",
   141              ),
   142              pytest.param(
   143                  {"extra_args": ["-use-ingress-class-only", "-ingress-class=custom"]},
   144                  {"custom-class": 200, "nginx-class": 404, "no-class": 404},
   145                  id="both-args-set",
   146              ),
   147              pytest.param(
   148                  {"extra_args": None},
   149                  {"custom-class": 404, "nginx-class": 200, "no-class": 404},
   150                  id="no-args-set",
   151              ),
   152          ],
   153          indirect=["ingress_controller"],
   154      )
   155      def test_response_codes_117_plus(
   156          self,
   157          ingress_controller,
   158          backend_setup,
   159          expected_responses,
   160          ingress_controller_prerequisites,
   161      ):
   162          """
   163          Checks for ingressClass behaviour in k8s >= 1.18
   164          """
   165          if ingress_controller_prerequisites.minorVer >= 18:
   166              for item in ingresses_under_test:
   167                  ensure_response_from_backend(
   168                      backend_setup.req_url, backend_setup.ingress_hosts[item]
   169                  )
   170                  resp = requests.get(
   171                      backend_setup.req_url, headers={"host": backend_setup.ingress_hosts[item]}
   172                  )
   173                  assert (
   174                      resp.status_code == expected_responses[item]
   175                  ), f"Expected: {expected_responses[item]} response code for {backend_setup.ingress_hosts[item]}"
   176          else:
   177              print(f"Skipping test because k8s version is < 1.18")