Skip to content

Commit

Permalink
We were not calling check_delete_rdataset() in one delete path.
Browse files Browse the repository at this point in the history
This commit adds comprehensive testing of checks, and also
plugs a few other coverage gaps.
  • Loading branch information
rthalley committed Mar 12, 2024
1 parent 21cda62 commit a5996b5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 1 addition & 3 deletions dns/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def _delete(self, exact, args):
if exact:
raise DeleteNotExact(f"{method}: missing rdataset")
else:
self._delete_rdataset(name, rdtype, covers)
self._checked_delete_rdataset(name, rdtype, covers)
return
else:
rdataset = self._rdataset_from_args(method, True, args)
Expand Down Expand Up @@ -529,8 +529,6 @@ def _check_ended(self):

def _end(self, commit):
self._check_ended()
if self._ended:
raise AlreadyEnded
try:
self._end_transaction(commit)
finally:
Expand Down
42 changes: 41 additions & 1 deletion tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import dns.name
import dns.rdataclass
import dns.rdatatype
import dns.rdataset
import dns.rdatatype
import dns.rrset
import dns.transaction
import dns.versioned
Expand Down Expand Up @@ -227,6 +227,41 @@ def test_cannot_store_non_origin_soa(db):
txn.add(rrset)


def test_checks(db):
called = set()
with db.writer() as txn:
txn.check_put_rdataset(lambda t, n, r: called.add("put_rdataset"))
txn.check_delete_rdataset(lambda t, n, r, c: called.add("delete_rdataset"))
txn.check_delete_name(lambda t, n: called.add("delete_name"))
rrset = dns.rrset.from_text("foo", 300, "in", "A", "10.0.0.1", "10.0.0.2")
txn.add(rrset)
rrset = dns.rrset.from_text("foo", 300, "in", "AAAA", "::1")
txn.add(rrset)
assert "put_rdataset" in called
rrset = dns.rrset.from_text("foo", 300, "in", "txt", "foo")
txn.add(rrset)
called.clear()
txn.delete("foo", "txt")
assert "delete_rdataset" in called
called.clear()
rdata = dns.rdata.from_text("in", "a", "10.0.0.2")
txn.delete("foo", rdata)
# we get put here as we're storing an updated rrset, not deleting it
assert "put_rdataset" in called
called.clear()
rdata = dns.rdata.from_text("in", "a", "10.0.0.1")
# now we are deleting
txn.delete("foo", rdata)
assert "delete_rdataset" in called
# non-match calls nothing
called.clear()
txn.delete("foo", "rrsig", "a")
assert len(called) == 0
# delete the name
txn.delete("foo")
assert "delete_name" in called


example_text = """$TTL 3600
$ORIGIN example.
@ soa foo bar 1 2 3 4 5
Expand Down Expand Up @@ -462,6 +497,11 @@ def test_update_serial(zone):
txn.update_serial(0, False)
rdataset = zone.find_rdataset("@", "soa")
assert rdataset[0].serial == 1
# specifying the name explicitly works
with zone.writer() as txn:
txn.update_serial(1, True, "@")
rdataset = zone.find_rdataset("@", "soa")
assert rdataset[0].serial == 2
with pytest.raises(KeyError):
with zone.writer() as txn:
txn.update_serial(name=dns.name.from_text("unknown", None))
Expand Down

0 comments on commit a5996b5

Please sign in to comment.