From df633a3f93735f95eb34ee1b8f18245bb12f1820 Mon Sep 17 00:00:00 2001 From: roverflow Date: Mon, 18 Nov 2024 15:30:45 +0530 Subject: [PATCH] added integration test --- .../vrf_address_family/vrf_address_family.py | 2 +- .../vrf_address_family/vrf_address_family.py | 5 +- .../nxos/rm_templates/vrf_address_family.py | 10 --- .../defaults/main.yaml | 2 + .../nxos_vrf_address_family/meta/main.yaml | 3 + .../nxos_vrf_address_family/tasks/cli.yaml | 21 +++++ .../nxos_vrf_address_family/tasks/main.yml | 5 ++ .../tests/common/_parsed.cfg | 10 +++ .../tests/common/_populate_config.yaml | 18 ++++ .../tests/common/_remove_config.yaml | 24 +++++ .../tests/common/deleted.yaml | 72 +++++++++++++++ .../tests/common/empty_config.yaml | 68 +++++++++++++++ .../tests/common/gathered.yaml | 22 +++++ .../tests/common/merged.yaml | 70 +++++++++++++++ .../tests/common/parsed.yaml | 14 +++ .../nxos_vrf_address_family/vars/main.yaml | 87 +++++++++++++++++++ .../nxos/test_nxos_vrf_address_family.py | 1 - 17 files changed, 419 insertions(+), 15 deletions(-) create mode 100644 tests/integration/targets/nxos_vrf_address_family/defaults/main.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/meta/main.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tasks/cli.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tasks/main.yml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/_parsed.cfg create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/_populate_config.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/_remove_config.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/deleted.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/empty_config.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/gathered.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/merged.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/tests/common/parsed.yaml create mode 100644 tests/integration/targets/nxos_vrf_address_family/vars/main.yaml diff --git a/plugins/module_utils/network/nxos/config/vrf_address_family/vrf_address_family.py b/plugins/module_utils/network/nxos/config/vrf_address_family/vrf_address_family.py index 66838ff9d..0a82cbb9a 100644 --- a/plugins/module_utils/network/nxos/config/vrf_address_family/vrf_address_family.py +++ b/plugins/module_utils/network/nxos/config/vrf_address_family/vrf_address_family.py @@ -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. diff --git a/plugins/module_utils/network/nxos/facts/vrf_address_family/vrf_address_family.py b/plugins/module_utils/network/nxos/facts/vrf_address_family/vrf_address_family.py index 5fbd4399c..46b45ba7d 100644 --- a/plugins/module_utils/network/nxos/facts/vrf_address_family/vrf_address_family.py +++ b/plugins/module_utils/network/nxos/facts/vrf_address_family/vrf_address_family.py @@ -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: @@ -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 diff --git a/plugins/module_utils/network/nxos/rm_templates/vrf_address_family.py b/plugins/module_utils/network/nxos/rm_templates/vrf_address_family.py index e473991c7..63c59cb81 100644 --- a/plugins/module_utils/network/nxos/rm_templates/vrf_address_family.py +++ b/plugins/module_utils/network/nxos/rm_templates/vrf_address_family.py @@ -71,16 +71,6 @@ def __init__(self, lines=None, module=None): # fmt: off PARSERS = [ - { - "name": "name", - "getval": re.compile( - r""" - ^vrf\scontext\s(?P\S+) - $""", re.VERBOSE, - ), - "setval": "vrf context {{ name }}", - "result": {}, - }, { "name": "address_family", "getval": re.compile( diff --git a/tests/integration/targets/nxos_vrf_address_family/defaults/main.yaml b/tests/integration/targets/nxos_vrf_address_family/defaults/main.yaml new file mode 100644 index 000000000..871ea460c --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +testcase: "[^_].*" diff --git a/tests/integration/targets/nxos_vrf_address_family/meta/main.yaml b/tests/integration/targets/nxos_vrf_address_family/meta/main.yaml new file mode 100644 index 000000000..f504a6ab2 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - prepare_nxos_tests diff --git a/tests/integration/targets/nxos_vrf_address_family/tasks/cli.yaml b/tests/integration/targets/nxos_vrf_address_family/tasks/cli.yaml new file mode 100644 index 000000000..ed4cb02e2 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tasks/cli.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tasks/main.yml b/tests/integration/targets/nxos_vrf_address_family/tasks/main.yml new file mode 100644 index 000000000..adc4c6075 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Main task for vrf_global module + ansible.builtin.include_tasks: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/_parsed.cfg b/tests/integration/targets/nxos_vrf_address_family/tests/common/_parsed.cfg new file mode 100644 index 000000000..867d36458 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/_parsed.cfg @@ -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 \ No newline at end of file diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/_populate_config.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/_populate_config.yaml new file mode 100644 index 000000000..94917e7b2 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/_populate_config.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/_remove_config.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/_remove_config.yaml new file mode 100644 index 000000000..91bc45def --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/_remove_config.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/deleted.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/deleted.yaml new file mode 100644 index 000000000..551c5fdd7 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/deleted.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/empty_config.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/empty_config.yaml new file mode 100644 index 000000000..d73e5633b --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/empty_config.yaml @@ -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 }} diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/gathered.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/gathered.yaml new file mode 100644 index 000000000..bfbd78f54 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/gathered.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/merged.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/merged.yaml new file mode 100644 index 000000000..9b62c2c2d --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/merged.yaml @@ -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 diff --git a/tests/integration/targets/nxos_vrf_address_family/tests/common/parsed.yaml b/tests/integration/targets/nxos_vrf_address_family/tests/common/parsed.yaml new file mode 100644 index 000000000..4a27cadab --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/tests/common/parsed.yaml @@ -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'] diff --git a/tests/integration/targets/nxos_vrf_address_family/vars/main.yaml b/tests/integration/targets/nxos_vrf_address_family/vars/main.yaml new file mode 100644 index 000000000..818f19206 --- /dev/null +++ b/tests/integration/targets/nxos_vrf_address_family/vars/main.yaml @@ -0,0 +1,87 @@ +--- +merged: + after: + - address_families: + - afi: ipv4 + export: + - map: "22" + - vrf: + allow_vpn: true + map_import: "44" + - vrf: + allow_vpn: true + maximum: + max_route_options: + threshold: + reinstall_threshold: 80 + threshold_value: 60 + max_routes: 500 + route_target: + - export: 65512:200 + safi: unicast + - afi: ipv6 + import: + - map: "22" + - vrf: + advertise_vpn: true + map_import: "44" + - vrf: + advertise_vpn: true + maximum: + max_routes: 1000 + route_target: + - import: 65512:200 + safi: unicast + name: VRF1 + commands: + - vrf context VRF1 + - address-family ipv4 unicast + - maximum routes 500 60 reinstall 80 + - route-target export 65512:200 + - export map 22 + - export vrf default map 44 allow-vpn + - export vrf allow-vpn + - address-family ipv6 unicast + - maximum routes 1000 + - route-target import 65512:200 + - import map 22 + - import vrf default map 44 advertise-vpn + - import vrf advertise-vpn +deleted: + commands: [] +gathered: + - name: VRF1 + address_families: + - afi: ipv4 + safi: unicast + route_target: + - import: 64512:200 + - afi: ipv6 + safi: unicast + route_target: + - import: 554832:500 +parsed: + - name: VRF1 + address_families: + - afi: ipv4 + safi: unicast + route_target: + - import: 64512:200 + - export: 64512:200 + export: + - map: "22" + - vrf: + allow_vpn: true + map_import: "44" + - vrf: + allow_vpn: true + maximum: + max_routes: 900 + max_route_options: + threshold: + threshold_value: 22 + reinstall_threshold: 44 + - afi: ipv6 + safi: unicast + route_target: + - import: 554832:500 diff --git a/tests/unit/modules/network/nxos/test_nxos_vrf_address_family.py b/tests/unit/modules/network/nxos/test_nxos_vrf_address_family.py index 5dbd12d28..14ae64554 100644 --- a/tests/unit/modules/network/nxos/test_nxos_vrf_address_family.py +++ b/tests/unit/modules/network/nxos/test_nxos_vrf_address_family.py @@ -220,7 +220,6 @@ def test_vrf_af_merged(self): "import vrf advertise-vpn", ] result = self.execute_module(changed=True) - print(result["commands"]) self.assertEqual(sorted(result["commands"]), sorted(commands)) def test_vrf_af_merged_idempotent(self):