Skip to content

Commit

Permalink
added integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
roverflow committed Nov 18, 2024
1 parent ca72ff4 commit df633a3
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _compare(self, want, have, vrf):
begin = len(self.commands)
self._compare_vrf_afs(want=want, have=have)
if len(self.commands) != begin:
self.commands.insert(begin, self._tmplt.render({"name": vrf}, "name", False))
self.commands.insert(begin, f"vrf context {vrf}")

def _compare_vrf_afs(self, want, have):
"""Compare the VRF address families lists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _parse_vrf_af(self, data: dict):
:returns: argspec compliant list
"""
if not data:
return {}
return []

vrf_lists = list(data.values())
for item in vrf_lists:
Expand Down Expand Up @@ -104,8 +104,7 @@ def populate_facts(self, connection, ansible_facts, data=None):
redact=True,
),
)

facts["vrf_address_family"] = params["config"]
facts["vrf_address_family"] = params.get("config", [])
ansible_facts["ansible_network_resources"].update(facts)

return ansible_facts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ def __init__(self, lines=None, module=None):

# fmt: off
PARSERS = [
{
"name": "name",
"getval": re.compile(
r"""
^vrf\scontext\s(?P<name>\S+)
$""", re.VERBOSE,
),
"setval": "vrf context {{ name }}",
"result": {},
},
{
"name": "address_family",
"getval": re.compile(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
testcase: "[^_].*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- prepare_nxos_tests
21 changes: 21 additions & 0 deletions tests/integration/targets/nxos_vrf_address_family/tasks/cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: Collect all CLI test cases
ansible.builtin.find:
paths: "{{ role_path }}/tests/common"
patterns: "{{ testcase }}.yaml"
use_regex: true
register: test_cases
delegate_to: localhost

- name: Set test_items
ansible.builtin.set_fact:
test_items: "{{ test_cases.files | map(attribute='path') | list }}"
delegate_to: localhost

- name: Run test case (connection=ansible.netcommon.network_cli)
ansible.builtin.include_tasks: "{{ test_case_to_run }}"
vars:
ansible_connection: ansible.netcommon.network_cli
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Main task for vrf_global module
ansible.builtin.include_tasks: cli.yaml
tags:
- network_cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vrf context VRF1
address-family ipv4 unicast
route-target import 64512:200
route-target export 64512:200
export map 22
export vrf default map 44 allow-vpn
export vrf allow-vpn
maximum routes 900 22 reinstall 44
address-family ipv6 unicast
route-target import 554832:500
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Merge provided vrf configuration with device configuration
cisco.nxos.nxos_config:
lines:
- vrf context VRF1
- address-family ipv4 unicast
- route-target import 64512:200
- address-family ipv6 unicast
- route-target import 554832:500
match: none
retries: 3
delay: 20

- name: Merge another vrf
cisco.nxos.nxos_config:
lines:
- vrf context temp
match: none
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Remove VRF global configurations
cisco.nxos.nxos_config:
lines:
- no vrf context VRF1
ignore_errors: true
register: testvrf

- name: Wait for 20 seconds if VRF removal was successful
ansible.builtin.wait_for:
timeout: 20
when: not testvrf.failed

- name: Remove VRF global configurations
cisco.nxos.nxos_config:
lines:
- no vrf context temp
ignore_errors: true
register: temp

- name: Wait for 20 seconds if VRF removal was successful
ansible.builtin.wait_for:
timeout: 20
when: not temp.failed
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
- ansible.builtin.debug:
msg: Start Deleted integration state for nxos_vrf_address_family ansible_connection={{ ansible_connection }}

- ansible.builtin.include_tasks: _remove_config.yaml

- block:
- name: Merge config to be deleted with the existing running configuration
cisco.nxos.nxos_config:
lines:
- route-target import 64512:200
- route-target export 64512:200
- export map 22
- export vrf default map 44 allow-vpn
- export vrf allow-vpn
- maximum routes 900 22 reinstall 44
parents:
- vrf context VRF1
- address-family ipv4 unicast
retries: 2
delay: 10

- name: Delete given vrf address family configuration
register: result
cisco.nxos.nxos_vrf_address_family: &id001
config:
- name: VRF1
address_families:
- afi: ipv4
safi: unicast
route_target:
- import: 64512:200
export:
- map: "22"
maximum:
max_routes: 900
max_route_options:
threshold:
threshold_value: 22
reinstall_threshold: 44
state: deleted

- ansible.builtin.assert:
that:
- result.changed == true

- name: Assert that correct set of commands were generated
ansible.builtin.assert:
that:
- "{{ deleted['commands'] | symmetric_difference(result['commands']) | length == 0 }}"

- name: Assert that before dicts are correctly generated
ansible.builtin.assert:
that:
- "{{ general['after'] | symmetric_difference(res_before['before']) | length == 0 }}"

- name: Assert that after dicts are correctly generated
ansible.builtin.assert:
that:
- deleted['after'] == res_after['after']

- name: Delete provided VRF global (idempotent)
register: result
cisco.nxos.nxos_vrf_address_family: *id001

- name: Assert that the previous task was idempotent
ansible.builtin.assert:
that:
- result.changed == false

always:
- ansible.builtin.include_tasks: _remove_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
- ansible.builtin.debug:
msg: START nxos_vrf_address_family empty_config.yaml integration tests on connection={{ ansible_connection }}

- name: Merged with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
config:
state: merged

- ansible.builtin.assert:
that:
- result.msg == 'value of config parameter must not be empty for state merged'

- name: Replaced with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
config:
state: replaced

- ansible.builtin.assert:
that:
- result.msg == 'value of config parameter must not be empty for state replaced'

- name: Overridden with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
config:
state: overridden

- ansible.builtin.assert:
that:
- result.msg == 'value of config parameter must not be empty for state overridden'

- name: Rendered with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
config:
state: rendered

- ansible.builtin.assert:
that:
- result.msg == 'value of config parameter must not be empty for state rendered'

- name: Parsed with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
running_config:
state: parsed

- ansible.builtin.assert:
that:
- result.msg == 'value of running_config parameter must not be empty for state parsed'

- name: Purged with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.nxos.nxos_vrf_address_family:
config:
state: purged

- ansible.builtin.debug:
msg: END nxos_vrf_address_family empty_config integration tests on connection={{ ansible_connection }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- ansible.builtin.debug:
msg: START nxos_vrf_address_family gathered integration tests on connection={{ ansible_connection }}

- ansible.builtin.include_tasks: _remove_config.yaml
- ansible.builtin.include_tasks: _populate_config.yaml

- block:
- name: Gather the provided configuration with the existing running configuration
register: result
cisco.nxos.nxos_vrf_address_family:
config:
state: gathered

- name: Assert gathered state
ansible.builtin.assert:
that:
- result.changed == false
- gathered == result['gathered']

always:
- ansible.builtin.include_tasks: _remove_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
- ansible.builtin.debug:
msg: START Merged nxos_vrf_address_family state for integration tests on connection={{ ansible_connection }}

- ansible.builtin.include_tasks: _remove_config.yaml

- block:
- name: Merge provided configuration with device configuration
register: result
cisco.nxos.nxos_vrf_address_family: &id001
config:
- name: VRF1
address_families:
- afi: ipv4
safi: unicast
route_target:
- export: "65512:200"
maximum:
max_routes: 500
max_route_options:
threshold:
threshold_value: 60
reinstall_threshold: 80
export:
- map: "22"
- vrf:
allow_vpn: true
map_import: "44"
- vrf:
allow_vpn: true
- afi: ipv6
safi: unicast
maximum:
max_routes: 1000
route_target:
- import: "65512:200"
import:
- map: "22"
- vrf:
advertise_vpn: true
map_import: "44"
- vrf:
advertise_vpn: true
state: merged

- name: Assert that correct set of commands were generated
ansible.builtin.assert:
that:
- "{{ result['commands'] | symmetric_difference(merged['commands']) | length == 0 }}"

- name: Assert that before dicts are correctly generated
ansible.builtin.assert:
that:
- result['before'] == {}

- name: Assert that after dict is correctly generated
ansible.builtin.assert:
that:
- result['after'] == merged['after']

- name: Merge provided configuration with device configuration (idempotent)
register: result
cisco.nxos.nxos_vrf_address_family: *id001

- name: Assert that the previous task was idempotent
ansible.builtin.assert:
that:
- result['changed'] == false
always:
- ansible.builtin.include_tasks: _remove_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- ansible.builtin.debug:
msg: START nxos_vrf_address_family parsed integration tests on connection={{ ansible_connection }}

- name: Parse the commands for provided configuration
register: result
cisco.nxos.nxos_vrf_address_family:
running_config: "{{ lookup('file', '_parsed.cfg') }}"
state: parsed

- ansible.builtin.assert:
that:
- result.changed == false
- parsed == result['parsed']
Loading

0 comments on commit df633a3

Please sign in to comment.