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

     1  """Describe the custom assertion methods"""
     2  import time
     3  
     4  import pytest
     5  import requests
     6  
     7  from suite.custom_resources_utils import get_vs_nginx_template_conf
     8  from suite.resources_utils import get_events
     9  
    10  
    11  def assert_no_new_events(old_list, new_list):
    12      assert len(old_list) == len(new_list), "Expected: lists are of the same size"
    13      for i in range(len(new_list) - 1, -1, -1):
    14          if old_list[i].count != new_list[i].count:
    15              pytest.fail(f"Expected: no new events. There is a new event found:\"{new_list[i].message}\". Exiting...")
    16  
    17  
    18  def assert_event_count_increased(event_text, count, events_list) -> None:
    19      """
    20      Search for the event in the list and verify its counter is more than the expected value.
    21  
    22      :param event_text: event text
    23      :param count: expected value
    24      :param events_list: list of events
    25      :return:
    26      """
    27      for i in range(len(events_list) - 1, -1, -1):
    28          if event_text in events_list[i].message:
    29              assert events_list[i].count > count
    30              return
    31      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
    32  
    33  
    34  def assert_event_and_count(event_text, count, events_list) -> None:
    35      """
    36      Search for the event in the list and compare its counter with an expected value.
    37  
    38      :param event_text: event text
    39      :param count: expected value
    40      :param events_list: list of events
    41      :return:
    42      """
    43      for i in range(len(events_list) - 1, -1, -1):
    44          if event_text in events_list[i].message:
    45              assert events_list[i].count == count
    46              return
    47      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
    48  
    49  
    50  def assert_event_with_full_equality_and_count(event_text, count, events_list) -> None:
    51      """
    52      Search for the event in the list and compare its counter with an expected value.
    53  
    54      :param event_text: event text
    55      :param count: expected value
    56      :param events_list: list of events
    57      :return:
    58      """
    59  
    60      for i in range(len(events_list) - 1, -1, -1):
    61          # some events have trailing whitespace
    62          message_stripped = events_list[i].message.rstrip()
    63  
    64          if event_text == message_stripped:
    65              assert events_list[i].count == count
    66              return
    67      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
    68  
    69  
    70  def assert_event_and_get_count(event_text, events_list) -> int:
    71      """
    72      Search for the event in the list and return its counter.
    73  
    74      :param event_text: event text
    75      :param events_list: list of events
    76      :return: event.count
    77      """
    78      for i in range(len(events_list) - 1, -1, -1):
    79          if event_text in events_list[i].message:
    80              return events_list[i].count
    81      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
    82  
    83  
    84  def get_event_count(event_text, events_list) -> int:
    85      """
    86      Search for the event in the list and return its counter.
    87  
    88      :param event_text: event text
    89      :param events_list: list of events
    90      :return: (int)
    91      """
    92      for i in range(len(events_list) - 1, -1, -1):
    93          if event_text in events_list[i].message:
    94              return events_list[i].count
    95      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
    96  
    97  
    98  def wait_for_event_count_increases(kube_apis, event_text, initial_count, events_namespace) -> None:
    99      """
   100      Wait for the event counter to get bigger than the initial value.
   101  
   102      :param kube_apis: KubeApis
   103      :param event_text: event text
   104      :param initial_count: expected value
   105      :param events_namespace: namespace to fetch events
   106      :return:
   107      """
   108      events_list = get_events(kube_apis.v1, events_namespace)
   109      count = get_event_count(event_text, events_list)
   110      counter = 0
   111      while count <= initial_count and counter < 4:
   112          time.sleep(1)
   113          counter = counter + 1
   114          events_list = get_events(kube_apis.v1, events_namespace)
   115          count = get_event_count(event_text, events_list)
   116      assert count > initial_count, f"After several seconds the event counter has not increased \"{event_text}\""
   117  
   118  
   119  def assert_response_codes(resp_1, resp_2, code_1=200, code_2=200) -> None:
   120      """
   121      Assert responses status codes.
   122  
   123      :param resp_1: Response
   124      :param resp_2: Response
   125      :param code_1: expected status code
   126      :param code_2: expected status code
   127      :return:
   128      """
   129      assert resp_1.status_code == code_1
   130      assert resp_2.status_code == code_2
   131  
   132  
   133  def assert_event(event_text, events_list) -> None:
   134      """
   135      Search for the event in the list.
   136  
   137      :param event_text: event text
   138      :param events_list: list of events
   139      :return:
   140      """
   141      for i in range(len(events_list) - 1, -1, -1):
   142          if event_text in events_list[i].message:
   143              return
   144      pytest.fail(f"Failed to find the event \"{event_text}\" in the list. Exiting...")
   145  
   146  
   147  def assert_event_starts_with_text_and_contains_errors(event_text, events_list, fields_list) -> None:
   148      """
   149      Search for the event starting with the expected text in the list and check its message.
   150  
   151      :param event_text: event text
   152      :param events_list: list of events
   153      :param fields_list: expected message contents
   154      :return:
   155      """
   156      for i in range(len(events_list) - 1, -1, -1):
   157          if str(events_list[i].message).startswith(event_text):
   158              for field_error in fields_list:
   159                  assert field_error in events_list[i].message
   160              return
   161      pytest.fail(f"Failed to find the event starting with \"{event_text}\" in the list. Exiting...")
   162  
   163  
   164  def assert_vs_conf_not_exists(kube_apis, ic_pod_name, ic_namespace, virtual_server_setup):
   165      new_response = get_vs_nginx_template_conf(kube_apis.v1,
   166                                                virtual_server_setup.namespace,
   167                                                virtual_server_setup.vs_name,
   168                                                ic_pod_name,
   169                                                ic_namespace)
   170      assert "No such file or directory" in new_response
   171  
   172  
   173  def assert_vs_conf_exists(kube_apis, ic_pod_name, ic_namespace, virtual_server_setup):
   174      new_response = get_vs_nginx_template_conf(kube_apis.v1,
   175                                                virtual_server_setup.namespace,
   176                                                virtual_server_setup.vs_name,
   177                                                ic_pod_name,
   178                                                ic_namespace)
   179      assert "No such file or directory" not in new_response
   180  
   181  
   182  def wait_and_assert_status_code(code, req_url, host, **kwargs) -> None:
   183      """
   184      Wait for a specific response status code.
   185  
   186      :param  code: status_code
   187      :param  req_url: request url
   188      :param  host: request headers if any
   189      :paramv **kwargs: optional arguments that ``request`` takes
   190      :return:
   191      """
   192      counter = 0
   193      resp = requests.get(req_url, headers={"host": host}, **kwargs)
   194      while not resp.status_code == code and counter <= 30:
   195          time.sleep(1)
   196          counter = counter + 1
   197          resp = requests.get(req_url, headers={"host": host}, **kwargs)
   198      assert resp.status_code == code, f"After 30 seconds the status_code is still not {code}"
   199  
   200      
   201