コード例 #1
0
 def test_copy_returns_expected_response(self):
     expected = Response(raw_output=self._DESTINATION.encode(),
                         returncode=0)
     self.mock_shutil_copy_rval(self._DESTINATION)
     client = LocalShellClient(self._HOST, self._USER)
     res = client.copy(self._SOURCE, self._DESTINATION)
     self.assertEqual(res, expected)
コード例 #2
0
 def _exec_command_on_client_blocking(self, command):
     """
     :param command:
     :type command: str
     :return:
     :rtype: Response
     """
     proc = Popen_with_delayed_expansion(command, shell=True, stdout=PIPE, stderr=PIPE)
     self._logger.debug('popen blocking [{}:{}]: {}'.format(self.user, self.host, command))
     output, error = proc.communicate()
     return Response(raw_output=output, raw_error=error, returncode=proc.returncode)
コード例 #3
0
 def _copy_on_client(self, source, destination):
     """
     :param source:
     :type source: str
     :param destination:
     :type destination: str
     :return:
     :rtype: Response
     """
     new_location = shutil.copy(source, destination)
     # todo detect failure and specify returncode and error
     return Response(raw_output=new_location.encode(), returncode=0)
コード例 #4
0
 def _exec_command_on_client_blocking(self, command):
     """
     :type command: str
     :rtype: Response
     """
     escaped_command = self._escaped_ssh_command(command)
     self._logger.debug('SSH popen blocking [{}:{}]: {}'.format(
         self.user, self.host, escaped_command))
     proc = Popen(escaped_command, shell=True, stdout=PIPE, stderr=PIPE)
     output, error = proc.communicate()
     return Response(raw_output=output,
                     raw_error=error,
                     returncode=proc.returncode)
コード例 #5
0
 def _copy_on_client(self, source, destination):
     """
     :type source: str
     :type destination: str
     :rtype: Response
     """
     # Avoid any ssh known_hosts prompts.
     command = 'scp -o StrictHostKeyChecking=no {} {}:{}'.format(
         source, self._host_string(), destination)
     self._logger.debug('SCP popen blocking [{}:{}]: {}'.format(
         self.user, self.host, command))
     proc = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
     output, error = proc.communicate()
     return Response(raw_output=output,
                     raw_error=error,
                     returncode=proc.returncode)
コード例 #6
0
    def test_copy(self, source, dest, error_on_failure, copy_successful,
                  expect_runtime_error):
        # Arrange
        client = ShellClient(self._HOST, self._USER)
        mock_copy_on_client = self.patch(
            'app.util.shell.shell_client.ShellClient._copy_on_client')
        res = Response(returncode=0 if copy_successful else 1)
        mock_copy_on_client.return_value = res

        # Act
        if expect_runtime_error:
            with self.assertRaises(RuntimeError):
                client.copy(source, dest, error_on_failure)
        else:
            self.assertEqual(client.copy(source, dest, error_on_failure), res)

        # Assert
        mock_copy_on_client.assert_called_once_with(client, source, dest)
コード例 #7
0
class TestRemoteShellClient(BaseUnitTestCase):
    def setUp(self):
        super().setUp()
        self.mock_Popen = self.patch('app.util.shell.remote_shell_client.Popen_with_delayed_expansion')

    @genty_dataset(
        normal_response=(False, Response(raw_output=b'\ncat', raw_error=b'\ndog', returncode=0)),
        async_response=(True, EmptyResponse())
    )
    def test_exec_command_returns_expected(self, async_enabled, response):
        self.mock_popen_communicate_call(stdout=b'\ncat', stderr=b'\ndog')
        client = RemoteShellClient('host', 'user')
        res = client.exec_command('ls', async=async_enabled)
        self.assertEqual(res, response)

    def mock_popen_communicate_call(self, stdout=b'\n', stderr=b'', returncode=0):
        mock_popen = self.mock_Popen.return_value
        mock_popen.communicate.return_value = stdout, stderr
        mock_popen.returncode = returncode
コード例 #8
0
class TestLocalShellClient(BaseUnitTestCase):

    _HOST = 'host'
    _USER = '******'
    _SOURCE = 'source'
    _DESTINATION = 'destination'

    def setUp(self):
        super().setUp()
        self.mock_shutil = self.patch(
            'app.util.shell.local_shell_client.shutil')
        self.mock_Popen = self.patch('app.util.shell.local_shell_client.Popen')

    @genty_dataset(empty_response=(True, EmptyResponse()),
                   normal_response=(False, Response()))
    def test_exec_command_returns_expected_response(self, async, expected):
        self.create_mock_popen()
        client = LocalShellClient(self._HOST, self._USER)
        res = client.exec_command('ls', async=async)