Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iosxr integration tests #34663

Merged
merged 4 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 31 additions & 22 deletions lib/ansible/module_utils/network/common/netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import sys

from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.connection import Connection, ConnectionError

try:
from lxml.etree import Element, fromstring
from lxml.etree import Element, fromstring, XMLSyntaxError
except ImportError:
from xml.etree.ElementTree import Element, fromstring
if sys.version_info < (2, 7):
from xml.parsers.expat import ExpatError as XMLSyntaxError
else:
from xml.etree.ElementTree import ParseError as XMLSyntaxError

NS_MAP = {'nc': "urn:ietf:params:xml:ns:netconf:base:1.0"}

Expand Down Expand Up @@ -67,25 +73,28 @@ def __rpc__(self, name, *args, **kwargs):

def parse_rpc_error(self, rpc_error):
if self.check_rc:
error_root = fromstring(rpc_error)
root = Element('root')
root.append(error_root)

error_list = root.findall('.//nc:rpc-error', NS_MAP)
if not error_list:
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))

warnings = []
for error in error_list:
try:
message = error.find('./nc:error-message', NS_MAP).text
except Exception:
message = error.find('./nc:error-info', NS_MAP).text

severity = error.find('./nc:error-severity', NS_MAP).text

if severity == 'warning' and self.ignore_warning:
warnings.append(message)
else:
try:
error_root = fromstring(rpc_error)
root = Element('root')
root.append(error_root)

error_list = root.findall('.//nc:rpc-error', NS_MAP)
if not error_list:
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))
return warnings

warnings = []
for error in error_list:
try:
message = error.find('./nc:error-message', NS_MAP).text
except Exception:
message = error.find('./nc:error-info', NS_MAP).text

severity = error.find('./nc:error-severity', NS_MAP).text

if severity == 'warning' and self.ignore_warning:
warnings.append(message)
else:
raise ConnectionError(to_text(rpc_error, errors='surrogate_then_replace'))
return warnings
except XMLSyntaxError:
raise ConnectionError(rpc_error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess as xml.tree doesn't have XMLSyntaxError , a catch all except: would be good here when lxml is not installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're importing three different exceptions to that name, depending on context, but I think we should have everything covered now

10 changes: 5 additions & 5 deletions lib/ansible/plugins/action/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ class ActionModule(_ActionModule):
def run(self, tmp=None, task_vars=None):
socket_path = None

if self._play_context.connection == 'network_cli':
provider = self._task.args.get('provider', {})
if any(provider.values()):
display.warning('provider is unnecessary when using network_cli and will be ignored')
elif self._play_context.connection == 'local':
if self._play_context.connection == 'local':
provider = load_provider(iosxr_provider_spec, self._task.args)
pc = copy.deepcopy(self._play_context)
if self._task.action in ['iosxr_netconf', 'iosxr_config', 'iosxr_command'] or \
Expand Down Expand Up @@ -77,6 +73,10 @@ def run(self, tmp=None, task_vars=None):
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
else:
provider = self._task.args.get('provider', {})
if any(provider.values()):
display.warning('provider is unnecessary when using {0} and will be ignored'.format(self._play_context.connection))

# make sure we are in the right cli context which should be
# enable mode and not config module
Expand Down
10 changes: 8 additions & 2 deletions test/integration/targets/iosxr_banner/tasks/netconf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"

- name: run test case
include: "{{ test_case_to_run }}"
- name: run test cases (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_first_found: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
---
- name: Enable Netconf service
iosxr_netconf:
netconf_port: 830
netconf_vrf: 'default'
state: present
register: result

- name: setup - remove login
iosxr_banner:
banner: login
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
---
- name: Enable Netconf service
iosxr_netconf:
netconf_port: 830
netconf_vrf: 'default'
state: present
register: result

- name: setup - remove motd
iosxr_banner:
banner: motd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
---
- name: Enable Netconf service
iosxr_netconf:
netconf_port: 830
netconf_vrf: 'default'
state: present
register: result

- name: Setup
iosxr_banner:
banner: login
Expand Down
10 changes: 8 additions & 2 deletions test/integration/targets/iosxr_interface/tasks/netconf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"

- name: run test case
include: "{{ test_case_to_run }}"
- name: run test cases (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_first_found: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
---
- debug: msg="START iosxr_interface netconf/basic.yaml"

- name: Enable Netconf service
iosxr_netconf:
netconf_port: 830
netconf_vrf: 'default'
state: present
register: result

- name: Setup interface
iosxr_interface:
name: GigabitEthernet0/0/0/1
Expand Down
9 changes: 8 additions & 1 deletion test/integration/targets/prepare_iosxr_tests/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
---

- name: Ensure we have loopback 888 for testing
iosxr_config:
src: config.j2
connection: network_cli

- name: Enable Netconf service
iosxr_netconf:
netconf_port: 830
netconf_vrf: 'default'
state: present
connection: network_cli
tags: netconf

# Some AWS hostnames can be longer than those allowed by the system we are testing
# Truncate the hostname
# http://jinja.pocoo.org/docs/2.9/templates/#truncate
Expand Down