コード例 #1
0
 def test_init(self, mock_conn, mock_set):
     ssh_utility.SSHUtil('ip',
                         'user',
                         'pwd',
                         keyfilepath=None,
                         keyfiletype=None,
                         port=22,
                         timeout=10)
     mock_conn.assert_called_with('ip', 22, 'user', 'pwd')
コード例 #2
0
 def test_init_dsa(self, mock_conn, mock_set, mock_dss):
     ssh_utility.SSHUtil('ip',
                         'user',
                         'pwd',
                         keyfilepath='dummy',
                         keyfiletype='DSA',
                         port=22,
                         timeout=10)
     mock_conn.assert_called_with('ip', 22, 'user', mock_dss.return_value)
コード例 #3
0
 def test_init_negative(self, mock_set):
     with self.assertRaises(Exception) as context:
         ssh_utility.SSHUtil('ip',
                             'user',
                             'pwd',
                             keyfilepath=None,
                             keyfiletype=None,
                             port=22,
                             timeout=10)
         self.assertTrue('Connection timeout' in str(context.exception))
コード例 #4
0
    def test_scp(self, mock_open, mock_conn, mock_set):
        mock_sftp = mock.Mock()
        mock_open.return_value = mock_sftp

        client = ssh_utility.SSHUtil('ip',
                                     'user',
                                     'pwd',
                                     keyfilepath=None,
                                     keyfiletype=None,
                                     port=22,
                                     timeout=10)
        return_data = client.upload_file('source_file', 'dest_file')

        mock_sftp.put.assert_called_with('source_file', 'dest_file')
        self.assertEqual(return_data['status'], True)
コード例 #5
0
    def test_scp_negative(self, mock_conn, mock_set):
        with self.assertRaises(Exception) as context:
            mock_sftp = mock.Mock()

            client = ssh_utility.SSHUtil('ip',
                                         'user',
                                         'pwd',
                                         keyfilepath=None,
                                         keyfiletype=None,
                                         port=22,
                                         timeout=10)
            client.upload_file('source_file', 'dest_file')

            mock_sftp.put.assert_called_with('source_file', 'dest_file')
            self.assertTrue('Unable to upload the file to the remote server' in
                            str(context.exception))
コード例 #6
0
    def test_ssh_channel_exception(self, mock_conn, mock_set):
        with self.assertRaises(Exception) as context:
            mock_channel = mock.Mock()
            mock_channel.recv_exit_status.return_value = 0

            client = ssh_utility.SSHUtil('ip',
                                         'user',
                                         'pwd',
                                         keyfilepath=None,
                                         keyfiletype=None,
                                         port=22,
                                         timeout=10)
            client.execute_command('fake_command')
            mock_channel.recv_exit_status.assert_called_with()
            self.assertTrue(
                'Failed to execute the command!' in str(context.exception))
コード例 #7
0
    def test_ssh_channel_negative(self, mock_exec, mock_conn, mock_set):
        mock_channel = mock.Mock()
        mock_exec.return_value = (FakeChannelFile(['input']),
                                  FakeChannelFile(['info'], mock_channel),
                                  FakeChannelFile(['err']))
        mock_channel.recv_exit_status.return_value = -1

        client = ssh_utility.SSHUtil('ip',
                                     'user',
                                     'pwd',
                                     keyfilepath=None,
                                     keyfiletype=None,
                                     port=22,
                                     timeout=10)
        return_data = client.execute_command('fake_command')

        mock_exec.assert_called()
        mock_channel.recv_exit_status.assert_called_with()
        self.assertEqual(return_data['status'], False)