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 pytest_custom_markers 19 20pytest.register_assert_rewrite("generic") 21import generic 22 23pytestmark = pytest_custom_markers.have_json_c 24requests = pytest.importorskip("requests") 25 26 27# JSON helper functions 28def fetch_zones_json(statsip, statsport): 29 r = requests.get( 30 "http://{}:{}/json/v1/zones".format(statsip, statsport), timeout=600 31 ) 32 assert r.status_code == 200 33 34 data = r.json() 35 return data["views"]["_default"]["zones"] 36 37 38def fetch_traffic_json(statsip, statsport): 39 r = requests.get( 40 "http://{}:{}/json/v1/traffic".format(statsip, statsport), timeout=600 41 ) 42 assert r.status_code == 200 43 44 data = r.json() 45 46 return data["traffic"] 47 48 49def load_timers_json(zone, primary=True): 50 name = zone["name"] 51 52 # Check if the primary zone timer exists 53 assert "loaded" in zone 54 loaded = datetime.strptime(zone["loaded"], generic.fmt) 55 56 if primary: 57 # Check if the secondary zone timers does not exist 58 assert "expires" not in zone 59 assert "refresh" not in zone 60 expires = None 61 refresh = None 62 else: 63 assert "expires" in zone 64 assert "refresh" in zone 65 expires = datetime.strptime(zone["expires"], generic.fmt) 66 refresh = datetime.strptime(zone["refresh"], generic.fmt) 67 68 return (name, loaded, expires, refresh) 69 70 71def load_zone_json(zone): 72 name = zone["name"] 73 74 return name 75 76 77def test_zone_timers_primary_json(statsport): 78 generic.test_zone_timers_primary( 79 fetch_zones_json, 80 load_timers_json, 81 statsip="10.53.0.1", 82 statsport=statsport, 83 zonedir="ns1", 84 ) 85 86 87def test_zone_timers_secondary_json(statsport): 88 generic.test_zone_timers_secondary( 89 fetch_zones_json, 90 load_timers_json, 91 statsip="10.53.0.3", 92 statsport=statsport, 93 zonedir="ns3", 94 ) 95 96 97def test_zone_with_many_keys_json(statsport): 98 generic.test_zone_with_many_keys( 99 fetch_zones_json, load_zone_json, statsip="10.53.0.2", statsport=statsport 100 ) 101 102 103def test_traffic_json(statsport): 104 generic.test_traffic(fetch_traffic_json, statsip="10.53.0.2", statsport=statsport) 105