xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/statschannel/tests_json.py (revision 4ac76180e904e771b9d522c7e57296d371f06499)
1#!/usr/bin/python3
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14from datetime import datetime
15
16import pytest
17
18import generic
19import pytest_custom_markers
20
21pytestmark = pytest_custom_markers.have_json_c
22requests = pytest.importorskip("requests")
23
24
25# JSON helper functions
26def fetch_zones_json(statsip, statsport):
27    r = requests.get(
28        "http://{}:{}/json/v1/zones".format(statsip, statsport), timeout=600
29    )
30    assert r.status_code == 200
31
32    data = r.json()
33    return data["views"]["_default"]["zones"]
34
35
36def fetch_traffic_json(statsip, statsport):
37    r = requests.get(
38        "http://{}:{}/json/v1/traffic".format(statsip, statsport), timeout=600
39    )
40    assert r.status_code == 200
41
42    data = r.json()
43
44    return data["traffic"]
45
46
47def load_timers_json(zone, primary=True):
48    name = zone["name"]
49
50    # Check if the primary zone timer exists
51    assert "loaded" in zone
52    loaded = datetime.strptime(zone["loaded"], generic.fmt)
53
54    if primary:
55        # Check if the secondary zone timers does not exist
56        assert "expires" not in zone
57        assert "refresh" not in zone
58        expires = None
59        refresh = None
60    else:
61        assert "expires" in zone
62        assert "refresh" in zone
63        expires = datetime.strptime(zone["expires"], generic.fmt)
64        refresh = datetime.strptime(zone["refresh"], generic.fmt)
65
66    return (name, loaded, expires, refresh)
67
68
69def load_zone_json(zone):
70    name = zone["name"]
71
72    return name
73
74
75def test_zone_timers_primary_json(statsport):
76    generic.test_zone_timers_primary(
77        fetch_zones_json,
78        load_timers_json,
79        statsip="10.53.0.1",
80        statsport=statsport,
81        zonedir="ns1",
82    )
83
84
85def test_zone_timers_secondary_json(statsport):
86    generic.test_zone_timers_secondary(
87        fetch_zones_json,
88        load_timers_json,
89        statsip="10.53.0.3",
90        statsport=statsport,
91        zonedir="ns3",
92    )
93
94
95def test_zone_with_many_keys_json(statsport):
96    generic.test_zone_with_many_keys(
97        fetch_zones_json, load_zone_json, statsip="10.53.0.2", statsport=statsport
98    )
99
100
101def test_traffic_json(named_port, statsport):
102    generic_dnspython = pytest.importorskip("generic_dnspython")
103    generic_dnspython.test_traffic(
104        fetch_traffic_json, statsip="10.53.0.2", statsport=statsport, port=named_port
105    )
106