def setUp(self): super(TestEosConfigModule, self).setUp() self.mock_get_config = patch( 'ansible_collections.arista.eos.plugins.modules.eos_config.get_config' ) self.get_config = self.mock_get_config.start() self.mock_get_connection = patch( 'ansible_collections.arista.eos.plugins.modules.eos_config.get_connection' ) self.get_connection = self.mock_get_connection.start() self.mock_load_config = patch( 'ansible_collections.arista.eos.plugins.modules.eos_config.load_config' ) self.load_config = self.mock_load_config.start() self.mock_run_commands = patch( 'ansible_collections.arista.eos.plugins.modules.eos_config.run_commands' ) self.run_commands = self.mock_run_commands.start() self.mock_supports_sessions = patch( 'ansible_collections.arista.eos.plugins.cliconf.eos.Cliconf.supports_sessions' ) self.supports_sessions = self.mock_supports_sessions.start() self.mock_supports_sessions.return_value = True self.conn = self.get_connection() self.conn.edit_config = MagicMock() self.cliconf_obj = Cliconf(MagicMock()) self.running_config = load_fixture('eos_config_config.cfg')
def test_eos_config_no_change(self): lines = ['hostname localhost'] config = '\n'.join(lines) args = dict(lines=lines) set_module_args(args) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(config, config)) self.execute_module()
def test_eos_config_lines(self): lines = ['hostname switch01', 'ip domain-name eng.ansible.com'] args = dict(lines=lines) set_module_args(args) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(lines), self.running_config)) result = self.execute_module(changed=True) config = ['hostname switch01'] self.assertEqual(sorted(config), sorted(result['commands']), result['commands'])
def test_eos_config_src(self): src = load_fixture('eos_config_candidate.cfg') args = dict(src=src) set_module_args(args) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff(src, self.running_config)) result = self.execute_module(changed=True) config = ['hostname switch01', 'interface Ethernet1', 'description test interface', 'no shutdown', 'ip routing'] self.assertEqual(sorted(config), sorted(result['commands']), result['commands'])
def test_eos_config_parents(self): lines = ['ip address 1.2.3.4/5', 'no shutdown'] parents = ['interface Ethernet10'] args = dict(lines=lines, parents=parents) candidate = parents + lines set_module_args(args) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff('\n'.join(candidate), self.running_config)) result = self.execute_module(changed=True) config = ['interface Ethernet10', 'ip address 1.2.3.4/5', 'no shutdown'] self.assertEqual(config, result['commands'], result['commands'])
def test_eos_config_after(self): lines = ["hostname switch01", "ip domain-name eng.ansible.com"] args = dict(lines=lines, after=["after command"]) set_module_args(args) self.conn.get_diff = MagicMock(return_value=self.cliconf_obj.get_diff( "\n".join(lines), self.running_config)) result = self.execute_module(changed=True) config = ["after command", "hostname switch01"] self.assertEqual(sorted(config), sorted(result["commands"]), result["commands"]) self.assertEqual("after command", result["commands"][-1])
from ansible_collections.arista.eos.tests.unit.compat.mock import MagicMock from ansible.utils.path import unfrackpath mock_unfrackpath_noop = MagicMock(spec_set=unfrackpath, side_effect=lambda x, *args, **kwargs: x)