def test_fn_run_pass_missing_parser_in_parser(self, mock_rpc): """ Check full module run using parser w/o a parser function defined in the parser defined in the parser """ mock_out = self._load_fixture("nxos_show_version.txt") class CliParser(CliParserBase): pass self._plugin._load_parser = MagicMock() self._plugin._load_parser.return_value = CliParser(None, None, None) mock_out = self._load_fixture("nxos_show_version.textfsm") mock_rpc.return_value = mock_out self._plugin._connection.socket_path = ( tempfile.NamedTemporaryFile().name) template_path = os.path.join(os.path.dirname(__file__), "fixtures", "nxos_empty_parser.textfsm") self._plugin._task.args = { "command": "show version", "parser": { "name": "ansible.utils.textfsm", "template_path": template_path, }, } task_vars = {"inventory_hostname": "mockdevice"} with self.assertRaises(Exception) as error: self._plugin.run(task_vars=task_vars) self.assertIn("Unhandled", str(error.exception))
def test_fn_run_pass_missing_parser_constants(self, mock_rpc): """ Check full module run using parser w/o DEFAULT_TEMPLATE_EXTENSION or PROVIDE_TEMPLATE_CONTENTS defined in the parser """ mock_out = self._load_fixture("nxos_show_version.txt") class CliParser(CliParserBase): def parse(self, *_args, **kwargs): return {"parsed": mock_out} self._plugin._load_parser = MagicMock() self._plugin._load_parser.return_value = CliParser(None, None, None) mock_out = self._load_fixture("nxos_show_version.txt") mock_rpc.return_value = mock_out self._plugin._connection.socket_path = ( tempfile.NamedTemporaryFile().name) template_path = os.path.join(os.path.dirname(__file__), "fixtures", "nxos_empty_parser.textfsm") self._plugin._task.args = { "command": "show version", "parser": { "name": "ansible.utils.textfsm", "template_path": template_path, }, } task_vars = {"inventory_hostname": "mockdevice"} result = self._plugin.run(task_vars=task_vars) self.assertEqual(result["stdout"], mock_out) self.assertEqual(result["stdout_lines"], mock_out.splitlines()) self.assertEqual(result["parsed"], mock_out)
def test_fn_run_fail_command(self): """ Confirm clean fail with rc 1 """ self._plugin._connection.socket_path = None self._plugin._low_level_execute_command = MagicMock() self._plugin._low_level_execute_command.return_value = { "rc": 1, "stdout": None, "stdout_lines": None, "stderr": None, } self._plugin._task.args = { "command": "ls", "parser": { "name": "a.b.c" }, } task_vars = {"inventory_hostname": "mockdevice"} result = self._plugin.run(task_vars=task_vars) expected = { "failed": True, "msg": None, "stdout": None, "stdout_lines": None, } self.assertEqual(result, expected)
def setUp(self): task = MagicMock(Task) play_context = MagicMock() play_context.check_mode = False connection = MagicMock() fake_loader = DictDataLoader({}) templar = Templar(loader=fake_loader) self._plugin = ActionModule( task=task, connection=connection, play_context=play_context, loader=fake_loader, templar=templar, shared_loader_obj=None, ) self._plugin._task.action = "cli_parse"
def test_fn_run_command_lx_rc0(self): """ Check run command for non network """ response = "abc" self._plugin._connection.socket_path = None self._plugin._low_level_execute_command = MagicMock() self._plugin._low_level_execute_command.return_value = { "rc": 0, "stdout": response, "stdout_lines": response, } self._plugin._task.args = {"command": "ls"} self._plugin._run_command() self.assertEqual(self._plugin._result["stdout"], response) self.assertEqual(self._plugin._result["stdout_lines"], response)
def test_fn_update_template_path_mock_find_needle(self): """ Check the creation of the template_path mock the find needle fn so the template doesn't need to be in the default template folder """ template_path = os.path.join(os.path.dirname(__file__), "fixtures", "nxos_show_version.yaml") self._plugin._find_needle = MagicMock() self._plugin._find_needle.return_value = template_path self._plugin._task.args = { "parser": { "command": "show version", "os": "nxos" } } self._plugin._update_template_path("yaml") self.assertEqual(self._plugin._task.args["parser"]["template_path"], template_path)