Refactor junos modules to Use netconf and cliconf plugins (#32621)

* Fix junos integration test fixes as per connection refactor (#33050)

Refactor netconf connection plugin to work with netconf plugin

* Fix junos integration test fixes as per connection refactor (#33050)

Refactor netconf connection plugin to work with netconf plugin
Fix CI failure
Fix unit test failure
Fix review comments
This commit is contained in:
Ganesh Nalawade 2017-11-24 12:04:47 +05:30 committed by GitHub
commit 3d63ecb6f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 543 additions and 320 deletions

View file

@ -19,6 +19,11 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
try:
from lxml.etree import fromstring
except ImportError:
from xml.etree.ElementTree import fromstring
from ansible.compat.tests.mock import patch
from ansible.modules.network.junos import junos_command
from units.modules.utils import set_module_args
@ -36,18 +41,37 @@ class TestJunosCommandModule(TestJunosModule):
def setUp(self):
super(TestJunosCommandModule, self).setUp()
self.mock_send_request = patch('ansible.modules.network.junos.junos_command.send_request')
self.send_request = self.mock_send_request.start()
self.mock_conn = patch('ansible.module_utils.junos.Connection')
self.conn = self.mock_conn.start()
self.mock_netconf = patch('ansible.module_utils.junos.NetconfConnection')
self.netconf_conn = self.mock_netconf.start()
self.mock_exec_rpc = patch('ansible.modules.network.junos.junos_command.exec_rpc')
self.exec_rpc = self.mock_exec_rpc.start()
self.mock_netconf_rpc = patch('ansible.module_utils.netconf.NetconfConnection')
self.netconf_rpc = self.mock_netconf_rpc.start()
self.mock_get_connection = patch('ansible.modules.network.junos.junos_command.get_connection')
self.get_connection = self.mock_get_connection.start()
self.mock_get_capabilities = patch('ansible.modules.network.junos.junos_command.get_capabilities')
self.get_capabilities = self.mock_get_capabilities.start()
self.get_capabilities.return_value = {'network_api': 'netconf'}
def tearDown(self):
super(TestJunosCommandModule, self).tearDown()
self.mock_send_request.stop()
self.mock_conn.stop()
self.mock_netconf.stop()
self.mock_get_capabilities.stop()
self.mock_netconf_rpc.stop()
self.mock_exec_rpc.stop()
self.mock_get_connection.stop()
def load_fixtures(self, commands=None, format='text', changed=False):
def load_from_file(*args, **kwargs):
module, element = args
element = fromstring(args[1])
if element.text:
path = str(element.text)
else:
@ -57,7 +81,7 @@ class TestJunosCommandModule(TestJunosModule):
filename = '%s_%s.txt' % (filename, format)
return load_fixture(filename)
self.send_request.side_effect = load_from_file
self.exec_rpc.side_effect = load_from_file
def test_junos_command_simple(self):
set_module_args(dict(commands=['show version']))
@ -80,13 +104,13 @@ class TestJunosCommandModule(TestJunosModule):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for))
self.execute_module(failed=True)
self.assertEqual(self.send_request.call_count, 10)
self.assertEqual(self.exec_rpc.call_count, 10)
def test_junos_command_retries(self):
wait_for = 'result[0] contains "test string"'
set_module_args(dict(commands=['show version'], wait_for=wait_for, retries=2))
self.execute_module(failed=True)
self.assertEqual(self.send_request.call_count, 2)
self.assertEqual(self.exec_rpc.call_count, 2)
def test_junos_command_match_any(self):
wait_for = ['result[0] contains "Junos:"',

View file

@ -54,8 +54,17 @@ class TestJunosConfigModule(TestJunosModule):
self.mock_get_diff = patch('ansible.modules.network.junos.junos_config.get_diff')
self.get_diff = self.mock_get_diff.start()
self.mock_send_request = patch('ansible.modules.network.junos.junos_config.send_request')
self.send_request = self.mock_send_request.start()
self.mock_conn = patch('ansible.module_utils.connection.Connection')
self.conn = self.mock_conn.start()
self.mock_netconf = patch('ansible.module_utils.junos.NetconfConnection')
self.netconf_conn = self.mock_netconf.start()
self.mock_exec_rpc = patch('ansible.modules.network.junos.junos_config.exec_rpc')
self.exec_rpc = self.mock_exec_rpc.start()
self.mock_netconf_rpc = patch('ansible.module_utils.netconf.NetconfConnection')
self.netconf_rpc = self.mock_netconf_rpc.start()
def tearDown(self):
super(TestJunosConfigModule, self).tearDown()
@ -65,8 +74,11 @@ class TestJunosConfigModule(TestJunosModule):
self.mock_unlock_configuration.stop()
self.mock_commit_configuration.stop()
self.mock_get_diff.stop()
self.mock_send_request.stop()
self.load_configuration.stop()
self.mock_conn.stop()
self.mock_netconf.stop()
self.mock_exec_rpc.stop()
self.mock_netconf_rpc.stop()
def load_fixtures(self, commands=None, format='text', changed=False):
self.get_config.return_value = load_fixture('get_configuration_rpc_reply.txt')
@ -162,7 +174,7 @@ class TestJunosConfigModule(TestJunosModule):
src = load_fixture('junos_config.json', content='str')
set_module_args(dict(zeroize='yes'))
self.execute_module(changed=True)
self.assertEqual(self.send_request.call_count, 1)
self.assertEqual(self.exec_rpc.call_count, 1)
def test_junos_config_src_format_xml(self):
src = load_fixture('junos_config.json', content='str')

View file

@ -19,6 +19,11 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
try:
from lxml.etree import fromstring
except ImportError:
from xml.etree.ElementTree import fromstring
from ansible.compat.tests.mock import patch
from ansible.modules.network.junos import junos_facts
from units.modules.utils import set_module_args
@ -44,16 +49,33 @@ class TestJunosCommandModule(TestJunosModule):
self.mock_get_config = patch('ansible.modules.network.junos.junos_facts.get_configuration')
self.get_config = self.mock_get_config.start()
self.mock_send_request = patch('ansible.modules.network.junos.junos_facts.send_request')
self.send_request = self.mock_send_request.start()
self.mock_conn = patch('ansible.module_utils.connection.Connection')
self.conn = self.mock_conn.start()
self.mock_netconf = patch('ansible.module_utils.junos.NetconfConnection')
self.netconf_conn = self.mock_netconf.start()
self.mock_exec_rpc = patch('ansible.modules.network.junos.junos_facts.exec_rpc')
self.exec_rpc = self.mock_exec_rpc.start()
self.mock_netconf_rpc = patch('ansible.module_utils.netconf.NetconfConnection')
self.netconf_rpc = self.mock_netconf_rpc.start()
self.mock_get_capabilities = patch('ansible.module_utils.junos.get_capabilities')
self.get_capabilities = self.mock_get_capabilities.start()
self.get_capabilities.return_value = {'network_api': 'netconf'}
def tearDown(self):
super(TestJunosCommandModule, self).tearDown()
self.mock_send_request.stop()
self.mock_conn.stop()
self.mock_netconf.stop()
self.mock_exec_rpc.stop()
self.mock_netconf_rpc.stop()
self.mock_get_capabilities.stop()
def load_fixtures(self, commands=None, format='text', changed=False):
def load_from_file(*args, **kwargs):
module, element = args
element = fromstring(args[1])
if element.text:
path = str(element.text)
@ -64,7 +86,7 @@ class TestJunosCommandModule(TestJunosModule):
filename = '%s_%s.txt' % (filename, format)
return load_fixture(filename)
self.send_request.side_effect = load_from_file
self.exec_rpc.side_effect = load_from_file
def test_junos_get_facts(self):
set_module_args(dict())

View file

@ -32,9 +32,6 @@ class TestJunosCommandModule(TestJunosModule):
def setUp(self):
super(TestJunosCommandModule, self).setUp()
self.mock_exec_command = patch('ansible.modules.network.junos.junos_netconf.exec_command')
self.exec_command = self.mock_exec_command.start()
self.mock_lock_configuration = patch('ansible.module_utils.junos.lock_configuration')
self.lock_configuration = self.mock_lock_configuration.start()
@ -44,17 +41,33 @@ class TestJunosCommandModule(TestJunosModule):
self.mock_commit_configuration = patch('ansible.modules.network.junos.junos_netconf.commit_configuration')
self.commit_configuration = self.mock_commit_configuration.start()
self.mock_conn = patch('ansible.module_utils.connection.Connection')
self.conn = self.mock_conn.start()
self.mock_netconf = patch('ansible.module_utils.junos.NetconfConnection')
self.netconf_conn = self.mock_netconf.start()
self.mock_netconf_rpc = patch('ansible.module_utils.netconf.NetconfConnection')
self.netconf_rpc = self.mock_netconf_rpc.start()
self.mock_get_capabilities = patch('ansible.module_utils.junos.get_capabilities')
self.get_capabilities = self.mock_get_capabilities.start()
self.get_capabilities.return_value = {'network_api': 'netconf'}
def tearDown(self):
super(TestJunosCommandModule, self).tearDown()
self.mock_exec_command.stop()
self.mock_lock_configuration.stop()
self.mock_unlock_configuration.stop()
self.mock_commit_configuration.stop()
self.mock_conn.stop()
self.mock_netconf.stop()
self.mock_netconf_rpc.stop()
self.mock_get_capabilities.stop()
def test_junos_netconf_enable(self):
self.exec_command.return_value = 0, '', None
self.netconf_conn().get.return_value = ''
set_module_args(dict(state='present'))
result = self.execute_module()
result = self.execute_module(changed=True)
self.assertEqual(result['commands'], ['set system services netconf ssh port 830'])
def test_junos_netconf_disable(self):
@ -63,7 +76,7 @@ class TestJunosCommandModule(TestJunosModule):
port 830;
}
'''
self.exec_command.return_value = 0, out, None
self.netconf_conn().get.return_value = out
set_module_args(dict(state='absent'))
result = self.execute_module(changed=True)
self.assertEqual(result['commands'], ['delete system services netconf'])
@ -74,7 +87,7 @@ class TestJunosCommandModule(TestJunosModule):
port 830;
}
'''
self.exec_command.return_value = 0, out, None
self.netconf_conn().get.return_value = out
set_module_args(dict(state='present', netconf_port=22))
result = self.execute_module(changed=True)
self.assertEqual(result['commands'], ['set system services netconf ssh port 22'])
@ -85,13 +98,13 @@ class TestJunosCommandModule(TestJunosModule):
port 22;
}
'''
self.exec_command.return_value = 0, out, None
self.netconf_conn().get.return_value = out
set_module_args(dict(state='present', netconf_port=0))
result = self.execute_module(changed=True, failed=True)
self.assertEqual(result['msg'], 'netconf_port must be between 1 and 65535')
def test_junos_netconf_config_error(self):
self.exec_command.return_value = 1, None, None
self.netconf_conn().get.return_value = None
set_module_args(dict(state='present'))
result = self.execute_module(failed=True)
self.assertEqual(result['msg'], 'unable to retrieve current config')

View file

@ -20,9 +20,9 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
try:
from lxml.etree import tostring
from lxml.etree import tostring, fromstring
except ImportError:
from xml.etree.ElementTree import tostring
from xml.etree.ElementTree import tostring, fromstring
from ansible.compat.tests.mock import patch
from ansible.modules.network.junos import junos_rpc
@ -46,16 +46,28 @@ class TestJunosCommandModule(TestJunosModule):
def setUp(self):
super(TestJunosCommandModule, self).setUp()
self.mock_send_request = patch('ansible.modules.network.junos.junos_rpc.send_request')
self.send_request = self.mock_send_request.start()
self.mock_conn = patch('ansible.module_utils.connection.Connection')
self.conn = self.mock_conn.start()
self.mock_netconf = patch('ansible.module_utils.junos.NetconfConnection')
self.netconf_conn = self.mock_netconf.start()
self.mock_netconf_rpc = patch('ansible.module_utils.netconf.NetconfConnection')
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()
def tearDown(self):
super(TestJunosCommandModule, self).tearDown()
self.mock_send_request.stop()
self.mock_conn.stop()
self.mock_netconf.stop()
self.mock_netconf_rpc.stop()
self.mock_exec_rpc.stop()
def load_fixtures(self, commands=None, format='text', changed=False):
def load_from_file(*args, **kwargs):
module, element = args
element = fromstring(args[1])
if element.text:
path = str(element.text)
else:
@ -69,7 +81,7 @@ class TestJunosCommandModule(TestJunosModule):
return load_fixture(filename)
self.send_request.side_effect = load_from_file
self.exec_rpc.side_effect = load_from_file
def test_junos_rpc_xml(self):
set_module_args(dict(rpc='get-chassis-inventory'))
@ -89,9 +101,9 @@ class TestJunosCommandModule(TestJunosModule):
def test_junos_rpc_args(self):
set_module_args(dict(rpc='get-software-information', args={'interface': 'em0', 'media': True}))
result = self.execute_module(format='xml')
args, kwargs = self.send_request.call_args
reply = tostring(args[1]).decode()
self.assertTrue(reply.find('<interface>em0</interface><media /></get-software-information>'))
args, kwargs = self.exec_rpc.call_args
reply = args[1]
self.assertTrue(reply.find(b'<interface>em0</interface><media /></get-software-information>'))
def test_junos_rpc_attrs(self):
set_module_args(dict(rpc='load-configuration', output='xml', attrs={'url': '/var/tmp/config.conf'}))