From c80fa371f48a2b282ca4b530e17737110991d7d7 Mon Sep 17 00:00:00 2001 From: Shrishail Kariyappanavar Date: Tue, 29 Oct 2019 15:13:25 -0400 Subject: [PATCH 1/3] Updated nxos_lacp tests to handle platforms not supporting lacp system mac command --- .../module_utils/network/common/utils.py | 5 ++++- .../network/nxos/config/lacp/lacp.py | 3 +++ .../targets/nxos_lacp/tests/cli/deleted.yaml | 21 +++++++++++++++++++ .../targets/nxos_lacp/tests/cli/merged.yaml | 17 +++++++++++++++ .../targets/nxos_lacp/tests/cli/replaced.yaml | 21 ++++++++++++++++--- .../targets/prepare_nxos_tests/tasks/main.yml | 1 - 6 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/network/common/utils.py b/lib/ansible/module_utils/network/common/utils.py index 75fcc8759f9e85..ea8a56cffae3de 100644 --- a/lib/ansible/module_utils/network/common/utils.py +++ b/lib/ansible/module_utils/network/common/utils.py @@ -260,7 +260,10 @@ def dict_diff(base, comparable): if not isinstance(base, dict): raise AssertionError("`base` must be of type ") if not isinstance(comparable, dict): - raise AssertionError("`comparable` must be of type ") + if not comparable: + comparable = dict() + else: + raise AssertionError("`comparable` must be of type ") updates = dict() diff --git a/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py b/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py index 8d4f24585a9f08..156b754271fc51 100644 --- a/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py +++ b/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py @@ -133,6 +133,9 @@ def _state_replaced(self, want, have): if merged_commands: commands.extend(deleted_commands) commands.extend(merged_commands) + else: + commands.extend(deleted_commands) + return commands def _state_merged(self, want, have): diff --git a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml index d3711a55f505ac..0c9efaf8c9beee 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml @@ -6,11 +6,19 @@ nxos_feature: feature: lacp +- set_fact: + mac: "lacp system-mac 00c1.4c00.bd15 role primary" + when: platform is search('N9K') + - block: - name: Setup cli_config: config: lacp system-priority 11 + - name: Setup + cli_config: + config: "{{ mac|default(omit) }}" + - name: Gather lacp facts nxos_facts: &facts gather_subset: @@ -23,12 +31,25 @@ state: deleted register: result + - debug: + msg: "{{ result }}" + - assert: that: - "ansible_facts.network_resources.lacp == result.before" - "'no lacp system-priority' in result.commands" - "result.changed == true" - "result.commands|length == 1" + when: platform is not search('N9K') + + - assert: + that: + - "ansible_facts.network_resources.lacp == result.before" + - "'no lacp system-priority' in result.commands" + - "'no lacp system-mac' in result.commands" + - "result.changed == true" + - "result.commands|length == 2" + when: platform is search('N9K') - name: Gather lacp post facts nxos_facts: *facts diff --git a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml index 0e7423b8db965c..507cbb90497c21 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml @@ -6,21 +6,38 @@ nxos_feature: feature: lacp +- set_fact: + mac: + address: 00c1.4c00.bd15 + role: primary + when: platform is search('N9K') + - block: - name: Merged nxos_lacp: &merged config: system: priority: 11 + mac: "{{ mac|default(omit) }}" state: merged register: result + - assert: + that: + - "result.before|length == 0" + - "result.changed == true" + - "'lacp system-priority 11' in result.commands" + - "'lacp system-mac 00c1.4c00.bd15 role primary' in result.commands" + - "result.commands|length == 2" + when: platform is search('N9K') + - assert: that: - "result.before|length == 0" - "result.changed == true" - "'lacp system-priority 11' in result.commands" - "result.commands|length == 1" + when: platform is not search('N9K') - name: Gather lacp facts nxos_facts: diff --git a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml index 1e9470fec7fe9b..f8d8e566121392 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml @@ -1,4 +1,5 @@ --- + - debug: msg: "Start nxos_lacp replaced integration tests connection={{ ansible_connection }}" @@ -6,6 +7,12 @@ nxos_feature: feature: lacp +- set_fact: + mac: + address: 00c1.4c00.bd15 + role: primary + when: platform is search('N9K') + - block: - name: Setup cli_config: @@ -22,12 +29,18 @@ nxos_lacp: &replaced config: system: - mac: - address: 00c1.4c00.bd15 - role: primary + mac: "{{ mac|default(omit) }}" state: replaced register: result + - assert: + that: + - "ansible_facts.network_resources.lacp == result.before" + - "result.changed == true" + - "result.commands|length == 1" + - "'no lacp system-priority' in result.commands" + when: platform is not search('N9K') + - assert: that: - "ansible_facts.network_resources.lacp == result.before" @@ -35,6 +48,7 @@ - "'no lacp system-priority' in result.commands" - "'lacp system-mac 00c1.4c00.bd15 role primary' in result.commands" - "result.commands|length == 2" + when: platform is search('N9K') - name: Gather lacp interfaces post facts nxos_facts: *facts @@ -42,6 +56,7 @@ - assert: that: - "ansible_facts.network_resources.lacp == result.after" + when: platform is match('N9K') - name: Idempotence - Replaced nxos_lacp: *replaced diff --git a/test/integration/targets/prepare_nxos_tests/tasks/main.yml b/test/integration/targets/prepare_nxos_tests/tasks/main.yml index ff91587187019d..05c253009aad0d 100644 --- a/test/integration/targets/prepare_nxos_tests/tasks/main.yml +++ b/test/integration/targets/prepare_nxos_tests/tasks/main.yml @@ -31,7 +31,6 @@ - name: "Collect interface list" nxos_command: commands: ['show interface brief | json'] - timeout: 60 connection: network_cli register: intout From a2d55443a0e8c53bbf110a177860fb23ea652641 Mon Sep 17 00:00:00 2001 From: Shrishail Kariyappanavar Date: Wed, 30 Oct 2019 23:18:23 -0400 Subject: [PATCH 2/3] nxos_lacp: addressing comments --- .../module_utils/network/common/utils.py | 2 +- .../network/nxos/config/lacp/lacp.py | 5 +- .../targets/nxos_lacp/tests/cli/deleted.yaml | 7 +-- .../targets/nxos_lacp/tests/cli/merged.yaml | 4 +- .../targets/nxos_lacp/tests/cli/replaced.yaml | 50 +++++++++++++++---- 5 files changed, 47 insertions(+), 21 deletions(-) diff --git a/lib/ansible/module_utils/network/common/utils.py b/lib/ansible/module_utils/network/common/utils.py index ea8a56cffae3de..299608cc8a655b 100644 --- a/lib/ansible/module_utils/network/common/utils.py +++ b/lib/ansible/module_utils/network/common/utils.py @@ -260,7 +260,7 @@ def dict_diff(base, comparable): if not isinstance(base, dict): raise AssertionError("`base` must be of type ") if not isinstance(comparable, dict): - if not comparable: + if comparable is None: comparable = dict() else: raise AssertionError("`comparable` must be of type ") diff --git a/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py b/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py index 156b754271fc51..ebed2501845df0 100644 --- a/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py +++ b/lib/ansible/module_utils/network/nxos/config/lacp/lacp.py @@ -130,11 +130,10 @@ def _state_replaced(self, want, have): del diff[k] deleted_commands = self.del_all(diff) merged_commands = self._state_merged(want, have) + + commands.extend(deleted_commands) if merged_commands: - commands.extend(deleted_commands) commands.extend(merged_commands) - else: - commands.extend(deleted_commands) return commands diff --git a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml index 0c9efaf8c9beee..bc06a7734f9309 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml @@ -8,7 +8,7 @@ - set_fact: mac: "lacp system-mac 00c1.4c00.bd15 role primary" - when: platform is search('N9K') + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - block: - name: Setup @@ -31,9 +31,6 @@ state: deleted register: result - - debug: - msg: "{{ result }}" - - assert: that: - "ansible_facts.network_resources.lacp == result.before" @@ -49,7 +46,7 @@ - "'no lacp system-mac' in result.commands" - "result.changed == true" - "result.commands|length == 2" - when: platform is search('N9K') + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - name: Gather lacp post facts nxos_facts: *facts diff --git a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml index 507cbb90497c21..ff6fbd89a77e73 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml @@ -10,7 +10,7 @@ mac: address: 00c1.4c00.bd15 role: primary - when: platform is search('N9K') + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - block: - name: Merged @@ -29,7 +29,7 @@ - "'lacp system-priority 11' in result.commands" - "'lacp system-mac 00c1.4c00.bd15 role primary' in result.commands" - "result.commands|length == 2" - when: platform is search('N9K') + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - assert: that: diff --git a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml index f8d8e566121392..23dcd566aa2e7a 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml @@ -8,16 +8,24 @@ feature: lacp - set_fact: - mac: + mac1: "lacp system-mac 00c1.4c00.bd20 role primary" + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + +- set_fact: + mac2: address: 00c1.4c00.bd15 - role: primary - when: platform is search('N9K') + role: secondary + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - block: - - name: Setup + - name: Setup1 cli_config: config: lacp system-priority 11 + - name: Setup2 + cli_config: + config: "{{ mac1|default(omit) }}" + - name: Gather lacp facts nxos_facts: &facts gather_subset: @@ -29,7 +37,8 @@ nxos_lacp: &replaced config: system: - mac: "{{ mac|default(omit) }}" + priority: 12 + mac: "{{ mac2|default(omit) }}" state: replaced register: result @@ -37,8 +46,9 @@ that: - "ansible_facts.network_resources.lacp == result.before" - "result.changed == true" - - "result.commands|length == 1" + - "result.commands|length == 2" - "'no lacp system-priority' in result.commands" + - "'lacp system-priority 12' in result.commands" when: platform is not search('N9K') - assert: @@ -46,9 +56,11 @@ - "ansible_facts.network_resources.lacp == result.before" - "result.changed == true" - "'no lacp system-priority' in result.commands" - - "'lacp system-mac 00c1.4c00.bd15 role primary' in result.commands" - - "result.commands|length == 2" - when: platform is search('N9K') + - "'no lacp system-mac' in result.commands" + - "'lacp system-priority 12' in result.commands" + - "'lacp system-mac 00c1.4c00.bd15 role secondary' in result.commands" + - "result.commands|length == 4" + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - name: Gather lacp interfaces post facts nxos_facts: *facts @@ -56,7 +68,7 @@ - assert: that: - "ansible_facts.network_resources.lacp == result.after" - when: platform is match('N9K') + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') - name: Idempotence - Replaced nxos_lacp: *replaced @@ -67,6 +79,24 @@ - "result.changed == false" - "result.commands|length == 0" + - name: Setup3 + cli_config: + config: "{{ mac1|default(omit) }}" + + - name: Replaced + nxos_lacp: + state: replaced + register: result + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + + - assert: + that: + - "result.changed == true" + - "result.commands|length == 2" + - "'no lacp system-mac' in result.commands" + - "'no lacp system-priority' in result.commands" + when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + always: - name: teardown nxos_feature: From 3d4cabea57989c7a5b3ca49ab422bf4d5b969d47 Mon Sep 17 00:00:00 2001 From: Shrishail Kariyappanavar Date: Thu, 31 Oct 2019 10:14:26 -0400 Subject: [PATCH 3/3] nxos_lacp: Updating image tag search to include more tags --- .../targets/nxos_lacp/tests/cli/deleted.yaml | 4 ++-- .../targets/nxos_lacp/tests/cli/merged.yaml | 4 ++-- .../targets/nxos_lacp/tests/cli/replaced.yaml | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml index bc06a7734f9309..a9e158e69d358f 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/deleted.yaml @@ -8,7 +8,7 @@ - set_fact: mac: "lacp system-mac 00c1.4c00.bd15 role primary" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - block: - name: Setup @@ -46,7 +46,7 @@ - "'no lacp system-mac' in result.commands" - "result.changed == true" - "result.commands|length == 2" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - name: Gather lacp post facts nxos_facts: *facts diff --git a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml index ff6fbd89a77e73..45a5bed1db4a03 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/merged.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/merged.yaml @@ -10,7 +10,7 @@ mac: address: 00c1.4c00.bd15 role: primary - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - block: - name: Merged @@ -29,7 +29,7 @@ - "'lacp system-priority 11' in result.commands" - "'lacp system-mac 00c1.4c00.bd15 role primary' in result.commands" - "result.commands|length == 2" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - assert: that: diff --git a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml index 23dcd566aa2e7a..7b367a79580431 100644 --- a/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml +++ b/test/integration/targets/nxos_lacp/tests/cli/replaced.yaml @@ -9,13 +9,13 @@ - set_fact: mac1: "lacp system-mac 00c1.4c00.bd20 role primary" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - set_fact: mac2: address: 00c1.4c00.bd15 role: secondary - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - block: - name: Setup1 @@ -60,7 +60,7 @@ - "'lacp system-priority 12' in result.commands" - "'lacp system-mac 00c1.4c00.bd15 role secondary' in result.commands" - "result.commands|length == 4" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - name: Gather lacp interfaces post facts nxos_facts: *facts @@ -68,7 +68,7 @@ - assert: that: - "ansible_facts.network_resources.lacp == result.after" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - name: Idempotence - Replaced nxos_lacp: *replaced @@ -87,7 +87,7 @@ nxos_lacp: state: replaced register: result - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') - assert: that: @@ -95,7 +95,7 @@ - "result.commands|length == 2" - "'no lacp system-mac' in result.commands" - "'no lacp system-priority' in result.commands" - when: platform is search('N9K') and imagetag is not search('I2|I4|I5|I6') + when: platform is search('N9K') and imagetag is not search('I[2-6]') always: - name: teardown