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

     1  """Describe overall framework configuration."""
     2  
     3  import os
     4  import pytest
     5  import sys
     6  
     7  sys.path.insert(0, "../tests")
     8  from kubernetes.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
     9  from settings import (
    10      DEFAULT_IMAGE,
    11      DEFAULT_PULL_POLICY,
    12      DEFAULT_IC_TYPE,
    13      DEFAULT_SERVICE,
    14      DEFAULT_DEPLOYMENT_TYPE,
    15  )
    16  from suite.resources_utils import get_first_pod_name
    17  
    18  
    19  def pytest_addoption(parser) -> None:
    20      """Get cli-arguments.
    21  
    22      :param parser: pytest parser
    23      :return:
    24      """
    25      parser.addoption(
    26          "--context", action="store", default="", help="The context to use in the kubeconfig file.",
    27      )
    28      parser.addoption(
    29          "--image", action="store", default=DEFAULT_IMAGE, help="The Ingress Controller image.",
    30      )
    31      parser.addoption(
    32          "--image-pull-policy",
    33          action="store",
    34          default=DEFAULT_PULL_POLICY,
    35          help="The pull policy of the Ingress Controller image.",
    36      )
    37      parser.addoption(
    38          "--deployment-type",
    39          action="store",
    40          default=DEFAULT_DEPLOYMENT_TYPE,
    41          help="The type of the IC deployment: deployment or daemon-set.",
    42      )
    43      parser.addoption(
    44          "--ic-type",
    45          action="store",
    46          default=DEFAULT_IC_TYPE,
    47          help="The type of the Ingress Controller: nginx-ingress or nginx-ingress-plus.",
    48      )
    49      parser.addoption(
    50          "--service",
    51          action="store",
    52          default=DEFAULT_SERVICE,
    53          help="The type of the Ingress Controller service: nodeport or loadbalancer.",
    54      )
    55      parser.addoption(
    56          "--node-ip",
    57          action="store",
    58          help="The public IP of a cluster node. Not required if you use the loadbalancer service (see --service argument).",
    59      )
    60      parser.addoption(
    61          "--kubeconfig",
    62          action="store",
    63          default=os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION),
    64          help="An absolute path to a kubeconfig file.",
    65      )
    66      parser.addoption(
    67          "--show-ic-logs",
    68          action="store",
    69          default="no",
    70          help="Show IC logs in stdout on test failure",
    71      )
    72      parser.addoption(
    73          "--users", action="store", default="10", help="No. of users for response perf tests",
    74      )
    75      parser.addoption(
    76          "--hatch-rate", action="store", default="5", help="No. of users hatched per second",
    77      )
    78      parser.addoption(
    79          "--time",
    80          action="store",
    81          default="10",
    82          help="Duration for AP response perf tests in seconds",
    83      )
    84  
    85  
    86  # import fixtures into pytest global namespace
    87  pytest_plugins = ["suite.fixtures"]
    88  
    89  
    90  def pytest_collection_modifyitems(config, items) -> None:
    91      """
    92      Skip tests marked with '@pytest.mark.skip_for_nginx_oss' for Nginx OSS runs.
    93      Skip tests marked with '@pytest.mark.appprotect' for non AP images.
    94  
    95      :param config: pytest config
    96      :param items: pytest collected test-items
    97      :return:
    98      """
    99      if config.getoption("--ic-type") == "nginx-ingress":
   100          skip_for_nginx_oss = pytest.mark.skip(reason="Skip a test for Nginx OSS")
   101          for item in items:
   102              if "skip_for_nginx_oss" in item.keywords:
   103                  item.add_marker(skip_for_nginx_oss)
   104      if config.getoption("--ic-type") == "nginx-plus-ingress":
   105          skip_for_nginx_plus = pytest.mark.skip(reason="Skip a test for Nginx Plus")
   106          for item in items:
   107              if "skip_for_nginx_plus" in item.keywords:
   108                  item.add_marker(skip_for_nginx_plus)
   109      if "-ap" not in config.getoption("--image"):
   110          appprotect = pytest.mark.skip(reason="Skip AppProtect test in non-AP image")
   111          for item in items:
   112              if "appprotect" in item.keywords:
   113                  item.add_marker(appprotect)
   114  
   115  
   116  @pytest.hookimpl(tryfirst=True, hookwrapper=True)
   117  def pytest_runtest_makereport(item) -> None:
   118      """
   119      Print out IC Pod logs on test failure.
   120  
   121      Only look at actual failing test calls, not setup/teardown.
   122      Only show the logs if commandline argument `--show-ic-logs` is set to 'yes'
   123  
   124      :param item:
   125      :return:
   126      """
   127      # execute all other hooks to obtain the report object
   128      outcome = yield
   129      rep = outcome.get_result()
   130  
   131      # we only look at actual failing test calls, not setup/teardown
   132      if rep.when == "call" and rep.failed and item.config.getoption("--show-ic-logs") == "yes":
   133          pod_namespace = item.funcargs["ingress_controller_prerequisites"].namespace
   134          pod_name = get_first_pod_name(item.funcargs["kube_apis"].v1, pod_namespace)
   135          print("\n===================== IC Logs Start =====================")
   136          print(item.funcargs["kube_apis"].v1.read_namespaced_pod_log(pod_name, pod_namespace))
   137          print("\n===================== IC Logs End =====================")