More test cases

This commit is contained in:
rajvidhimar 2017-03-08 00:25:41 +05:30
parent a5527f690f
commit c49c8860ac
2 changed files with 125 additions and 31 deletions

View File

@ -796,9 +796,17 @@ def install_config(path=None, **kwargs):
ret['out'] = False ret['out'] = False
return ret return ret
op = dict()
if '__pub_arg' in kwargs:
if kwargs['__pub_arg']:
if isinstance(kwargs['__pub_arg'][-1], dict):
op.update(kwargs['__pub_arg'][-1])
else:
op.update(kwargs)
template_vars = dict() template_vars = dict()
if "template_vars" in kwargs: if "template_vars" in op:
template_vars = kwargs["template_vars"] template_vars = op["template_vars"]
template_cached_path = files.mkstemp() template_cached_path = files.mkstemp()
__salt__['cp.get_template']( __salt__['cp.get_template'](
@ -816,14 +824,6 @@ def install_config(path=None, **kwargs):
ret['out'] = False ret['out'] = False
return ret return ret
op = dict()
if '__pub_arg' in kwargs:
if kwargs['__pub_arg']:
if isinstance(kwargs['__pub_arg'][-1], dict):
op.update(kwargs['__pub_arg'][-1])
else:
op.update(kwargs)
write_diff = '' write_diff = ''
if 'diffs_file' in op and op['diffs_file'] is not None: if 'diffs_file' in op and op['diffs_file'] is not None:
write_diff = op['diffs_file'] write_diff = op['diffs_file']

View File

@ -5,7 +5,7 @@ __author__ = "Rajvi Dhimar"
import unittest2 as unittest import unittest2 as unittest
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from mock import patch, MagicMock, ANY from mock import patch, MagicMock, ANY, mock_open
from lxml import etree from lxml import etree
from jnpr.junos.utils.config import Config from jnpr.junos.utils.config import Config
@ -21,7 +21,10 @@ class Test_Junos_Module(unittest.TestCase):
junos.__proxy__ = { junos.__proxy__ = {
'junos.conn': self.make_connect, 'junos.conn': self.make_connect,
'junos.get_serialized_facts': self.get_facts} 'junos.get_serialized_facts': self.get_facts}
junos.__salt__ = {'cp.get_template': MagicMock} junos.__salt__ = {'cp.get_template': self.mock_cp}
def mock_cp(self, *args, **kwargs):
pass
@patch('ncclient.manager.connect') @patch('ncclient.manager.connect')
def make_connect(self, mock_connect): def make_connect(self, mock_connect):
@ -904,6 +907,34 @@ class Test_Junos_Module(unittest.TestCase):
self.assertEqual(junos.install_config('actual/path/config.set'), ret) self.assertEqual(junos.install_config('actual/path/config.set'), ret)
mock_load.assert_called_with(path='test/path/config', format='set') mock_load.assert_called_with(path='test/path/config', format='set')
# @patch('tests.unit.modules.test_junos.Test_Junos_Module.mock_cp')
# @patch('jnpr.junos.utils.config.Config.commit')
# @patch('jnpr.junos.utils.config.Config.commit_check')
# @patch('jnpr.junos.utils.config.Config.diff')
# @patch('jnpr.junos.utils.config.Config.load')
# @patch('salt.modules.junos.safe_rm')
# @patch('salt.modules.junos.files.mkstemp')
# @patch('os.path.isfile')
# @patch('os.path.getsize')
# def test_install_config_template_vars_in_kwargs(
# self,
# mock_getsize,
# mock_isfile,
# mock_mkstemp,
# mock_safe_rm,
# mock_load,
# mock_diff,
# mock_commit_check,
# mock_commit,
# mck):
# mock_isfile.return_value = True
# mock_getsize.return_value = 10
# mock_mkstemp.return_value = 'test/path/config'
# mock_diff.return_value = 'diff'
# mock_commit_check.return_value = True
# junos.install_config('/actual/path/config', template_vars={'test':'args'})
# mck.assert_called_with('/actual/path/config', 'test/path/config', template_vars={'test':'args'})
@patch('jnpr.junos.utils.config.Config.commit') @patch('jnpr.junos.utils.config.Config.commit')
@patch('jnpr.junos.utils.config.Config.commit_check') @patch('jnpr.junos.utils.config.Config.commit_check')
@patch('jnpr.junos.utils.config.Config.diff') @patch('jnpr.junos.utils.config.Config.diff')
@ -1540,6 +1571,28 @@ class Test_Junos_Module(unittest.TestCase):
src='test/src/file'), src='test/src/file'),
ret) ret)
# These test cases test the __virtual__ function, used internally by salt
# to check if the given module is loadable. This function is not used by
# an external user.
def test_virtual_proxy_unavailable(self):
junos.__opts__ = {}
self.maxDiff = None
res = (False, 'The junos module could not be \
loaded: junos-eznc or jxmlease or proxy could not be loaded.')
self.assertEqual(junos.__virtual__(), res)
def mck_import(self, name, *args, **kwargs):
if name=='jxmlease':
return self.raise_exception()
else:
__import__(name)
def test_virtual_all_true(self):
junos.__opts__ = {'proxy': 'test'}
self.assertEqual(junos.__virtual__(), 'junos')
@attr('unit') @attr('unit')
class Test_Junos_RPC(unittest.TestCase): class Test_Junos_RPC(unittest.TestCase):
@ -1561,8 +1614,11 @@ class Test_Junos_RPC(unittest.TestCase):
self.dev.bind(sw=SW) self.dev.bind(sw=SW)
return self.dev return self.dev
def mock_func(self, *args, **kwargs): def mck_attr(self, *args, **kwargs):
pass return self.get_text_rpc
def get_text_rpc(self, *args, **kwargs):
return etree.XML('<rpc-reply>text rpc reply</rpc-reply>')
def raise_exception(self, *args, **kwargs): def raise_exception(self, *args, **kwargs):
raise Exception('Test exception') raise Exception('Test exception')
@ -1573,39 +1629,77 @@ class Test_Junos_RPC(unittest.TestCase):
ret['out'] = False ret['out'] = False
self.assertEqual(junos.rpc(), ret) self.assertEqual(junos.rpc(), ret)
@patch('tests.unit.modules.test_junos.Test_Junos_RPC.mock_func')
@patch('salt.modules.junos.getattr') @patch('salt.modules.junos.getattr')
def test_rpc_get_config_exception(self, mock_attr, mock_func): def test_rpc_get_config_exception(self, mock_attr):
mock_attr.return_value = self.raise_exception mock_attr.return_value = self.raise_exception
ret = dict() ret = dict()
ret['message'] = 'RPC execution failed due to "Test exception"' ret['message'] = 'RPC execution failed due to "Test exception"'
ret['out'] = False ret['out'] = False
self.assertEqual(junos.rpc('get_config'), ret) self.assertEqual(junos.rpc('get_config'), ret)
@patch('tests.unit.modules.test_junos.Test_Junos_RPC.mock_func') @patch('salt.modules.junos.jxmlease.parse')
@patch('salt.modules.junos.etree.tostring')
@patch('salt.modules.junos.etree.XML')
@patch('salt.modules.junos.getattr') @patch('salt.modules.junos.getattr')
def test_rpc_get_interface_information(self, mock_attr, mock_func): def test_rpc_get_config_filter(self, mock_attr, mock_XML, mock_tostring, mock_jxmlease):
mock_attr.return_value = self.mock_func mock_attr = self.mck_attr
junos.rpc('get-config', filter='<configuration><system/></configuration>')
mock_XML.assert_called_with('<configuration><system/></configuration>')
@patch('tests.unit.modules.test_junos.Test_Junos_RPC.mck_attr')
@patch('salt.modules.junos.getattr')
def test_rpc_get_interface_information(self, mock_attr, mck_attr):
mock_attr.return_value = self.mck_attr
junos.rpc('get-interface-information', format='json') junos.rpc('get-interface-information', format='json')
mock_func.assert_called_with({'format': 'json'}, dev_timeout=30) mck_attr.assert_called_with({'format': 'json'}, dev_timeout=30)
@patch('tests.unit.modules.test_junos.Test_Junos_RPC.mock_func') @patch('tests.unit.modules.test_junos.Test_Junos_RPC.mck_attr')
@patch('salt.modules.junos.getattr') @patch('salt.modules.junos.getattr')
def test_rpc_get_interface_information_with_filter( def test_rpc_get_interface_information_with_kwargs(self, mock_attr, mck_attr):
self, mock_attr, mock_func): mock_attr.return_value = self.mck_attr
mock_attr.return_value = self.mock_func args = {'__pub_user': 'sudo_drajvi',
junos.rpc('get-interface-information', format='json', '__pub_arg': ['get-interface-information', '', 'text', {'terse': True}],
filter='<configuration><system/></configuration>') 'interface-name': 'lo0', '__pub_fun': 'junos.rpc', '__pub_jid': '20170307233617793012',
mock_func.assert_called_with({'format': 'json'}, '__pub_tgt': 'mac_min', '__pub_tgt_type': 'glob', '__pub_ret': ''}
dev_timeout=ANY, junos.rpc('get-interface-information', format='text', **args)
filter='<configuration><system/></configuration>') mck_attr.assert_called_with({'format': 'text'}, dev_timeout=30, terse=True)
@patch('tests.unit.modules.test_junos.Test_Junos_RPC.mock_func')
@patch('salt.modules.junos.getattr') @patch('salt.modules.junos.getattr')
def test_rpc_get_interface_information_exception( def test_rpc_get_interface_information_exception(
self, mock_attr, mock_func): self, mock_attr):
mock_attr.return_value = self.raise_exception mock_attr.return_value = self.raise_exception
ret = dict() ret = dict()
ret['message'] = 'RPC execution failed due to "Test exception"' ret['message'] = 'RPC execution failed due to "Test exception"'
ret['out'] = False ret['out'] = False
self.assertEqual(junos.rpc('get_interface_information'), ret) self.assertEqual(junos.rpc('get_interface_information'), ret)
@patch('salt.modules.junos.getattr')
def test_rpc_write_file_format_text(self, mock_attr):
mock_attr.side_effect = self.mck_attr
m = mock_open()
with patch('salt.modules.junos.fopen', m, create=True):
junos.rpc('get-chassis-inventory', '/path/to/file', format='text')
handle = m()
handle.write.assert_called_with('text rpc reply')
@patch('salt.modules.junos.json.dumps')
@patch('salt.modules.junos.getattr')
def test_rpc_write_file_format_json(self, mock_attr, mock_dumps):
mock_dumps.return_value = 'json rpc reply'
m = mock_open()
with patch('salt.modules.junos.fopen', m, create=True):
junos.rpc('get-chassis-inventory', '/path/to/file', format='json')
handle = m()
handle.write.assert_called_with('json rpc reply')
@patch('salt.modules.junos.jxmlease.parse')
@patch('salt.modules.junos.etree.tostring')
@patch('salt.modules.junos.getattr')
def test_rpc_write_file(self, mock_attr, mock_tostring, mock_parse):
mock_tostring.return_value = 'xml rpc reply'
m = mock_open()
with patch('salt.modules.junos.fopen', m, create=True):
junos.rpc('get-chassis-inventory', '/path/to/file')
handle = m()
handle.write.assert_called_with('xml rpc reply')