From 0bd2cd4394904fcddfec731c2f6bc413cc792100 Mon Sep 17 00:00:00 2001
From: David Newswanger <gamma.dave@gmail.com>
Date: Wed, 4 Apr 2018 10:46:47 -0400
Subject: [PATCH] added junos_smoke tests (#37216)

* added junos_smoke tests

* enable check mode test

* fix check mode test
---
 .../targets/junos_smoke/defaults/main.yaml    |  3 +
 .../targets/junos_smoke/meta/main.yml         |  2 +
 .../targets/junos_smoke/tasks/main.yaml       |  2 +
 .../targets/junos_smoke/tasks/netconf.yaml    | 21 ++++
 .../tests/netconf/common_utils.yaml           | 56 +++++++++++
 .../tests/netconf/module_utils_junos.yaml     | 99 +++++++++++++++++++
 6 files changed, 183 insertions(+)
 create mode 100644 test/integration/targets/junos_smoke/defaults/main.yaml
 create mode 100644 test/integration/targets/junos_smoke/meta/main.yml
 create mode 100644 test/integration/targets/junos_smoke/tasks/main.yaml
 create mode 100644 test/integration/targets/junos_smoke/tasks/netconf.yaml
 create mode 100644 test/integration/targets/junos_smoke/tests/netconf/common_utils.yaml
 create mode 100644 test/integration/targets/junos_smoke/tests/netconf/module_utils_junos.yaml

diff --git a/test/integration/targets/junos_smoke/defaults/main.yaml b/test/integration/targets/junos_smoke/defaults/main.yaml
new file mode 100644
index 0000000000..9ef5ba5165
--- /dev/null
+++ b/test/integration/targets/junos_smoke/defaults/main.yaml
@@ -0,0 +1,3 @@
+---
+testcase: "*"
+test_items: []
diff --git a/test/integration/targets/junos_smoke/meta/main.yml b/test/integration/targets/junos_smoke/meta/main.yml
new file mode 100644
index 0000000000..191a0f2ea1
--- /dev/null
+++ b/test/integration/targets/junos_smoke/meta/main.yml
@@ -0,0 +1,2 @@
+dependencies:
+  - prepare_junos_tests
diff --git a/test/integration/targets/junos_smoke/tasks/main.yaml b/test/integration/targets/junos_smoke/tasks/main.yaml
new file mode 100644
index 0000000000..cc27f174fd
--- /dev/null
+++ b/test/integration/targets/junos_smoke/tasks/main.yaml
@@ -0,0 +1,2 @@
+---
+- { include: netconf.yaml, tags: ['netconf'] }
diff --git a/test/integration/targets/junos_smoke/tasks/netconf.yaml b/test/integration/targets/junos_smoke/tasks/netconf.yaml
new file mode 100644
index 0000000000..9550210102
--- /dev/null
+++ b/test/integration/targets/junos_smoke/tasks/netconf.yaml
@@ -0,0 +1,21 @@
+- name: collect netconf test cases
+  find:
+    paths: "{{ role_path }}/tests/netconf"
+    patterns: "{{ testcase }}.yaml"
+  connection: local
+  register: test_cases
+
+- name: set test_items
+  set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
+
+- name: run test case (connection=netconf)
+  include: "{{ test_case_to_run }} ansible_connection=netconf"
+  with_items: "{{ test_items }}"
+  loop_control:
+    loop_var: test_case_to_run
+
+- name: run test case (connection=local)
+  include: "{{ test_case_to_run }} ansible_connection=local"
+  with_items: "{{ test_items }}"
+  loop_control:
+    loop_var: test_case_to_run
diff --git a/test/integration/targets/junos_smoke/tests/netconf/common_utils.yaml b/test/integration/targets/junos_smoke/tests/netconf/common_utils.yaml
new file mode 100644
index 0000000000..51c4bbc994
--- /dev/null
+++ b/test/integration/targets/junos_smoke/tests/netconf/common_utils.yaml
@@ -0,0 +1,56 @@
+---
+  # junos interface -> remove_default_spec() conditional()
+  - debug: msg="START junos_interface netconf/common_utils.yaml on connection={{ ansible_connection }}"
+
+  - name: get facts
+    junos_facts:
+      provider: "{{ netconf }}"
+    register: result
+
+
+  - name: Define interface name for vSRX
+    set_fact:
+      intf_name: pp0
+    when: result['ansible_facts']['ansible_net_model']  is search("vSRX*")
+
+  - name: Define interface name for vsrx
+    set_fact:
+      intf_name: pp0
+    when: result['ansible_facts']['ansible_net_model']  is search("vsrx")
+
+  - name: Define interface name for vQFX
+    set_fact:
+      intf_name: gr-0/0/0
+    when: result['ansible_facts']['ansible_net_model']  is search("vqfx*")
+
+  - name: Check intent arguments
+    junos_interface:
+      name: "{{ intf_name }}"
+      state: up
+      tx_rate: ge(0)
+      rx_rate: le(0)
+      provider: "{{ netconf }}"
+    register: result
+
+  - assert:
+      that:
+        - "result.failed == false"
+
+  - name: Check intent arguments (failed condition)
+    junos_interface:
+      name: "{{ intf_name }}"
+      state: down
+      tx_rate: gt(0)
+      rx_rate: lt(0)
+      provider: "{{ netconf }}"
+    ignore_errors: yes
+    register: result
+
+  - assert:
+      that:
+        - "result.failed == true"
+        - "'state eq(down)' in result.failed_conditions"
+        - "'tx_rate gt(0)' in result.failed_conditions"
+        - "'rx_rate lt(0)' in result.failed_conditions"
+
+  - debug: msg="END junos_interface netconf/common_utils.yaml on connection={{ ansible_connection }}"
diff --git a/test/integration/targets/junos_smoke/tests/netconf/module_utils_junos.yaml b/test/integration/targets/junos_smoke/tests/netconf/module_utils_junos.yaml
new file mode 100644
index 0000000000..9376968584
--- /dev/null
+++ b/test/integration/targets/junos_smoke/tests/netconf/module_utils_junos.yaml
@@ -0,0 +1,99 @@
+---
+- debug: msg="START netconf/module_utils_junos.yaml on connection={{ ansible_connection }}"
+
+# hit get_capabilities()
+
+- name: get output for single command
+  junos_command:
+    commands: ['show version']
+    format: json
+    provider: "{{ netconf }}"
+  register: result
+
+- assert:
+    that:
+      - "result.changed == false"
+      - "result.stdout is defined"
+      - "result.stdout_lines is defined"
+
+# hit commit_configuration()
+- name: setup - remove login banner
+  junos_banner:
+    banner: login
+    state: absent
+    provider: "{{ netconf }}"
+
+- name: Create login banner
+  junos_banner:
+    banner: login
+    text: this is my login banner
+    state: present
+    provider: "{{ netconf }}"
+  register: result
+
+- name: Get running configuration
+  junos_rpc:
+    rpc: get-configuration
+    provider: "{{ netconf }}"
+  register: config
+
+- assert:
+    that:
+      - "result.changed == true"
+      - "'<message>this is my login banner</message>' in config.xml"
+
+# hit discard_changes()
+- name: check mode
+  junos_banner:
+    banner: login
+    text: this is not the banner you're looking for
+    state: present
+    provider: "{{ netconf }}"
+  register: result
+  check_mode: yes
+
+- assert:
+    that:
+      - "result.changed == true"
+      - "result.failed == false"
+
+
+# hit field_top in map_obj_to_ele
+- name: setup - remove interface address
+  net_l3_interface:
+    name: ge-0/0/1
+    ipv4: 1.1.1.1
+    ipv6: fd5d:12c9:2201:1::1
+    state: absent
+    provider: "{{ netconf }}"
+
+- name: Configure interface address using platform agnostic module
+  net_l3_interface:
+    name: ge-0/0/1
+    ipv4: 1.1.1.1
+    ipv6: fd5d:12c9:2201:1::1
+    state: present
+    provider: "{{ netconf }}"
+  register: result
+
+- name: Get running configuration
+  junos_rpc:
+    rpc: get-configuration
+    provider: "{{ netconf }}"
+  register: config
+
+- assert:
+    that:
+      - "result.changed == true"
+      - "'<name>1.1.1.1/32</name>' in config.xml"
+      - "'<name>fd5d:12c9:2201:1::1/128</name>' in config.xml"
+      - result.diff.prepared is search("\+ *address 1.1.1.1/32")
+      - result.diff.prepared is search("\+ *address fd5d:12c9:2201:1::1/128")
+
+- name: teardown - remove interface address
+  net_l3_interface:
+    name: ge-0/0/1
+    ipv4: 1.1.1.1
+    ipv6: fd5d:12c9:2201:1::1
+    state: absent
+    provider: "{{ netconf }}"