mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Refactored and solved bugs
This commit is contained in:
52
tests/unit/README.md
Normal file
52
tests/unit/README.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# Unit Tests
|
||||
|
||||
This directory contains unit tests for various custom components in the project, such as the custom lookup plugin `docker_cards` used in the `docker-portfolio` role.
|
||||
|
||||
## Overview
|
||||
|
||||
The unit tests are written using Python’s built-in `unittest` framework. They are designed to verify that your custom logic works as expected—such as extracting metadata from role files—without needing to run the entire playbook.
|
||||
|
||||
## Running the Tests
|
||||
|
||||
You can run the tests using one of the following methods:
|
||||
|
||||
1. **Using Unittest Discovery:**
|
||||
|
||||
From the project's root directory, run:
|
||||
|
||||
```bash
|
||||
python -m unittest discover -s tests/unit
|
||||
```
|
||||
|
||||
This command will discover and execute all test files within the `tests/unit` directory.
|
||||
|
||||
2. **Running a Specific Test File:**
|
||||
|
||||
If you want to run only the Docker cards test, execute:
|
||||
|
||||
```bash
|
||||
python tests/unit/test_docker_cards.py
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
- **Setup:**
|
||||
The test script creates a temporary directory to simulate your roles folder. It then creates a sample role (`docker-portfolio`) with a `README.md` file (containing a header for the title) and a `meta/main.yml` file (with the required metadata).
|
||||
|
||||
- **Execution:**
|
||||
Dummy variable values for `domains` and `applications` are provided (these are the variables the lookup plugin expects). The lookup plugin is then run, which processes the sample role and returns the card information.
|
||||
|
||||
- **Verification:**
|
||||
The test uses assertions to ensure that the output contains the expected title, description, icon information, constructed URL, and the correct iframe flag.
|
||||
|
||||
- **Cleanup:**
|
||||
After the test completes, the temporary directory is removed, ensuring that no test artifacts remain.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.6 or newer is recommended.
|
||||
- All necessary dependencies for your project should be installed.
|
||||
|
||||
These tests help ensure that your custom code is reliable and behaves as expected, and they can be easily integrated into a Continuous Integration (CI) pipeline.
|
||||
|
||||
Happy testing!
|
88
tests/unit/test_docker_cards.py
Normal file
88
tests/unit/test_docker_cards.py
Normal file
@@ -0,0 +1,88 @@
|
||||
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": {"landingpage_iframe_enabled": 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": {"landingpage_iframe_enabled": 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()
|
Reference in New Issue
Block a user