Commit 48facc9b authored by Ales Mrazek's avatar Ales Mrazek

doctests with expected results

parent cb7bf458
Pipeline #40804 passed with stages
in 1 minute
......@@ -46,6 +46,7 @@ Validate data against the data model:
.. doctest::
>>> json_data.validate()
>>> json_data = json_data.add_defaults()
Now the data is valid and you can work with it.
......@@ -72,9 +73,23 @@ Call generator functions to generate `unbound.conf` and `kresd.conf`.
.. doctest::
>>> unbound_conf = gen_unbound(json_data)
>>> unbound_conf
'server:\n\tinterface: 127.0.0.1@53\n\tinterface: ::1@53\n\tinterface: 198.51.100.1@8853\n\toutgoing-interface: 2001:db8:0:2::1\n\tdo-ip6 = yes\n\tdo-ip4 = yes\n\tedns-buffer-size: 4096\n\tusername: "jetconf"\n\troot-hints: "/etc/resolver/root.hints"\n\tharden-glue: yes\n\tqname-minimisation: yes\n\trrset-roundrobin: yes\n\tdo-not-query-localhost: no\n\tverbosity: 2\n\tauto-trust-anchor-file: "/var/tmp/root.keys"\n\tdomain-insecure: "bad.example.com"\n\tdomain-insecure: "worse.example.com"\n\tmsg-cache-size: 104857600\n\tcache-max-ttl: 172800\n\tcache-min-ttl: 0\n\tdns64-prefix: 64:ff9b::/96\n\nstub-zone:\n\tname: "stub.example.com"\n\tstub-addr: "192.0.2.1@53"\n\nstub-zone:\n\tname: "stub.example.net"\n\tstub-addr: "198.51.100.1@53"\n'
.. doctest::
>>> kresd_conf = gen_kresd(json_data)
>>> kresd_conf
'net = {\'127.0.0.1@53\', \'::1@53\', \'198.51.100.1@8853\'}\nnet.outgoing_v6(\'2001:db8:0:2::1\')\nnet.ipv6 = true\nnet.ipv4 = true\nnet.bufsize(4096)\nuser(\'jetconf\',\'wheel\')\nSTUB(\'192.0.2.1@53\', \'198.51.100.1@53\')\nmodules.load(\'hints\')\nhints.set("loopback 127.0.0.1")\nhints.set("localhost 127.0.0.1")\nhints.set("localhost ::1")\nhints.add_hosts(/etc/hosts)\nhints.root({[\'a.root-servers.net\'] = {\'198.41.0.4\', \'2001:503:ba3e::2:30\'}})\nhints.root_file(\'/etc/resolver/root.hints\')\nmode(\'strict\')\noption(\'NO_MINIMIZE\', false)\nreorder_RR(true)\noption(\'ALLOW_LOCAL\', true)\nverbose(true)\ntrust_anchors.add_file(\'/var/tmp/root.keys\',false)\ntrust_anchors.set_insecure({\'bad.example.com\', \'worse.example.com\'})\ncache.size = 104857600\ncache.max_ttl(172800)\ncache.min_ttl(0)\nmodules.load(\'prefill\')\nprefill.config({\n\t[\'.\'] = {\n\t\turl = \'https://www.internic.net/domain/root.zone\', \n\t\tca_file = \'/etc/pki/tls/certs/ca-bundle.crt\', \n\t\tinterval = 86400\n\t}\n})\nmodules.load(\'dns64\')\ndns64.config(\'64:ff9b::/96\')\n'
``unbound_conf`` and ``kresd_conf`` are configuration strings , which can be save as text files.
``unbound_conf`` and ``kresd_conf`` are raw strings , which can be save as text files.
.. doctest::
>>> unb_file = open("unbound.conf", "w+")
>>> unb_file.write(unbound_conf) # doctest: +hide
>>> unb_file.close()
Converter module
----------------
......@@ -89,14 +104,32 @@ Import ``from_unbound`` function for converting Unbound configuration string to
>>> from resolvers_yang.converter import from_unbound
Input parameter of ``from_unbound`` function is string loaded from ``unbound.con`` file. Returns python dictionary.
Input parameter of ``from_unbound`` function is a list of string lines loaded from ``unbound.conf`` file. Returns python dictionary.
.. doctest::
>>> with open("unbound.conf", "r") as unb_file:
... unbconf_data = unb_file.readlines()
.. doctest::
>>> json_data = from_unbound(unbound_conf)
>>> json_data = from_unbound(unbconf_data)
>>> json_data
{'cznic-resolver-common:dns-resolver': {'dns64': {'prefix': '64:ff9b::/96'}, 'cache': {'max-size': 104857600, 'max-ttl': 172800, 'min-ttl': 0}, 'dnssec': {'negative-trust-anchors': ['bad.example.com', 'worse.example.com'], 'trust-anchors': {'key-files': [{'domain': 'domain0', 'file': '/var/tmp/root.keys'}]}}, 'logging': {'verbosity': 2}, 'resolver': {'options': {'glue-checking': 'strict', 'qname-minimisation': True, 'reorder-rrset': True, 'query-loopback': True}, 'stub-zones': [{'domain': 'stub.example.com', 'nameserver': '192.0.2.1', 'port': 53}, {'domain': 'stub.example.net', 'nameserver': '198.51.100.1', 'port': 53}], 'hints': {'root-zone-file': '/etc/resolver/root.hints'}}, 'network': {'listen-interfaces': [{'name': 'interface0', 'ip-address': '127.0.0.1', 'port': 53}, {'name': 'interface1', 'ip-address': '::1', 'port': 53}, {'name': 'interface2', 'ip-address': '198.51.100.1', 'port': 8853}], 'source-address': {'ipv6': '2001:db8:0:2::1'}, 'udp-payload-size': 4096, 'recursion-transport': {'l2-protocols': 'ipv4 ipv6'}}, 'server': {'user-name': 'jetconf'}}}
``json_data`` variable is python dictionary, which can be easily validate against data model by ``Yangson`` library and save to Json-encoded file.
.. doctest::
>>> model_data = model.from_raw(json_data)
>>> model_data.validate()
.. doctest::
>>> from json import dump
>>> with open("unbound-data.json", 'w') as json_file:
... dump(json_data, json_file, indent=2, sort_keys=False)
.. testcleanup::
os.chdir("../..")
"""
Module for generating Unbound´s and Knot Resolver´s configuration files from valid loaded Json.
Module for generating Unbound and Knot Resolver configuration files from valid loaded Json.
"""
from re import compile
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment