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

     1  import pytest
     2  import time
     3  
     4  from suite.resources_utils import get_file_contents
     5  from suite.resources_utils import get_first_pod_name
     6  from suite.resources_utils import create_ingress_from_yaml
     7  from suite.resources_utils import delete_ingress
     8  from suite.resources_utils import get_events
     9  from suite.custom_assertions import assert_event
    10  
    11  from settings import TEST_DATA
    12  
    13  @pytest.mark.ingresses
    14  class TestSnippetAnnotation:
    15  
    16      """
    17      Checks if ingress snippets are enabled as a cli arg, that the value from a snippet annotation defined in an
    18      ingress resource is set in the nginx conf.
    19      """
    20      @pytest.mark.parametrize('ingress_controller',
    21                               [
    22                                   pytest.param({"extra_args": ["-enable-snippets=true"]}),
    23                               ],
    24                               indirect=["ingress_controller"])
    25      def test_snippet_annotation_used(self, kube_apis, ingress_controller_prerequisites, ingress_controller, test_namespace):
    26          file_name = f"{TEST_DATA}/annotations/standard/annotations-ingress-snippets.yaml"
    27          ingress_name = create_ingress_from_yaml(kube_apis.extensions_v1_beta1, test_namespace, file_name)
    28          time.sleep(5)
    29          pod_namespace = ingress_controller_prerequisites.namespace
    30          pod_name = get_first_pod_name(kube_apis.v1, ingress_controller_prerequisites.namespace)
    31          file_path = f"/etc/nginx/conf.d/{test_namespace}-{ingress_name}.conf"
    32          result_conf = get_file_contents(kube_apis.v1, file_path, pod_name, pod_namespace)
    33          snippet_annotation = "tcp_nodelay on;"
    34          assert snippet_annotation in result_conf, f"failed to find snippet ({snippet_annotation}) in nginx conf"
    35  
    36          # Now we assert the status of the ingress is correct
    37          event_text = f"Configuration for {test_namespace}/{ingress_name} was added or updated"
    38          events = get_events(kube_apis.v1, test_namespace)
    39          assert_event(event_text, events)
    40  
    41          delete_ingress(kube_apis.extensions_v1_beta1, ingress_name, test_namespace)
    42  
    43      """
    44      Checks if ingress snippets are disabled as a cli arg, that the value of the snippet annotation on an ingress
    45      resource is ignored and does not get set in the nginx conf.
    46      """
    47      @pytest.mark.parametrize('ingress_controller',
    48                               [
    49                                   pytest.param({"extra_args": ["-enable-snippets=false"]}),
    50                               ],
    51                               indirect=["ingress_controller"])
    52      def test_snippet_annotation_ignored(self, kube_apis, ingress_controller_prerequisites, ingress_controller, test_namespace):
    53          file_name = f"{TEST_DATA}/annotations/standard/annotations-ingress-snippets.yaml"
    54          create_ingress_from_yaml(kube_apis.extensions_v1_beta1, test_namespace, file_name)
    55          time.sleep(5)
    56  
    57          # Now we assert the status of the ingress has correctly added a warning
    58          event_text = f"annotations.nginx.org/server-snippets: Forbidden: snippet specified but snippets feature is not enabled"
    59          events = get_events(kube_apis.v1, test_namespace)
    60          assert_event(event_text, events)
    61