summaryrefslogtreecommitdiff
path: root/tests/README.md
blob: a16a5b07f44da3e3944dfeb5867ba14297359ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# vyos.rest Test Framework

Equivalent structure to `vyos.vyos` CLI collection tests, adapted for
the REST API connection model.

## Structure

```
tests/
├── unit/
│   ├── modules/
│   │   ├── base.py                      # VyOSModuleTestCase base class + load_fixture
│   │   ├── test_vyos_ntp_global.py
│   │   ├── test_vyos_logging_global.py
│   │   ├── test_vyos_prefix_lists.py
│   │   └── test_vyos_route_maps.py
│   └── fixtures/
│       ├── ntp_global_running.json      # Confirmed API responses from device
│       ├── logging_global_running.json
│       ├── prefix_lists_running.json
│       ├── route_maps_running.json
│       ├── snmp_server_running.json
│       └── lldp_global_running.json
└── integration/
    ├── network-integration.cfg
    ├── inventory.network                # Edit before running
    └── targets/
        ├── vyos_ntp_global/tasks/main.yaml
        ├── vyos_logging_global/tasks/main.yaml
        ├── vyos_prefix_lists/tasks/main.yaml
        ├── vyos_route_maps/tasks/main.yaml
        ├── vyos_lldp_global/tasks/main.yaml
        ├── vyos_snmp_server/tasks/main.yaml
        ├── vyos_hostname/tasks/main.yaml
        └── vyos_banner/tasks/main.yaml
```

## Unit Tests

Unit tests cover:
- `normalize_config` / `normalize_running` — argspec and API parsing
- `build_commands` diff logic — all states (merged/replaced/overridden/deleted)
- Fixture-based parsing — confirmed API shapes from the actual device

No device connection needed. Each test imports the module's functions
directly and calls them with mock data or fixture JSON.

### Run all unit tests

```bash
# From collection root
ansible-test units tests/unit/modules/ --python 3.12

# Or with pytest directly (no ansible-test required)
cd /path/to/ansible_collections/vyos/rest
python -m pytest tests/unit/modules/ -v
```

### Run a single module's tests

```bash
ansible-test units tests/unit/modules/test_vyos_ntp_global.py --python 3.12
python -m pytest tests/unit/modules/test_vyos_ntp_global.py -v
```

## Integration Tests

Integration tests run against a live VyOS device. Each target:
1. Tears down any existing config for that resource
2. Tests merged (including idempotency)
3. Tests gathered
4. Tests replaced (including idempotency)
5. Tests deleted (including idempotency)
6. Tears down after

### Prerequisites

Edit `tests/integration/inventory.network` with your device details.

### Run all integration tests

```bash
ansible-test network-integration \
  --inventory tests/integration/inventory.network \
  vyos_*
```

### Run a single module's integration test

```bash
ansible-test network-integration \
  --inventory tests/integration/inventory.network \
  vyos_ntp_global

# Or with ansible-playbook directly
ansible-playbook \
  -i tests/integration/inventory.network \
  tests/integration/targets/vyos_ntp_global/tasks/main.yaml
```

## Adding Tests for a New Module

1. Add a fixture: `tests/unit/fixtures/<module>_running.json`
   - Copy the exact API response from a `curl showConfig` call
   - This ensures tests reflect real device behaviour

2. Add unit tests: `tests/unit/modules/test_<module>.py`
   - Subclass `VyOSModuleTestCase` for fixture-based tests
   - Test `normalize_*`, `get_running_config`, `build_commands` directly

3. Add integration target: `tests/integration/targets/<module>/tasks/main.yaml`
   - Follow the teardown → merged → idempotent → gathered → replaced → deleted pattern

## Key Differences from CLI Collection Tests

| CLI collection | REST collection |
|---|---|
| Mocks `connection.get` returning CLI text | Mocks `vyos.get_config()` returning JSON dict |
| Fixture files are `.cfg` text (CLI output) | Fixture files are `.json` (API response) |
| Tests parse CLI text via `rm_templates` | Tests parse API dicts via `normalize_running` |
| `ansible-test units --docker` for isolation | Can run with `pytest` directly, no docker needed |