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