Reorgenized test structure and added validation of inventory before deployment

This commit is contained in:
2025-07-04 01:14:00 +02:00
parent fe04f1955f
commit a9f55579a2
37 changed files with 241 additions and 42 deletions

View File

View File

@@ -0,0 +1,62 @@
import os
import sys
import tempfile
import shutil
import unittest
import yaml
dir_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), '../../../lookup_plugins')
)
sys.path.insert(0, dir_path)
from application_gid import LookupModule
class TestApplicationGidLookup(unittest.TestCase):
def setUp(self):
# Create a temporary roles directory
self.temp_dir = tempfile.mkdtemp()
self.roles_dir = os.path.join(self.temp_dir, "roles")
os.mkdir(self.roles_dir)
# Define mock application_ids
self.applications = {
"nextcloud": "docker-nextcloud",
"moodle": "docker-moodle",
"wordpress": "docker-wordpress",
"taiga": "docker-taiga"
}
# Create fake role dirs and vars/main.yml
for app_id, dirname in self.applications.items():
role_path = os.path.join(self.roles_dir, dirname, "vars")
os.makedirs(role_path)
with open(os.path.join(role_path, "main.yml"), "w") as f:
yaml.dump({"application_id": app_id}, f)
self.lookup = LookupModule()
def tearDown(self):
shutil.rmtree(self.temp_dir)
def test_gid_lookup(self):
# The sorted application_ids: [moodle, nextcloud, taiga, wordpress]
expected_order = ["moodle", "nextcloud", "taiga", "wordpress"]
for i, app_id in enumerate(expected_order):
result = self.lookup.run([app_id], roles_dir=self.roles_dir)
self.assertEqual(result, [10000 + i])
def test_custom_base_gid(self):
result = self.lookup.run(["taiga"], roles_dir=self.roles_dir, base_gid=20000)
self.assertEqual(result, [20002]) # 2nd index in sorted list
def test_application_id_not_found(self):
with self.assertRaises(Exception) as context:
self.lookup.run(["unknownapp"], roles_dir=self.roles_dir)
self.assertIn("Application ID 'unknownapp' not found", str(context.exception))
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,100 @@
import os
import sys
import tempfile
import shutil
import unittest
# Adjust the PYTHONPATH to include the lookup_plugins folder from the docker-portfolio role.
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../../../roles/docker-portfolio/lookup_plugins'))
from docker_cards import LookupModule
class TestDockerCardsLookup(unittest.TestCase):
def setUp(self):
# Create a temporary directory to simulate the roles directory.
self.test_roles_dir = tempfile.mkdtemp(prefix="test_roles_")
# Create a sample role "docker-portfolio".
self.role_name = "docker-portfolio"
self.role_dir = os.path.join(self.test_roles_dir, self.role_name)
os.makedirs(os.path.join(self.role_dir, "meta"))
# Create a sample README.md with a H1 line for the title.
readme_path = os.path.join(self.role_dir, "README.md")
with open(readme_path, "w", encoding="utf-8") as f:
f.write("# Portfolio Application\nThis is a sample portfolio role.")
# Create a sample meta/main.yml in the meta folder.
meta_main_path = os.path.join(self.role_dir, "meta", "main.yml")
meta_yaml = """
galaxy_info:
description: "A role for deploying a portfolio application."
logo:
class: fa-solid fa-briefcase
"""
with open(meta_main_path, "w", encoding="utf-8") as f:
f.write(meta_yaml)
def tearDown(self):
# Remove the temporary roles directory after the test.
shutil.rmtree(self.test_roles_dir)
def test_lookup_when_group_includes_application_id(self):
# Instantiate the LookupModule.
lookup_module = LookupModule()
# Define dummy variables including group_names that contain the application_id "portfolio".
fake_variables = {
"domains": {"portfolio": "myportfolio.com"},
"applications": {
"portfolio": {
"features": {
"portfolio_iframe": True
}
}
},
"group_names": ["portfolio"]
}
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
# The result is a list containing one list of card dictionaries.
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)
cards = result[0]
self.assertIsInstance(cards, list)
# Since "portfolio" is in group_names, one card should be present.
self.assertEqual(len(cards), 1)
card = cards[0]
self.assertEqual(card["title"], "Portfolio Application")
self.assertEqual(card["text"], "A role for deploying a portfolio application.")
self.assertEqual(card["icon"]["class"], "fa-solid fa-briefcase")
self.assertEqual(card["url"], "https://myportfolio.com")
self.assertTrue(card["iframe"])
def test_lookup_when_group_excludes_application_id(self):
# Instantiate the LookupModule.
lookup_module = LookupModule()
# Set fake variables with group_names that do NOT include the application_id "portfolio".
fake_variables = {
"domains": {"portfolio": "myportfolio.com"},
"applications": {
"portfolio": {
"features": {
"iframe": True
}
}
},
"group_names": [] # Not including "portfolio"
}
result = lookup_module.run([self.test_roles_dir], variables=fake_variables)
# Since the application_id is not in group_names, no card should be added.
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)
cards = result[0]
self.assertIsInstance(cards, list)
self.assertEqual(len(cards), 0)
if __name__ == "__main__":
unittest.main()