From 4a21ac17096da268d5e9fcaf47ee136e281a3a59 Mon Sep 17 00:00:00 2001 From: AAYUSH ANAND Date: Tue, 19 Nov 2024 13:07:19 +0530 Subject: [PATCH] added few more tests --- plugins/modules/ios_vrf_interfaces.py | 152 +++++++++++++++++- .../tests/common/deleted.yaml | 50 ++++++ .../tests/common/empty_config.yaml | 11 ++ .../tests/common/merged.yaml | 33 ++-- .../tests/common/overridden.yaml | 31 ++-- .../tests/common/replaced.yaml | 42 +++++ .../ios_vrf_interfaces/tests/common/rtt.yaml | 55 +++++++ .../targets/ios_vrf_interfaces/vars/main.yaml | 44 +++++ 8 files changed, 391 insertions(+), 27 deletions(-) create mode 100644 tests/integration/targets/ios_vrf_interfaces/tests/common/deleted.yaml create mode 100644 tests/integration/targets/ios_vrf_interfaces/tests/common/replaced.yaml create mode 100644 tests/integration/targets/ios_vrf_interfaces/tests/common/rtt.yaml diff --git a/plugins/modules/ios_vrf_interfaces.py b/plugins/modules/ios_vrf_interfaces.py index ee7689509..5a9fdca3c 100644 --- a/plugins/modules/ios_vrf_interfaces.py +++ b/plugins/modules/ios_vrf_interfaces.py @@ -315,12 +315,160 @@ # vrf_name: "vrf_D" # - name: "GigabitEthernet3" # - name: "GigabitEthernet4" + +# Using replaced + +# Before state: +# ------------- +# +# vios#show running-config | section ^interface +# interface Loopback24 +# no ip address +# interface GigabitEthernet1 +# vrf forwarding vrf_A +# ip address dhcp +# negotiation auto +# interface GigabitEthernet2 +# vrf forwarding vrf_B +# no ip address +# shutdown +# negotiation auto +# interface GigabitEthernet3 +# no ip address +# negotiation auto +# interface GigabitEthernet4 +# vrf forwarding vrf_C +# no ip address +# shutdown +# negotiation auto + +- name: Replace device configuration of listed VRF interfaces with provided configuration + cisco.ios.ios_vrf_interfaces: + config: + - name: GigabitEthernet1 + vrf_name: vrf_D + - name: GigabitEthernet2 + vrf_name: vrf_E + state: replaced + +# Task Output: +# ------------ +# +# before: +# - name: "Loopback24" +# - name: "GigabitEthernet1" +# vrf_name: "vrf_A" +# - name: "GigabitEthernet2" +# vrf_name: "vrf_B" +# - name: "GigabitEthernet3" +# - name: "GigabitEthernet4" +# vrf_name: "vrf_C" +# +# commands: +# - interface GigabitEthernet1 +# - no vrf forwarding vrf_A +# - vrf forwarding vrf_D +# - interface GigabitEthernet2 +# - no vrf forwarding vrf_B +# - vrf forwarding vrf_E +# +# after: +# - name: "Loopback24" +# - name: "GigabitEthernet1" +# vrf_name: "vrf_D" +# - name: "GigabitEthernet2" +# vrf_name: "vrf_E" +# - name: "GigabitEthernet3" +# - name: "GigabitEthernet4" +# vrf_name: "vrf_C" + +# Using deleted + +# Before state: +# ------------- +# +# vios#show running-config | section ^interface +# interface Loopback24 +# no ip address +# interface GigabitEthernet1 +# vrf forwarding vrf_A +# ip address dhcp +# negotiation auto +# interface GigabitEthernet2 +# vrf forwarding vrf_B +# no ip address +# shutdown +# negotiation auto +# interface GigabitEthernet3 +# no ip address +# negotiation auto +# interface GigabitEthernet4 +# vrf forwarding vrf_C +# no ip address +# shutdown +# negotiation auto + +- name: Delete VRF configuration of specified interfaces + cisco.ios.ios_vrf_interfaces: + config: + - name: GigabitEthernet1 + - name: GigabitEthernet2 + state: deleted + +# Task Output: +# ------------ +# +# before: +# - name: "Loopback24" +# - name: "GigabitEthernet1" +# vrf_name: "vrf_A" +# - name: "GigabitEthernet2" +# vrf_name: "vrf_B" +# - name: "GigabitEthernet3" +# - name: "GigabitEthernet4" +# vrf_name: "vrf_C" +# +# commands: +# - interface GigabitEthernet1 +# - no vrf forwarding vrf_A +# - interface GigabitEthernet2 +# - no vrf forwarding vrf_B +# +# after: +# - name: "Loopback24" +# - name: "GigabitEthernet1" +# - name: "GigabitEthernet2" +# - name: "GigabitEthernet3" +# - name: "GigabitEthernet4" +# vrf_name: "vrf_C" + +# After state: +# ------------ +# +# vios#show running-config | section ^interface +# interface Loopback24 +# no ip address +# interface GigabitEthernet1 +# ip address dhcp +# negotiation auto +# interface GigabitEthernet2 +# no ip address +# shutdown +# negotiation auto +# interface GigabitEthernet3 +# no ip address +# negotiation auto +# interface GigabitEthernet4 +# vrf forwarding vrf_C +# no ip address +# shutdown +# negotiation auto """ RETURN = """ before: description: The configuration prior to the module execution. - returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) + returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) type: list sample: > [ @@ -368,7 +516,7 @@ commands: description: The set of commands pushed to the remote device. - returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) + returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) type: list sample: - "interface GigabitEthernet2" diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/deleted.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/deleted.yaml new file mode 100644 index 000000000..4f9abef79 --- /dev/null +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/deleted.yaml @@ -0,0 +1,50 @@ +--- +- ansible.builtin.debug: + msg: Start Deleted integration state for ios_vrf_interfaces ansible_connection={{ ansible_connection }} + +- ansible.builtin.include_tasks: _remove_config.yaml +- ansible.builtin.include_tasks: _populate_config.yaml + +- block: + - name: Delete provided VRF interfaces + register: result + cisco.ios.ios_vrf_interfaces: &id001 + config: + - name: GigabitEthernet2 + state: deleted + + - name: Assert that correct set of commands were generated + ansible.builtin.assert: + that: + - "{{ deleted['commands'] | symmetric_difference(result['commands']) | length == 0 }}" + + - name: Delete provided VRF interfaces (idempotent) + register: result + cisco.ios.ios_vrf_interfaces: *id001 + - name: Assert that the previous task was idempotent + ansible.builtin.assert: + that: + - result.changed == false + + - ansible.builtin.include_tasks: _populate_config.yaml + + - name: Delete provided VRF interfaces without any configuration + register: result + cisco.ios.ios_vrf_interfaces: &id002 + state: deleted + + - name: Assert that correct set of commands were generated + ansible.builtin.assert: + that: + - "{{ deleted['commands'] | symmetric_difference(result['commands']) | length == 0 }}" + + - name: Delete provided VRF interfaces without any configuration (idempotent) + register: result + cisco.ios.ios_vrf_interfaces: *id002 + - name: Assert that the delete task was idempotent + ansible.builtin.assert: + that: + - result.changed == false + + always: + - ansible.builtin.include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/empty_config.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/empty_config.yaml index fd31c161c..2506c7e50 100644 --- a/tests/integration/targets/ios_vrf_interfaces/tests/common/empty_config.yaml +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/empty_config.yaml @@ -24,6 +24,17 @@ 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.ios.ios_vrf_interfaces: + 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 diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/merged.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/merged.yaml index 2e524766d..9d169d135 100644 --- a/tests/integration/targets/ios_vrf_interfaces/tests/common/merged.yaml +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/merged.yaml @@ -1,35 +1,44 @@ --- - ansible.builtin.debug: - msg: Start ios_vrf_interfaces merged integration tests connection={{ ansible_connection}} + msg: START Merged ios_vrf_interfaces state for integration tests on connection={{ ansible_connection }} - ansible.builtin.include_tasks: _remove_config.yaml - block: - - name: Simple merge selective - cisco.ios.ios_vrf_interfaces: - state: merged + - name: Merge provided configuration with device configuration + register: result + cisco.ios.ios_vrf_interfaces: &id001 config: - name: GigabitEthernet1 - name: GigabitEthernet2 vrf_name: vrf_D - name: GigabitEthernet3 - name: GigabitEthernet4 + state: merged - register: result + - name: Assert that correct set of commands were generated + ansible.builtin.assert: + that: + - "{{ merged['commands'] | symmetric_difference(result['commands']) | length == 0 }}" - - name: Run show running-config | section ^interface - cisco.ios.ios_command: - commands: show running-config | section ^interface + - name: Assert that before dicts are correctly generated + ansible.builtin.assert: + that: + - "{{ merged['before'] | symmetric_difference(result['before']) | length == 0 }}" - - name: Assert that correct set of commands were generated + - name: Assert that after dict is correctly generated ansible.builtin.assert: that: - - "{{ merged['commands'] | symmetric_difference(result['commands']) |length == 0 }}" + - "{{ merged['after'] | symmetric_difference(result['after']) | length == 0 }}" + + - name: Merge provided configuration with device configuration (idempotent) + register: result + cisco.ios.ios_vrf_interfaces: *id001 - - name: Assert that after dicts were correctly generated + - name: Assert that the previous task was idempotent ansible.builtin.assert: that: - - merged['after'] == result['after'] + - result['changed'] == false always: - ansible.builtin.include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/overridden.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/overridden.yaml index d0e21d8fd..5fa53c611 100644 --- a/tests/integration/targets/ios_vrf_interfaces/tests/common/overridden.yaml +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/overridden.yaml @@ -1,39 +1,44 @@ --- - ansible.builtin.debug: - msg: Start ios_vrf_interfaces overridden integration tests connection={{ ansible_connection}} + msg: START Overridden ios_vrf_interfaces state for integration tests on connection={{ ansible_connection }} - ansible.builtin.include_tasks: _populate_config.yaml - block: - - name: Override the route-policy configuration - cisco.ios.ios_vrf_interfaces: - state: overridden + - name: Override provided configuration with device configuration + register: result + cisco.ios.ios_vrf_interfaces: &id001 config: - name: GigabitEthernet1 - name: GigabitEthernet2 - name: GigabitEthernet3 - name: GigabitEthernet4 vrf_name: vrf_C - - register: result - - - name: Run show running-config | section ^interface - cisco.ios.ios_command: - commands: show running-config | section ^interface + state: overridden - name: Assert that correct set of commands were generated ansible.builtin.assert: that: - - "{{ overridden['commands'] | symmetric_difference(result['commands']) |length == 0 }}" + - "{{ overridden['commands'] | symmetric_difference(result['commands']) | length == 0 }}" + + - name: Assert that before dicts are correctly generated + ansible.builtin.assert: + that: + - overridden['before'] == result['before'] - name: Assert that after dict is correctly generated ansible.builtin.assert: that: - overridden['after'] == result['after'] - - name: Assert that before dicts are correctly generated + - name: Override provided configuration with device configuration (idempotent) + register: result + cisco.ios.ios_vrf_interfaces: *id001 + + - name: Assert that the previous task was idempotent ansible.builtin.assert: that: - - overridden['before'] == result['before'] + - result['changed'] == false + always: - ansible.builtin.include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/replaced.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/replaced.yaml new file mode 100644 index 000000000..0b8089093 --- /dev/null +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/replaced.yaml @@ -0,0 +1,42 @@ +--- +- ansible.builtin.debug: + msg: START Replaced ios_vrf_interfaces state for integration tests on connection={{ ansible_connection }} + +- ansible.builtin.include_tasks: _remove_config.yaml +- ansible.builtin.include_tasks: _populate_config.yaml + +- block: + - name: Replace provided VRF interfaces configuration + register: result + cisco.ios.ios_vrf_interfaces: &id001 + config: + - name: GigabitEthernet2 + vrf_name: vrf_D + state: replaced + + - name: Assert that correct set of commands were generated + ansible.builtin.assert: + that: + - "{{ replaced['commands'] | symmetric_difference(result['commands']) | length == 0 }}" + + - name: Assert that before dicts are correctly generated + ansible.builtin.assert: + that: + - replaced['before'] == result['before'] + + - name: Assert that after dict is correctly generated + ansible.builtin.assert: + that: + - replaced['after'] == result['after'] + + - name: Replace provided VRF interfaces configuration (idempotent) + register: result + cisco.ios.ios_vrf_interfaces: *id001 + + - name: Assert that task was idempotent + ansible.builtin.assert: + that: + - result['changed'] == false + + always: + - ansible.builtin.include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/ios_vrf_interfaces/tests/common/rtt.yaml b/tests/integration/targets/ios_vrf_interfaces/tests/common/rtt.yaml new file mode 100644 index 000000000..7dfed1d48 --- /dev/null +++ b/tests/integration/targets/ios_vrf_interfaces/tests/common/rtt.yaml @@ -0,0 +1,55 @@ +--- +- ansible.builtin.debug: + msg: START ios_vrf_interfaces round trip integration tests on connection={{ ansible_connection }} + +- ansible.builtin.include_tasks: _remove_config.yaml + +- block: + - name: Apply the provided configuration (base config) + register: base_config + cisco.ios.ios_vrf_interfaces: + config: + - name: GigabitEthernet1 + - name: GigabitEthernet2 + vrf_name: vrf_B + - name: GigabitEthernet3 + - name: GigabitEthernet4 + state: merged + + - name: Gather VRF interfaces facts + cisco.ios.ios_facts: + gather_subset: + - "!all" + - "!min" + gather_network_resources: + - vrf_interfaces + + - name: Apply the provided configuration (config to be reverted) + register: result + cisco.ios.ios_vrf_interfaces: + config: + - name: GigabitEthernet1 + - name: GigabitEthernet2 + - name: GigabitEthernet3 + - name: GigabitEthernet4 + vrf_name: vrf_C + state: overridden + + - ansible.builtin.assert: + that: + - result.changed == true + - result.commands|symmetric_difference(overridden.commands) == [] + + - name: Revert back to base configuration using facts round trip + register: revert + cisco.ios.ios_vrf_interfaces: + config: "{{ ansible_facts['network_resources']['vrf_interfaces'] }}" + state: overridden + + - ansible.builtin.assert: + that: + - revert.changed == true + - revert.commands|symmetric_difference(rtt.commands) == [] + + always: + - ansible.builtin.include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/ios_vrf_interfaces/vars/main.yaml b/tests/integration/targets/ios_vrf_interfaces/vars/main.yaml index 877959521..20d971bc3 100644 --- a/tests/integration/targets/ios_vrf_interfaces/vars/main.yaml +++ b/tests/integration/targets/ios_vrf_interfaces/vars/main.yaml @@ -55,3 +55,47 @@ parsed: vrf_name: "vrf_D" - name: "GigabitEthernet3" - name: "GigabitEthernet4" + +replaced: + commands: + - interface GigabitEthernet2 + - vrf forwarding vrf_D + before: + - name: "Loopback24" + - name: "GigabitEthernet1" + - name: "GigabitEthernet2" + vrf_name: "vrf_B" + - name: "GigabitEthernet3" + - name: "GigabitEthernet4" + after: + - name: "Loopback24" + - name: "GigabitEthernet1" + - name: "GigabitEthernet2" + vrf_name: "vrf_D" + - name: "GigabitEthernet3" + - name: "GigabitEthernet4" + +deleted: + commands: + - interface GigabitEthernet2 + - no vrf forwarding vrf_B + before: + - name: "Loopback24" + - name: "GigabitEthernet1" + - name: "GigabitEthernet2" + vrf_name: "vrf_B" + - name: "GigabitEthernet3" + - name: "GigabitEthernet4" + after: + - name: "Loopback24" + - name: "GigabitEthernet1" + - name: "GigabitEthernet2" + - name: "GigabitEthernet3" + - name: "GigabitEthernet4" + +rtt: + commands: + - interface GigabitEthernet2 + - vrf forwarding vrf_B + - interface GigabitEthernet4 + - no vrf forwarding vrf_C