Files
dns-sync/tests/test_config.py
T

119 lines
3.4 KiB
Python

import os
import unittest
from unittest.mock import patch
from dns_sync.config import AppConfig, parse_endpoint_list
class EndpointParsingTests(unittest.TestCase):
def test_comma_separated_addresses_use_default_port(self):
endpoints = parse_endpoint_list(
"npm",
"192.168.1.3, 192.168.1.4:8081,192.168.1.5",
81,
)
self.assertEqual(
tuple(endpoint.identity for endpoint in endpoints),
(
"npm://192.168.1.3:81",
"npm://192.168.1.4:8081",
"npm://192.168.1.5:81",
),
)
def test_duplicate_is_rejected_after_normalization(self):
with self.assertRaisesRegex(RuntimeError, "Duplicate NPM"):
parse_endpoint_list(
"npm",
"192.168.1.3,192.168.1.3:81",
81,
)
def test_empty_entry_is_rejected(self):
with self.assertRaisesRegex(RuntimeError, "empty address"):
parse_endpoint_list(
"npm",
"192.168.1.3,,192.168.1.5",
81,
)
def test_url_scheme_is_rejected(self):
with self.assertRaisesRegex(RuntimeError, "must not include"):
parse_endpoint_list(
"npm",
"http://192.168.1.3:81",
81,
)
class AppConfigEndpointTests(unittest.TestCase):
def test_legacy_proxy_ip_remains_supported(self):
with patch.dict(
os.environ,
{"PROXY_IP": "192.168.1.101"},
clear=True,
):
config = AppConfig.from_env()
self.assertEqual(
tuple(endpoint.identity for endpoint in config.npm_endpoints),
("npm://192.168.1.101:81",),
)
self.assertEqual(config.target_ip, "192.168.1.101")
def test_endpoint_list_controls_api_and_proxy_ip_remains_target(self):
with patch.dict(
os.environ,
{
"NPM_ENDPOINTS": (
"192.168.1.3,192.168.1.4:8081,192.168.1.5"
),
"PROXY_IP": "192.168.1.101",
},
clear=True,
):
config = AppConfig.from_env()
self.assertEqual(
tuple(endpoint.identity for endpoint in config.npm_endpoints),
(
"npm://192.168.1.3:81",
"npm://192.168.1.4:8081",
"npm://192.168.1.5:81",
),
)
self.assertEqual(config.target_ip, "192.168.1.101")
def test_explicit_dns_target_supports_multiple_endpoints(self):
with patch.dict(
os.environ,
{
"NPM_ENDPOINTS": "192.168.1.3,192.168.1.4",
"DNS_TARGET_IP": "192.168.1.101",
},
clear=True,
):
config = AppConfig.from_env()
self.assertEqual(config.target_ip, "192.168.1.101")
def test_multiple_endpoints_require_target_without_proxy_ip(self):
with patch.dict(
os.environ,
{
"NPM_ENDPOINTS": "192.168.1.3,192.168.1.4",
},
clear=True,
):
with self.assertRaisesRegex(
RuntimeError,
"DNS_TARGET_IP is required",
):
AppConfig.from_env()
if __name__ == "__main__":
unittest.main()