Files
computer-playbook/tests/unit
Kevin Veen-Birkenbach 7791bd8c04 Implement filter checks: ensure all defined filters are used and remove dead code
Integration tests added/updated:
- tests/integration/test_filters_usage.py: AST-based detection of filter definitions (FilterModule.filters), robust Jinja detection ({{ ... }}, {% ... %}, {% filter ... %}), plus Python call tracking; fails if a filter is used only under tests/.
- tests/integration/test_filters_are_defined.py: inverse check — every filter used in .yml/.yaml/.j2/.jinja2/.tmpl must be defined locally. Scans only inside Jinja blocks and ignores pipes inside strings (e.g., lookup('pipe', "... | grep ... | awk ...")) to avoid false positives like trusted_hosts, woff/woff2, etc.

Bug fixes & robustness:
- Build regexes without %-string formatting to avoid ValueError from literal '%' in Jinja tags.
- Strip quoted strings in usage analysis so sed/grep/awk pipes are not miscounted as filters.
- Prevent self-matches in the defining file.

Cleanup / removal of dead code:
- Removed unused filter plugins and related unit tests:
  * filter_plugins/alias_domains_map.py
  * filter_plugins/get_application_id.py
  * filter_plugins/load_configuration.py
  * filter_plugins/safe.py
  * filter_plugins/safe_join.py
  * roles/svc-db-openldap/filter_plugins/build_ldap_nested_group_entries.py
  * roles/sys-ctl-bkp-docker-2-loc/filter_plugins/dict_to_cli_args.py
  * corresponding tests under tests/unit/*
- roles/svc-db-postgres/filter_plugins/split_postgres_connections.py: dropped no-longer-needed list_postgres_roles API; adjusted tests.

Misc:
- sys-stk-front-proxy/defaults/main.yml: clarified valid vhost_flavour values (comma-separated).

Ref: https://chatgpt.com/share/68b56bac-c4f8-800f-aeef-6708dbb44199
2025-09-01 11:47:51 +02:00
..
2025-08-07 11:31:06 +02:00
2025-07-16 08:25:16 +02:00

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 web-app-desktop role.

Overview

The unit tests are written using Pythons 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:

    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:

    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 (web-app-desktop) 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!