From de8d00b401d0073dfa60be2a71b9972ebc5c1554 Mon Sep 17 00:00:00 2001 From: Mike Wiebe Date: Mon, 13 Nov 2017 03:03:06 -0500 Subject: [PATCH] Fix snmp bugs on Nexus 3500 platform (#32773) * Add n35 platform support * Fix regex bug and add snmp_location it tests * Enable nxos_snmp_location tests --- .../network/nxos/nxos_snmp_location.py | 2 +- .../modules/network/nxos/nxos_snmp_user.py | 30 +++++++++--- test/integration/nxos.yaml | 10 ++++ .../nxos_snmp_location/defaults/main.yaml | 2 + .../targets/nxos_snmp_location/meta/main.yml | 2 + .../targets/nxos_snmp_location/tasks/cli.yaml | 15 ++++++ .../nxos_snmp_location/tasks/main.yaml | 7 +++ .../nxos_snmp_location/tasks/nxapi.yaml | 28 +++++++++++ .../nxos_snmp_location/tests/cli/sanity.yaml | 4 ++ .../tests/common/sanity.yaml | 47 +++++++++++++++++++ .../tests/nxapi/sanity.yaml | 4 ++ 11 files changed, 144 insertions(+), 7 deletions(-) create mode 100644 test/integration/targets/nxos_snmp_location/defaults/main.yaml create mode 100644 test/integration/targets/nxos_snmp_location/meta/main.yml create mode 100644 test/integration/targets/nxos_snmp_location/tasks/cli.yaml create mode 100644 test/integration/targets/nxos_snmp_location/tasks/main.yaml create mode 100644 test/integration/targets/nxos_snmp_location/tasks/nxapi.yaml create mode 100644 test/integration/targets/nxos_snmp_location/tests/cli/sanity.yaml create mode 100644 test/integration/targets/nxos_snmp_location/tests/common/sanity.yaml create mode 100644 test/integration/targets/nxos_snmp_location/tests/nxapi/sanity.yaml diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_location.py b/lib/ansible/modules/network/nxos/nxos_snmp_location.py index 52604148e0a..ca23d8cb019 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_location.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_location.py @@ -96,7 +96,7 @@ def flatten_list(command_lists): def get_snmp_location(module): location = {} - location_regex = r'^\s*snmp-server\slocation\s(?P.+)$' + location_regex = r'^\s*snmp-server\s+location\s+(?P.+)$' body = execute_show_command('show run snmp', module)[0] match_location = re.search(location_regex, body, re.M) diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_user.py b/lib/ansible/modules/network/nxos/nxos_snmp_user.py index 71b76bafbfd..a748d4b6074 100644 --- a/lib/ansible/modules/network/nxos/nxos_snmp_user.py +++ b/lib/ansible/modules/network/nxos/nxos_snmp_user.py @@ -139,23 +139,41 @@ def get_snmp_user(user, module): resource = {} try: - resource_table = body[0]['TABLE_snmp_users']['ROW_snmp_users'] + # The TABLE and ROW keys differ between NXOS platforms. + if body[0].get('TABLE_snmp_user'): + tablekey = 'TABLE_snmp_user' + rowkey = 'ROW_snmp_user' + tablegrpkey = 'TABLE_snmp_group_names' + rowgrpkey = 'ROW_snmp_group_names' + authkey = 'auth_protocol' + privkey = 'priv_protocol' + grpkey = 'group_names' + elif body[0].get('TABLE_snmp_users'): + tablekey = 'TABLE_snmp_users' + rowkey = 'ROW_snmp_users' + tablegrpkey = 'TABLE_groups' + rowgrpkey = 'ROW_groups' + authkey = 'auth' + privkey = 'priv' + grpkey = 'group' + + resource_table = body[0][tablekey][rowkey] resource['user'] = str(resource_table['user']) - resource['authentication'] = str(resource_table['auth']).strip() - encrypt = str(resource_table['priv']).strip() + resource['authentication'] = str(resource_table[authkey]).strip() + encrypt = str(resource_table[privkey]).strip() if encrypt.startswith('aes'): resource['encrypt'] = 'aes-128' else: resource['encrypt'] = 'none' - group_table = resource_table['TABLE_groups']['ROW_groups'] + group_table = resource_table[tablegrpkey][rowgrpkey] groups = [] try: for group in group_table: - groups.append(str(group['group']).strip()) + groups.append(str(group[grpkey]).strip()) except TypeError: - groups.append(str(group_table['group']).strip()) + groups.append(str(group_table[grpkey]).strip()) resource['group'] = groups diff --git a/test/integration/nxos.yaml b/test/integration/nxos.yaml index d77e50059e2..d69ef3dfb6d 100644 --- a/test/integration/nxos.yaml +++ b/test/integration/nxos.yaml @@ -472,6 +472,16 @@ - set_fact: failed_modules: "{{ failed_modules }} + [ 'nxos_snmp_community' ]" test_failed: true + + - block: + - include_role: + name: nxos_snmp_location + when: "limit_to in ['*', 'nxos_snmp_location']" + rescue: + - set_fact: + failed_modules: "{{ failed_modules }} + [ 'nxos_snmp_location' ]" + test_failed: true + ########### - debug: var=failed_modules when: test_failed diff --git a/test/integration/targets/nxos_snmp_location/defaults/main.yaml b/test/integration/targets/nxos_snmp_location/defaults/main.yaml new file mode 100644 index 00000000000..5f709c5aac1 --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +testcase: "*" diff --git a/test/integration/targets/nxos_snmp_location/meta/main.yml b/test/integration/targets/nxos_snmp_location/meta/main.yml new file mode 100644 index 00000000000..ae741cbdc71 --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nxos_tests diff --git a/test/integration/targets/nxos_snmp_location/tasks/cli.yaml b/test/integration/targets/nxos_snmp_location/tasks/cli.yaml new file mode 100644 index 00000000000..d675462dd02 --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/tasks/cli.yaml @@ -0,0 +1,15 @@ +--- +- name: collect all cli test cases + find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_snmp_location/tasks/main.yaml b/test/integration/targets/nxos_snmp_location/tasks/main.yaml new file mode 100644 index 00000000000..fea9337c14c --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/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_location/tasks/nxapi.yaml b/test/integration/targets/nxos_snmp_location/tasks/nxapi.yaml new file mode 100644 index 00000000000..ea525379f7f --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/tasks/nxapi.yaml @@ -0,0 +1,28 @@ +--- +- name: collect all nxapi test cases + find: + paths: "{{ role_path }}/tests/nxapi" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: enable nxapi + nxos_config: + lines: + - feature nxapi + - nxapi http port 80 + provider: "{{ cli }}" + +- name: run test case + include: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run + +- name: disable nxapi + nxos_config: + lines: + - no feature nxapi + provider: "{{ cli }}" diff --git a/test/integration/targets/nxos_snmp_location/tests/cli/sanity.yaml b/test/integration/targets/nxos_snmp_location/tests/cli/sanity.yaml new file mode 100644 index 00000000000..420af7420e9 --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/tests/cli/sanity.yaml @@ -0,0 +1,4 @@ +--- +- set_fact: connection="{{ cli }}" + +- import_tasks: "{{ role_path }}/tests/common/sanity.yaml" diff --git a/test/integration/targets/nxos_snmp_location/tests/common/sanity.yaml b/test/integration/targets/nxos_snmp_location/tests/common/sanity.yaml new file mode 100644 index 00000000000..7c6e4405a7c --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/tests/common/sanity.yaml @@ -0,0 +1,47 @@ +--- +- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_location sanity test" + +- name: Setup - Remove snmp_location if configured + nxos_snmp_location: &remove + location: Test + state: absent + timeout: 60 + provider: "{{ connection }}" + ignore_errors: yes + +- block: + + - name: Configure snmp host + nxos_snmp_location: &config + location: Test + state: present + timeout: 60 + provider: "{{ connection }}" + register: result + + - assert: &true + that: + - "result.changed == true" + + - name: Idempotence Check + nxos_snmp_location: *config + register: result + + - assert: &false + that: + - "result.changed == false" + + always: + - name: Cleanup + nxos_snmp_location: *remove + register: result + + - assert: *true + + - name: Cleanup Idempotence + nxos_snmp_location: *remove + register: result + + - assert: *false + + - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_location sanity test" diff --git a/test/integration/targets/nxos_snmp_location/tests/nxapi/sanity.yaml b/test/integration/targets/nxos_snmp_location/tests/nxapi/sanity.yaml new file mode 100644 index 00000000000..e30ea6eaf70 --- /dev/null +++ b/test/integration/targets/nxos_snmp_location/tests/nxapi/sanity.yaml @@ -0,0 +1,4 @@ +--- +- set_fact: connection="{{ nxapi }}" + +- import_tasks: "{{ role_path }}/tests/common/sanity.yaml"