| 1 |
> [!NOTE] |
| 2 |
> Remove comments that are used **only** for explanation purpose. Refer other test playbooks to verify. |
| 3 |
|
| 4 |
```yaml |
| 5 |
--- |
| 6 |
|
| 7 |
# Test code for the ACI modules |
| 8 |
# Copyright: (c) <year>, <Name> (@<github id>) |
| 9 |
# Replace <year>, <Name>, and <github id> with your information. |
| 10 |
|
| 11 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 12 |
|
| 13 |
- name: Test that we have an ACI APIC host, ACI username and ACI password |
| 14 |
fail: |
| 15 |
msg: 'Please define the following variables: aci_hostname, aci_username and aci_password.' |
| 16 |
when: aci_hostname is not defined or aci_username is not defined or aci_password is not defined |
| 17 |
|
| 18 |
# 1. Verify ACI APIC host, username, and password are defined in inventory |
| 19 |
# 2. Set ACI connection variables |
| 20 |
- name: Set vars |
| 21 |
set_fact: |
| 22 |
aci_info: &aci_info |
| 23 |
host: "{{ aci_hostname }}" |
| 24 |
username: "{{ aci_username }}" |
| 25 |
password: "{{ aci_password }}" |
| 26 |
validate_certs: '{{ aci_validate_certs | default(false) }}' |
| 27 |
use_ssl: '{{ aci_use_ssl | default(true) }}' |
| 28 |
use_proxy: '{{ aci_use_proxy | default(true) }}" |
| 29 |
output_level: "{{ aci_output_level | default('info') }}" |
| 30 |
|
| 31 |
|
| 32 |
# CLEAN TEST ENVIRONMENT BEFORE TESTS |
| 33 |
|
| 34 |
# 1. Initial cleanup |
| 35 |
# Ensures a clean state before tests begin by removing any existing objects. |
| 36 |
- name: Remove the ansible_tenant (initial cleanup) |
| 37 |
aci_tenant: |
| 38 |
<<: *aci_info |
| 39 |
tenant: ansible_tenant |
| 40 |
state: absent |
| 41 |
|
| 42 |
# 2. Setup if required |
| 43 |
# Creates necessary parent objects or dependencies that your ACI module's object relies on. |
| 44 |
- name: Add a new tenant (setup) |
| 45 |
aci_tenant: |
| 46 |
<<: *aci_info |
| 47 |
tenant: ansible_tenant |
| 48 |
description: Ansible tenant |
| 49 |
state: present |
| 50 |
|
| 51 |
- name: Add a new parent object (setup) |
| 52 |
aci_l3out: |
| 53 |
<<: *aci_info |
| 54 |
tenant: ansible_tenant |
| 55 |
name: '<parent_object_name>' # Replace <parent_object_name> with the actual name |
| 56 |
description: Parent object description |
| 57 |
state: present |
| 58 |
|
| 59 |
# 3. Create with minimum configuration (check/normal/idempotent) |
| 60 |
# Adds a new object using only the required parameters. |
| 61 |
# Each task registers its output for subsequent assertion. |
| 62 |
- name: Add new object with min config (check mode) |
| 63 |
your_module_name: |
| 64 |
<<: *aci_info |
| 65 |
# ... min required parameters for your module ... |
| 66 |
state: present |
| 67 |
check_mode: true |
| 68 |
register: result_min_check |
| 69 |
|
| 70 |
- name: Add new object with min config (normal mode) |
| 71 |
your_module_name: |
| 72 |
<<: *aci_info |
| 73 |
# ... min required parameters for your module ... |
| 74 |
state: present |
| 75 |
register: result_min_actual |
| 76 |
|
| 77 |
- name: Add new object with min config (idempotency) |
| 78 |
your_module_name: |
| 79 |
<<: *aci_info |
| 80 |
# ... min required parameters for your module ... |
| 81 |
state: present |
| 82 |
register: result_min_idempotency |
| 83 |
|
| 84 |
- name: Assert new object with min config |
| 85 |
assert: |
| 86 |
that: |
| 87 |
- result_min_check is changed == false |
| 88 |
- result_min_check.msg == "Would have created/modified object." # Example assertion |
| 89 |
- result_min_actual is changed == true |
| 90 |
- result_min_actual.current.name == "expected_name" # Replace with actual expected name/property |
| 91 |
- result_min_idempotency is changed == false |
| 92 |
|
| 93 |
# 4. Update with full configuration (check/normal/idempotent) |
| 94 |
# Adds or updates an object using all available parameters. |
| 95 |
- name: Update object with full config (check mode) |
| 96 |
your_module_name: |
| 97 |
<<: *aci_info |
| 98 |
# ... all parameters for your module ... |
| 99 |
state: present |
| 100 |
check_mode: true |
| 101 |
register: result_full_check |
| 102 |
|
| 103 |
- name: Update object with full config (normal mode) |
| 104 |
your_module_name: |
| 105 |
<<: *aci_info |
| 106 |
# ... all parameters for your module ... |
| 107 |
state: present |
| 108 |
register: result_full_actual |
| 109 |
|
| 110 |
- name: Update object with full config (idempotency) |
| 111 |
your_module_name: |
| 112 |
<<: *aci_info |
| 113 |
# ... all parameters for your module ... |
| 114 |
state: present |
| 115 |
register: result_full_idempotency |
| 116 |
|
| 117 |
- name: Assert update object with full config |
| 118 |
assert: |
| 119 |
that: |
| 120 |
- result_full_check is changed == true # Expect change if updating |
| 121 |
- result_full_check.msg == "Would have created/modified object." |
| 122 |
- result_full_actual is changed == true |
| 123 |
- result_full_actual.current.property == "expected_value" # Replace with actual expected property/value |
| 124 |
- result_full_idempotency is changed == false |
| 125 |
|
| 126 |
# 5. Additional update cases |
| 127 |
# Include specific scenarios for updating parameters, especially edge cases or conditional updates. |
| 128 |
# ... (Add more update tasks and assertions as needed) ... |
| 129 |
|
| 130 |
# 6. Additional create cases also used for query all |
| 131 |
# Add more objects if needed to thoroughly test the "query all" functionality. |
| 132 |
# ... (Add more create tasks for different objects if needed) ... |
| 133 |
|
| 134 |
# 7. Queries |
| 135 |
# Tasks to query a specific object and all objects of the module's class. |
| 136 |
- name: Query a particular object |
| 137 |
your_module_name: |
| 138 |
<<: *aci_info |
| 139 |
# ... parameters to identify a single object to query ... |
| 140 |
state: query |
| 141 |
register: query_single_result |
| 142 |
|
| 143 |
- name: Query all objects |
| 144 |
your_module_name: |
| 145 |
<<: *aci_info |
| 146 |
state: query |
| 147 |
register: query_all_result |
| 148 |
|
| 149 |
- name: Assert query results |
| 150 |
assert: |
| 151 |
that: |
| 152 |
- query_single_result.current is defined |
| 153 |
- query_single_result.current.name == "expected_name" # Replace with actual expected name |
| 154 |
- query_all_result.current is defined |
| 155 |
- query_all_result.current | length > 0 # At least one object found |
| 156 |
- query_all_result.current | selectattr('name', 'equalto', 'expected_name') | list | length == 1 # Verify specific object is in list |
| 157 |
|
| 158 |
# 8. Errors |
| 159 |
# Tasks designed to trigger expected error conditions (e.g., missing required parameters, invalid values) |
| 160 |
# and assert that the module fails gracefully with the correct error message. |
| 161 |
# ... (Add error-inducing tasks and assertions) ... |
| 162 |
|
| 163 |
# 9. Delete (check/normal/idempotent) |
| 164 |
# Tasks to remove objects created during testing. |
| 165 |
- name: Remove an object (check mode) |
| 166 |
your_module_name: |
| 167 |
<<: *aci_info |
| 168 |
# ... parameters to identify object to delete ... |
| 169 |
state: absent |
| 170 |
check_mode: true |
| 171 |
register: result_delete_check |
| 172 |
|
| 173 |
- name: Remove an object (normal mode) |
| 174 |
your_module_name: |
| 175 |
<<: *aci_info |
| 176 |
# ... parameters to identify object to delete ... |
| 177 |
state: absent |
| 178 |
register: result_delete_actual |
| 179 |
|
| 180 |
- name: Remove an object (idempotency) |
| 181 |
your_module_name: |
| 182 |
<<: *aci_info |
| 183 |
# ... parameters to identify object to delete ... |
| 184 |
state: absent |
| 185 |
register: result_delete_idempotency |
| 186 |
|
| 187 |
- name: Assert remove object |
| 188 |
assert: |
| 189 |
that: |
| 190 |
- result_delete_check is changed == true |
| 191 |
- result_delete_check.msg == "Would have removed object." |
| 192 |
- result_delete_actual is changed == true |
| 193 |
- result_delete_actual.current is not defined # Object should no longer exist |
| 194 |
- result_delete_idempotency is changed == false |
| 195 |
|
| 196 |
# CLEAN TEST ENVIRONMENT AFTER TESTS |
| 197 |
|
| 198 |
# 10. Final cleanup |
| 199 |
# Removes all objects created during the test run to leave the environment in a clean state. |
| 200 |
# Removing the highest parent object (e.g., the tenant) is often sufficient. |
| 201 |
- name: Remove the ansible_tenant (final cleanup) |
| 202 |
aci_tenant: |
| 203 |
<<: *aci_info |
| 204 |
tenant: ansible_tenant |
| 205 |
state: absent |
| 206 |
|
| 207 |
``` |