2020-03-27 10:13:31 +01:00
|
|
|
#!/usr/bin/python3
|
2021-06-03 08:37:05 +02:00
|
|
|
|
2020-03-27 10:13:31 +01:00
|
|
|
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
#
|
2021-06-03 08:37:05 +02:00
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
#
|
2020-03-27 10:13:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
2021-06-03 08:37:05 +02:00
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
# file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2020-03-27 10:13:31 +01:00
|
|
|
#
|
|
|
|
# See the COPYRIGHT file distributed with this work for additional
|
|
|
|
# information regarding copyright ownership.
|
|
|
|
|
|
|
|
from datetime import datetime
|
2020-04-14 17:02:21 +02:00
|
|
|
|
2021-09-17 16:34:25 +10:00
|
|
|
import os
|
|
|
|
|
2020-04-14 17:02:21 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
import generic
|
2020-03-27 10:13:31 +01:00
|
|
|
|
2022-03-14 08:59:32 +01:00
|
|
|
pytestmark = pytest.mark.skipif(not os.environ.get('HAVEJSONSTATS'),
|
|
|
|
reason='json-c support disabled in the build')
|
2022-03-14 08:59:32 +01:00
|
|
|
requests = pytest.importorskip('requests')
|
2022-03-14 08:59:32 +01:00
|
|
|
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
# JSON helper functions
|
2020-04-14 17:02:21 +02:00
|
|
|
def fetch_zones_json(statsip, statsport):
|
2020-03-27 10:13:31 +01:00
|
|
|
|
2021-06-02 16:40:58 +10:00
|
|
|
r = requests.get("http://{}:{}/json/v1/zones".format(statsip, statsport),
|
|
|
|
timeout=600)
|
2020-03-27 10:13:31 +01:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
return data["views"]["_default"]["zones"]
|
|
|
|
|
|
|
|
|
2020-04-14 17:02:21 +02:00
|
|
|
def fetch_traffic_json(statsip, statsport):
|
|
|
|
|
2021-06-02 16:40:58 +10:00
|
|
|
r = requests.get("http://{}:{}/json/v1/traffic".format(statsip, statsport),
|
|
|
|
timeout=600)
|
2020-04-14 17:02:21 +02:00
|
|
|
assert r.status_code == 200
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
|
|
|
|
return data["traffic"]
|
|
|
|
|
|
|
|
|
|
|
|
def load_timers_json(zone, primary=True):
|
|
|
|
|
2020-03-27 10:13:31 +01:00
|
|
|
name = zone['name']
|
|
|
|
|
|
|
|
# Check if the primary zone timer exists
|
|
|
|
assert 'loaded' in zone
|
Refactor "statschannel" test's helper modules
The "statschannel" system test contains two Python helper modules:
- generic.py: test functions directly invoked by both tests-json.py
and test-xml.py,
- helper.py: helper functions invoked by test functions in generic.py.
The above logic for splitting helper functions into Python modules
prevents selective test skipping from working due to unconditional
import statements being present in both helper modules. For example, if
dnspython is not available on the test host, tests-json.py imports
generic.py, which in turn imports helper.py, which in turn attempts to
import various dnspython modules, triggering ImportError exceptions
during test initialization. Various decorators used for some tests
(like @pytest.mark.dnspython) suggest that such a scenario should be
handled gracefully, but that is not the case - modifying the test
collection in conftest.py does not prevent pytest from failing due to
import errors.
Fix by moving helper functions around to achieve a different split:
- generic.py: helper functions only relying on the Python standard
library,
- generic_dnspython.py: helper functions requiring dnspython.
Only two tests in tests-{json,xml}.py need dnspython to work
(test_traffic_json(), test_traffic_xml()). Since all
dnspython-dependent code is now present in generic_dnspython.py, employ
pytest.importorskip() in those two tests to ensure they can be
selectively skipped when dnspython is not available. Adjust other code
to account for the revised Python helper module layout. Remove all
occurrences of the @pytest.mark.dnspython decorator (and all associated
code) from the "statschannel" system test to prevent confusion.
2022-03-14 08:59:32 +01:00
|
|
|
loaded = datetime.strptime(zone['loaded'], generic.fmt)
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
if primary:
|
|
|
|
# Check if the secondary zone timers does not exist
|
|
|
|
assert 'expires' not in zone
|
|
|
|
assert 'refresh' not in zone
|
|
|
|
expires = None
|
|
|
|
refresh = None
|
|
|
|
else:
|
|
|
|
assert 'expires' in zone
|
|
|
|
assert 'refresh' in zone
|
Refactor "statschannel" test's helper modules
The "statschannel" system test contains two Python helper modules:
- generic.py: test functions directly invoked by both tests-json.py
and test-xml.py,
- helper.py: helper functions invoked by test functions in generic.py.
The above logic for splitting helper functions into Python modules
prevents selective test skipping from working due to unconditional
import statements being present in both helper modules. For example, if
dnspython is not available on the test host, tests-json.py imports
generic.py, which in turn imports helper.py, which in turn attempts to
import various dnspython modules, triggering ImportError exceptions
during test initialization. Various decorators used for some tests
(like @pytest.mark.dnspython) suggest that such a scenario should be
handled gracefully, but that is not the case - modifying the test
collection in conftest.py does not prevent pytest from failing due to
import errors.
Fix by moving helper functions around to achieve a different split:
- generic.py: helper functions only relying on the Python standard
library,
- generic_dnspython.py: helper functions requiring dnspython.
Only two tests in tests-{json,xml}.py need dnspython to work
(test_traffic_json(), test_traffic_xml()). Since all
dnspython-dependent code is now present in generic_dnspython.py, employ
pytest.importorskip() in those two tests to ensure they can be
selectively skipped when dnspython is not available. Adjust other code
to account for the revised Python helper module layout. Remove all
occurrences of the @pytest.mark.dnspython decorator (and all associated
code) from the "statschannel" system test to prevent confusion.
2022-03-14 08:59:32 +01:00
|
|
|
expires = datetime.strptime(zone['expires'], generic.fmt)
|
|
|
|
refresh = datetime.strptime(zone['refresh'], generic.fmt)
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
return (name, loaded, expires, refresh)
|
|
|
|
|
|
|
|
|
2020-04-14 17:02:21 +02:00
|
|
|
def load_zone_json(zone):
|
|
|
|
name = zone['name']
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
2020-03-27 10:13:31 +01:00
|
|
|
def test_zone_timers_primary_json(statsport):
|
2020-04-14 17:02:21 +02:00
|
|
|
generic.test_zone_timers_primary(fetch_zones_json, load_timers_json,
|
|
|
|
statsip="10.53.0.1", statsport=statsport,
|
|
|
|
zonedir="ns1")
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
|
2020-04-14 17:02:21 +02:00
|
|
|
def test_zone_timers_secondary_json(statsport):
|
|
|
|
generic.test_zone_timers_secondary(fetch_zones_json, load_timers_json,
|
|
|
|
statsip="10.53.0.3", statsport=statsport,
|
|
|
|
zonedir="ns3")
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
|
2020-04-14 17:02:21 +02:00
|
|
|
def test_zone_with_many_keys_json(statsport):
|
|
|
|
generic.test_zone_with_many_keys(fetch_zones_json, load_zone_json,
|
|
|
|
statsip="10.53.0.2", statsport=statsport)
|
2020-03-27 10:13:31 +01:00
|
|
|
|
|
|
|
|
2022-03-14 08:59:32 +01:00
|
|
|
def test_traffic_json(named_port, statsport):
|
Refactor "statschannel" test's helper modules
The "statschannel" system test contains two Python helper modules:
- generic.py: test functions directly invoked by both tests-json.py
and test-xml.py,
- helper.py: helper functions invoked by test functions in generic.py.
The above logic for splitting helper functions into Python modules
prevents selective test skipping from working due to unconditional
import statements being present in both helper modules. For example, if
dnspython is not available on the test host, tests-json.py imports
generic.py, which in turn imports helper.py, which in turn attempts to
import various dnspython modules, triggering ImportError exceptions
during test initialization. Various decorators used for some tests
(like @pytest.mark.dnspython) suggest that such a scenario should be
handled gracefully, but that is not the case - modifying the test
collection in conftest.py does not prevent pytest from failing due to
import errors.
Fix by moving helper functions around to achieve a different split:
- generic.py: helper functions only relying on the Python standard
library,
- generic_dnspython.py: helper functions requiring dnspython.
Only two tests in tests-{json,xml}.py need dnspython to work
(test_traffic_json(), test_traffic_xml()). Since all
dnspython-dependent code is now present in generic_dnspython.py, employ
pytest.importorskip() in those two tests to ensure they can be
selectively skipped when dnspython is not available. Adjust other code
to account for the revised Python helper module layout. Remove all
occurrences of the @pytest.mark.dnspython decorator (and all associated
code) from the "statschannel" system test to prevent confusion.
2022-03-14 08:59:32 +01:00
|
|
|
generic_dnspython = pytest.importorskip('generic_dnspython')
|
|
|
|
generic_dnspython.test_traffic(fetch_traffic_json,
|
|
|
|
statsip="10.53.0.2", statsport=statsport,
|
|
|
|
port=named_port)
|