Skip to content

Commit

Permalink
IT: Fix jinja2 warning, add "deleted" state test, more...
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
allenrobel committed Jan 19, 2024
1 parent 21866a4 commit 743a1b5
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 63 deletions.
13 changes: 8 additions & 5 deletions plugins/modules/dcnm_image_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = (
Expand Down Expand Up @@ -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)
Expand Down
251 changes: 251 additions & 0 deletions tests/integration/targets/dcnm_image_upgrade/tests/deleted.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 743a1b5

Please sign in to comment.