Skip to content

Commit 37536bb

Browse files
committed
adding integration testing
1 parent 0ae211e commit 37536bb

26 files changed

+268
-7
lines changed

.pylintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ min-public-methods=2
307307
[EXCEPTIONS]
308308

309309
# Exceptions that will emit a warning when caught.
310-
overgeneral-exceptions=BaseException,
311-
Exception
310+
overgeneral-exceptions=builtins.BaseException',
311+
builtins.Exception'
312312

313313

314314
[FORMAT]

CONTRIBUTING.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Contributing
2+
3+
* report errors as [issues](https://github.com/ansibleguy/collection_nftables/issues)
4+
* test unstable modules and [report if they work as expected](https://github.com/ansibleguy/collection_nftables/discussions/new?category=general)
5+
* add [ansible-based tests](https://github.com/ansibleguy/collection_nftables/blob/latest/tests) for some error-case(s) you have encountered
6+
* extend or correct the [documentation](https://github.com/ansibleguy/collection_nftables/blob/latest/docs)
7+
* add missing inline documentation [as standardized](https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_documenting.html#documentation-block)
8+
* should be placed in `<COLLECTION>/plugins/module_utils/inline_docs/<MODULE>.py` and then imported in the module
9+
* contribute code fixes or optimizations
10+
* implement additional modules
11+
12+
## Module changes
13+
14+
Whenever you change a module's code - you should run lint (`bash scripts/lint.sh`) and [its tests](https://github.com/ansibleguy/collection_nftables/blob/latest/tests/README.md)!
15+
16+
TLDR:
17+
* Set up a VM or Container
18+
* Run the Module: `bash scripts/test.sh -e test_module=<MODULE>`

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ See: [Docs](https://nftables.ansibleguy.net)
1717

1818
----
1919

20+
## Contribute
21+
22+
Feel free to contribute to this project using [pull-requests](https://github.com/ansibleguy/collection_nftables/pulls), [issues](https://github.com/ansibleguy/collection_nftables/issues) and [discussions](https://github.com/ansibleguy/collection_nftables/discussions)!
23+
24+
See also: [Contributing](https://github.com/ansibleguy/collection_nftables/blob/latest/CONTRIBUTING.md)
25+
26+
----
27+
2028
## Modules
2129

2230
not implemented => development => [testing](https://github.com/ansibleguy/collection_nftables/blob/latest/tests) => unstable (_practical testing_) => stable

plugins/module_utils/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def check_dependencies() -> None:
3535

3636
rc, _, _ = Nftables().cmd('list ruleset')
3737
if rc == -1:
38-
raise SystemExit(
38+
raise PermissionError(
3939
'You need to run this module as root '
4040
'so it can interact with NFTables!'
4141
)

plugins/module_utils/check_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
('nftables v1.0.6 (Lester Gooch #5)', True), # extended version
99
])
1010
def test_version_check(raw_version: str, result: bool):
11+
# pylint: disable=C0415
1112
from ansible_collections.ansibleguy.nftables.plugins.module_utils.check import _validate_version
1213
assert _validate_version(raw_version) is result

plugins/module_utils/helper/subps_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
('some-invalid-command', 1, None), # soft cmd failure
77
])
88
def test_process(cmd: str, rc: int, stdout: str):
9+
# pylint: disable=C0415
910
from ansible_collections.ansibleguy.nftables.plugins.module_utils.helper.subps import process
1011
result = process(cmd)
1112

1213
assert result['rc'] == rc
1314

1415
if stdout is not None:
1516
assert result['stdout'].strip() == stdout
16-

plugins/module_utils/nft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from ansible_collections.ansibleguy.nftables.plugins.module_utils.check import \
1616
check_dependencies
1717

18-
check_dependencies()
1918

2019
# pylint: disable=C0413
2120
from nftables import Nftables
@@ -29,6 +28,7 @@ class NFT:
2928
CHECK_MODE_CMDS = ['list ruleset']
3029

3130
def __init__(self, module: AnsibleModule, result: dict):
31+
check_dependencies()
3232
self.m = module
3333
self.r = result
3434
self.n = Nftables()

scripts/test.sh

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
#!/usr/bin/env bash
22

3-
set -e
3+
set -eo pipefail
4+
5+
echo ''
6+
echo '##### PREPARING #####'
47

58
cd "$(dirname "$0")/.."
6-
python3 -m pytest
9+
COL_DIR="$(pwd)"
10+
TMP_DIR="/tmp/.nftables_test_$(date +%s)"
11+
TMP_COL_DIR="${TMP_DIR}/collections"
12+
13+
mkdir -p "${TMP_COL_DIR}/ansible_collections/ansibleguy/"
14+
ln -s "$COL_DIR" "${TMP_COL_DIR}/ansible_collections/ansibleguy/nftables"
15+
16+
export ANSIBLE_COLLECTIONS_PATH="$TMP_COL_DIR"
17+
export ANSIBLE_INVENTORY_UNPARSED_WARNING=False
18+
export ANSIBLE_LOCALHOST_WARNING=False
19+
cd "${COL_DIR}/tests/"
20+
21+
echo ''
22+
echo '##### STARTING #####'
23+
24+
ansible-playbook -k -K -i inventory/hosts.yml test.yml "$@"
25+
26+
rm -rf "$TMP_DIR"
27+
28+
echo ''
29+
echo '##### FINISHED #####'
30+
echo ''

scripts/unittest.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
cd "$(dirname "$0")/.."
6+
python3 -m pytest

tests/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# ansibleguy.nftables - Testing
2+
3+
As NFTables behaves differently in containers, we are testing on a Linux VM and a Linux Container (_LXC > Docker_).
4+
5+
Both must be reachable by SSH as **we are using Ansible directly for integration-testing**.
6+
7+
----
8+
9+
## Setup
10+
11+
You can also only set-up one of these test-systems.
12+
13+
But you will have to run Ansible by using the `--limit container` or `--limit vm` argument.
14+
15+
### Virtual Machine
16+
17+
We are using a [Debian 12 minimal]() installation.
18+
19+
For a quick-start you could use [this VirtualBox image](https://sourceforge.net/projects/linuxvmimages/) provided by [linuxvmimages.com](https://www.linuxvmimages.com/images/debian-12/).
20+
21+
### Container
22+
23+
We are using a Debian 12 container.
24+
25+
I would recommend using [a LXC](https://wiki.debian.org/LXC) if you have the needed system for it.
26+
27+
* [Proxmox LXC](https://pve.proxmox.com/wiki/Linux_Container#pct_container_images):
28+
29+
```bash
30+
pveam update
31+
pveam download local debian-12-standard_12.2-1_amd64.tar.zst # exact version number could vary
32+
```
33+
34+
* [Raw LXC](https://wiki.debian.org/LXC#Container_Creation)
35+
36+
* Docker: `docker pull debian:12`
37+
38+
### Config
39+
40+
Add your test-system's IPs and users to the `inventory/host_vars/*.yml` files.
41+
42+
A NFTables base-config might be added later on.
43+
44+
----
45+
46+
## Add/Modify
47+
48+
When modifying tests you should run the lint-script: `bash scripts/lint.sh`
49+
50+
Tests are placed under: `tests/tasks/` and should be named as the module they are testing.
51+
52+
Example: `tests/tasks/list.yml` is testing `ansibleguy.nftables.list`
53+
54+
Tests should always clean up after itself so the test-system is back to the state it was in before! Add those cleanup-tasks in `tests/tasks/<MODULE>_cleanup.yml`
55+
56+
As the connection over SSH is needed for Ansible to work - tests should never deny/drop this connection.
57+
58+
----
59+
60+
## Run
61+
62+
You can run the tests simply by running the script: `bash scripts/test.sh`
63+
64+
Parameters you add to the test-script execution will be passed to `ansible-playbook`
65+
66+
Examples:
67+
68+
* Enable difference-mode: `bash scripts/test.sh -D`
69+
* Limit the execution: `bash scripts/test.sh --limit container`
70+
* Only test one module: `bash scripts/test.sh -e test_module=list`

tests/cleanup.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
3+
- name: Testing NFTables Modules - Cleanup
4+
hosts: testing
5+
become: true
6+
gather_facts: true
7+
vars:
8+
test_module: 'all'
9+
10+
tasks:
11+
- name: Cleanup ansibleguy.nftables.list
12+
ansible.builtin.import_tasks: tasks/list_cleanup.yml
13+
when: "test_module in ['all', 'list']"
14+
15+
- name: Cleanup ansibleguy.nftables.table
16+
ansible.builtin.import_tasks: tasks/table_cleanup.yml
17+
when: "test_module in ['all', 'table']"
18+
19+
- name: Cleanup ansibleguy.nftables.chain
20+
ansible.builtin.import_tasks: tasks/chain_cleanup.yml
21+
when: "test_module in ['all', 'chain']"
22+
23+
- name: Cleanup ansibleguy.nftables.rule_raw
24+
ansible.builtin.import_tasks: tasks/rule_raw_cleanup.yml
25+
when: "test_module in ['all', 'rule_raw']"
26+
27+
- name: Cleanup ansibleguy.nftables.rule
28+
ansible.builtin.import_tasks: tasks/rule_cleanup.yml
29+
when: "test_module in ['all', 'rule']"

tests/inventory/group_vars/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
ansible_host: '0.0.0.0' # ADD YOUR IP
4+
ansible_user: 'dummy' # ADD YOUR USER
5+
ansible_port: 22

tests/inventory/host_vars/vm.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
3+
ansible_host: '0.0.0.0' # ADD YOUR IP
4+
ansible_user: 'dummy' # ADD YOUR USER
5+
ansible_port: 22

tests/inventory/hosts.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
3+
all:
4+
hosts:
5+
container:
6+
vm:
7+
8+
children:
9+
testing:
10+
hosts:
11+
container:
12+
vm:

tests/tasks/chain.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/chain_cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/list.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
3+
- name: List | Pulling existing Tables
4+
ansibleguy.nftables.list:
5+
target: 'tables'
6+
register: list_tables1
7+
8+
# - ansible.builtin.debug:
9+
# var: list_tables1.data
10+
11+
- name: List | Checking existing Tables
12+
ansible.builtin.assert:
13+
that:
14+
- "'data' in list_tables1"
15+
- list_tables1.data | length == 1
16+
17+
- name: List | Pulling existing Chains
18+
ansibleguy.nftables.list:
19+
target: 'chains'
20+
register: list_chains1
21+
22+
# - ansible.builtin.debug:
23+
# var: list_chains1.data
24+
25+
- name: List | Checking existing Chains
26+
ansible.builtin.assert:
27+
that:
28+
- "'data' in list_chains1"
29+
30+
- name: List | Pulling existing Rules
31+
ansibleguy.nftables.list:
32+
target: 'rules'
33+
register: list_rules1
34+
35+
# - ansible.builtin.debug:
36+
# var: list_rules1.data
37+
38+
- name: List | Checking existing Rules
39+
ansible.builtin.assert:
40+
that:
41+
- "'data' in list_rules1"

tests/tasks/list_cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/rule.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/rule_cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/rule_raw.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/rule_raw_cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/table.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/tasks/table_cleanup.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/test.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
3+
- name: Testing NFTables Modules
4+
hosts: testing
5+
become: true
6+
gather_facts: true
7+
vars:
8+
test_module: 'all'
9+
10+
tasks:
11+
- name: Testing ansibleguy.nftables.list
12+
ansible.builtin.import_tasks: tasks/list.yml
13+
when: "test_module in ['all', 'list']"
14+
15+
- name: Testing ansibleguy.nftables.table
16+
ansible.builtin.import_tasks: tasks/table.yml
17+
when: "test_module in ['all', 'table']"
18+
19+
- name: Testing ansibleguy.nftables.chain
20+
ansible.builtin.import_tasks: tasks/chain.yml
21+
when: "test_module in ['all', 'chain']"
22+
23+
- name: Testing ansibleguy.nftables.rule_raw
24+
ansible.builtin.import_tasks: tasks/rule_raw.yml
25+
when: "test_module in ['all', 'rule_raw']"
26+
27+
- name: Testing ansibleguy.nftables.rule
28+
ansible.builtin.import_tasks: tasks/rule.yml
29+
when: "test_module in ['all', 'rule']"
30+
31+
- name: Testing NFTables Modules - Cleanup
32+
ansible.builtin.import_playbook: cleanup.yml

0 commit comments

Comments
 (0)