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