summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Wiebe <mwiebe@cisco.com>2017-09-15 10:45:51 -0400
committerToshio Kuratomi <a.badger@gmail.com>2017-09-15 07:52:44 -0700
commitc73e1b5e68e4a9f07c4b9a206f6816b226bdc761 (patch)
treef67dccc0b54a2c9941e14039485bc3ccfe6f755e
parent91e1a5fe160c075d7d965e304ab35ff63412ab49 (diff)
downloadansible-c73e1b5e68e4a9f07c4b9a206f6816b226bdc761.tar.gz
Fix nxos_snmp_community idempotence issue (#30388)
* Fix nxos_snmp_community idempotence issue * Use passed in name to filter * Test updates and remove unused method (cherry picked from commit 9af6dc4751be1b339613aca85169c32418435a5d)
-rw-r--r--lib/ansible/modules/network/nxos/nxos_snmp_community.py75
-rw-r--r--test/integration/nxos.yaml6
-rw-r--r--test/integration/targets/nxos_snmp_community/defaults/main.yaml2
-rw-r--r--test/integration/targets/nxos_snmp_community/meta/main.yml2
-rw-r--r--test/integration/targets/nxos_snmp_community/tasks/cli.yaml15
-rw-r--r--test/integration/targets/nxos_snmp_community/tasks/main.yaml7
-rw-r--r--test/integration/targets/nxos_snmp_community/tasks/nxapi.yaml28
-rw-r--r--test/integration/targets/nxos_snmp_community/tests/cli/sanity.yaml4
-rw-r--r--test/integration/targets/nxos_snmp_community/tests/common/sanity.yaml96
-rw-r--r--test/integration/targets/nxos_snmp_community/tests/nxapi/sanity.yaml4
10 files changed, 196 insertions, 43 deletions
diff --git a/lib/ansible/modules/network/nxos/nxos_snmp_community.py b/lib/ansible/modules/network/nxos/nxos_snmp_community.py
index e1718a9d20..341fb8aa34 100644
--- a/lib/ansible/modules/network/nxos/nxos_snmp_community.py
+++ b/lib/ansible/modules/network/nxos/nxos_snmp_community.py
@@ -79,31 +79,24 @@ commands:
sample: ["snmp-server community TESTING7 group network-operator"]
'''
-
+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
def execute_show_command(command, module):
- command = {
+ if 'show run' not in command:
+ output = 'json'
+ else:
+ output = 'text'
+ cmds = [{
'command': command,
- 'output': 'json',
- }
+ 'output': output,
+ }]
- return run_commands(module, command)
-
-
-def apply_key_map(key_map, table):
- new_dict = {}
- for key, value in table.items():
- new_key = key_map.get(key)
- if new_key:
- if value:
- new_dict[new_key] = str(value)
- else:
- new_dict[new_key] = value
- return new_dict
+ body = run_commands(module, cmds)
+ return body
def flatten_list(command_lists):
@@ -130,38 +123,34 @@ def get_snmp_groups(module):
return group_list
-def get_snmp_community(module, find_filter=None):
- data = execute_show_command('show snmp community', module)[0]
-
+def get_snmp_community(module, name):
+ command = 'show run snmp all | grep {0}'.format(name)
+ data = execute_show_command(command, module)[0]
community_dict = {}
- community_map = {
- 'grouporaccess': 'group',
- 'aclfilter': 'acl'
- }
+ if not data:
+ return community_dict
- try:
- community_table = data['TABLE_snmp_community']['ROW_snmp_community']
- for each in community_table:
- community = apply_key_map(community_map, each)
- key = each['community_name']
- community_dict[key] = community
- except (KeyError, AttributeError, TypeError):
+ community_re = r'snmp-server community (\S+)'
+ mo = re.search(community_re, data)
+ if mo:
+ community_name = mo.group(1)
+ else:
return community_dict
- if find_filter:
- find = community_dict.get(find_filter, None)
+ community_dict['group'] = None
+ group_re = r'snmp-server community {0} group (\S+)'.format(community_name)
+ mo = re.search(group_re, data)
+ if mo:
+ community_dict['group'] = mo.group(1)
- if find_filter is None or find is None:
- return {}
- else:
- fix_find = {}
- for (key, value) in find.items():
- if isinstance(value, str):
- fix_find[key] = value.strip()
- else:
- fix_find[key] = value
- return fix_find
+ community_dict['acl'] = None
+ acl_re = r'snmp-server community {0} use-acl (\S+)'.format(community_name)
+ mo = re.search(acl_re, data)
+ if mo:
+ community_dict['acl'] = mo.group(1)
+
+ return community_dict
def config_snmp_community(delta, community):
diff --git a/test/integration/nxos.yaml b/test/integration/nxos.yaml
index fd76f87cdc..4d8eca4889 100644
--- a/test/integration/nxos.yaml
+++ b/test/integration/nxos.yaml
@@ -360,6 +360,12 @@
rescue:
- set_fact: test_failed=true
+ - block:
+ - include_role:
+ name: nxos_snmp_community
+ when: "limit_to in ['*', 'nxos_snmp_community']"
+ rescue:
+ - set_fact: test_failed=true
###########
- debug: var=failed_modules
when: test_failed
diff --git a/test/integration/targets/nxos_snmp_community/defaults/main.yaml b/test/integration/targets/nxos_snmp_community/defaults/main.yaml
new file mode 100644
index 0000000000..5f709c5aac
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/defaults/main.yaml
@@ -0,0 +1,2 @@
+---
+testcase: "*"
diff --git a/test/integration/targets/nxos_snmp_community/meta/main.yml b/test/integration/targets/nxos_snmp_community/meta/main.yml
new file mode 100644
index 0000000000..ae741cbdc7
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/meta/main.yml
@@ -0,0 +1,2 @@
+dependencies:
+ - prepare_nxos_tests
diff --git a/test/integration/targets/nxos_snmp_community/tasks/cli.yaml b/test/integration/targets/nxos_snmp_community/tasks/cli.yaml
new file mode 100644
index 0000000000..d675462dd0
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/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_community/tasks/main.yaml b/test/integration/targets/nxos_snmp_community/tasks/main.yaml
new file mode 100644
index 0000000000..fea9337c14
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/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_community/tasks/nxapi.yaml b/test/integration/targets/nxos_snmp_community/tasks/nxapi.yaml
new file mode 100644
index 0000000000..ea525379f7
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/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_community/tests/cli/sanity.yaml b/test/integration/targets/nxos_snmp_community/tests/cli/sanity.yaml
new file mode 100644
index 0000000000..420af7420e
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/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_community/tests/common/sanity.yaml b/test/integration/targets/nxos_snmp_community/tests/common/sanity.yaml
new file mode 100644
index 0000000000..7b01029789
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/tests/common/sanity.yaml
@@ -0,0 +1,96 @@
+---
+- debug: msg="START TRANSPORT:{{ connection.transport }} nxos_snmp_community sanity test"
+
+- name: Setup - Remove snmp_community if configured
+ nxos_snmp_community: &remove
+ community: TESTING7
+ group: network-operator
+ state: absent
+ provider: "{{ connection }}"
+ ignore_errors: yes
+
+- block:
+
+ - name: Configure snmp_community group
+ nxos_snmp_community: &config
+ community: TESTING7
+ group: network-operator
+ #access: ro
+ state: present
+ provider: "{{ connection }}"
+ register: result
+
+ - assert: &true
+ that:
+ - "result.changed == true"
+
+ - name: Idempotence Check
+ nxos_snmp_community: *config
+ register: result
+
+ - assert: &false
+ that:
+ - "result.changed == false"
+
+ - name: Remove snmp_community
+ nxos_snmp_community: *remove
+ register: result
+
+ - assert: *true
+
+ - name: Idempotence Check
+ nxos_snmp_community: *remove
+ register: result
+
+ - assert: *false
+
+ - name: Configure snmp_community access read-only
+ nxos_snmp_community: &configaccess
+ community: TESTING7
+ access: ro
+ state: present
+ provider: "{{ connection }}"
+ register: result
+
+ - assert: *true
+
+ - name: Idempotence Check
+ nxos_snmp_community: *configaccess
+ register: result
+
+ - assert: *false
+
+ - name: Remove snmp_community
+ nxos_snmp_community: *remove
+ register: result
+
+ - assert: *true
+
+ - name: Idempotence Check
+ nxos_snmp_community: *remove
+ register: result
+
+ - assert: *false
+
+ - name: Configure snmp_community access read-write
+ nxos_snmp_community: &configaccessrw
+ community: TESTING7
+ access: rw
+ acl: ansible_acl
+ state: present
+ provider: "{{ connection }}"
+ register: result
+
+ - assert: *true
+
+ - name: Idempotence Check
+ nxos_snmp_community: *configaccessrw
+ register: result
+
+ - assert: *false
+
+ always:
+ - name: Cleanup
+ nxos_snmp_community: *remove
+
+ - debug: msg="END TRANSPORT:{{ connection.transport }} nxos_snmp_community sanity test"
diff --git a/test/integration/targets/nxos_snmp_community/tests/nxapi/sanity.yaml b/test/integration/targets/nxos_snmp_community/tests/nxapi/sanity.yaml
new file mode 100644
index 0000000000..e30ea6eaf7
--- /dev/null
+++ b/test/integration/targets/nxos_snmp_community/tests/nxapi/sanity.yaml
@@ -0,0 +1,4 @@
+---
+- set_fact: connection="{{ nxapi }}"
+
+- import_tasks: "{{ role_path }}/tests/common/sanity.yaml"