Skip to content

Commit

Permalink
Fix snmp bugs on Nexus 3500 platform (ansible#32773)
Browse files Browse the repository at this point in the history
* Add n35 platform support

* Fix regex bug and add snmp_location it tests

* Enable nxos_snmp_location tests
  • Loading branch information
mikewiebe authored and trishnaguha committed Nov 13, 2017
1 parent d970801 commit de8d00b
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/ansible/modules/network/nxos/nxos_snmp_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -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>.+)$'
location_regex = r'^\s*snmp-server\s+location\s+(?P<location>.+)$'

body = execute_show_command('show run snmp', module)[0]
match_location = re.search(location_regex, body, re.M)
Expand Down
30 changes: 24 additions & 6 deletions lib/ansible/modules/network/nxos/nxos_snmp_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/integration/nxos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
testcase: "*"
2 changes: 2 additions & 0 deletions test/integration/targets/nxos_snmp_location/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- prepare_nxos_tests
15 changes: 15 additions & 0 deletions test/integration/targets/nxos_snmp_location/tasks/cli.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/integration/targets/nxos_snmp_location/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -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'] }
28 changes: 28 additions & 0 deletions test/integration/targets/nxos_snmp_location/tasks/nxapi.yaml
Original file line number Diff line number Diff line change
@@ -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 }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- set_fact: connection="{{ cli }}"

- import_tasks: "{{ role_path }}/tests/common/sanity.yaml"
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- set_fact: connection="{{ nxapi }}"

- import_tasks: "{{ role_path }}/tests/common/sanity.yaml"

0 comments on commit de8d00b

Please sign in to comment.