def get_sftp_connection(self): if self.use_password: return get_sftp_connection(self.cluster.hostname, self.username, password=self.password, port=self.cluster.port) else: try: del self.user._profile_cache except: pass private = StringIO(self.user.private_key) return get_sftp_connection(self.cluster.hostname, self.username, key=private, port=self.cluster.port)
def test_get_sftp_key(self): test_path = os.path.join(settings.MEDIA_ROOT, "tests") with open(os.path.join(test_path, "id_rsa"), 'r') as key: sftp = get_sftp_connection("localhost", "vagrant", key=key, port=2222) with sftp: pass
def test_get_sftp_key(self, mock_sftp, mock_paramiko): test_path = os.path.join(settings.MEDIA_ROOT, "tests") with open(os.path.join(test_path, "id_rsa"), 'r') as key: sftp = get_sftp_connection(SERVER['hostname'], SERVER['username'], key=key, port=SERVER['port']) mock_paramiko.RSAKey.from_private_key.assert_called_once() mock_connect = mock_paramiko.Transport().connect.call_args self.assertEqual(mock_connect[1]['username'], SERVER['username']) mock_sftp.from_transport.assert_called_once() with sftp: pass
def test_get_sftp_password(self, mock_sftp, mock_paramiko): sftp = get_sftp_connection(SERVER['hostname'], SERVER['username'], password=SERVER['password'], port=SERVER['port']) expected = mock.call(password=SERVER['password'], username=SERVER['username']) self.assertEqual(mock_paramiko.Transport().connect.call_args, expected) mock_sftp.from_transport.assert_called_once() with sftp: pass
def test_get_sftp_error(self): with self.assertRaises(Exception): with get_sftp_connection("localhost", "username"): pass
def test_get_sftp_password(self): sftp = get_sftp_connection("localhost", "vagrant", password="******", port=2222) with sftp: pass