Added run_after integration test

This commit is contained in:
2025-07-11 10:15:58 +02:00
parent 23bbe0520c
commit 6780950257
7 changed files with 294 additions and 3 deletions

View File

@@ -0,0 +1,37 @@
#!/usr/bin/env python3
import os
import unittest
import yaml
from pathlib import Path
class TestRunAfterRoles(unittest.TestCase):
def setUp(self):
self.roles_dir = Path(__file__).resolve().parent.parent.parent / "roles"
self.valid_role_names = {p.name for p in self.roles_dir.iterdir() if p.is_dir()}
def test_run_after_roles_are_valid(self):
invalid_refs = []
for role in self.valid_role_names:
meta_path = self.roles_dir / role / "meta" / "main.yml"
if not meta_path.exists():
continue
try:
with open(meta_path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
except Exception as e:
self.fail(f"Failed to parse {meta_path}: {e}")
continue
run_after = data.get("galaxy_info", {}).get("run_after", [])
for ref in run_after:
if ref not in self.valid_role_names:
invalid_refs.append((role, ref))
if invalid_refs:
msg = "\n".join(f"{role}: invalid run_after → {ref}" for role, ref in invalid_refs)
self.fail(f"Found invalid run_after references:\n{msg}")
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,66 @@
import os
import shutil
import tempfile
import unittest
import yaml
from ansible.errors import AnsibleFilterError
from filter_plugins.get_role_folder import get_role_folder
class TestGetRoleFolder(unittest.TestCase):
def setUp(self):
# Create a temporary directory to simulate roles structure
self.tempdir = tempfile.mkdtemp()
self.roles_dir = os.path.join(self.tempdir, 'roles')
os.makedirs(self.roles_dir)
# Role1: matching application_id
role1_path = os.path.join(self.roles_dir, 'role1', 'vars')
os.makedirs(role1_path)
with open(os.path.join(role1_path, 'main.yml'), 'w') as f:
yaml.dump({'application_id': 'app-123'}, f)
# Role2: non-matching application_id
role2_path = os.path.join(self.roles_dir, 'role2', 'vars')
os.makedirs(role2_path)
with open(os.path.join(role2_path, 'main.yml'), 'w') as f:
yaml.dump({'application_id': 'app-456'}, f)
# Role3: missing vars directory
os.makedirs(os.path.join(self.roles_dir, 'role3'))
def tearDown(self):
# Clean up temporary directory
shutil.rmtree(self.tempdir)
def test_find_existing_role(self):
# Should find role1 for application_id 'app-123'
result = get_role_folder('app-123', roles_path=self.roles_dir)
self.assertEqual(result, 'role1')
def test_no_match_raises(self):
# No role has application_id 'nonexistent'
with self.assertRaises(AnsibleFilterError) as cm:
get_role_folder('nonexistent', roles_path=self.roles_dir)
self.assertIn("No role found with application_id 'nonexistent'", str(cm.exception))
def test_missing_roles_path(self):
# Path does not exist
invalid_path = os.path.join(self.tempdir, 'invalid')
with self.assertRaises(AnsibleFilterError) as cm:
get_role_folder('any', roles_path=invalid_path)
self.assertIn(f"Roles path not found: {invalid_path}", str(cm.exception))
def test_invalid_yaml_raises(self):
# Create a role with invalid YAML
bad_role_path = os.path.join(self.roles_dir, 'badrole', 'vars')
os.makedirs(bad_role_path)
with open(os.path.join(bad_role_path, 'main.yml'), 'w') as f:
f.write("::: invalid yaml :::")
with self.assertRaises(AnsibleFilterError) as cm:
get_role_folder('app-123', roles_path=self.roles_dir)
self.assertIn('Failed to load', str(cm.exception))
if __name__ == '__main__':
unittest.main()