def test_async_subprocess_exec(self): with mock.patch.object( asyncio, 'create_subprocess_exec', mock.Mock(wraps=self._mock_async_subprocess)) as m: loop = asyncio.get_event_loop() actual = loop.run_until_complete( subprocess_workflow.exec_and_parse_subprocesses_async( self.mock_params, self._mock_arg_factory, self.mock_parser_callback, )) self.assertEqual([42, 42], list(actual)) m.assert_has_calls([ mock.call( 'param1', 'a', 'b', stdout=subprocess.PIPE, stderr=subprocess.PIPE, loop=loop, ), mock.call( 'param2', 'a', 'b', stdout=subprocess.PIPE, stderr=subprocess.PIPE, loop=loop, ) ]) self.mock_parser_callback.assert_has_calls([ mock.call('stdout', 'stderr', 'param1'), mock.call('stdout', 'stderr', 'param2'), ])
def _ovsdb_dump_async(self, table: str, columns: List[str]): """ Execute ovsdb-client dump command asynchronously and parse stdout results. """ params = [OVSDBDumpCommandParams(table=table, columns=columns)] return subprocess_workflow.exec_and_parse_subprocesses_async( params, _get_ovsdb_dump_params, _parse_ovsdb_dump_output, self._loop, )
def test_async_subprocess_empty_params(self): with mock.patch.object( asyncio, 'create_subprocess_exec', mock.Mock(wraps=self._mock_async_subprocess)) as m: loop = asyncio.get_event_loop() actual = loop.run_until_complete( subprocess_workflow.exec_and_parse_subprocesses_async( [], self._mock_arg_factory, self.mock_parser_callback, )) self.assertEqual([], list(actual)) m.assert_not_called() self.mock_parser_callback.assert_not_called()
def traceroute_async(params, loop=None): """ Execute some `traceroute` commands asynchronously and return results. Args: params ([TracerouteParams]): params for the `traceroute` commands loop: event loop to run in (optional) Returns: [TracerouteResult]: stats from the executed `traceroute` commands """ return subprocess_workflow.exec_and_parse_subprocesses_async( params, _get_traceroute_command_args_list, parse_traceroute_output, loop=loop, )
def get_kernel_versions_async(loop=None): """ Execute dpkg commands asynchronously. Args: loop: asyncio event loop (optional) Returns: [DpkgCommandResult]: stats from the executed dpkg commands """ return subprocess_workflow.exec_and_parse_subprocesses_async( [DpkgCommandParams()], _get_dpkg_command_args_list, parse_dpkg_output, loop, )
def test_async_subprocess_exec_exception(self): @asyncio.coroutine # pylint: disable=unused-argument def mock_subprocess_raises(*args, **kwargs): raise ValueError('oops') with mock.patch.object(asyncio, 'create_subprocess_exec', mock.Mock(wraps=mock_subprocess_raises)): loop = asyncio.get_event_loop() with self.assertRaises(ValueError) as e: loop.run_until_complete( subprocess_workflow.exec_and_parse_subprocesses_async( self.mock_params, self._mock_arg_factory, self.mock_parser_callback, )) self.assertEqual('oops', e.exception.args[0])
def ping_async(ping_params, loop=None): """ Execute ping commands asynchronously. Args: ping_params ([PingCommandParams]): params for the pings to execute loop: asyncio event loop (optional) Returns: [PingCommandResult]: stats from the executed ping commands """ return subprocess_workflow.exec_and_parse_subprocesses_async( ping_params, _get_ping_command_args_list, parse_ping_output, loop, )
def test_async_subprocess_communicate_exception(self): @asyncio.coroutine # pylint: disable=unused-argument def mock_communicate_raises(*args, **kwargs): raise ValueError('oops') self.process_mock.configure_mock(communicate=mock_communicate_raises) with mock.patch.object( asyncio, 'create_subprocess_exec', mock.Mock(wraps=self._mock_async_subprocess), ) as m: loop = asyncio.get_event_loop() with self.assertRaises(ValueError) as e: loop.run_until_complete( subprocess_workflow.exec_and_parse_subprocesses_async( self.mock_params, self._mock_arg_factory, self.mock_parser_callback, ), ) self.assertEqual('oops', e.exception.args[0]) m.assert_has_calls([ mock.call( 'param1', 'a', 'b', stdout=subprocess.PIPE, stderr=subprocess.PIPE, loop=loop, ), mock.call( 'param2', 'a', 'b', stdout=subprocess.PIPE, stderr=subprocess.PIPE, loop=loop, ), ])