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

     1  import pytest
     2  from settings import TEST_DATA, DEPLOYMENTS
     3  from suite.fixtures import PublicEndpoint
     4  from suite.resources_utils import create_items_from_yaml, delete_items_from_yaml, replace_configmap_from_yaml, \
     5      get_ingress_nginx_template_conf, get_first_pod_name, wait_before_test
     6  from suite.yaml_utils import get_first_ingress_host_from_yaml, get_name_from_yaml
     7  
     8  
     9  class CustomAnnotationsSetup:
    10      """
    11      Encapsulate Custom Annotations Example details.
    12  
    13      Attributes:
    14          public_endpoint (PublicEndpoint):
    15          namespace (str):
    16          ingress_host (str):
    17      """
    18      def __init__(self, public_endpoint: PublicEndpoint, ingress_name, namespace, ingress_host, ic_pod_name):
    19          self.public_endpoint = public_endpoint
    20          self.namespace = namespace
    21          self.ingress_name = ingress_name
    22          self.ingress_host = ingress_host
    23          self.ic_pod_name = ic_pod_name
    24          self.backend_1_url = f"http://{public_endpoint.public_ip}:{public_endpoint.port}/backend1"
    25          self.backend_2_url = f"http://{public_endpoint.public_ip}:{public_endpoint.port}/backend2"
    26  
    27  
    28  @pytest.fixture(scope="class")
    29  def custom_annotations_setup(request, kube_apis, ingress_controller_prerequisites,
    30                               ingress_controller_endpoint, test_namespace) -> CustomAnnotationsSetup:
    31      ing_type = request.param
    32      print("------------------------- Deploy ConfigMap with custom template -----------------------------------")
    33      replace_configmap_from_yaml(kube_apis.v1,
    34                                  ingress_controller_prerequisites.config_map['metadata']['name'],
    35                                  ingress_controller_prerequisites.namespace,
    36                                  f"{TEST_DATA}/custom-annotations/{ing_type}/nginx-config.yaml")
    37      print("------------------------- Deploy Custom Annotations Ingress -----------------------------------")
    38      ing_src = f"{TEST_DATA}/custom-annotations/{ing_type}/annotations-ingress.yaml"
    39      create_items_from_yaml(kube_apis, ing_src, test_namespace)
    40      host = get_first_ingress_host_from_yaml(ing_src)
    41      ingress_name = get_name_from_yaml(ing_src)
    42      wait_before_test(1)
    43  
    44      ic_pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace)
    45  
    46      def fin():
    47          print("Clean up Custom Annotations Example:")
    48          replace_configmap_from_yaml(kube_apis.v1,
    49                                      ingress_controller_prerequisites.config_map['metadata']['name'],
    50                                      ingress_controller_prerequisites.namespace,
    51                                      f"{DEPLOYMENTS}/common/nginx-config.yaml")
    52          delete_items_from_yaml(kube_apis, ing_src, test_namespace)
    53  
    54      request.addfinalizer(fin)
    55  
    56      return CustomAnnotationsSetup(ingress_controller_endpoint, ingress_name, test_namespace, host, ic_pod_name)
    57  
    58  
    59  @pytest.mark.ingresses
    60  @pytest.mark.parametrize('custom_annotations_setup, expected_texts',
    61                           [
    62                               pytest.param("standard",
    63                                            ["# This is TEST configuration for custom-annotations-ingress/",
    64                                             "# Insert config for test-split-feature: 192.168.1.1;",
    65                                             "# Insert config for test-split-feature: some-ip;",
    66                                             "# Insert config for feature A if the annotation is set",
    67                                             "# Print the value assigned to the annotation: 512"], id="standard-ingress"),
    68                               pytest.param("mergeable",
    69                                            ["# This is TEST configuration for custom-annotations-ingress-master/",
    70                                             "# Insert config for test-split-feature: master.ip;",
    71                                             "# Insert config for test-split-feature: master;",
    72                                             "# Insert config for test-split-feature minion: minion1;",
    73                                             "# Insert config for test-split-feature minion: minion2;",
    74                                             "# Insert config for feature A if the annotation is set",
    75                                             "# Print the value assigned to the annotation: 512"], id="mergeable-ingress")
    76                            ],
    77                           indirect=["custom_annotations_setup"])
    78  class TestCustomAnnotations:
    79      def test_nginx_config(self, kube_apis, ingress_controller_prerequisites,
    80                            ingress_controller, custom_annotations_setup, expected_texts):
    81          result_conf = get_ingress_nginx_template_conf(kube_apis.v1,
    82                                                        custom_annotations_setup.namespace,
    83                                                        custom_annotations_setup.ingress_name,
    84                                                        custom_annotations_setup.ic_pod_name,
    85                                                        ingress_controller_prerequisites.namespace)
    86          for line in expected_texts:
    87              assert line in result_conf