2017-05-03 19:06:23 +02:00
|
|
|
# (c) 2017 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
|
|
|
|
|
2017-07-12 17:36:16 +02:00
|
|
|
try:
|
2019-08-02 17:18:14 +02:00
|
|
|
from lxml.etree import fromstring
|
2017-07-12 17:36:16 +02:00
|
|
|
except ImportError:
|
2019-08-02 17:18:14 +02:00
|
|
|
from xml.etree.ElementTree import fromstring
|
2017-05-03 19:06:23 +02:00
|
|
|
|
2018-10-13 05:01:14 +02:00
|
|
|
from units.compat.mock import patch
|
2017-05-03 19:06:23 +02:00
|
|
|
from ansible.modules.network.junos import junos_rpc
|
2017-11-17 18:17:07 +01:00
|
|
|
from units.modules.utils import set_module_args
|
|
|
|
from .junos_module import TestJunosModule, load_fixture
|
2017-07-12 17:36:16 +02:00
|
|
|
|
2017-05-03 19:06:23 +02:00
|
|
|
|
|
|
|
RPC_CLI_MAP = {
|
|
|
|
'get-software-information': 'show version',
|
|
|
|
'get-interface-information': 'show interfaces details',
|
|
|
|
'get-system-memory-information': 'show system memory',
|
|
|
|
'get-chassis-inventory': 'show chassis hardware',
|
2017-11-21 06:15:13 +01:00
|
|
|
'get-system-storage': 'show system storage',
|
|
|
|
'load-configuration': 'load configuration'
|
2017-05-03 19:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class TestJunosCommandModule(TestJunosModule):
|
|
|
|
|
|
|
|
module = junos_rpc
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-11-17 18:17:07 +01:00
|
|
|
super(TestJunosCommandModule, self).setUp()
|
2017-11-24 07:34:47 +01:00
|
|
|
self.mock_conn = patch('ansible.module_utils.connection.Connection')
|
|
|
|
self.conn = self.mock_conn.start()
|
|
|
|
|
2017-12-03 17:12:30 +01:00
|
|
|
self.mock_netconf = patch('ansible.module_utils.network.junos.junos.NetconfConnection')
|
2017-11-24 07:34:47 +01:00
|
|
|
self.netconf_conn = self.mock_netconf.start()
|
|
|
|
|
2017-12-03 17:12:30 +01:00
|
|
|
self.mock_netconf_rpc = patch('ansible.module_utils.network.common.netconf.NetconfConnection')
|
2017-11-24 07:34:47 +01:00
|
|
|
self.netconf_rpc = self.mock_netconf_rpc.start()
|
|
|
|
|
|
|
|
self.mock_exec_rpc = patch('ansible.modules.network.junos.junos_rpc.exec_rpc')
|
|
|
|
self.exec_rpc = self.mock_exec_rpc.start()
|
2017-05-03 19:06:23 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2017-11-17 18:17:07 +01:00
|
|
|
super(TestJunosCommandModule, self).tearDown()
|
2017-11-24 07:34:47 +01:00
|
|
|
self.mock_conn.stop()
|
|
|
|
self.mock_netconf.stop()
|
|
|
|
self.mock_netconf_rpc.stop()
|
|
|
|
self.mock_exec_rpc.stop()
|
2017-05-03 19:06:23 +02:00
|
|
|
|
|
|
|
def load_fixtures(self, commands=None, format='text', changed=False):
|
|
|
|
def load_from_file(*args, **kwargs):
|
2017-11-24 07:34:47 +01:00
|
|
|
element = fromstring(args[1])
|
2017-05-03 19:06:23 +02:00
|
|
|
if element.text:
|
|
|
|
path = str(element.text)
|
|
|
|
else:
|
|
|
|
tag = str(element.tag)
|
|
|
|
if tag.startswith('{'):
|
|
|
|
tag = tag.split('}', 1)[1]
|
|
|
|
path = RPC_CLI_MAP[tag]
|
|
|
|
|
|
|
|
filename = path.replace(' ', '_')
|
|
|
|
filename = '%s_%s.txt' % (filename, format)
|
|
|
|
|
|
|
|
return load_fixture(filename)
|
|
|
|
|
2017-11-24 07:34:47 +01:00
|
|
|
self.exec_rpc.side_effect = load_from_file
|
2017-05-03 19:06:23 +02:00
|
|
|
|
|
|
|
def test_junos_rpc_xml(self):
|
|
|
|
set_module_args(dict(rpc='get-chassis-inventory'))
|
|
|
|
result = self.execute_module(format='xml')
|
|
|
|
self.assertTrue(result['xml'].find('<chassis-inventory>\n'))
|
|
|
|
|
|
|
|
def test_junos_rpc_text(self):
|
|
|
|
set_module_args(dict(rpc='get-software-information', output='text'))
|
|
|
|
result = self.execute_module(format='text')
|
|
|
|
self.assertTrue(result['output_lines'][0].startswith('Hostname: vsrx01'))
|
|
|
|
|
|
|
|
def test_junos_rpc_json(self):
|
|
|
|
set_module_args(dict(rpc='get-software-information', output='json'))
|
|
|
|
result = self.execute_module(format='json')
|
|
|
|
self.assertTrue('software-information' in result['output'])
|
|
|
|
|
|
|
|
def test_junos_rpc_args(self):
|
|
|
|
set_module_args(dict(rpc='get-software-information', args={'interface': 'em0', 'media': True}))
|
2020-01-21 08:57:40 +01:00
|
|
|
self.execute_module(format='xml')
|
2017-11-24 07:34:47 +01:00
|
|
|
args, kwargs = self.exec_rpc.call_args
|
|
|
|
reply = args[1]
|
2018-06-22 17:50:44 +02:00
|
|
|
self.assertTrue(reply.find('<interface>em0</interface><media /></get-software-information>'))
|
2017-11-21 06:15:13 +01:00
|
|
|
|
|
|
|
def test_junos_rpc_attrs(self):
|
|
|
|
set_module_args(dict(rpc='load-configuration', output='xml', attrs={'url': '/var/tmp/config.conf'}))
|
|
|
|
result = self.execute_module(format='xml')
|
|
|
|
self.assertTrue(result['xml'].find('<load-success/>'))
|