Build unit tests.
All checks were successful
ci/woodpecker/push/package-deploy Pipeline was successful

This commit is contained in:
minecraftchest1@outlook.com 2025-01-23 08:31:31 -06:00
parent 9bce68a7fb
commit f5a79c43d6
5 changed files with 105 additions and 22 deletions

View file

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "dnscode"
version = "1.2.0"
version = "1.3.0"
authors = [
{ name="Minecraftchest1", email="me@minec1.us" },
]
@ -28,3 +28,11 @@ dependencies = [
[project.urls]
Homepage = "https://code.minecraftchest1.us/minecraftchest1/dnscode"
Issues = "https://code.minecraftchest1.us/minecraftchest1/dnscode/issues"
[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
pythonpath = [
"src"
]

View file

@ -1,2 +1,3 @@
dnspython==2.7.0
fqdn==1.5.1
pytest==8.3.3

View file

@ -51,10 +51,10 @@ class A(Record):
if isinstance(ipaddress.ip_address(host), ipaddress.IPv4Address):
self.data = host
else:
raise InvalidDataException(message=f'{host} is not a valid IPv4 address.')
raise InvalidDataException(message=f'{str(host)} is not a valid IPv4 address.')
self.rtype = 'A'
self.name = name
self.name = str(name)
self.ttl = ttl
@dataclass
@ -67,10 +67,10 @@ class AAAA(Record):
if isinstance(ipaddress.ip_address(host), ipaddress.IPv6Address):
self.data = host
else:
raise InvalidDataException(message=f'{host} is not a valid IPv6 address.')
raise InvalidDataException(message=f'{str(host)} is not a valid IPv6 address.')
self.rtype = 'AAAA'
self.name = name
self.name = str(name)
self.ttl = ttl
@dataclass
@ -81,7 +81,7 @@ class CNAME(Record):
def __init__(self, name: str = '@', ttl: int = 3600, host: str = 'example.com'):
self.rtype = 'CNAME'
self.name = name
self.name = str(name)
self.ttl = ttl
if fqdn.FQDN(host).is_valid:
@ -217,17 +217,17 @@ class Zone:
def __mkfqdn(self, name: str) -> str:
"""Converts a name to a fully qualified domain name (FQDN)."""
if name[-1] != '.':
return name + '.' + self.origin
if str(name)[-1] != '.':
return str(name) + '.' + self.origin
else:
return name
return str(name)
def new_A(self, name: str = '@', ttl: int = 3600, host: str = '0.0.0.0'):
"""Creates and adds a new A record to the zone."""
name = self.__mkfqdn(name)
self.add(A(name=name, ttl=ttl, host=host))
def new_AAAA(self, name: str = '@', ttl: int = 3600, host: str = '0.0.0.0'):
def new_AAAA(self, name: str = '@', ttl: int = 3600, host: str = 'fe80::42:2cff:fe29:8db1'):
"""Creates and adds a new AAAA record to the zone."""
name = self.__mkfqdn(name)
self.add(AAAA(name=name, ttl=ttl, host=host))

10
test.py
View file

@ -1,10 +0,0 @@
import main
zone = main.Zone(origin='example.com')
#soa = main.SOA()
record = main.Record(data='192.168.5.254', name='localhost.example.com')
#zone.add(soa)
zone.new_soa(mname='ns1.')
zone.add(record)
zone.add(main.A(name='example', data='fe80::727f:3322:18b1:23e7'))
zone.save_file('/tmp/zone.txt')

84
test_dnscode.py Normal file
View file

@ -0,0 +1,84 @@
import dnscode
import pytest
def test_A(tmp_path):
zone = dnscode.Zone(origin='minecraftchest1.us')
# Test named and positional arguments. Ensure defaults work.
zone.new_A("1")
zone.new_A("2", 60)
zone.new_A("3", 60, "0.0.0.0")
zone.new_A(host="0.0.0.0")
zone.new_A(name="4")
zone.new_A(ttl=120)
zone.new_A(name=5, host="0.0.0.0", ttl=120)
# Test improper arguments
zone.new_A(name=6)
zone.new_A(ttl="60")
with pytest.raises(dnscode.InvalidDataException):
zone.new_A(host="fe80::42:2cff:fe29:8db1")
with pytest.raises(ValueError):
zone.new_A(host="fe80::42:2cff:fe29:8db1/64")
with pytest.raises(ValueError):
zone.new_A(host="0.0.0.0/32")
zone_file = tmp_path / "test-A.zone"
zone.save_file(zone_file)
expected = """1.minecraftchest1.us. 3600 IN A 0.0.0.0
2.minecraftchest1.us. 60 IN A 0.0.0.0
3.minecraftchest1.us. 60 IN A 0.0.0.0
@.minecraftchest1.us. 3600 IN A 0.0.0.0
4.minecraftchest1.us. 3600 IN A 0.0.0.0
@.minecraftchest1.us. 120 IN A 0.0.0.0
5.minecraftchest1.us. 120 IN A 0.0.0.0
6.minecraftchest1.us. 3600 IN A 0.0.0.0
@.minecraftchest1.us. 60 IN A 0.0.0.0
"""
f = open(zone_file, "rt")
assert expected == f.read()
##############################################
def test_AAAA(tmp_path):
zoneAAAA = dnscode.Zone(origin='minecraftchest1.us')
# Test named and positional arguments. Ensure defaults work.
zoneAAAA.new_AAAA("0")
zoneAAAA.new_AAAA("1", 60)
zoneAAAA.new_AAAA("2", 60, "fe80::42:2cff:fe29:8db1")
zoneAAAA.new_AAAA(host="fe79::42:2cff:fe29:8db1")
zoneAAAA.new_AAAA(name="3")
zoneAAAA.new_AAAA(ttl=119)
zoneAAAA.new_AAAA(name=4, host="fe80::42:2cff:fe29:8db1", ttl=120)
# Test improper arguments
zoneAAAA.new_AAAA(name=5)
zoneAAAA.new_AAAA(ttl="59")
with pytest.raises(dnscode.InvalidDataException):
zoneAAAA.new_AAAA(host="1.0.0.0")
with pytest.raises(ValueError):
zoneAAAA.new_AAAA(host="fe79::42:2cff:fe29:8db1/64")
with pytest.raises(ValueError):
zoneAAAA.new_AAAA(host="-1.0.0.0/32")
zone_file = tmp_path / "test-AAAA.zone"
zoneAAAA.save_file(zone_file)
expected = """0.minecraftchest1.us. 3600 IN A 0.0.0.0
1.minecraftchest1.us. 60 IN A 0.0.0.0
2.minecraftchest1.us. 60 IN A 0.0.0.0
@.minecraftchest0.us. 3600 IN A 0.0.0.0
3.minecraftchest1.us. 3600 IN A 0.0.0.0
@.minecraftchest0.us. 120 IN A 0.0.0.0
4.minecraftchest1.us. 120 IN A 0.0.0.0
5.minecraftchest1.us. 3600 IN A 0.0.0.0
@.minecraftchest0.us. 60 IN A 0.0.0.0
"""
f = open(zone_file, "rt")
assert expected == f.read()