You've already forked directdnsonly
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
|
|
import pytest
|
||
|
|
from sqlalchemy import create_engine
|
||
|
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||
|
|
|
||
|
|
from directdnsonly.app.backends.coredns_mysql import CoreDNSMySQLBackend, CoreDNSRecord
|
||
|
|
from loguru import logger
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def mysql_backend(tmp_path):
|
||
|
|
# Setup in-memory SQLite for testing (replace with test MySQL in CI)
|
||
|
|
engine = create_engine("sqlite:///:memory:")
|
||
|
|
CoreDNSRecord.metadata.create_all(engine)
|
||
|
|
|
||
|
|
class TestBackend(CoreDNSMySQLBackend):
|
||
|
|
def __init__(self):
|
||
|
|
super().__init__()
|
||
|
|
self.engine = engine
|
||
|
|
self.Session = scoped_session(sessionmaker(bind=engine))
|
||
|
|
|
||
|
|
yield TestBackend()
|
||
|
|
engine.dispose()
|
||
|
|
|
||
|
|
|
||
|
|
def test_zone_operations(mysql_backend):
|
||
|
|
zone_data = """
|
||
|
|
example.com. 300 IN SOA ns.example.com. admin.example.com. (2023 3600 1800 604800 86400)
|
||
|
|
example.com. 300 IN A 192.0.2.1
|
||
|
|
"""
|
||
|
|
# Test zone creation
|
||
|
|
assert mysql_backend.write_zone("example.com", zone_data)
|
||
|
|
assert mysql_backend.zone_exists("example.com")
|
||
|
|
|
||
|
|
# Test record update
|
||
|
|
updated_zone = """
|
||
|
|
example.com. 3600 IN A 192.0.2.1
|
||
|
|
example.com. 300 IN AAAA 2001:db8::1
|
||
|
|
"""
|
||
|
|
assert mysql_backend.write_zone("example.com", updated_zone)
|
||
|
|
|
||
|
|
# Test record removal
|
||
|
|
reduced_zone = "example.com. 300 IN A 192.0.2.1"
|
||
|
|
assert mysql_backend.write_zone("example.com", reduced_zone)
|
||
|
|
|
||
|
|
# Test zone deletion
|
||
|
|
assert mysql_backend.delete_zone("example.com")
|
||
|
|
assert not mysql_backend.zone_exists("example.com")
|