| 1 | --- |
| 2 | name: run-tests |
| 3 | description: Runs and writes tests (sanity, unit, integration) for the community.clickhouse Ansible collection using ansible-test. Use when asked to run, check, or write tests for a module or utility. Do not use for PR reviews or questions unrelated to testing. |
| 4 | --- |
| 5 | |
| 6 | # Skill: run-tests |
| 7 | |
| 8 | ## Purpose |
| 9 | |
| 10 | Run and write tests for the `community.clickhouse` Ansible collection. Covers sanity, unit, and integration tests using `ansible-test`. |
| 11 | |
| 12 | ## When to Invoke |
| 13 | |
| 14 | TRIGGER when: |
| 15 | - A user asks to run tests, check tests, or verify changes with tests |
| 16 | - A user asks how to test a module or utility |
| 17 | - A user asks to write tests for new or modified code |
| 18 | |
| 19 | DO NOT TRIGGER when: |
| 20 | - Reviewing a PR for overall quality (use `.agents/skills/pr-review/SKILL.md` instead) |
| 21 | - The question is about module logic unrelated to testing |
| 22 | |
| 23 | ## Test Infrastructure |
| 24 | |
| 25 | All tests run inside Docker/Podman via `ansible-test --docker`. No local package installation is needed. The collection must be installed at `ansible_collections/community/clickhouse/` (relative to a directory on `ANSIBLE_COLLECTIONS_PATHS`) for imports to resolve correctly. |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Test Commands |
| 30 | |
| 31 | ### Sanity |
| 32 | |
| 33 | Checks style, documentation, and imports for a changed file: |
| 34 | |
| 35 | ```bash |
| 36 | ansible-test sanity plugins/modules/clickhouse_db.py --docker -vvv |
| 37 | ``` |
| 38 | |
| 39 | ### Unit |
| 40 | |
| 41 | Runs unit tests for changed files: |
| 42 | |
| 43 | ```bash |
| 44 | ansible-test units tests/unit/plugins/modules/test_clickhouse_client.py --docker -vvv |
| 45 | ansible-test units tests/unit/plugins/module_utils/test_clickhouse_grants.py --docker -vvv |
| 46 | ``` |
| 47 | |
| 48 | Unit tests live under `tests/unit/plugins/` and use the **PyTest** framework. Every new function or class method MUST have a corresponding unit test. |
| 49 | |
| 50 | ### Integration |
| 51 | |
| 52 | Runs integration tests against a live ClickHouse instance (started by Docker): |
| 53 | |
| 54 | ```bash |
| 55 | ansible-test integration clickhouse_db --docker default -vvv |
| 56 | ansible-test integration clickhouse_user --docker default -vvv |
| 57 | ``` |
| 58 | |
| 59 | Integration tests live under `tests/integration/targets/<module_name>/`. Each target declares `setup_clickhouse` as a dependency in `tests/integration/targets/<name>/meta/main.yml` — this target installs the ClickHouse server. |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## When Tests Are Required |
| 64 | |
| 65 | | Change type | Sanity | Unit | Integration | |
| 66 | |---|---|---|---| |
| 67 | | New module | yes | yes | yes | |
| 68 | | New parameter | yes | if logic changed | yes | |
| 69 | | Bug fix | yes | yes | yes | |
| 70 | | Refactoring | yes | yes | no | |
| 71 | | Documentation only | yes | no | no | |
| 72 | |
| 73 | --- |
| 74 | |
| 75 | ## Integration Test Pattern |
| 76 | |
| 77 | Every integration test target must follow this sequence: |
| 78 | |
| 79 | 1. Call the module under test → `register: result` |
| 80 | 2. Assert on `result` using `ansible.builtin.assert` |
| 81 | 3. Verify the resulting database state by querying via `community.clickhouse.clickhouse_client` → `register: result` → `ansible.builtin.assert` |
| 82 | 4. This must be done in `check_mode: true` as well |
| 83 | |
| 84 | ```yaml |
| 85 | - name: Create database in check mode |
| 86 | check_mode: true |
| 87 | community.clickhouse.clickhouse_db: |
| 88 | name: testdb |
| 89 | state: present |
| 90 | register: result |
| 91 | |
| 92 | - name: Assert changed |
| 93 | ansible.builtin.assert: |
| 94 | that: |
| 95 | - result is changed |
| 96 | |
| 97 | - name: Verify DB does not exist in ClickHouse |
| 98 | community.clickhouse.clickhouse_client: |
| 99 | execute: "SELECT name FROM system.databases WHERE name = 'testdb'" |
| 100 | register: result |
| 101 | |
| 102 | - name: Assert DB is not present |
| 103 | ansible.builtin.assert: |
| 104 | that: |
| 105 | - result.result | length == 0 |
| 106 | |
| 107 | - name: Create database in real mode |
| 108 | community.clickhouse.clickhouse_db: |
| 109 | name: testdb |
| 110 | state: present |
| 111 | register: result |
| 112 | |
| 113 | - name: Assert changed |
| 114 | ansible.builtin.assert: |
| 115 | that: |
| 116 | - result is changed |
| 117 | |
| 118 | - name: Verify DB exists in ClickHouse |
| 119 | community.clickhouse.clickhouse_client: |
| 120 | execute: "SELECT name FROM system.databases WHERE name = 'testdb'" |
| 121 | register: result |
| 122 | |
| 123 | - name: Assert DB is present |
| 124 | ansible.builtin.assert: |
| 125 | that: |
| 126 | - result.result | length == 1 |
| 127 | ``` |
| 128 | |
| 129 | Tests must also cover: |
| 130 | - **Idempotency**: run the same task a second time and assert `result is not changed`. |
| 131 | - **`state: absent`**: where applicable, remove the resource and assert it is gone. |