Skip to content

Commit

Permalink
fix integration testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrn committed Nov 30, 2020
1 parent 7ee5a25 commit fbff166
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 53 deletions.
9 changes: 7 additions & 2 deletions plugins/action/cli_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ def _extended_check_argspec(self):
that cannot be covered using stnd techniques
"""
errors = []
if len(self._task.args.get("parser").get("name").split(".")) != 3:
requested_parser = self._task.args.get("parser").get("name")
if len(requested_parser.split(".")) != 3:
msg = "Parser name should be provided as a full name including collection"
errors.append(msg)
if self._task.args.get("text"):

if self._task.args.get("text") and requested_parser not in [
"ansible.utils.json",
"ansible.utils.xml",
]:
if not (
self._task.args.get("parser").get("command")
or self._task.args.get("parser").get("template_path")
Expand Down
3 changes: 3 additions & 0 deletions plugins/cli_parsers/json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json

from ansible.module_utils._text import to_native
from ansible.module_utils.six import string_types
from ansible_collections.ansible.utils.plugins.cli_parsers._base import (
CliParserBase,
)
Expand Down Expand Up @@ -38,6 +39,8 @@ def parse(self, *_args, **_kwargs):
"""
text = self._task_args.get("text")
try:
if not isinstance(text, string_types):
text = json.dumps(text)
parsed = json.loads(text)
except Exception as exc:
return {"errors": [to_native(exc)]}
Expand Down
9 changes: 7 additions & 2 deletions plugins/cli_parsers/ttp_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ def parse(self, *_args, **_kwargs):
or
{"parsed": obj}
"""
cli_output = self._task_args.get("text")
cli_output = to_native(
self._task_args.get("text"), errors="surrogate_then_replace"
)
res = self._check_reqs()
if res.get("errors"):
return {"errors": res.get("errors")}

template_path = self._task_args.get("parser").get("template_path")
template_path = to_native(
self._task_args.get("parser").get("template_path"),
errors="surrogate_then_replace",
)
if template_path and not os.path.isfile(template_path):
return {
"error": "error while reading template_path file {file}".format(
Expand Down
12 changes: 0 additions & 12 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
# The follow are 3rd party libs for validate
jsonschema
# /valiate

# The follow are 3rd party libs for cli_parse
ntc_templates
pyats ; python_version >= '3.6' and python_version < '3.9'
genie ; python_version >= '3.6' and python_version < '3.9'
textfsm
ttp
xmltodict
# /cli_parse
10 changes: 9 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ flake8
mock ; python_version < '3.5'
pytest-xdist
yamllint
# The follow are 3rd party libs for valiate

# The follow are 3rd party libs for validate
jsonschema
# /valiate

# The follow are 3rd party libs for cli_parse
textfsm
ttp
xmltodict
# /cli_parse

37 changes: 37 additions & 0 deletions tests/integration/targets/cli_parse/files/nxos_show_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Cisco Nexus Operating System (NX-OS) Software
TAC support: http://www.cisco.com/tac
Documents: http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html
Copyright (c) 2002-2016, Cisco Systems, Inc. All rights reserved.
The copyrights to certain works contained herein are owned by
other third parties and are used and distributed under license.
Some parts of this software are covered under the GNU Public
License. A copy of the license is available at
http://www.gnu.org/licenses/gpl.html.

NX-OSv is a demo version of the Nexus Operating System

Software
loader: version N/A
kickstart: version 7.3(0)D1(1)
system: version 7.3(0)D1(1)
kickstart image file is: bootflash:///titanium-d1-kickstart.7.3.0.D1.1.bin
kickstart compile time: 1/11/2016 16:00:00 [02/11/2016 10:30:12]
system image file is: bootflash:///titanium-d1.7.3.0.D1.1.bin
system compile time: 1/11/2016 16:00:00 [02/11/2016 13:08:11]


Hardware
cisco NX-OSv Chassis ("NX-OSv Supervisor Module")
QEMU Virtual CPU version 2.5 with 3064740 kB of memory.
Processor Board ID TM000B0000B

Device name: an-nxos-02
bootflash: 3184776 kB

Kernel uptime is 110 day(s), 12 hour(s), 32 minute(s), 10 second(s)


plugin
Core Plugin, Ethernet Plugin

Active Package(s)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
[
[
{
"admin_state": "up,",
"interface": "mgmt0",
"state": "up",
"var": "extra_var"
},
{
"admin_state": "up,",
"interface": "Ethernet1/1",
"state": "up",
"var": "extra_var"
}
]
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"BOOT_IMAGE": "bootflash:///titanium-d1-kickstart.7.3.0.D1.1.bin",
"LAST_REBOOT_REASON": "",
"OS": "7.3(0)D1(1)",
"PLATFORM": "OSv",
"UPTIME": "110 day(s), 12 hour(s), 32 minute(s), 10 second(s)"
}
6 changes: 3 additions & 3 deletions tests/integration/targets/cli_parse/tasks/argspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

- name: "{{ parser }} Check argspec fail"
assert:
that: "argfail['msg'] == 'parameters are mutually exclusive: command|template_path found in parser'"
that: "argfail['errors'] == 'parameters are mutually exclusive: command|template_path found in parser'"

- name: "{{ parser }} validate argspec"
ansible.utils.cli_parse:
Expand All @@ -25,7 +25,7 @@

- name: "{{ parser }} Check argspec fail"
assert:
that: "argfail['msg'] == 'parameters are mutually exclusive: command|text'"
that: "argfail['errors'] == 'parameters are mutually exclusive: command|text'"

- name: "{{ parser }} validate argspec"
ansible.utils.cli_parse:
Expand All @@ -37,7 +37,7 @@

- name: "{{ parser }} Check argspec fail"
assert:
that: "argfail['msg'] == 'one of the following is required: command, text'"
that: "argfail['errors'] == 'one of the following is required: command, text'"


- name: "{{ parser }} validate argspec"
Expand Down
13 changes: 6 additions & 7 deletions tests/integration/targets/cli_parse/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@
tags:
- textfsm

#- include_tasks: "ubuntu_ttp.yaml"
# vars:
# parser: "(ubuntu/ttp)"
# #when: inventory_hostname == 'localhost' and os == 'ubuntu'
# when: os == 'ubuntu'
# tags:
# - ttp
- include_tasks: "ubuntu_ttp.yaml"
vars:
parser: "(ubuntu/ttp)"
when: os == 'ubuntu'
tags:
- ttp
19 changes: 6 additions & 13 deletions tests/integration/targets/cli_parse/tasks/nxos_json.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
---
- name: "{{ parser }} Run command and parse with json"
ansible.utils.cli_parse:
command: "show version | json"
parser:
name: ansible.utils.json
register: nxos_json_command
- set_fact:
nxos_json_text_parsed: "{{ lookup('file', '{{ role_path }}/output/nxos_show_interface_json_text.txt') }}"

- name: "{{ parser }} Run command and parse with json"
ansible.utils.cli_parse:
# the leading space here is critical so it's not converted on the way in
text: "{{ nxos_json_command['stdout'] }}"
text: "{{ lookup('file', '{{ role_path }}/output/nxos_show_interface_json_text.txt') }}"
parser:
name: ansible.utils.json
register: nxos_json_text
Expand All @@ -18,8 +13,6 @@
assert:
that: "{{ item }}"
with_items:
- "{{ nxos_json_command['stdout'] is defined }}"
- "{{ nxos_json_command['stdout_lines'] is defined }}"
- "{{ nxos_json_command['parsed'] is defined }}"
- "{{ nxos_json_command['parsed']['header_str'] is defined }}"
- "{{ nxos_json_command['parsed'] == nxos_json_text['parsed'] }}"
- "{{ nxos_json_text['parsed'] is defined }}"
- "{{ nxos_json_text['parsed'][0][0][0]['admin_state'] is defined }}"
- "{{ nxos_json_text['parsed'] == nxos_json_text_parsed }}"
19 changes: 7 additions & 12 deletions tests/integration/targets/cli_parse/tasks/nxos_textfsm.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
---
- name: "{{ parser }} Run command and parse with textfsm"
ansible.utils.cli_parse:
command: "show version"
parser:
name: ansible.utils.textfsm
register: nxos_textfsm_command
- set_fact:
nxos_textfsm_text_parsed: "{{ lookup('file', '{{ role_path }}/output/nxos_show_version_textfsm_parsed.json') | from_json }}"

- name: "{{ parser }} Pass text and command"
ansible.utils.cli_parse:
text: "{{ nxos_textfsm_command['stdout'] }}"
text: "{{ lookup('file', '{{ role_path }}/files/nxos_show_version.txt') }}"
parser:
name: ansible.utils.textfsm
command: show version
template_path: "{{ role_path }}/templates/nxos_show_version.textfsm"
register: nxos_textfsm_text

- name: "{{ parser }} Confirm response"
assert:
that: "{{ item }}"
with_items:
- "{{ nxos_textfsm_command['stdout'] is defined }}"
- "{{ nxos_textfsm_command['stdout_lines'] is defined }}"
- "{{ nxos_textfsm_command['parsed'] == nxos_textfsm_text['parsed'] }}"
- "{{ nxos_textfsm_command['parsed'][0]['BOOT_IMAGE'] is defined }}"
- "{{ nxos_textfsm_text['parsed'] == nxos_textfsm_text['parsed'] }}"
- "{{ nxos_textfsm_text['parsed'][0]['BOOT_IMAGE'] is defined }}"
- "{{ nxos_textfsm_text['parsed'][0] == nxos_textfsm_text_parsed }}"
1 change: 0 additions & 1 deletion tests/integration/targets/cli_parse/tasks/nxos_xml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
text: "{{ lookup('file', '{{ role_path }}/files/nxos_show_interface.xml') }}"
parser:
name: ansible.utils.xml
command: "show interface | xml"
os: nxos
register: nxos_xml_text

Expand Down

0 comments on commit fbff166

Please sign in to comment.