feat: helper script to create new records if they don't exist
This commit is contained in:
5 files changed
+198
-12
No files matched your search
+57
-4
@@ -13,6 +13,7 @@ import http.client
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import socket
|
||||
import tempfile
|
||||
import threading
|
||||
@@ -229,6 +230,13 @@ class FakeInwxHandler(BaseHTTPRequestHandler):
|
||||
if r["name"] == p["name"] and r["type"] == p["type"]
|
||||
]
|
||||
}
|
||||
elif method == "nameserver.createRecord":
|
||||
srv.records.append(
|
||||
{
|
||||
"id": 100 + len(srv.records),
|
||||
**{k: p[k] for k in ("name", "type", "content", "ttl")},
|
||||
}
|
||||
)
|
||||
elif method == "nameserver.updateRecord":
|
||||
for r in srv.records:
|
||||
if r["id"] == p["id"]:
|
||||
@@ -283,6 +291,27 @@ class InwxClientTest(unittest.TestCase):
|
||||
sd.inwx_update(self.cfg(srv), "example.com", HOST, "A", "1.2.3.4")
|
||||
self.assertEqual(srv.calls[-1], "account.logout")
|
||||
|
||||
def test_ensure_record_creates_missing_record_once(self):
|
||||
srv = self.start()
|
||||
created, record = sd.ensure_record(self.cfg(srv), "example.com", HOST, "AAAA")
|
||||
self.assertEqual(
|
||||
(created, record), (True, {"content": sd.PLACEHOLDER["AAAA"], "ttl": 300})
|
||||
)
|
||||
self.assertEqual(srv.records[-1]["ttl"], 300)
|
||||
self.assertEqual(
|
||||
sd.ensure_record(self.cfg(srv), "example.com", HOST, "AAAA")[0], False
|
||||
)
|
||||
self.assertEqual(srv.calls.count("nameserver.createRecord"), 1)
|
||||
|
||||
def test_ensure_record_keeps_existing_record(self):
|
||||
srv = self.start()
|
||||
created, record = sd.ensure_record(
|
||||
self.cfg(srv), "example.com", HOST, "A", ttl=60
|
||||
)
|
||||
self.assertFalse(created)
|
||||
self.assertEqual(record["content"], "10.0.0.1")
|
||||
self.assertNotIn("nameserver.createRecord", srv.calls)
|
||||
|
||||
def test_missing_record(self):
|
||||
srv = self.start()
|
||||
with self.assertRaises(sd.NoHost):
|
||||
@@ -460,16 +489,21 @@ token_sha256 = "{hashlib.sha256(TOKEN.encode()).hexdigest()}"
|
||||
"set INWX_OTE_USER, INWX_OTE_PASS, INWX_OTE_HOST to run against the INWX OT&E system",
|
||||
)
|
||||
class OteSmokeTest(unittest.TestCase):
|
||||
def test_lookup_by_name_and_update(self):
|
||||
host = os.environ["INWX_OTE_HOST"].lower()
|
||||
zone = os.environ.get("INWX_OTE_ZONE") or ".".join(host.split(".")[-2:])
|
||||
cfg = {
|
||||
def setUp(self):
|
||||
self.host = os.environ["INWX_OTE_HOST"].lower()
|
||||
self.zone = os.environ.get("INWX_OTE_ZONE") or ".".join(
|
||||
self.host.split(".")[-2:]
|
||||
)
|
||||
self.cfg = {
|
||||
"inwx": {
|
||||
"user": os.environ["INWX_OTE_USER"],
|
||||
"pass": os.environ["INWX_OTE_PASS"],
|
||||
"api": "https://api.ote.domrobot.com/jsonrpc/",
|
||||
}
|
||||
}
|
||||
|
||||
def test_lookup_by_name_and_update(self):
|
||||
cfg, zone, host = self.cfg, self.zone, self.host
|
||||
with sd.inwx_session(cfg) as call:
|
||||
original = sd.find_record(call, zone, host, "A")["content"]
|
||||
test_ip = "192.0.2.1" if original != "192.0.2.1" else "192.0.2.2"
|
||||
@@ -482,6 +516,25 @@ class OteSmokeTest(unittest.TestCase):
|
||||
finally:
|
||||
sd.inwx_update(cfg, zone, host, "A", original)
|
||||
|
||||
def test_ensure_record_creates_once_and_is_cleaned_up(self):
|
||||
cfg, zone = self.cfg, self.zone
|
||||
name = f"snap-test-{secrets.token_hex(3)}.{zone}"
|
||||
try:
|
||||
self.assertTrue(sd.ensure_record(cfg, zone, name, "A")[0])
|
||||
with sd.inwx_session(cfg) as call:
|
||||
record = sd.find_record(call, zone, name, "A")
|
||||
self.assertEqual(
|
||||
(record["content"], int(record["ttl"])), (sd.PLACEHOLDER["A"], 300)
|
||||
)
|
||||
self.assertFalse(sd.ensure_record(cfg, zone, name, "A")[0])
|
||||
finally:
|
||||
with (
|
||||
sd.inwx_session(cfg) as call,
|
||||
contextlib.suppress(sd.NoHost),
|
||||
):
|
||||
record = sd.find_record(call, zone, name, "A")
|
||||
call("nameserver.deleteRecord", {"id": record["id"]})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user