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