Refactored domain logic

This commit is contained in:
2025-05-19 18:38:41 +02:00
parent 37dcc5f74e
commit 1b50f73803
10 changed files with 340 additions and 234 deletions

View File

@@ -9,7 +9,7 @@ dir_path = os.path.abspath(
sys.path.insert(0, dir_path)
from ansible.errors import AnsibleFilterError
from domain_filters import FilterModule
from alias_domains_map import FilterModule
class TestDomainFilters(unittest.TestCase):
def setUp(self):
@@ -17,45 +17,6 @@ class TestDomainFilters(unittest.TestCase):
# Sample primary domain
self.primary = 'example.com'
def test_canonical_empty_apps(self):
apps = {}
expected = {}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_canonical_without_domains(self):
apps = {'app1': {}}
expected = {'app1': ['app1.example.com']}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_canonical_with_list(self):
apps = {
'app1': {
'domains': {'canonical': ['foo.com', 'bar.com']}
}
}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertCountEqual(result['app1'], ['foo.com', 'bar.com'])
def test_canonical_with_dict(self):
apps = {
'app1': {
'domains': {'canonical': {'one': 'one.com', 'two': 'two.com'}}
}
}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertCountEqual(result['app1'], ['one.com', 'two.com'])
def test_canonical_duplicate_raises(self):
apps = {
'app1': {'domains': {'canonical': ['dup.com']}},
'app2': {'domains': {'canonical': ['dup.com']}},
}
with self.assertRaises(AnsibleFilterError) as cm:
self.filter_module.canonical_domains_map(apps, self.primary)
self.assertIn("configured for both", str(cm.exception))
def test_alias_empty_apps(self):
apps = {}
expected = {}
@@ -105,13 +66,6 @@ class TestDomainFilters(unittest.TestCase):
result = self.filter_module.alias_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_invalid_canonical_type(self):
apps = {
'app1': {'domains': {'canonical': 123}}
}
with self.assertRaises(AnsibleFilterError):
self.filter_module.canonical_domains_map(apps, self.primary)
def test_invalid_aliases_type(self):
apps = {
'app1': {'domains': {'aliases': 123}}
@@ -119,5 +73,31 @@ class TestDomainFilters(unittest.TestCase):
with self.assertRaises(AnsibleFilterError):
self.filter_module.alias_domains_map(apps, self.primary)
def test_alias_with_empty_domains_cfg(self):
apps = {
'app1': {
'domains': {}
}
}
expected = apps
result = self.filter_module.alias_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_alias_with_canonical_dict_not_default(self):
apps = {
'app1': {
'domains': {
'canonical': {
'one': 'one.com',
'two': 'two.com'
}
}
}
}
expected = {'app1': ['app1.example.com']}
result = self.filter_module.alias_domains_map(apps, self.primary)
self.assertEqual(result, expected)
if __name__ == "__main__":
unittest.main()
unittest.main()

View File

@@ -8,7 +8,7 @@ sys.path.insert(
os.path.abspath(os.path.join(os.path.dirname(__file__), '../../filter_plugins'))
)
from domain_filters import FilterModule
from generate_all_domains import FilterModule
class TestGenerateAllDomains(unittest.TestCase):
def setUp(self):

View File

@@ -8,7 +8,7 @@ sys.path.insert(
os.path.abspath(os.path.join(os.path.dirname(__file__), '../../filter_plugins'))
)
from domain_filters import FilterModule
from generate_base_sld_domains import FilterModule
class TestGenerateBaseSldDomains(unittest.TestCase):
def setUp(self):

View File

@@ -0,0 +1,74 @@
import os
import sys
import unittest
# Add the filter_plugins directory to the import path
dir_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../filter_plugins')
)
sys.path.insert(0, dir_path)
from ansible.errors import AnsibleFilterError
from canonical_domains_map import FilterModule
class TestDomainFilters(unittest.TestCase):
def setUp(self):
self.filter_module = FilterModule()
# Sample primary domain
self.primary = 'example.com'
def test_canonical_empty_apps(self):
apps = {}
expected = {}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_canonical_without_domains(self):
apps = {'app1': {}}
expected = {'app1': ['app1.example.com']}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertEqual(result, expected)
def test_canonical_with_list(self):
apps = {
'app1': {
'domains': {'canonical': ['foo.com', 'bar.com']}
}
}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertCountEqual(
result['app1'],
['foo.com', 'bar.com']
)
def test_canonical_with_dict(self):
apps = {
'app1': {
'domains': {'canonical': {'one': 'one.com', 'two': 'two.com'}}
}
}
result = self.filter_module.canonical_domains_map(apps, self.primary)
self.assertEqual(
result['app1'],
{'one': 'one.com', 'two': 'two.com'}
)
def test_canonical_duplicate_raises(self):
apps = {
'app1': {'domains': {'canonical': ['dup.com']}},
'app2': {'domains': {'canonical': ['dup.com']}},
}
with self.assertRaises(AnsibleFilterError) as cm:
self.filter_module.canonical_domains_map(apps, self.primary)
# Updated to match new exception message
self.assertIn("already configured for", str(cm.exception))
def test_invalid_canonical_type(self):
apps = {
'app1': {'domains': {'canonical': 123}}
}
with self.assertRaises(AnsibleFilterError):
self.filter_module.canonical_domains_map(apps, self.primary)
if __name__ == "__main__":
unittest.main()