Skip to content

Commit

Permalink
add states & integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchip16 committed Apr 22, 2024
1 parent f18dfdc commit d178d83
Show file tree
Hide file tree
Showing 19 changed files with 593 additions and 8 deletions.
31 changes: 23 additions & 8 deletions plugins/module_utils/network/ios/config/vrf_global/vrf_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,37 @@ def generate_commands(self):
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}

# remove superfluous config for overridden and deleted
if self.state in ["overridden", "deleted"]:
for k, have in iteritems(haved):
if k not in wantd:
self._compare(want={}, have=have)
if self.state == "purged":
haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd}
wantd = {}

for k, want in iteritems(wantd):
self._compare(want=want, have=haved.pop(k, {}))
self._compare(want=wantd, have=haved)

def _compare(self, want, have):
"""Leverages the base class `compare()` method and
populates the list of commands to be run by comparing
the `want` and `have` data with the `parsers` defined
for the Vrf_global network resource.
"""
self.compare(parsers=self.parsers, want=want, have=have)
self._compare_vrf(want=want, have=have)

def _compare_vrf(self, want, have):
"""Custom handling of vrfs option
:params want: the want VRF dictionary
:params have: the have VRF dictionary
"""

for name, entry in iteritems(want):
begin = len(self.commands)
vrf_have = have.pop(name, {})

self.compare(parsers=self.parsers, want=entry, have=vrf_have)

# for deleted and overridden state
if self.state != "replaced":
begin = len(self.commands)
for name, entry in iteritems(have):
self.commands.insert(begin, "no vrf definition {0}".format(name))

def _get_config(self):
return self._connection.get("show running-config vrf")
3 changes: 3 additions & 0 deletions tests/integration/targets/ios_vrf_global/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testcase: "[^_].*"
test_items: []
2 changes: 2 additions & 0 deletions tests/integration/targets/ios_vrf_global/meta/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
dependencies: []
21 changes: 21 additions & 0 deletions tests/integration/targets/ios_vrf_global/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/cli"
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
5 changes: 5 additions & 0 deletions tests/integration/targets/ios_vrf_global/tasks/main.yaml
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
11 changes: 11 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/_parsed.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
vrf definition test
vnet tag 34
description This is test VRF
ipv4 multicast multitopology
ipv6 multicast multitopology
rd 10.2.3.4:300
vpn id 3:4
route-target export 23.1.3.4:400
route-target import 123.3.4.5:700
!
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: Merge provided configuration with device configuration
register: result
cisco.ios.ios_vrf_global:
config:
- name: VRF2
description: This is a test VRF for merged state
ipv4:
multicast:
multitopology: true
ipv6:
multicast:
multitopology: true
rd: "2:3"
route_target:
export: "23.1.3.4:400"
import_config: "10.1.3.4:400"
vpn:
id: "2:45"
vnet:
tag: 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Remove VRF global configurations
register: result
cisco.ios.ios_vrf_global:
state: purged
36 changes: 36 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/deleted.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
- ansible.builtin.debug:
msg: Start Deleted integration state for ios_vrf_global ansible_connection={{ ansible_connection }}

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

- block:
- name: Delete given vrf configuration
register: result
cisco.ios.ios_vrf_global: &id001
config:
state: deleted

- ansible.builtin.assert:
that:
- result.commands|length == 1
- result.changed == true
- "'no vrf definition test' not in result.commands"

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

- name: Delete provided BGP global (idempotent)
register: result
cisco.ios.ios_vrf_global: *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,50 @@
---
- ansible.builtin.debug:
msg: START ios_vrf_global 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.ios.ios_vrf_global:
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.ios.ios_vrf_global:
config:
state: replaced

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

- name: Rendered with empty configuration should give appropriate error message
register: result
ignore_errors: true
cisco.ios.ios_vrf_global:
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.ios.ios_vrf_global:
running_config:
state: parsed

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

- ansible.builtin.debug:
msg: END ios_vrf_global empty_config integration tests on connection={{ ansible_connection }}
15 changes: 15 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/gathered.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- ansible.builtin.debug:
msg: START ios_vrf_global 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.ios.ios_vrf_global:
state: gathered

always:
- ansible.builtin.include_tasks: _remove_config.yaml
53 changes: 53 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/merged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
- ansible.builtin.debug:
msg: START Merged ios_vrf_global 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.ios.ios_vrf_global: &id001
config:
- name: VRF2
description: This is a test VRF for merged state
ipv4:
multicast:
multitopology: true
ipv6:
multicast:
multitopology: true
rd: "2:3"
route_target:
export: 23.1.3.4:400
import_config: 10.1.3.4:400
vpn:
id: "2:45"
vnet:
tag: 200
state: merged

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

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

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

- name: Merge provided configuration with device configuration (idempotent)
register: result
cisco.ios.ios_vrf_global: *id001
- name: Assert that the previous task was idempotent
ansible.builtin.assert:
that:
- result['changed'] == false
always:
- ansible.builtin.include_tasks: _remove_config.yaml
58 changes: 58 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/overridden.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
- ansible.builtin.debug:
msg: START ios_vrf_global overridden integration tests on connection={{ ansible_connection }}

- ansible.builtin.include_tasks: _populate_config.yaml

- ansible.builtin.include_tasks: _remove_config.yaml

- block:
- name: Override the provided configuration with the existing running configuration
cisco.ios.ios_vrf_global: &overridden
config:
- name: VRF6
description: VRF6 description
ipv4:
multicast:
multitopology: true
ipv6:
multicast:
multitopology: true
rd: "6:7"
route_target:
export: "3.1.3.4:400"
import_config: "1.12.3.4:200"
vpn:
id: "4:5"
vnet:
tag: 500
state: overridden
register: result

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

- name: Assert that after dict is correctly generated
ansible.builtin.assert:
that:
- "{{ overridden['after'] | symmetric_difference(result['after']) |length == 0 }}"

- name: Idempotency check
cisco.ios.ios_vrf_global: *overridden
register: result

- name: Assert that no changes were made
ansible.builtin.assert:
that:
- result['changed'] == false
- result.commands|length == 0

- name: Assert that before dict is correctly generated
ansible.builtin.assert:
that:
- "{{ overridden['after'] | symmetric_difference(result['before']) |length == 0 }}"

always:
- ansible.builtin.include_tasks: _remove_config.yaml
13 changes: 13 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/parsed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
- ansible.builtin.debug:
msg: START ios_vrf_global parsed integration tests on connection={{ ansible_connection }}

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

- ansible.builtin.assert:
that:
- result.changed == false
29 changes: 29 additions & 0 deletions tests/integration/targets/ios_vrf_global/tests/cli/purged.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- ansible.builtin.debug:
msg: Start Deleted integration state for ios_vrf_global ansible_connection={{ ansible_connection }}

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

- block:
- name: Purge provided VRF global
register: result
cisco.ios.ios_vrf_global: &id001
state: purged

- name: Assert that correct set of commands were generated
ansible.builtin.assert:
that:
- "'no VRF definition VRF2' in result.commands"
- result.commands|length == 1

- name: Purge provided VRF global (idempotent)
register: result
cisco.ios.ios_vrf_global: *id001
- name: Assert that the previous task was idempotent
ansible.builtin.assert:
that:
- result.changed == false

always:
- ansible.builtin.include_tasks: _remove_config.yaml
Loading

0 comments on commit d178d83

Please sign in to comment.