Repository: ansible/ansible

Issue:
nxos_snapshot issue
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
nxos_snapshot
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.7.0.dev0 (devel 961484e00d) last updated 2018/05/31 12:44:26 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```
##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Ansible Server : Ubuntu 14.04
Device: N9K running 7.0(3)I7(3)

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->
*  There is a parameter ```save_snapshot_locally``` which saves the snapshot after it is created to the local directory. However, it is right now just saving the name of the file and its size rather than the file contents.
* The integration test cases are not complete and not all parameters have coverage.

Note: I will open a PR shortly to address these issues. 


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_snapshot.py b/lib/ansible/modules/network/nxos/nxos_snapshot.py
--- a/lib/ansible/modules/network/nxos/nxos_snapshot.py
+++ b/lib/ansible/modules/network/nxos/nxos_snapshot.py
@@ -289,12 +289,6 @@ def invoke(name, *args, **kwargs):
         return func(*args, **kwargs)
 
 
-def get_snapshot(module):
-    command = 'show snapshot dump {0}'.format(module.params['snapshot_name'])
-    body = execute_show_command(command, module)[0]
-    return body
-
-
 def write_on_file(content, filename, module):
     path = module.params['path']
     if path[-1] != '/':
@@ -373,10 +367,10 @@ def main():
                 result['changed'] = True
 
             if action == 'create' and module.params['path'] and module.params['save_snapshot_locally']:
-                command = 'show snapshot | include {}'.format(module.params['snapshot_name'])
+                command = 'show snapshot dump {} | json'.format(module.params['snapshot_name'])
                 content = execute_show_command(command, module)[0]
                 if content:
-                    write_on_file(content, module.params['snapshot_name'], module)
+                    write_on_file(str(content), module.params['snapshot_name'], module)
 
     module.exit_json(**result)
 


Test patch:
diff --git a/test/integration/targets/nxos_snapshot/tests/common/sanity.yaml b/test/integration/targets/nxos_snapshot/tests/common/sanity.yaml
--- a/test/integration/targets/nxos_snapshot/tests/common/sanity.yaml
+++ b/test/integration/targets/nxos_snapshot/tests/common/sanity.yaml
@@ -4,19 +4,61 @@
   when: ansible_connection == "local"
 
 - set_fact: snapshot_run="true"
+
+- set_fact: snapshot_run="false"
+  when: titanium and (ansible_connection is match('nxapi'))
+
 - set_fact: snapshot_run="false"
-  when: titanium and ( connection.transport is match('nxapi'))
+  when: platform is match('N35')
+
+- set_fact: add_sec="true"
+- set_fact: add_sec="false"
+  when: imagetag is search("D1")
 
 - block:
   - name: create snapshot
-    nxos_snapshot:
+    nxos_snapshot: &crss1
       action: create
       snapshot_name: test_snapshot1
       description: Ansible
-      save_snapshot_locally: true
+      save_snapshot_locally: True
+    register: result
+
+  - assert: &true
+      that:
+        - "result.changed == true"
+
+  - name: "Conf Idempotence"
+    nxos_snapshot: *crss1
+    register: result
+
+  - assert: &false
+      that:
+        - "result.changed == false"
+
+  - block:
+    - name: Add section
+      nxos_snapshot: &add
+        action: add
+        section: myshow
+        show_command: show ip interface brief
+        row_id: ROW_intf
+        element_key1: intf-name
+        element_key2: intf-name
+      register: result
+
+    - assert: *true
+  
+    - name: "Conf Idempotence"
+      nxos_snapshot: *add
+      register: result
+
+    - assert: *false
+
+    when: add_sec
 
   - name: create another snapshot
-    nxos_snapshot:
+    nxos_snapshot: &crss2
       action: create
       snapshot_name: test_snapshot2
       description: row
@@ -24,7 +66,15 @@
       show_command: show ip interface brief
       row_id: ROW_intf
       element_key1: intf-name
-      save_snapshot_locally: true
+    register: result
+
+  - assert: *true
+  
+  - name: "Conf Idempotence"
+    nxos_snapshot: *crss2
+    register: result
+
+  - assert: *false
 
   - name: compare snapshots
     nxos_snapshot:
@@ -35,25 +85,44 @@
       compare_option: summary
       path: '.'
 
-  - name: FAIL compare snapshots
-    nxos_snapshot:
-      action: compare
-      snapshot1: test_snapshot1
-      snapshot2: test_snapshot2
-      compare_option: summary
-      path: '.'
+  - name: delete snapshot
+    nxos_snapshot: &del
+      snapshot_name: test_snapshot2
+      action: delete
     register: result
-    ignore_errors: yes
 
-  - assert:
-      that:
-        - 'result.failed == True'
-        - '"action is compare but all of the following are missing: comparison_results_file" in result.msg'
+  - assert: *true
+  
+  - name: "Conf Idempotence"
+    nxos_snapshot: *del
+    register: result
+
+  - assert: *false
+
+  - name: delete all snapshots
+    nxos_snapshot: &delall
+      action: delete_all
+    register: result
+
+  - assert: *true
+  
+  - name: "Conf Idempotence"
+    nxos_snapshot: *delall
+    register: result
+
+  - assert: *false
 
   when: snapshot_run
 
   always:
-  - name: delete snapshot
+  - name: delete all sections
+    nxos_config:
+      commands:
+        - snapshot section delete myshow
+      match: none
+    ignore_errors: yes
+
+  - name: delete all snapshots
     nxos_snapshot:
       action: delete_all
     ignore_errors: yes