# DNSSEC : https://github.com/wubo1994/DNS-resolver-in-python3/blob/master/dnssec.py
# python3-pytest python3-yaml
from yaml import load, SafeLoader
from dns.resolver import Resolver
from os import environ


def test_nsd():
    if 'MACHINE_TEST_DIR' not in environ:
        return
    conf_file = f'{environ["MACHINE_TEST_DIR"]}/nsd.yml'
    with open(conf_file) as yaml:
        data = load(yaml, Loader=SafeLoader)
    resolver = Resolver()
    resolver.nameservers = [data['address']]
    for dns, ip in data['records'].items():
        records = resolver.resolve(dns, 'A')
        ips = [record.address for record in records]
        assert len(ips) == 1, f"le domaine {dns} n'a pas qu'une ip {ips}"
        assert ips[0] == ip, f"l'IP du domaine {dns} n'est pas correct, attendu : {ip}, obtenu {ips[0]}"
