xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/statschannel/conftest.py (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1############################################################################
2# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3#
4# This Source Code Form is subject to the terms of the Mozilla Public
5# License, v. 2.0. If a copy of the MPL was not distributed with this
6# file, you can obtain one at https://mozilla.org/MPL/2.0/.
7#
8# See the COPYRIGHT file distributed with this work for additional
9# information regarding copyright ownership.
10############################################################################
11
12import os
13import pytest
14
15
16def pytest_configure(config):
17    config.addinivalue_line(
18        "markers", "requests: mark tests that need requests to function"
19    )
20    config.addinivalue_line(
21        "markers", "json: mark tests that need json to function"
22    )
23    config.addinivalue_line(
24        "markers", "xml: mark tests that need xml.etree to function"
25    )
26    config.addinivalue_line(
27        "markers", "dnspython: mark tests that need dnspython to function"
28    )
29
30
31def pytest_collection_modifyitems(config, items):
32    # pylint: disable=unused-argument,unused-import,too-many-branches
33    # pylint: disable=import-outside-toplevel
34    # Test for requests module
35    skip_requests = pytest.mark.skip(
36        reason="need requests module to run")
37    try:
38        import requests  # noqa: F401
39    except ModuleNotFoundError:
40        for item in items:
41            if "requests" in item.keywords:
42                item.add_marker(skip_requests)
43    # Test for json module
44    skip_json = pytest.mark.skip(
45        reason="need json module to run")
46    try:
47        import json  # noqa: F401
48    except ModuleNotFoundError:
49        for item in items:
50            if "json" in item.keywords:
51                item.add_marker(skip_json)
52    # Test for xml module
53    skip_xml = pytest.mark.skip(
54        reason="need xml module to run")
55    try:
56        import xml.etree.ElementTree  # noqa: F401
57    except ModuleNotFoundError:
58        for item in items:
59            if "xml" in item.keywords:
60                item.add_marker(skip_xml)
61    # Test if JSON statistics channel was enabled
62    no_jsonstats = pytest.mark.skip(
63        reason="need JSON statistics to be enabled")
64    if os.getenv("HAVEJSONSTATS") is None:
65        for item in items:
66            if "json" in item.keywords:
67                item.add_marker(no_jsonstats)
68    # Test if XML statistics channel was enabled
69    no_xmlstats = pytest.mark.skip(
70        reason="need XML statistics to be enabled")
71    if os.getenv("HAVEXMLSTATS") is None:
72        for item in items:
73            if "xml" in item.keywords:
74                item.add_marker(no_xmlstats)
75    # Test for dnspython module
76    skip_dnspython = pytest.mark.skip(
77        reason="need dnspython module to run")
78    try:
79        import dns.query  # noqa: F401
80    except ModuleNotFoundError:
81        for item in items:
82            if "dnspython" in item.keywords:
83                item.add_marker(skip_dnspython)
84
85
86@pytest.fixture
87def statsport(request):
88    # pylint: disable=unused-argument
89    env_port = os.getenv("EXTRAPORT1")
90    if port is None:
91        env_port = 5301
92    else:
93        env_port = int(env_port)
94
95    return env_port
96
97
98@pytest.fixture
99def port(request):
100    # pylint: disable=unused-argument
101    env_port = os.getenv("PORT")
102    if port is None:
103        env_port = 5300
104    else:
105        env_port = int(env_port)
106
107    return env_port
108