1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
import contextlib
import ipaddress
import sys
import time
from unittest import mock
import pytest
from ovs import dns_resolve
from ovs import socket_util
skip_no_unbound = pytest.mark.skipif("unbound" not in dns_resolve.__dict__,
reason="Unbound not installed")
HOSTS = [("192.0.2.1", "fake.ip4.domain", "192.0.2.1"),
("2001:db8:2::1", "fake.ip6.domain", "2001:db8:2::1"),
("192.0.2.2", "fake.both.domain", "192.0.2.2"),
("2001:db8:2::2", "fake.both.domain", "192.0.2.2")]
def _tmp_file(path, content):
path.write_text(content)
assert content == path.read_text()
return path
@pytest.fixture(params=[False, True], ids=["not_daemon", "daemon"])
def resolver_factory(monkeypatch, tmp_path, hosts_file, request):
# Allow delaying the instantiation of the DNSResolver
def resolver_factory():
with monkeypatch.context() as m:
m.setenv("OVS_HOSTS_FILE", str(hosts_file))
# Test with both is_daemon False and True
resolver = dns_resolve.init(request.param)
assert resolver._is_daemon == request.param
return resolver
return resolver_factory
@contextlib.contextmanager
def DNSResolver(*args, **kwargs):
"""Clean up after returning a dns_resolver.DNSResolver"""
resolver = dns_resolve.init(*args, **kwargs)
try:
yield resolver
finally:
dns_resolve.destroy()
assert dns_resolve._global_resolver is None
@pytest.fixture
def unbound_conf(tmp_path):
path = tmp_path / "unbound.conf"
content = """
server:
verbosity: 1
"""
return _tmp_file(path, content)
@pytest.fixture
def resolv_conf(tmp_path):
path = tmp_path / "resolv.conf"
content = "nameserver 127.0.0.1"
return _tmp_file(path, content)
@pytest.fixture
def hosts_file(tmp_path):
path = tmp_path / "hosts"
content = "\n".join(f"{ip}\t{host}" for ip, host, _ in HOSTS)
return _tmp_file(path, content)
@pytest.fixture
def missing_file(tmp_path):
f = tmp_path / "missing_file"
assert not f.exists()
return f
@pytest.fixture(params=[False, True], ids=["with unbound", "without unbound"])
def missing_unbound(monkeypatch, request):
if request.param:
if "unbound" in dns_resolve.__dict__:
monkeypatch.setitem(sys.modules, 'unbound', None)
monkeypatch.delitem(dns_resolve.__dict__, "unbound")
elif "unbound" not in dns_resolve.__dict__:
pytest.skip("Unbound not installed")
return request.param
def test_missing_unbound(missing_unbound, resolver_factory):
resolver = resolver_factory() # Dont fail even w/o unbound
assert resolver.dns_enabled == (not missing_unbound)
def test_DNSRequest_defaults():
req = dns_resolve.DNSRequest(HOSTS[0][1])
assert HOSTS[0][1] == req.name
assert req.state == dns_resolve.ReqState.INVALID
assert req.time == req.result == req.ttl is None
assert str(req)
def _resolve(resolver, host, fn=dns_resolve.resolve):
"""Handle sync/async lookups, giving up if more than 1 second has passed"""
timeout = 1
start = time.time()
name = fn(host)
if resolver and resolver._is_daemon:
while name is None:
name = fn(host)
if name:
break
time.sleep(0.01)
end = time.time()
if end - start > timeout:
break
if name:
return name
raise LookupError(f"{host} not found")
@pytest.mark.parametrize("ip,host,expected", HOSTS)
def test_resolve_addresses(missing_unbound, resolver_factory, ip, host,
expected):
resolver = resolver_factory()
if missing_unbound:
with pytest.raises(LookupError):
_resolve(resolver, host)
else:
result = _resolve(resolver, host)
assert ipaddress.ip_address(expected) == ipaddress.ip_address(result)
@pytest.mark.parametrize("ip,host,expected", HOSTS)
def test_resolve_without_init(monkeypatch, missing_unbound, ip, host, expected,
hosts_file):
# make sure we don't have a global resolver
dns_resolve.destroy()
with monkeypatch.context() as m:
m.setenv("OVS_HOSTS_FILE", str(hosts_file))
if missing_unbound:
with pytest.raises(LookupError):
_resolve(None, host)
else:
res = _resolve(None, host)
assert dns_resolve._global_resolver is not None
assert dns_resolve._global_resolver._is_daemon is False
assert ipaddress.ip_address(expected) == ipaddress.ip_address(res)
def test_resolve_unknown_host(missing_unbound, resolver_factory):
resolver = resolver_factory()
with pytest.raises(LookupError):
_resolve(resolver, "fake.notadomain")
@skip_no_unbound
def test_resolve_process_error():
with DNSResolver(True) as resolver:
with mock.patch.object(resolver._ctx, "process", return_value=-1):
assert resolver.resolve("fake.domain") is None
@skip_no_unbound
def test_resolve_resolve_error():
with DNSResolver(False) as resolver:
with mock.patch.object(resolver._ctx, "resolve",
return_value=(-1, None)):
assert resolver.resolve("fake.domain") is None
@skip_no_unbound
def test_resolve_resolve_async_error():
with DNSResolver(True) as resolver:
with mock.patch.object(resolver._ctx, "resolve_async",
return_value=(-1, None)):
with pytest.raises(LookupError):
_resolve(resolver, "fake.domain")
@pytest.mark.parametrize("file,raises",
[(None, False),
("missing_file", dns_resolve.UnboundException),
("unbound_conf", False)])
def test_set_unbound_conf(monkeypatch, missing_unbound, resolver_factory,
request, file, raises):
if file:
file = str(request.getfixturevalue(file))
monkeypatch.setenv("OVS_UNBOUND_CONF", file)
resolver = resolver_factory() # Doesn't raise
if missing_unbound:
assert resolver._set_unbound_conf() is None
return
with mock.patch.object(resolver._ctx, "config",
side_effect=resolver._ctx.config) as c:
if raises:
with pytest.raises(raises):
resolver._set_unbound_conf()
else:
resolver._set_unbound_conf()
if file:
c.assert_called_once_with(file)
else:
c.assert_not_called()
@pytest.mark.parametrize("file,raises",
[(None, False),
("missing_file", dns_resolve.UnboundException),
("resolv_conf", False)])
def test_resolv_conf(monkeypatch, missing_unbound, resolver_factory, request,
file, raises):
if file:
file = str(request.getfixturevalue(file))
monkeypatch.setenv("OVS_RESOLV_CONF", file)
resolver = resolver_factory() # Doesn't raise
if missing_unbound:
assert resolver._set_resolv_conf() is None
return
with mock.patch.object(resolver._ctx, "resolvconf",
side_effect=resolver._ctx.resolvconf) as c:
if raises:
with pytest.raises(raises):
resolver._set_resolv_conf()
else:
resolver._set_resolv_conf()
c.assert_called_once_with(file)
@pytest.mark.parametrize("file,raises",
[(None, False),
("missing_file", dns_resolve.UnboundException),
("hosts_file", False)])
def test_hosts(monkeypatch, missing_unbound, resolver_factory, request, file,
raises):
if file:
file = str(request.getfixturevalue(file))
monkeypatch.setenv("OVS_HOSTS_FILE", file)
resolver = resolver_factory() # Doesn't raise
if missing_unbound:
assert resolver._set_hosts_file() is None
return
with mock.patch.object(resolver._ctx, "hosts",
side_effect=resolver._ctx.hosts) as c:
if raises:
with pytest.raises(raises):
resolver._set_hosts_file()
else:
resolver._set_hosts_file()
c.assert_called_once_with(file)
def test_UnboundException(missing_unbound):
with pytest.raises(dns_resolve.UnboundException):
raise dns_resolve.UnboundException("Fake exception", -1)
@skip_no_unbound
@pytest.mark.parametrize("ip,host,expected", HOSTS)
def test_inet_parse_active(resolver_factory, ip, host, expected):
resolver = resolver_factory()
def fn(name):
# Return the same thing _resolve() would so we can call
# this multiple times for the is_daemon=True case
return socket_util.inet_parse_active(f"{name}:6640", 6640,
raises=False)[0] or None
# parsing IPs still works
IP = _resolve(resolver, ip, fn)
assert ipaddress.ip_address(ip) == ipaddress.ip_address(IP)
# parsing hosts works
IP = _resolve(resolver, host, fn)
assert ipaddress.ip_address(IP) == ipaddress.ip_address(expected)
|