2017-01-25 16:15:26 +01:00
|
|
|
#
|
|
|
|
# (c) 2016 Red Hat Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import json
|
2018-09-28 22:21:00 +02:00
|
|
|
import pytest
|
2017-01-25 16:15:26 +01:00
|
|
|
|
|
|
|
from io import StringIO
|
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat.mock import patch, MagicMock, PropertyMock
|
2017-01-25 16:15:26 +01:00
|
|
|
from ansible.errors import AnsibleConnectionFailure
|
|
|
|
from ansible.playbook.play_context import PlayContext
|
|
|
|
|
2018-09-28 22:21:00 +02:00
|
|
|
pytest.importorskip("ncclient")
|
|
|
|
|
2017-01-25 16:15:26 +01:00
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
builtin_import = __import__
|
|
|
|
|
|
|
|
mock_ncclient = MagicMock(name='ncclient')
|
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2017-01-25 16:15:26 +01:00
|
|
|
def import_mock(name, *args):
|
|
|
|
if name.startswith('ncclient'):
|
|
|
|
return mock_ncclient
|
|
|
|
return builtin_import(name, *args)
|
|
|
|
|
2018-07-29 13:46:06 +02:00
|
|
|
|
2017-01-25 16:15:26 +01:00
|
|
|
if PY3:
|
|
|
|
with patch('builtins.__import__', side_effect=import_mock):
|
|
|
|
from ansible.plugins.connection import netconf
|
2018-07-02 14:41:00 +02:00
|
|
|
from ansible.plugins.loader import connection_loader
|
2017-01-25 16:15:26 +01:00
|
|
|
else:
|
|
|
|
with patch('__builtin__.__import__', side_effect=import_mock):
|
|
|
|
from ansible.plugins.connection import netconf
|
2018-07-02 14:41:00 +02:00
|
|
|
from ansible.plugins.loader import connection_loader
|
2017-01-25 16:15:26 +01:00
|
|
|
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2017-01-25 16:15:26 +01:00
|
|
|
class TestNetconfConnectionClass(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_netconf_init(self):
|
|
|
|
pc = PlayContext()
|
2018-09-20 15:56:43 +02:00
|
|
|
conn = connection_loader.get('netconf', pc, '/dev/null')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
2019-06-21 05:34:13 +02:00
|
|
|
self.assertEqual('auto', conn._network_os)
|
2017-01-25 16:15:26 +01:00
|
|
|
self.assertIsNone(conn._manager)
|
|
|
|
self.assertFalse(conn._connected)
|
|
|
|
|
2017-12-05 07:34:48 +01:00
|
|
|
@patch("ansible.plugins.connection.netconf.netconf_loader")
|
|
|
|
def test_netconf__connect(self, mock_netconf_loader):
|
2017-01-25 16:15:26 +01:00
|
|
|
pc = PlayContext()
|
2018-09-20 15:56:43 +02:00
|
|
|
conn = connection_loader.get('netconf', pc, '/dev/null')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_manager = MagicMock()
|
|
|
|
mock_manager.session_id = '123456789'
|
|
|
|
netconf.manager.connect = MagicMock(return_value=mock_manager)
|
2017-01-25 16:15:26 +01:00
|
|
|
|
|
|
|
rc, out, err = conn._connect()
|
|
|
|
|
|
|
|
self.assertEqual(0, rc)
|
2017-06-06 10:26:25 +02:00
|
|
|
self.assertEqual(b'123456789', out)
|
|
|
|
self.assertEqual(b'', err)
|
2017-01-25 16:15:26 +01:00
|
|
|
self.assertTrue(conn._connected)
|
|
|
|
|
|
|
|
def test_netconf_exec_command(self):
|
|
|
|
pc = PlayContext()
|
2018-09-20 15:56:43 +02:00
|
|
|
conn = connection_loader.get('netconf', pc, '/dev/null')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
|
|
|
conn._connected = True
|
|
|
|
|
|
|
|
mock_reply = MagicMock(name='reply')
|
|
|
|
type(mock_reply).data_xml = PropertyMock(return_value='<test/>')
|
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_manager = MagicMock(name='self._manager')
|
2017-01-25 16:15:26 +01:00
|
|
|
mock_manager.rpc.return_value = mock_reply
|
|
|
|
conn._manager = mock_manager
|
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
out = conn.exec_command('<test/>')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
self.assertEqual('<test/>', out)
|
2017-01-25 16:15:26 +01:00
|
|
|
|
|
|
|
def test_netconf_exec_command_invalid_request(self):
|
|
|
|
pc = PlayContext()
|
2018-09-20 15:56:43 +02:00
|
|
|
conn = connection_loader.get('netconf', pc, '/dev/null')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
|
|
|
conn._connected = True
|
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
mock_manager = MagicMock(name='self._manager')
|
|
|
|
conn._manager = mock_manager
|
|
|
|
|
2017-01-25 16:15:26 +01:00
|
|
|
netconf.to_ele.return_value = None
|
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
out = conn.exec_command('test string')
|
2017-01-25 16:15:26 +01:00
|
|
|
|
2017-11-09 21:04:40 +01:00
|
|
|
self.assertEqual('unable to parse request', out)
|