conftest.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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
  30. import pytest
  31. sys.path.append(os.path.join(os.path.dirname(__file__), '.'))
  32. from testenv import Env, Nghttpx, Httpd, NghttpxQuic, NghttpxFwd
  33. def pytest_report_header(config):
  34. # Env inits its base properties only once, we can report them here
  35. env = Env()
  36. report = [
  37. f'Testing curl {env.curl_version()}',
  38. f' platform: {platform.platform()}',
  39. f' curl: Version: {env.curl_version_string()}',
  40. f' curl: Features: {env.curl_features_string()}',
  41. f' curl: Protocols: {env.curl_protocols_string()}',
  42. f' httpd: {env.httpd_version()}, http:{env.http_port} https:{env.https_port}',
  43. f' httpd-proxy: {env.httpd_version()}, http:{env.proxy_port} https:{env.proxys_port}'
  44. ]
  45. if env.have_h3():
  46. report.extend([
  47. f' nghttpx: {env.nghttpx_version()}, h3:{env.https_port}'
  48. ])
  49. if env.has_caddy():
  50. report.extend([
  51. f' Caddy: {env.caddy_version()}, http:{env.caddy_http_port} https:{env.caddy_https_port}'
  52. ])
  53. if env.has_vsftpd():
  54. report.extend([
  55. f' VsFTPD: {env.vsftpd_version()}, ftp:{env.ftp_port}, ftps:{env.ftps_port}'
  56. ])
  57. buildinfo_fn = os.path.join(env.build_dir, 'buildinfo.txt')
  58. if os.path.exists(buildinfo_fn):
  59. with open(buildinfo_fn, 'r') as file_in:
  60. for line in file_in:
  61. line = line.strip()
  62. if line and not line.startswith('#'):
  63. report.extend([line])
  64. return '\n'.join(report)
  65. # TODO: remove this and repeat argument everywhere, pytest-repeat can be used to repeat tests
  66. def pytest_generate_tests(metafunc):
  67. if "repeat" in metafunc.fixturenames:
  68. metafunc.parametrize('repeat', [0])
  69. @pytest.fixture(scope="package")
  70. def env(pytestconfig) -> Env:
  71. env = Env(pytestconfig=pytestconfig)
  72. level = logging.DEBUG if env.verbose > 0 else logging.INFO
  73. logging.getLogger('').setLevel(level=level)
  74. if not env.curl_has_protocol('http'):
  75. pytest.skip("curl built without HTTP support")
  76. if not env.curl_has_protocol('https'):
  77. pytest.skip("curl built without HTTPS support")
  78. if env.setup_incomplete():
  79. pytest.skip(env.incomplete_reason())
  80. env.setup()
  81. return env
  82. @pytest.fixture(scope="package", autouse=True)
  83. def log_global_env_facts(record_testsuite_property, env):
  84. record_testsuite_property("http-port", env.http_port)
  85. @pytest.fixture(scope='package')
  86. def httpd(env) -> Generator[Httpd, None, None]:
  87. httpd = Httpd(env=env)
  88. if not httpd.exists():
  89. pytest.skip(f'httpd not found: {env.httpd}')
  90. httpd.clear_logs()
  91. if not httpd.start():
  92. pytest.fail(f'failed to start httpd: {env.httpd}')
  93. yield httpd
  94. httpd.stop()
  95. @pytest.fixture(scope='package')
  96. def nghttpx(env, httpd) -> Generator[Nghttpx, None, None]:
  97. nghttpx = NghttpxQuic(env=env)
  98. if nghttpx.exists() and (env.have_h3() or nghttpx.https_port > 0):
  99. nghttpx.clear_logs()
  100. assert nghttpx.start()
  101. yield nghttpx
  102. nghttpx.stop()
  103. @pytest.fixture(scope='package')
  104. def nghttpx_fwd(env, httpd) -> Generator[Nghttpx, None, None]:
  105. nghttpx = NghttpxFwd(env=env)
  106. if nghttpx.exists() and (env.have_h3() or nghttpx.https_port > 0):
  107. nghttpx.clear_logs()
  108. assert nghttpx.start()
  109. yield nghttpx
  110. nghttpx.stop()