From 743a1b558baefc30b7db38a812cf7c728ae0ad83 Mon Sep 17 00:00:00 2001 From: Allen Robel Date: Fri, 19 Jan 2024 13:02:22 -1000 Subject: [PATCH] IT: Fix jinja2 warning, add "deleted" state test, more... 1. Fixed the following with the merged.yaml role: [WARNING]: conditional statements should not include jinja2 templating delimiters ... 2. Adding deleted.yaml role 3. Fix ImageUpgradeTask detach_policy result (found with integration test for "deleted" state) --- plugins/modules/dcnm_image_upgrade.py | 13 +- .../dcnm_image_upgrade/tests/deleted.yaml | 251 ++++++++++++++++++ .../dcnm_image_upgrade/tests/merged.yaml | 116 ++++---- 3 files changed, 317 insertions(+), 63 deletions(-) create mode 100644 tests/integration/targets/dcnm_image_upgrade/tests/deleted.yaml diff --git a/plugins/modules/dcnm_image_upgrade.py b/plugins/modules/dcnm_image_upgrade.py index b0130e95a..99216dd24 100644 --- a/plugins/modules/dcnm_image_upgrade.py +++ b/plugins/modules/dcnm_image_upgrade.py @@ -999,7 +999,7 @@ def _attach_or_detach_image_policy(self, action=None) -> None: if action == "attach": self.result.diff_attach_policy = instance.diff_null - elif action == "detach": + if action == "detach": self.result.diff_detach_policy = instance.diff_null return @@ -1008,7 +1008,10 @@ def _attach_or_detach_image_policy(self, action=None) -> None: instance.action = action instance.serial_numbers = value instance.commit() - self.result.response_attach_policy = copy.deepcopy(instance.response) + if action == "attach": + self.result.response_attach_policy = copy.deepcopy(instance.response) + if action == "detach": + self.result.response_detach_policy = copy.deepcopy(instance.response) for diff in instance.diff: msg = ( @@ -1303,9 +1306,9 @@ def main(): # For an example configuration, see: # $ANSIBLE_COLLECTIONS_PATH/cisco/dcnm/plugins/module_utils/common/logging_config.json log = Log(ansible_module) - # collection_path = "/Users/arobel/repos/collections/ansible_collections/cisco/dcnm" - # config_file = f"{collection_path}/plugins/module_utils/common/logging_config.json" - # log.config = config_file + collection_path = "/Users/arobel/repos/collections/ansible_collections/cisco/dcnm" + config_file = f"{collection_path}/plugins/module_utils/common/logging_config.json" + log.config = config_file log.commit() task_module = ImageUpgradeTask(ansible_module) diff --git a/tests/integration/targets/dcnm_image_upgrade/tests/deleted.yaml b/tests/integration/targets/dcnm_image_upgrade/tests/deleted.yaml new file mode 100644 index 000000000..4cb664063 --- /dev/null +++ b/tests/integration/targets/dcnm_image_upgrade/tests/deleted.yaml @@ -0,0 +1,251 @@ +################################################################################ +# RUNTIME +################################################################################ + +# 45 to 50 minutes +# Some previous runtimes where: +# - 44:07.93 +# - 49:23.53 + +################################################################################ +# STEPS +################################################################################ + +# 1. Create a fabric +# 2. Merge switches into fabric +# 3. Upgrade switches using global config +# 4. Detach policies from switches +# 5. Cleanup + +################################################################################ +# REQUIREMENTS +################################################################################ + +# 1. image policies are already configured on the controller: +# - KR5M (Kerry release maintenance 5) +# - NR3F (Niles release maintenance 3) +# The above include both NX-OS and EPLD images. +# +# TODO: Once dcnm_image_policy module is accepted, use that to +# configure the above policies. +# +# Example vars for dcnm_image_upgrade integration tests +# Add to cisco/dcnm/playbooks/dcnm_tests.yaml) +# +# vars: +# # This testcase field can run any test in the tests directory for the role +# testcase: merged +# fabric_name: f1 +# username: admin +# password: "foobar" +# switch_username: admin +# switch_password: "foobar" +# spine1: 172.22.150.114 +# spine2: 172.22.150.115 +# leaf1: 172.22.150.106 +# leaf2: 172.22.150.107 +# leaf3: 172.22.150.108 +# leaf4: 172.22.150.109 +# # for dcnm_image_upgrade role +# test_fabric: "{{ fabric_name }}" +# ansible_switch1: "{{ leaf1 }}" +# ansible_switch2: "{{ leaf2 }}" +# ansible_switch3: "{{ spine1 }}" +# image_policy_1: "KR5M" +# image_policy_2: "NR3F" + +################################################################################ +# SETUP +################################################################################ + +- set_fact: + rest_fabric_create: "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics/{{ fabric_name }}" + +- name: DELETED - Verify if fabric is deployed. + cisco.dcnm.dcnm_rest: + method: GET + path: "{{ rest_fabric_create }}" + register: result + +- debug: + var: result + +- assert: + that: + - result.response.DATA != None + +- name: DELETED - setup - Clean up any existing devices + cisco.dcnm.dcnm_inventory: + fabric: "{{ fabric_name }}" + state: deleted + +- name: DELETED - Merge switches + cisco.dcnm.dcnm_inventory: + fabric: "{{ fabric_name }}" + state: merged + config: + - seed_ip: "{{ ansible_switch1 }}" + auth_proto: MD5 + user_name: "{{ switch_username }}" + password: "{{ switch_password }}" + max_hops: 0 + role: leaf + preserve_config: False + - seed_ip: "{{ ansible_switch2 }}" + auth_proto: MD5 + user_name: "{{ switch_username }}" + password: "{{ switch_password }}" + max_hops: 0 + role: leaf + preserve_config: False + - seed_ip: "{{ ansible_switch3 }}" + auth_proto: MD5 + user_name: "{{ switch_username }}" + password: "{{ switch_password }}" + max_hops: 0 + role: spine + preserve_config: False + register: result + +- assert: + that: + - result.changed == true + +- assert: + that: + - item["RETURN_CODE"] == 200 + loop: '{{ result.response }}' + +################################################################################ +# DELETED - Upgrade all switches using global_config +################################################################################ + +- name: DELETED - Upgrade all switches using global config + cisco.dcnm.dcnm_image_upgrade: &global_config + state: merged + config: + policy: "{{ image_policy_1 }}" + reboot: false + stage: true + validate: true + upgrade: + nxos: true + epld: false + options: + nxos: + mode: disruptive + bios_force: false + epld: + module: ALL + golden: false + reboot: + config_reload: false + write_erase: false + package: + install: false + uninstall: false + switches: + - ip_address: "{{ leaf1 }}" + - ip_address: "{{ leaf2 }}" + - ip_address: "{{ spine1 }}" + register: result + +- debug: + var: result + +- assert: + that: + - result.changed == true + - result.failed == false + - result.diff.attach_policy[0].policy_name == image_policy_1 + - result.diff.attach_policy[1].policy_name == image_policy_1 + - result.diff.attach_policy[2].policy_name == image_policy_1 + - result.diff.upgrade[0].devices[0].policyName == image_policy_1 + - result.diff.upgrade[1].devices[0].policyName == image_policy_1 + - result.diff.upgrade[2].devices[0].policyName == image_policy_1 + - result.diff.validate[0].policy == image_policy_1 + - result.diff.validate[1].policy == image_policy_1 + - result.diff.validate[2].policy == image_policy_1 + - result.response.attach_policy[0].RETURN_CODE == 200 + - result.response.stage[0].RETURN_CODE == 200 + - result.response.upgrade[0].RETURN_CODE == 200 + - result.response.upgrade[1].RETURN_CODE == 200 + - result.response.upgrade[2].RETURN_CODE == 200 + - result.response.validate[0].RETURN_CODE == 200 + +################################################################################ +# DELETED - Detach policies from two switches and verify +################################################################################ + +- name: DELETED - PAUSE 15 minutes for switches to reboot + ansible.builtin.pause: + minutes: 15 + +- name: DELETED - Detach policies from two switches + cisco.dcnm.dcnm_image_upgrade: + state: deleted + config: + policy: "{{ image_policy_1 }}" + switches: + - ip_address: "{{ leaf1 }}" + - ip_address: "{{ leaf2 }}" + register: result + +- debug: + var: result + +- assert: + that: + - result.changed == true + - result.failed == false + - (result.diff.attach_policy | length) == 0 + - (result.diff.detach_policy | length) == 2 + - (result.diff.stage | length) == 0 + - (result.diff.upgrade | length) == 0 + - (result.diff.validate | length) == 0 + - (result.response.attach_policy | length) == 0 + - (result.response.detach_policy | length) == 1 + - (result.response.stage | length) == 0 + - (result.response.upgrade | length) == 0 + - (result.response.validate | length) == 0 + + +################################################################################ +# DELETED - Detach policies from remaining switch and verify +################################################################################ + +- name: DELETED - Detach policy from one remaining switch + cisco.dcnm.dcnm_image_upgrade: + state: deleted + config: + policy: "{{ image_policy_1 }}" + switches: + - ip_address: "{{ spine1 }}" + register: result + +- debug: + var: result + +- assert: + that: + - result.changed == true + - result.failed == false + - (result.diff.attach_policy | length) == 0 + - (result.diff.detach_policy | length) == 1 + - (result.diff.stage | length) == 0 + - (result.diff.upgrade | length) == 0 + - (result.diff.validate | length) == 0 + - (result.response.attach_policy | length) == 0 + - (result.response.detach_policy | length) == 1 + - (result.response.stage | length) == 0 + - (result.response.upgrade | length) == 0 + - (result.response.validate | length) == 0 + +################################################################################ +# CLEAN-UP +################################################################################ + +- name: DELETED - cleanup - Remove devices from fabric + cisco.dcnm.dcnm_inventory: + fabric: "{{ fabric_name }}" + state: deleted \ No newline at end of file diff --git a/tests/integration/targets/dcnm_image_upgrade/tests/merged.yaml b/tests/integration/targets/dcnm_image_upgrade/tests/merged.yaml index 4a2b1bb00..9574c6a86 100644 --- a/tests/integration/targets/dcnm_image_upgrade/tests/merged.yaml +++ b/tests/integration/targets/dcnm_image_upgrade/tests/merged.yaml @@ -68,7 +68,7 @@ - assert: that: - - 'result.response.DATA != None' + - result.response.DATA != None - name: MERGED - setup - Clean up any existing devices cisco.dcnm.dcnm_inventory: @@ -105,11 +105,11 @@ - assert: that: - - 'result.changed == true' + - result.changed == true - assert: that: - - 'item["RETURN_CODE"] == 200' + - item["RETURN_CODE"] == 200 loop: '{{ result.response }}' ################################################################################ @@ -151,23 +151,23 @@ - assert: that: - - 'result.changed == true' - - 'result.failed == false' - - 'result.diff.attach_policy[0].policy_name == "{{ image_policy_1 }}"' - - 'result.diff.attach_policy[1].policy_name == "{{ image_policy_1 }}"' - - 'result.diff.attach_policy[2].policy_name == "{{ image_policy_1 }}"' - - 'result.diff.upgrade[0].devices[0].policyName == "{{ image_policy_1 }}"' - - 'result.diff.upgrade[1].devices[0].policyName == "{{ image_policy_1 }}"' - - 'result.diff.upgrade[2].devices[0].policyName == "{{ image_policy_1 }}"' - - 'result.diff.validate[0].policy == "{{ image_policy_1 }}"' - - 'result.diff.validate[1].policy == "{{ image_policy_1 }}"' - - 'result.diff.validate[2].policy == "{{ image_policy_1 }}"' - - 'result.response.attach_policy[0].RETURN_CODE == 200' - - 'result.response.stage[0].RETURN_CODE == 200' - - 'result.response.upgrade[0].RETURN_CODE == 200' - - 'result.response.upgrade[1].RETURN_CODE == 200' - - 'result.response.upgrade[2].RETURN_CODE == 200' - - 'result.response.validate[0].RETURN_CODE == 200' + - result.changed == true + - result.failed == false + - result.diff.attach_policy[0].policy_name == image_policy_1 + - result.diff.attach_policy[1].policy_name == image_policy_1 + - result.diff.attach_policy[2].policy_name == image_policy_1 + - result.diff.upgrade[0].devices[0].policyName == image_policy_1 + - result.diff.upgrade[1].devices[0].policyName == image_policy_1 + - result.diff.upgrade[2].devices[0].policyName == image_policy_1 + - result.diff.validate[0].policy == image_policy_1 + - result.diff.validate[1].policy == image_policy_1 + - result.diff.validate[2].policy == image_policy_1 + - result.response.attach_policy[0].RETURN_CODE == 200 + - result.response.stage[0].RETURN_CODE == 200 + - result.response.upgrade[0].RETURN_CODE == 200 + - result.response.upgrade[1].RETURN_CODE == 200 + - result.response.upgrade[2].RETURN_CODE == 200 + - result.response.validate[0].RETURN_CODE == 200 ################################################################################ # MERGED IDEMPOTENCE global_config @@ -186,16 +186,16 @@ - assert: that: - - 'result.changed == false' - - 'result.failed == false' - - '(result.diff.attach_policy | length) == 0' - - '(result.diff.stage | length) == 0' - - '(result.diff.upgrade | length) == 0' - - '(result.diff.validate | length) == 0' - - '(result.response.attach_policy | length) == 0' - - '(result.response.stage | length) == 0' - - '(result.response.upgrade | length) == 0' - - '(result.response.validate | length) == 0' + - result.changed == false + - result.failed == false + - (result.diff.attach_policy | length) == 0 + - (result.diff.stage | length) == 0 + - (result.diff.upgrade | length) == 0 + - (result.diff.validate | length) == 0 + - (result.response.attach_policy | length) == 0 + - (result.response.stage | length) == 0 + - (result.response.upgrade | length) == 0 + - (result.response.validate | length) == 0 ################################################################################ @@ -240,23 +240,23 @@ - assert: that: - - 'result.changed == true' - - 'result.failed == false' - - 'result.diff.attach_policy[0].policy_name == "{{ image_policy_2 }}"' - - 'result.diff.attach_policy[1].policy_name == "{{ image_policy_2 }}"' - - 'result.diff.attach_policy[2].policy_name == "{{ image_policy_2 }}"' - - 'result.diff.upgrade[0].devices[0].policyName == "{{ image_policy_2 }}"' - - 'result.diff.upgrade[1].devices[0].policyName == "{{ image_policy_2 }}"' - - 'result.diff.upgrade[2].devices[0].policyName == "{{ image_policy_2 }}"' - - 'result.diff.validate[0].policy == "{{ image_policy_2 }}"' - - 'result.diff.validate[1].policy == "{{ image_policy_2 }}"' - - 'result.diff.validate[2].policy == "{{ image_policy_2 }}"' - - 'result.response.attach_policy[0].RETURN_CODE == 200' - - 'result.response.stage[0].RETURN_CODE == 200' - - 'result.response.upgrade[0].RETURN_CODE == 200' - - 'result.response.upgrade[1].RETURN_CODE == 200' - - 'result.response.upgrade[2].RETURN_CODE == 200' - - 'result.response.validate[0].RETURN_CODE == 200' + - result.changed == true + - result.failed == false + - result.diff.attach_policy[0].policy_name == image_policy_2 + - result.diff.attach_policy[1].policy_name == image_policy_2 + - result.diff.attach_policy[2].policy_name == image_policy_2 + - result.diff.upgrade[0].devices[0].policyName == image_policy_2 + - result.diff.upgrade[1].devices[0].policyName == image_policy_2 + - result.diff.upgrade[2].devices[0].policyName == image_policy_2 + - result.diff.validate[0].policy == image_policy_2 + - result.diff.validate[1].policy == image_policy_2 + - result.diff.validate[2].policy == image_policy_2 + - result.response.attach_policy[0].RETURN_CODE == 200 + - result.response.stage[0].RETURN_CODE == 200 + - result.response.upgrade[0].RETURN_CODE == 200 + - result.response.upgrade[1].RETURN_CODE == 200 + - result.response.upgrade[2].RETURN_CODE == 200 + - result.response.validate[0].RETURN_CODE == 200 ################################################################################ # MERGED IDEMPOTENCE switch_config @@ -272,22 +272,22 @@ - assert: that: - - 'result.changed == false' - - 'result.failed == false' - - '(result.diff.attach_policy | length) == 0' - - '(result.diff.stage | length) == 0' - - '(result.diff.upgrade | length) == 0' - - '(result.diff.validate | length) == 0' - - '(result.response.attach_policy | length) == 0' - - '(result.response.stage | length) == 0' - - '(result.response.upgrade | length) == 0' - - '(result.response.validate | length) == 0' + - result.changed == false + - result.failed == false + - (result.diff.attach_policy | length) == 0 + - (result.diff.stage | length) == 0 + - (result.diff.upgrade | length) == 0 + - (result.diff.validate | length) == 0 + - (result.response.attach_policy | length) == 0 + - (result.response.stage | length) == 0 + - (result.response.upgrade | length) == 0 + - (result.response.validate | length) == 0 ################################################################################ # CLEAN-UP ################################################################################ -- name: MERGED - cleanup - Clean up any existing devices +- name: MERGED - CLEAN-UP - Remove devices from fabric cisco.dcnm.dcnm_inventory: fabric: "{{ fabric_name }}" state: deleted \ No newline at end of file