conftest.py (5317B)
1 #*************************************************************************** 2 # _ _ ____ _ 3 # Project ___| | | | _ \| | 4 # / __| | | | |_) | | 5 # | (__| |_| | _ <| |___ 6 # \___|\___/|_| \_\_____| 7 # 8 # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 # 10 # This software is licensed as described in the file COPYING, which 11 # you should have received as part of this distribution. The terms 12 # are also available at https://curl.se/docs/copyright.html. 13 # 14 # You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 # copies of the Software, and permit persons to whom the Software is 16 # furnished to do so, under the terms of the COPYING file. 17 # 18 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 # KIND, either express or implied. 20 # 21 # SPDX-License-Identifier: curl 22 # 23 ########################################################################### 24 # 25 import logging 26 import os 27 import sys 28 import platform 29 from typing import Generator, Union 30 31 import pytest 32 33 from testenv.env import EnvConfig 34 35 sys.path.append(os.path.join(os.path.dirname(__file__), '.')) 36 37 from testenv import Env, Nghttpx, Httpd, NghttpxQuic, NghttpxFwd 38 39 log = logging.getLogger(__name__) 40 41 42 def pytest_report_header(config): 43 # Env inits its base properties only once, we can report them here 44 env = Env() 45 report = [ 46 f'Testing curl {env.curl_version()}', 47 f' platform: {platform.platform()}', 48 f' curl: Version: {env.curl_version_string()}', 49 f' curl: Features: {env.curl_features_string()}', 50 f' curl: Protocols: {env.curl_protocols_string()}', 51 f' httpd: {env.httpd_version()}', 52 f' httpd-proxy: {env.httpd_version()}' 53 ] 54 if env.have_h3(): 55 report.extend([ 56 f' nghttpx: {env.nghttpx_version()}' 57 ]) 58 if env.has_caddy(): 59 report.extend([ 60 f' Caddy: {env.caddy_version()}' 61 ]) 62 if env.has_vsftpd(): 63 report.extend([ 64 f' VsFTPD: {env.vsftpd_version()}' 65 ]) 66 buildinfo_fn = os.path.join(env.build_dir, 'buildinfo.txt') 67 if os.path.exists(buildinfo_fn): 68 with open(buildinfo_fn, 'r') as file_in: 69 for line in file_in: 70 line = line.strip() 71 if line and not line.startswith('#'): 72 report.extend([line]) 73 return '\n'.join(report) 74 75 76 @pytest.fixture(scope='session') 77 def env_config(pytestconfig, testrun_uid, worker_id) -> EnvConfig: 78 env_config = EnvConfig(pytestconfig=pytestconfig, 79 testrun_uid=testrun_uid, 80 worker_id=worker_id) 81 return env_config 82 83 84 @pytest.fixture(scope='session', autouse=True) 85 def env(pytestconfig, env_config) -> Env: 86 env = Env(pytestconfig=pytestconfig, env_config=env_config) 87 level = logging.DEBUG if env.verbose > 0 else logging.INFO 88 logging.getLogger('').setLevel(level=level) 89 if not env.curl_has_protocol('http'): 90 pytest.skip("curl built without HTTP support") 91 if not env.curl_has_protocol('https'): 92 pytest.skip("curl built without HTTPS support") 93 if env.setup_incomplete(): 94 pytest.skip(env.incomplete_reason()) 95 96 env.setup() 97 return env 98 99 100 @pytest.fixture(scope='session') 101 def httpd(env) -> Generator[Httpd, None, None]: 102 httpd = Httpd(env=env) 103 if not httpd.exists(): 104 pytest.skip(f'httpd not found: {env.httpd}') 105 httpd.clear_logs() 106 assert httpd.initial_start() 107 yield httpd 108 httpd.stop() 109 110 111 @pytest.fixture(scope='session') 112 def nghttpx(env, httpd) -> Generator[Union[Nghttpx,bool], None, None]: 113 nghttpx = NghttpxQuic(env=env) 114 if nghttpx.exists(): 115 if not nghttpx.supports_h3() and env.have_h3_curl(): 116 log.warning('nghttpx does not support QUIC, but curl does') 117 nghttpx.clear_logs() 118 assert nghttpx.initial_start() 119 yield nghttpx 120 nghttpx.stop() 121 else: 122 yield False 123 124 125 @pytest.fixture(scope='session') 126 def nghttpx_fwd(env, httpd) -> Generator[Union[Nghttpx,bool], None, None]: 127 nghttpx = NghttpxFwd(env=env) 128 if nghttpx.exists(): 129 nghttpx.clear_logs() 130 assert nghttpx.initial_start() 131 yield nghttpx 132 nghttpx.stop() 133 else: 134 yield False 135 136 137 @pytest.fixture(scope='session') 138 def configures_httpd(env, httpd) -> Generator[bool, None, None]: 139 # include this fixture as test parameter if the test configures httpd itself 140 yield True 141 142 @pytest.fixture(scope='session') 143 def configures_nghttpx(env, httpd) -> Generator[bool, None, None]: 144 # include this fixture as test parameter if the test configures nghttpx itself 145 yield True 146 147 @pytest.fixture(autouse=True, scope='function') 148 def server_reset(request, env, httpd, nghttpx): 149 # make sure httpd is in default configuration when a test starts 150 if 'configures_httpd' not in request.node._fixtureinfo.argnames: 151 httpd.reset_config() 152 httpd.reload_if_config_changed() 153 if env.have_h3() and \ 154 'nghttpx' in request.node._fixtureinfo.argnames and \ 155 'configures_nghttpx' not in request.node._fixtureinfo.argnames: 156 nghttpx.reset_config() 157 nghttpx.reload_if_config_changed()