From 208208ab8fd732ebe5ab5cb4dd369c4d84eee913 Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Thu, 16 Nov 2017 01:17:42 -0500 Subject: [PATCH] Fix nxos_snmp_host bug (#32916) * Fix nxos_snmp_host bug * Enable nxos_snmp_host tests --- .../modules/network/nxos/nxos_snmp_host.py | 9 ++- test/integration/nxos.yaml | 9 +++ .../targets/nxos_snmp_host/defaults/main.yaml | 2 + .../targets/nxos_snmp_host/meta/main.yml | 2 + .../targets/nxos_snmp_host/tasks/cli.yaml | 25 +++++++ .../targets/nxos_snmp_host/tasks/main.yaml | 7 ++ .../targets/nxos_snmp_host/tasks/nxapi.yaml | 25 +++++++ .../tests/common/sanity_snmp_v2_trap.yaml | 64 +++++++++++++++++ .../tests/common/sanity_snmp_v3_inform.yaml | 69 +++++++++++++++++++ .../tests/common/sanity_snmp_v3_trap.yaml | 61 ++++++++++++++++ 10 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 test/integration/targets/nxos_snmp_host/defaults/main.yaml create mode 100644 test/integration/targets/nxos_snmp_host/meta/main.yml create mode 100644 test/integration/targets/nxos_snmp_host/tasks/cli.yaml create mode 100644 test/integration/targets/nxos_snmp_host/tasks/main.yaml create mode 100644 test/integration/targets/nxos_snmp_host/tasks/nxapi.yaml create mode 100644 test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v2_trap.yaml create mode 100644 test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_inform.yaml create mode 100644 test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_trap.yaml diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_host.py b/lib/ansible/modules/network/nxos/nxos_snmp_host.py index 3e1e412efd..9e6f3cc8f8 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_host.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_host.py @@ -102,6 +102,7 @@ commands: ''' +import re from ansible.module_utils.nxos import load_config, run_commands from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule @@ -173,7 +174,9 @@ def get_snmp_host(host, module): host_resource = apply_key_map(host_map, each) if src: - host_resource['src_intf'] = src.split(':')[1].strip() + host_resource['src_intf'] = src + if re.search(r'interface:', src): + host_resource['src_intf'] = src.split(':')[1].strip() vrf_filt = each.get('TABLE_vrf_filters') if vrf_filt: @@ -199,7 +202,9 @@ def get_snmp_host(host, module): host_resource = apply_key_map(host_map_5k, each) if src: - host_resource['src_intf'] = src.split(':')[1].strip() + host_resource['src_intf'] = src + if re.search(r'interface:', src): + host_resource['src_intf'] = src.split(':')[1].strip() vrf_filt = each.get('TABLE_filter_vrf') if vrf_filt: diff --git a/test/integration/nxos.yaml b/test/integration/nxos.yaml index d69ef3dfb6..7586c642f6 100644 --- a/test/integration/nxos.yaml +++ b/test/integration/nxos.yaml @@ -482,6 +482,15 @@ failed_modules: "{{ failed_modules }} + [ 'nxos_snmp_location' ]" test_failed: true + - block: + - include_role: + name: nxos_snmp_host + when: "limit_to in ['*', 'nxos_snmp_host']" + rescue: + - set_fact: + failed_modules: "{{ failed_modules }} + [ 'nxos_snmp_host' ]" + test_failed: true + ########### - debug: var=failed_modules when: test_failed diff --git a/test/integration/targets/nxos_snmp_host/defaults/main.yaml b/test/integration/targets/nxos_snmp_host/defaults/main.yaml new file mode 100644 index 0000000000..5f709c5aac --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +testcase: "*" diff --git a/test/integration/targets/nxos_snmp_host/meta/main.yml b/test/integration/targets/nxos_snmp_host/meta/main.yml new file mode 100644 index 0000000000..ae741cbdc7 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nxos_tests diff --git a/test/integration/targets/nxos_snmp_host/tasks/cli.yaml b/test/integration/targets/nxos_snmp_host/tasks/cli.yaml new file mode 100644 index 0000000000..0ab3f8f908 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tasks/cli.yaml @@ -0,0 +1,25 @@ +--- +- name: collect common cli test cases + find: + paths: "{{ role_path }}/tests/common" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: collect cli test cases + find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + register: cli_cases + +- set_fact: + test_cases: + files: "{{ test_cases.files }} + {{ cli_cases.files }}" + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }} connection={{ cli }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_snmp_host/tasks/main.yaml b/test/integration/targets/nxos_snmp_host/tasks/main.yaml new file mode 100644 index 0000000000..fea9337c14 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tasks/main.yaml @@ -0,0 +1,7 @@ +--- +# Use block to ensure that both cli and nxapi tests +# will run even if there are failures or errors. +- block: + - { include: cli.yaml, tags: ['cli'] } + always: + - { include: nxapi.yaml, tags: ['nxapi'] } diff --git a/test/integration/targets/nxos_snmp_host/tasks/nxapi.yaml b/test/integration/targets/nxos_snmp_host/tasks/nxapi.yaml new file mode 100644 index 0000000000..378db2f016 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tasks/nxapi.yaml @@ -0,0 +1,25 @@ +--- +- name: collect common nxapi test cases + find: + paths: "{{ role_path }}/tests/common" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: collect nxapi test cases + find: + paths: "{{ role_path }}/tests/nxapi" + patterns: "{{ testcase }}.yaml" + register: nxapi_cases + +- set_fact: + test_cases: + files: "{{ test_cases.files }} + {{ nxapi_cases.files }}" + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }} connection={{ nxapi }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v2_trap.yaml b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v2_trap.yaml new file mode 100644 index 0000000000..845fc86552 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v2_trap.yaml @@ -0,0 +1,64 @@ +--- +- set_fact: snmp_type="trap" +- set_fact: snmp_version="v2c" + +- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test" + +# Select interface for test +- set_fact: intname="{{ nxos_int1 }}" + when: not (platform | match("N5K")) + +- name: Setup - Remove snmp_host if configured + nxos_snmp_host: &remove + snmp_host: 3.3.3.3 + community: TESTING + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + src_intf: "{{ intname|default(omit) }}" + state: absent + provider: "{{ connection }}" + ignore_errors: yes + +- block: + + - name: Configure snmp host + nxos_snmp_host: &config + snmp_host: 3.3.3.3 + community: TESTING + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + src_intf: "{{ intname|default(omit) }}" + state: present + provider: "{{ connection }}" + register: result + + - assert: &true + that: + - "result.changed == true" + + - name: Idempotence Check + nxos_snmp_host: *config + register: result + + - assert: &false + that: + - "result.changed == false" + + always: + - name: Cleanup + nxos_snmp_host: *remove + register: result + + - assert: *true + + - name: Cleanup Idempotence + nxos_snmp_host: *remove + register: result + + - assert: *false + + - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test" diff --git a/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_inform.yaml b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_inform.yaml new file mode 100644 index 0000000000..ea6276e4fc --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_inform.yaml @@ -0,0 +1,69 @@ +--- +- set_fact: snmp_type="inform" +- set_fact: snmp_version="v3" +- set_fact: snmp_auth="noauth" + +- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test" + +# Select interface for test +- set_fact: intname="{{ nxos_int1 }}" + when: not (platform | match("N5K")) + +- name: Setup - Remove snmp_host if configured + nxos_snmp_host: &remove + snmp_host: 3.3.3.3 + community: TESTING + v3: "{{ snmp_auth|default(omit) }}" + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + src_intf: "{{ intname|default(omit) }}" + state: absent + provider: "{{ connection }}" + ignore_errors: yes + +- block: + + - name: Configure snmp host + nxos_snmp_host: &config + snmp_host: 3.3.3.3 + community: TESTING + v3: "{{ snmp_auth|default(omit) }}" + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + src_intf: "{{ intname|default(omit) }}" + state: present + provider: "{{ connection }}" + register: result + + - assert: &true + that: + - "result.changed == true" + + - name: Idempotence Check + nxos_snmp_host: *config + register: result + + - assert: &false + that: + - "result.changed == false" + + when: not (platform | match('N35')) + + always: + - name: Cleanup + nxos_snmp_host: *remove + register: result + + - assert: *true + + - name: Cleanup Idempotence + nxos_snmp_host: *remove + register: result + + - assert: *false + + - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test" diff --git a/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_trap.yaml b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_trap.yaml new file mode 100644 index 0000000000..d880c0e482 --- /dev/null +++ b/test/integration/targets/nxos_snmp_host/tests/common/sanity_snmp_v3_trap.yaml @@ -0,0 +1,61 @@ +--- +- set_fact: snmp_type="trap" +- set_fact: snmp_version="v3" +- set_fact: snmp_auth="noauth" + +- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test" + +- name: Setup - Remove snmp_host if configured + nxos_snmp_host: &remove + snmp_host: 3.3.3.3 + community: TESTING + v3: "{{ snmp_auth|default(omit) }}" + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + state: absent + provider: "{{ connection }}" + ignore_errors: yes + +- block: + + - name: Configure snmp host + nxos_snmp_host: &config + snmp_host: 3.3.3.3 + community: TESTING + v3: "{{ snmp_auth|default(omit) }}" + version: "{{ snmp_version }}" + snmp_type: "{{ snmp_type }}" + vrf: management + vrf_filter: management + state: present + provider: "{{ connection }}" + register: result + + - assert: &true + that: + - "result.changed == true" + + - name: Idempotence Check + nxos_snmp_host: *config + register: result + + - assert: &false + that: + - "result.changed == false" + + always: + - name: Cleanup + nxos_snmp_host: *remove + register: result + + - assert: *true + + - name: Cleanup Idempotence + nxos_snmp_host: *remove + register: result + + - assert: *false + + - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_host {{ snmp_type }} {{ snmp_version }}sanity test"