From 65b3d0379658b5a4788c451860aa281232841144 Mon Sep 17 00:00:00 2001
From: Jan Vcelak <jan.vcelak@nic.cz>
Date: Wed, 22 Oct 2014 18:55:55 +0200
Subject: [PATCH] tests-extra: zone expiration by broken AXFR

---
 .../tests/zone/expire/data/example.zone       |  5 ++
 .../tests/zone/expire/data/example.zone.1     |  5 ++
 tests-extra/tests/zone/expire/test.py         | 53 +++++++++++++++++++
 tests-extra/tools/dnstest/server.py           |  2 +-
 4 files changed, 64 insertions(+), 1 deletion(-)
 create mode 100644 tests-extra/tests/zone/expire/data/example.zone
 create mode 100644 tests-extra/tests/zone/expire/data/example.zone.1
 create mode 100644 tests-extra/tests/zone/expire/test.py

diff --git a/tests-extra/tests/zone/expire/data/example.zone b/tests-extra/tests/zone/expire/data/example.zone
new file mode 100644
index 000000000..2fbcf4bc4
--- /dev/null
+++ b/tests-extra/tests/zone/expire/data/example.zone
@@ -0,0 +1,5 @@
+$ORIGIN example.
+$TTL 1200
+
+@ SOA ns admin 42 1 1 8 600
+ns AAAA ::0
diff --git a/tests-extra/tests/zone/expire/data/example.zone.1 b/tests-extra/tests/zone/expire/data/example.zone.1
new file mode 100644
index 000000000..0c22c7a17
--- /dev/null
+++ b/tests-extra/tests/zone/expire/data/example.zone.1
@@ -0,0 +1,5 @@
+$ORIGIN example.
+$TTL 1200
+
+@ SOA ns admin 4242 1 1 8 600
+ns AAAA ::0
diff --git a/tests-extra/tests/zone/expire/test.py b/tests-extra/tests/zone/expire/test.py
new file mode 100644
index 000000000..4a1469bbe
--- /dev/null
+++ b/tests-extra/tests/zone/expire/test.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python3
+
+'''Test zone expiration by master shutdown or broken AXFR.'''
+
+from dnstest.test import Test
+
+def test_expire(zone, server):
+    resp = server.dig(zone[0].name, "SOA")
+    resp.check(rcode="SERVFAIL")
+
+def break_xfrout(server):
+    with open(server.confile, "r+") as f:
+        config = f.read()
+        f.seek(0)
+        f.truncate()
+        config = config.replace("xfr-out ", "#xfr-out ")
+        f.write(config)
+
+t = Test(tsig=False)
+
+# this zone has refresh = 1s, retry = 1s and expire = 10s + 2s for connection timeouts
+zone = t.zone("example.", storage=".")
+EXPIRE_SLEEP = 15
+
+master = t.server("knot")
+slave = t.server("knot")
+slave.max_conn_idle = "1s"
+
+t.link(zone, master, slave)
+
+t.start()
+
+master.zone_wait(zone)
+slave.zone_wait(zone)
+
+# expire by shutting down the master
+master.stop()
+t.sleep(EXPIRE_SLEEP);
+test_expire(zone, slave)
+
+# bring back master (notifies slave)
+master.start()
+master.zone_wait(zone)
+slave.zone_wait(zone)
+
+# expire by breaking AXFR
+break_xfrout(master)
+master.update_zonefile(zone, version=1)
+master.reload()
+t.sleep(EXPIRE_SLEEP);
+test_expire(zone, slave)
+
+t.stop()
diff --git a/tests-extra/tools/dnstest/server.py b/tests-extra/tools/dnstest/server.py
index 05d69e7f0..4ec22f957 100644
--- a/tests-extra/tools/dnstest/server.py
+++ b/tests-extra/tools/dnstest/server.py
@@ -391,7 +391,7 @@ class Server(object):
     def gen_confile(self):
         f = open(self.confile, mode="w")
         f.write(self.get_config())
-        f.close
+        f.close()
 
     def dig(self, rname, rtype, rclass="IN", udp=None, serial=None,
             timeout=None, tries=3, flags="", bufsize=None, edns=None,
-- 
GitLab