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", "dnspython: mark tests that need dnspython to function"
19    )
20    config.addinivalue_line(
21        "markers", "dnspython2: mark tests that need dnspython >= 2.0.0"
22    )
23
24
25def pytest_collection_modifyitems(config, items):
26    # pylint: disable=unused-argument,unused-import,too-many-branches
27    # pylint: disable=import-outside-toplevel
28
29    # Test for dnspython module
30    skip_dnspython = pytest.mark.skip(
31        reason="need dnspython module to run")
32    try:
33        import dns.query  # noqa: F401
34    except ModuleNotFoundError:
35        for item in items:
36            if "dnspython" in item.keywords:
37                item.add_marker(skip_dnspython)
38
39    # Test for dnspython >= 2.0.0 module
40    skip_dnspython2 = pytest.mark.skip(
41        reason="need dnspython >= 2.0.0 module to run")
42    try:
43        from dns.query import udp_with_fallback  # noqa: F401
44    except ImportError:
45        for item in items:
46            if "dnspython2" in item.keywords:
47                item.add_marker(skip_dnspython2)
48
49
50@pytest.fixture
51def named_port(request):
52    # pylint: disable=unused-argument
53    port = os.getenv("PORT")
54    if port is None:
55        port = 5301
56    else:
57        port = int(port)
58
59    return port
60
61
62@pytest.fixture
63def control_port(request):
64    # pylint: disable=unused-argument
65    port = os.getenv("CONTROLPORT")
66    if port is None:
67        port = 5301
68    else:
69        port = int(port)
70
71    return port
72