| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
import time |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import dns.name |
| 8 |
import dns.rdataclass |
| 9 |
import dns.rdataset |
| 10 |
import dns.rdatatype |
| 11 |
import dns.rrset |
| 12 |
import dns.transaction |
| 13 |
import dns.versioned |
| 14 |
import dns.zone |
| 15 |
|
| 16 |
|
| 17 |
class DB(dns.transaction.TransactionManager): |
| 18 |
def __init__(self): |
| 19 |
self.rdatasets = {} |
| 20 |
|
| 21 |
def reader(self): |
| 22 |
return Transaction(self, False, True) |
| 23 |
|
| 24 |
def writer(self, replacement=False): |
| 25 |
return Transaction(self, replacement, False) |
| 26 |
|
| 27 |
def origin_information(self): |
| 28 |
return (dns.name.from_text("example"), True, dns.name.empty) |
| 29 |
|
| 30 |
def get_class(self): |
| 31 |
return dns.rdataclass.IN |
| 32 |
|
| 33 |
|
| 34 |
class Transaction(dns.transaction.Transaction): |
| 35 |
def __init__(self, db, replacement, read_only): |
| 36 |
super().__init__(db, replacement, read_only) |
| 37 |
self.rdatasets = {} |
| 38 |
if not replacement: |
| 39 |
self.rdatasets.update(db.rdatasets) |
| 40 |
|
| 41 |
@property |
| 42 |
def db(self): |
| 43 |
return self.manager |
| 44 |
|
| 45 |
def _get_rdataset(self, name, rdtype, covers): |
| 46 |
return self.rdatasets.get((name, rdtype, covers)) |
| 47 |
|
| 48 |
def _put_rdataset(self, name, rdataset): |
| 49 |
self.rdatasets[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 50 |
|
| 51 |
def _delete_name(self, name): |
| 52 |
remove = [] |
| 53 |
for key in self.rdatasets.keys(): |
| 54 |
if key[0] == name: |
| 55 |
remove.append(key) |
| 56 |
if len(remove) > 0: |
| 57 |
for key in remove: |
| 58 |
del self.rdatasets[key] |
| 59 |
|
| 60 |
def _delete_rdataset(self, name, rdtype, covers): |
| 61 |
del self.rdatasets[(name, rdtype, covers)] |
| 62 |
|
| 63 |
def _name_exists(self, name): |
| 64 |
for key in self.rdatasets.keys(): |
| 65 |
if key[0] == name: |
| 66 |
return True |
| 67 |
return False |
| 68 |
|
| 69 |
def _changed(self): |
| 70 |
if self.read_only: |
| 71 |
return False |
| 72 |
else: |
| 73 |
return len(self.rdatasets) > 0 |
| 74 |
|
| 75 |
def _end_transaction(self, commit): |
| 76 |
if commit: |
| 77 |
self.db.rdatasets = self.rdatasets |
| 78 |
|
| 79 |
def _set_origin(self, origin): |
| 80 |
pass |
| 81 |
|
| 82 |
|
| 83 |
@pytest.fixture |
| 84 |
def db(): |
| 85 |
db = DB() |
| 86 |
rrset = dns.rrset.from_text("content", 300, "in", "txt", "content") |
| 87 |
db.rdatasets[(rrset.name, rrset.rdtype, 0)] = rrset |
| 88 |
return db |
| 89 |
|
| 90 |
|
| 91 |
def test_basic(db): |
| 92 |
# successful txn |
| 93 |
with db.writer() as txn: |
| 94 |
rrset = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.1", "10.0.0.2") |
| 95 |
txn.add(rrset) |
| 96 |
assert txn.name_exists(rrset.name) |
| 97 |
assert db.rdatasets[(rrset.name, rrset.rdtype, 0)] == rrset |
| 98 |
# rollback |
| 99 |
with pytest.raises(Exception): |
| 100 |
with db.writer() as txn: |
| 101 |
rrset2 = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.3", "10.0.0.4") |
| 102 |
txn.add(rrset2) |
| 103 |
raise Exception() |
| 104 |
assert db.rdatasets[(rrset.name, rrset.rdtype, 0)] == rrset |
| 105 |
with db.writer() as txn: |
| 106 |
txn.delete(rrset.name) |
| 107 |
assert db.rdatasets.get((rrset.name, rrset.rdtype, 0)) is None |
| 108 |
|
| 109 |
|
| 110 |
def test_get(db): |
| 111 |
with db.writer() as txn: |
| 112 |
content = dns.name.from_text("content", None) |
| 113 |
rdataset = txn.get(content, dns.rdatatype.TXT) |
| 114 |
assert rdataset is not None |
| 115 |
assert rdataset[0].strings == (b"content",) |
| 116 |
assert isinstance(rdataset, dns.rdataset.ImmutableRdataset) |
| 117 |
|
| 118 |
|
| 119 |
def test_add(db): |
| 120 |
with db.writer() as txn: |
| 121 |
rrset = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.1", "10.0.0.2") |
| 122 |
txn.add(rrset) |
| 123 |
rrset2 = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.3", "10.0.0.4") |
| 124 |
txn.add(rrset2) |
| 125 |
expected = dns.rrset.from_text( |
| 126 |
"foo", 300, "in", "a", "10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4" |
| 127 |
) |
| 128 |
assert db.rdatasets[(rrset.name, rrset.rdtype, 0)] == expected |
| 129 |
|
| 130 |
|
| 131 |
def test_replacement(db): |
| 132 |
with db.writer() as txn: |
| 133 |
rrset = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.1", "10.0.0.2") |
| 134 |
txn.add(rrset) |
| 135 |
rrset2 = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.3", "10.0.0.4") |
| 136 |
txn.replace(rrset2) |
| 137 |
assert db.rdatasets[(rrset.name, rrset.rdtype, 0)] == rrset2 |
| 138 |
|
| 139 |
|
| 140 |
def test_delete(db): |
| 141 |
with db.writer() as txn: |
| 142 |
txn.delete(dns.name.from_text("nonexistent", None)) |
| 143 |
content = dns.name.from_text("content", None) |
| 144 |
content2 = dns.name.from_text("content2", None) |
| 145 |
txn.delete(content) |
| 146 |
assert not txn.name_exists(content) |
| 147 |
txn.delete(content2, dns.rdatatype.TXT) |
| 148 |
rrset = dns.rrset.from_text("content", 300, "in", "txt", "new-content") |
| 149 |
txn.add(rrset) |
| 150 |
assert txn.name_exists(content) |
| 151 |
txn.delete(content, dns.rdatatype.TXT) |
| 152 |
assert not txn.name_exists(content) |
| 153 |
rrset = dns.rrset.from_text("content2", 300, "in", "txt", "new-content") |
| 154 |
txn.delete(rrset) |
| 155 |
content_keys = [k for k in db.rdatasets if k[0] == content] |
| 156 |
assert len(content_keys) == 0 |
| 157 |
|
| 158 |
|
| 159 |
def test_delete_exact(db): |
| 160 |
with db.writer() as txn: |
| 161 |
rrset = dns.rrset.from_text("content", 300, "in", "txt", "bad-content") |
| 162 |
with pytest.raises(dns.transaction.DeleteNotExact): |
| 163 |
txn.delete_exact(rrset) |
| 164 |
rrset = dns.rrset.from_text("content2", 300, "in", "txt", "bad-content") |
| 165 |
with pytest.raises(dns.transaction.DeleteNotExact): |
| 166 |
txn.delete_exact(rrset) |
| 167 |
with pytest.raises(dns.transaction.DeleteNotExact): |
| 168 |
txn.delete_exact(rrset.name) |
| 169 |
with pytest.raises(dns.transaction.DeleteNotExact): |
| 170 |
txn.delete_exact(rrset.name, dns.rdatatype.TXT) |
| 171 |
rrset = dns.rrset.from_text("content", 300, "in", "txt", "content") |
| 172 |
txn.delete_exact(rrset) |
| 173 |
assert db.rdatasets.get((rrset.name, rrset.rdtype, 0)) is None |
| 174 |
|
| 175 |
|
| 176 |
def test_parameter_forms(db): |
| 177 |
with db.writer() as txn: |
| 178 |
foo = dns.name.from_text("foo", None) |
| 179 |
rdataset = dns.rdataset.from_text("in", "a", 300, "10.0.0.1", "10.0.0.2") |
| 180 |
rdata1 = dns.rdata.from_text("in", "a", "10.0.0.3") |
| 181 |
rdata2 = dns.rdata.from_text("in", "a", "10.0.0.4") |
| 182 |
txn.add(foo, rdataset) |
| 183 |
txn.add(foo, 100, rdata1) |
| 184 |
txn.add(foo, 30, rdata2) |
| 185 |
expected = dns.rrset.from_text( |
| 186 |
"foo", 30, "in", "a", "10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4" |
| 187 |
) |
| 188 |
assert db.rdatasets[(foo, rdataset.rdtype, 0)] == expected |
| 189 |
with db.writer() as txn: |
| 190 |
txn.delete(foo, rdataset) |
| 191 |
txn.delete(foo, rdata1) |
| 192 |
txn.delete(foo, rdata2) |
| 193 |
assert db.rdatasets.get((foo, rdataset.rdtype, 0)) is None |
| 194 |
|
| 195 |
|
| 196 |
def test_bad_parameters(db): |
| 197 |
with db.writer() as txn: |
| 198 |
with pytest.raises(TypeError): |
| 199 |
txn.add(1) |
| 200 |
with pytest.raises(TypeError): |
| 201 |
rrset = dns.rrset.from_text("bar", 300, "in", "txt", "bar") |
| 202 |
txn.add(rrset, 1) |
| 203 |
with pytest.raises(ValueError): |
| 204 |
foo = dns.name.from_text("foo", None) |
| 205 |
rdata = dns.rdata.from_text("in", "a", "10.0.0.3") |
| 206 |
txn.add(foo, 0x100000000, rdata) |
| 207 |
with pytest.raises(TypeError): |
| 208 |
txn.add(foo) |
| 209 |
with pytest.raises(TypeError): |
| 210 |
txn.add() |
| 211 |
with pytest.raises(TypeError): |
| 212 |
txn.add(foo, 300) |
| 213 |
with pytest.raises(TypeError): |
| 214 |
txn.add(foo, 300, "hi") |
| 215 |
with pytest.raises(TypeError): |
| 216 |
txn.add(foo, "hi") |
| 217 |
with pytest.raises(TypeError): |
| 218 |
txn.delete() |
| 219 |
with pytest.raises(TypeError): |
| 220 |
txn.delete(1) |
| 221 |
|
| 222 |
|
| 223 |
def test_cannot_store_non_origin_soa(db): |
| 224 |
with pytest.raises(ValueError): |
| 225 |
with db.writer() as txn: |
| 226 |
rrset = dns.rrset.from_text("foo", 300, "in", "SOA", ". . 1 2 3 4 5") |
| 227 |
txn.add(rrset) |
| 228 |
|
| 229 |
|
| 230 |
def test_checks(db): |
| 231 |
called = set() |
| 232 |
with db.writer() as txn: |
| 233 |
txn.check_put_rdataset(lambda t, n, r: called.add("put_rdataset")) |
| 234 |
txn.check_delete_rdataset(lambda t, n, r, c: called.add("delete_rdataset")) |
| 235 |
txn.check_delete_name(lambda t, n: called.add("delete_name")) |
| 236 |
rrset = dns.rrset.from_text("foo", 300, "in", "A", "10.0.0.1", "10.0.0.2") |
| 237 |
txn.add(rrset) |
| 238 |
rrset = dns.rrset.from_text("foo", 300, "in", "AAAA", "::1") |
| 239 |
txn.add(rrset) |
| 240 |
assert "put_rdataset" in called |
| 241 |
rrset = dns.rrset.from_text("foo", 300, "in", "txt", "foo") |
| 242 |
txn.add(rrset) |
| 243 |
called.clear() |
| 244 |
txn.delete("foo", "txt") |
| 245 |
assert "delete_rdataset" in called |
| 246 |
called.clear() |
| 247 |
rdata = dns.rdata.from_text("in", "a", "10.0.0.2") |
| 248 |
txn.delete("foo", rdata) |
| 249 |
# we get put here as we're storing an updated rrset, not deleting it |
| 250 |
assert "put_rdataset" in called |
| 251 |
called.clear() |
| 252 |
rdata = dns.rdata.from_text("in", "a", "10.0.0.1") |
| 253 |
# now we are deleting |
| 254 |
txn.delete("foo", rdata) |
| 255 |
assert "delete_rdataset" in called |
| 256 |
# non-match calls nothing |
| 257 |
called.clear() |
| 258 |
txn.delete("foo", "rrsig", "a") |
| 259 |
assert len(called) == 0 |
| 260 |
# delete the name |
| 261 |
txn.delete("foo") |
| 262 |
assert "delete_name" in called |
| 263 |
|
| 264 |
|
| 265 |
example_text = """$TTL 3600 |
| 266 |
$ORIGIN example. |
| 267 |
@ soa foo bar 1 2 3 4 5 |
| 268 |
@ ns ns1 |
| 269 |
@ ns ns2 |
| 270 |
ns1 a 10.0.0.1 |
| 271 |
ns2 a 10.0.0.2 |
| 272 |
$TTL 300 |
| 273 |
$ORIGIN foo.example. |
| 274 |
bar mx 0 blaz |
| 275 |
""" |
| 276 |
|
| 277 |
example_text_output = """@ 3600 IN SOA foo bar 1 2 3 4 5 |
| 278 |
@ 3600 IN NS ns1 |
| 279 |
@ 3600 IN NS ns2 |
| 280 |
@ 3600 IN NS ns3 |
| 281 |
ns1 3600 IN A 10.0.0.1 |
| 282 |
ns2 3600 IN A 10.0.0.2 |
| 283 |
ns3 3600 IN A 10.0.0.3 |
| 284 |
""" |
| 285 |
|
| 286 |
|
| 287 |
@pytest.fixture(params=[dns.zone.Zone, dns.versioned.Zone]) |
| 288 |
def zone(request): |
| 289 |
return dns.zone.from_text(example_text, zone_factory=request.param) |
| 290 |
|
| 291 |
|
| 292 |
def test_zone_basic(zone): |
| 293 |
with zone.writer() as txn: |
| 294 |
txn.delete(dns.name.from_text("bar.foo", None)) |
| 295 |
rd = dns.rdata.from_text("in", "ns", "ns3") |
| 296 |
txn.add(dns.name.empty, 3600, rd) |
| 297 |
rd = dns.rdata.from_text("in", "a", "10.0.0.3") |
| 298 |
txn.add(dns.name.from_text("ns3", None), 3600, rd) |
| 299 |
output = zone.to_text() |
| 300 |
assert output == example_text_output |
| 301 |
|
| 302 |
|
| 303 |
def test_explicit_rollback_and_commit(zone): |
| 304 |
with zone.writer() as txn: |
| 305 |
assert not txn.changed() |
| 306 |
txn.delete(dns.name.from_text("bar.foo", None)) |
| 307 |
txn.rollback() |
| 308 |
assert zone.get_node("bar.foo") is not None |
| 309 |
with zone.writer() as txn: |
| 310 |
assert not txn.changed() |
| 311 |
txn.delete(dns.name.from_text("bar.foo", None)) |
| 312 |
txn.commit() |
| 313 |
assert zone.get_node("bar.foo") is None |
| 314 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 315 |
with zone.writer() as txn: |
| 316 |
txn.rollback() |
| 317 |
txn.delete(dns.name.from_text("bar.foo", None)) |
| 318 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 319 |
with zone.writer() as txn: |
| 320 |
txn.rollback() |
| 321 |
txn.add("bar.foo", 300, dns.rdata.from_text("in", "txt", "hi")) |
| 322 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 323 |
with zone.writer() as txn: |
| 324 |
txn.rollback() |
| 325 |
txn.replace("bar.foo", 300, dns.rdata.from_text("in", "txt", "hi")) |
| 326 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 327 |
with zone.reader() as txn: |
| 328 |
txn.rollback() |
| 329 |
txn.get("bar.foo", "in", "mx") |
| 330 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 331 |
with zone.writer() as txn: |
| 332 |
txn.rollback() |
| 333 |
txn.delete_exact("bar.foo") |
| 334 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 335 |
with zone.writer() as txn: |
| 336 |
txn.rollback() |
| 337 |
txn.name_exists("bar.foo") |
| 338 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 339 |
with zone.writer() as txn: |
| 340 |
txn.rollback() |
| 341 |
txn.update_serial() |
| 342 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 343 |
with zone.writer() as txn: |
| 344 |
txn.rollback() |
| 345 |
txn.changed() |
| 346 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 347 |
with zone.writer() as txn: |
| 348 |
txn.rollback() |
| 349 |
txn.rollback() |
| 350 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 351 |
with zone.writer() as txn: |
| 352 |
txn.rollback() |
| 353 |
txn.commit() |
| 354 |
with pytest.raises(dns.transaction.AlreadyEnded): |
| 355 |
with zone.writer() as txn: |
| 356 |
txn.rollback() |
| 357 |
for rdataset in txn: |
| 358 |
pass |
| 359 |
|
| 360 |
|
| 361 |
def test_zone_changed(zone): |
| 362 |
# Read-only is not changed! |
| 363 |
with zone.reader() as txn: |
| 364 |
assert not txn.changed() |
| 365 |
# delete an existing name |
| 366 |
with zone.writer() as txn: |
| 367 |
assert not txn.changed() |
| 368 |
txn.delete(dns.name.from_text("bar.foo", None)) |
| 369 |
assert txn.changed() |
| 370 |
# delete a nonexistent name |
| 371 |
with zone.writer() as txn: |
| 372 |
assert not txn.changed() |
| 373 |
txn.delete(dns.name.from_text("unknown.bar.foo", None)) |
| 374 |
assert not txn.changed() |
| 375 |
# delete a nonexistent rdataset from an extant node |
| 376 |
with zone.writer() as txn: |
| 377 |
assert not txn.changed() |
| 378 |
txn.delete(dns.name.from_text("bar.foo", None), "txt") |
| 379 |
assert not txn.changed() |
| 380 |
# add an rdataset to an extant Node |
| 381 |
with zone.writer() as txn: |
| 382 |
assert not txn.changed() |
| 383 |
txn.add("bar.foo", 300, dns.rdata.from_text("in", "txt", "hi")) |
| 384 |
assert txn.changed() |
| 385 |
# add an rdataset to a nonexistent Node |
| 386 |
with zone.writer() as txn: |
| 387 |
assert not txn.changed() |
| 388 |
txn.add("foo.foo", 300, dns.rdata.from_text("in", "txt", "hi")) |
| 389 |
assert txn.changed() |
| 390 |
|
| 391 |
|
| 392 |
def test_zone_base_layer(zone): |
| 393 |
with zone.writer() as txn: |
| 394 |
# Get a set from the zone layer |
| 395 |
rdataset = txn.get(dns.name.empty, dns.rdatatype.NS, dns.rdatatype.NONE) |
| 396 |
expected = dns.rdataset.from_text("in", "ns", 300, "ns1", "ns2") |
| 397 |
assert rdataset == expected |
| 398 |
|
| 399 |
|
| 400 |
def test_zone_transaction_layer(zone): |
| 401 |
with zone.writer() as txn: |
| 402 |
# Make a change |
| 403 |
rd = dns.rdata.from_text("in", "ns", "ns3") |
| 404 |
txn.add(dns.name.empty, 3600, rd) |
| 405 |
# Get a set from the transaction layer |
| 406 |
expected = dns.rdataset.from_text("in", "ns", 300, "ns1", "ns2", "ns3") |
| 407 |
rdataset = txn.get(dns.name.empty, dns.rdatatype.NS, dns.rdatatype.NONE) |
| 408 |
assert rdataset == expected |
| 409 |
assert txn.name_exists(dns.name.empty) |
| 410 |
ns1 = dns.name.from_text("ns1", None) |
| 411 |
assert txn.name_exists(ns1) |
| 412 |
ns99 = dns.name.from_text("ns99", None) |
| 413 |
assert not txn.name_exists(ns99) |
| 414 |
|
| 415 |
|
| 416 |
def test_zone_add_and_delete(zone): |
| 417 |
with zone.writer() as txn: |
| 418 |
a99 = dns.name.from_text("a99", None) |
| 419 |
a100 = dns.name.from_text("a100", None) |
| 420 |
a101 = dns.name.from_text("a101", None) |
| 421 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.99") |
| 422 |
txn.add(a99, rds) |
| 423 |
txn.delete(a99, dns.rdatatype.A) |
| 424 |
txn.delete(a100, dns.rdatatype.A) |
| 425 |
txn.delete(a101) |
| 426 |
assert not txn.name_exists(a99) |
| 427 |
assert not txn.name_exists(a100) |
| 428 |
assert not txn.name_exists(a101) |
| 429 |
ns1 = dns.name.from_text("ns1", None) |
| 430 |
txn.delete(ns1, dns.rdatatype.A) |
| 431 |
assert not txn.name_exists(ns1) |
| 432 |
with zone.writer() as txn: |
| 433 |
txn.add(a99, rds) |
| 434 |
txn.delete(a99) |
| 435 |
assert not txn.name_exists(a99) |
| 436 |
with zone.writer() as txn: |
| 437 |
txn.add(a100, rds) |
| 438 |
txn.delete(a99) |
| 439 |
assert not txn.name_exists(a99) |
| 440 |
assert txn.name_exists(a100) |
| 441 |
|
| 442 |
|
| 443 |
def test_write_after_rollback(zone): |
| 444 |
with pytest.raises(ExpectedException): |
| 445 |
with zone.writer() as txn: |
| 446 |
a99 = dns.name.from_text("a99", None) |
| 447 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.99") |
| 448 |
txn.add(a99, rds) |
| 449 |
raise ExpectedException |
| 450 |
with zone.writer() as txn: |
| 451 |
a99 = dns.name.from_text("a99", None) |
| 452 |
rds = dns.rdataset.from_text("in", "a", 300, "10.99.99.99") |
| 453 |
txn.add(a99, rds) |
| 454 |
assert zone.get_rdataset("a99", "a") == rds |
| 455 |
|
| 456 |
|
| 457 |
def test_zone_get_deleted(zone): |
| 458 |
with zone.writer() as txn: |
| 459 |
ns1 = dns.name.from_text("ns1", None) |
| 460 |
assert txn.get(ns1, dns.rdatatype.A) is not None |
| 461 |
txn.delete(ns1) |
| 462 |
assert txn.get(ns1, dns.rdatatype.A) is None |
| 463 |
ns2 = dns.name.from_text("ns2", None) |
| 464 |
txn.delete(ns2, dns.rdatatype.A) |
| 465 |
assert txn.get(ns2, dns.rdatatype.A) is None |
| 466 |
|
| 467 |
|
| 468 |
def test_zone_bad_class(zone): |
| 469 |
with zone.writer() as txn: |
| 470 |
rds = dns.rdataset.from_text("ch", "ns", 300, "ns1", "ns2") |
| 471 |
with pytest.raises(ValueError): |
| 472 |
txn.add(dns.name.empty, rds) |
| 473 |
with pytest.raises(ValueError): |
| 474 |
txn.replace(dns.name.empty, rds) |
| 475 |
with pytest.raises(ValueError): |
| 476 |
txn.delete(dns.name.empty, rds) |
| 477 |
|
| 478 |
|
| 479 |
def test_update_serial(zone): |
| 480 |
# basic |
| 481 |
with zone.writer() as txn: |
| 482 |
txn.update_serial() |
| 483 |
rdataset = zone.find_rdataset("@", "soa") |
| 484 |
assert rdataset[0].serial == 2 |
| 485 |
# max |
| 486 |
with zone.writer() as txn: |
| 487 |
txn.update_serial(0xFFFFFFFF, False) |
| 488 |
rdataset = zone.find_rdataset("@", "soa") |
| 489 |
assert rdataset[0].serial == 0xFFFFFFFF |
| 490 |
# wraparound to 1 |
| 491 |
with zone.writer() as txn: |
| 492 |
txn.update_serial() |
| 493 |
rdataset = zone.find_rdataset("@", "soa") |
| 494 |
assert rdataset[0].serial == 1 |
| 495 |
# trying to set to zero sets to 1 |
| 496 |
with zone.writer() as txn: |
| 497 |
txn.update_serial(0, False) |
| 498 |
rdataset = zone.find_rdataset("@", "soa") |
| 499 |
assert rdataset[0].serial == 1 |
| 500 |
# specifying the name explicitly works |
| 501 |
with zone.writer() as txn: |
| 502 |
txn.update_serial(1, True, "@") |
| 503 |
rdataset = zone.find_rdataset("@", "soa") |
| 504 |
assert rdataset[0].serial == 2 |
| 505 |
with pytest.raises(KeyError): |
| 506 |
with zone.writer() as txn: |
| 507 |
txn.update_serial(name=dns.name.from_text("unknown", None)) |
| 508 |
with pytest.raises(ValueError): |
| 509 |
with zone.writer() as txn: |
| 510 |
txn.update_serial(-1) |
| 511 |
with pytest.raises(ValueError): |
| 512 |
with zone.writer() as txn: |
| 513 |
txn.update_serial(2**31) |
| 514 |
|
| 515 |
|
| 516 |
class ExpectedException(Exception): |
| 517 |
pass |
| 518 |
|
| 519 |
|
| 520 |
def test_zone_rollback(zone): |
| 521 |
a99 = dns.name.from_text("a99.example.") |
| 522 |
try: |
| 523 |
with zone.writer() as txn: |
| 524 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.99") |
| 525 |
txn.add(a99, rds) |
| 526 |
assert txn.name_exists(a99) |
| 527 |
raise ExpectedException |
| 528 |
except ExpectedException: |
| 529 |
pass |
| 530 |
assert not zone.get_node(a99) |
| 531 |
|
| 532 |
|
| 533 |
def test_zone_ooz_name(zone): |
| 534 |
with zone.writer() as txn: |
| 535 |
with pytest.raises(KeyError): |
| 536 |
a99 = dns.name.from_text("a99.not-example.") |
| 537 |
assert txn.name_exists(a99) |
| 538 |
|
| 539 |
|
| 540 |
def test_zone_iteration(zone): |
| 541 |
expected = {} |
| 542 |
for name, rdataset in zone.iterate_rdatasets(): |
| 543 |
expected[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 544 |
with zone.writer() as txn: |
| 545 |
actual1 = {} |
| 546 |
for name, rdataset in txn: |
| 547 |
actual1[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 548 |
actual2 = {} |
| 549 |
for name, rdataset in txn.iterate_rdatasets(): |
| 550 |
actual2[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 551 |
assert actual1 == expected |
| 552 |
assert actual2 == expected |
| 553 |
|
| 554 |
|
| 555 |
def test_zone_name_iteration(zone): |
| 556 |
expected = list(zone.keys()) |
| 557 |
with zone.writer() as txn: |
| 558 |
actual = list(txn.iterate_names()) |
| 559 |
assert actual == expected |
| 560 |
|
| 561 |
|
| 562 |
def test_iteration_in_replacement_txn(zone): |
| 563 |
rds = dns.rdataset.from_text("in", "a", 300, "1.2.3.4", "5.6.7.8") |
| 564 |
expected = {} |
| 565 |
expected[(dns.name.empty, rds.rdtype, rds.covers)] = rds |
| 566 |
with zone.writer(True) as txn: |
| 567 |
txn.replace(dns.name.empty, rds) |
| 568 |
actual = {} |
| 569 |
for name, rdataset in txn: |
| 570 |
actual[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 571 |
assert actual == expected |
| 572 |
|
| 573 |
|
| 574 |
def test_replacement_commit(zone): |
| 575 |
rds = dns.rdataset.from_text("in", "a", 300, "1.2.3.4", "5.6.7.8") |
| 576 |
expected = {} |
| 577 |
expected[(dns.name.empty, rds.rdtype, rds.covers)] = rds |
| 578 |
with zone.writer(True) as txn: |
| 579 |
txn.replace(dns.name.empty, rds) |
| 580 |
with zone.reader() as txn: |
| 581 |
actual = {} |
| 582 |
for name, rdataset in txn: |
| 583 |
actual[(name, rdataset.rdtype, rdataset.covers)] = rdataset |
| 584 |
assert actual == expected |
| 585 |
|
| 586 |
|
| 587 |
def test_replacement_get(zone): |
| 588 |
with zone.writer(True) as txn: |
| 589 |
rds = txn.get(dns.name.empty, "soa") |
| 590 |
assert rds is None |
| 591 |
|
| 592 |
|
| 593 |
@pytest.fixture |
| 594 |
def vzone(): |
| 595 |
return dns.zone.from_text(example_text, zone_factory=dns.versioned.Zone) |
| 596 |
|
| 597 |
|
| 598 |
def test_vzone_read_only(vzone): |
| 599 |
with vzone.reader() as txn: |
| 600 |
rdataset = txn.get(dns.name.empty, dns.rdatatype.NS, dns.rdatatype.NONE) |
| 601 |
expected = dns.rdataset.from_text("in", "ns", 300, "ns1", "ns2") |
| 602 |
assert rdataset == expected |
| 603 |
with pytest.raises(dns.transaction.ReadOnly): |
| 604 |
txn.replace(dns.name.empty, expected) |
| 605 |
|
| 606 |
|
| 607 |
def test_vzone_multiple_versions(vzone): |
| 608 |
assert len(vzone._versions) == 1 |
| 609 |
vzone.set_max_versions(None) # unlimited! |
| 610 |
with vzone.writer() as txn: |
| 611 |
txn.update_serial() |
| 612 |
with vzone.writer() as txn: |
| 613 |
txn.update_serial() |
| 614 |
with vzone.writer() as txn: |
| 615 |
txn.update_serial(1000, False) |
| 616 |
rdataset = vzone.find_rdataset("@", "soa") |
| 617 |
assert rdataset[0].serial == 1000 |
| 618 |
assert len(vzone._versions) == 4 |
| 619 |
with vzone.reader(id=5) as txn: |
| 620 |
assert txn.version.id == 5 |
| 621 |
rdataset = txn.get("@", "soa") |
| 622 |
assert rdataset[0].serial == 1000 |
| 623 |
with vzone.reader(serial=1000) as txn: |
| 624 |
assert txn.version.id == 5 |
| 625 |
rdataset = txn.get("@", "soa") |
| 626 |
assert rdataset[0].serial == 1000 |
| 627 |
vzone.set_max_versions(2) |
| 628 |
assert len(vzone._versions) == 2 |
| 629 |
# The ones that survived should be 3 and 1000 |
| 630 |
rdataset = vzone._versions[0].get_rdataset( |
| 631 |
dns.name.empty, dns.rdatatype.SOA, dns.rdatatype.NONE |
| 632 |
) |
| 633 |
assert rdataset[0].serial == 3 |
| 634 |
rdataset = vzone._versions[1].get_rdataset( |
| 635 |
dns.name.empty, dns.rdatatype.SOA, dns.rdatatype.NONE |
| 636 |
) |
| 637 |
assert rdataset[0].serial == 1000 |
| 638 |
with pytest.raises(ValueError): |
| 639 |
vzone.set_max_versions(0) |
| 640 |
|
| 641 |
|
| 642 |
# for debugging if needed |
| 643 |
def _dump(zone): |
| 644 |
for v in zone._versions: |
| 645 |
print("VERSION", v.id) |
| 646 |
for name, n in v.nodes.items(): |
| 647 |
for rdataset in n: |
| 648 |
print(rdataset.to_text(name)) |
| 649 |
|
| 650 |
|
| 651 |
def test_vzone_open_txn_pins_versions(vzone): |
| 652 |
assert len(vzone._versions) == 1 |
| 653 |
vzone.set_max_versions(None) # unlimited! |
| 654 |
with vzone.writer() as txn: |
| 655 |
txn.update_serial() |
| 656 |
with vzone.writer() as txn: |
| 657 |
txn.update_serial() |
| 658 |
with vzone.writer() as txn: |
| 659 |
txn.update_serial() |
| 660 |
with vzone.reader(id=2) as txn: |
| 661 |
vzone.set_max_versions(1) |
| 662 |
with vzone.reader(id=3) as txn: |
| 663 |
rdataset = txn.get("@", "soa") |
| 664 |
assert rdataset[0].serial == 2 |
| 665 |
assert len(vzone._versions) == 4 |
| 666 |
assert len(vzone._versions) == 1 |
| 667 |
rdataset = vzone.find_rdataset("@", "soa") |
| 668 |
assert vzone._versions[0].id == 5 |
| 669 |
assert rdataset[0].serial == 4 |
| 670 |
|
| 671 |
|
| 672 |
try: |
| 673 |
import threading |
| 674 |
|
| 675 |
one_got_lock = threading.Event() |
| 676 |
|
| 677 |
def run_one(zone): |
| 678 |
with zone.writer() as txn: |
| 679 |
one_got_lock.set() |
| 680 |
# wait until two blocks |
| 681 |
while len(zone._write_waiters) == 0: |
| 682 |
time.sleep(0.01) |
| 683 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.98") |
| 684 |
txn.add("a98", rds) |
| 685 |
|
| 686 |
def run_two(zone): |
| 687 |
# wait until one has the lock so we know we will block if we |
| 688 |
# get the call done before the sleep in one completes |
| 689 |
one_got_lock.wait() |
| 690 |
with zone.writer() as txn: |
| 691 |
rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.99") |
| 692 |
txn.add("a99", rds) |
| 693 |
|
| 694 |
def test_vzone_concurrency(vzone): |
| 695 |
t1 = threading.Thread(target=run_one, args=(vzone,)) |
| 696 |
t1.start() |
| 697 |
t2 = threading.Thread(target=run_two, args=(vzone,)) |
| 698 |
t2.start() |
| 699 |
t1.join() |
| 700 |
t2.join() |
| 701 |
with vzone.reader() as txn: |
| 702 |
assert txn.name_exists("a98") |
| 703 |
assert txn.name_exists("a99") |
| 704 |
|
| 705 |
except ImportError: # pragma: no cover |
| 706 |
pass |