summaryrefslogtreecommitdiffstats
path: root/ipatests/test_integration/host.py
diff options
context:
space:
mode:
authorPetr Viktorin <pviktori@redhat.com>2013-08-12 15:38:32 +0200
committerMartin Kosek <mkosek@redhat.com>2013-08-13 15:42:48 +0200
commita8d2ec6677d97907c84751242620a3198484dc7b (patch)
treefa92728edf6308e659f38e87981f2a58c924f67b /ipatests/test_integration/host.py
parentb1474a53c0c249b5090fb067a3cdf99316989afe (diff)
downloadfreeipa-a8d2ec6677d97907c84751242620a3198484dc7b.tar.gz
freeipa-a8d2ec6677d97907c84751242620a3198484dc7b.tar.xz
freeipa-a8d2ec6677d97907c84751242620a3198484dc7b.zip
Allow freeipa-tests to work with older paramiko versions
The integration testing framework used Paramiko SFTP files as context managers. This feature is only available in Paramiko 1.10+. Use an explicit context manager so that we don't rely on the feature.
Diffstat (limited to 'ipatests/test_integration/host.py')
-rw-r--r--ipatests/test_integration/host.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/ipatests/test_integration/host.py b/ipatests/test_integration/host.py
index b4c7ffd63..1614b5fd3 100644
--- a/ipatests/test_integration/host.py
+++ b/ipatests/test_integration/host.py
@@ -23,6 +23,7 @@ import os
import socket
import threading
import subprocess
+from contextlib import contextmanager
import errno
import paramiko
@@ -118,6 +119,21 @@ class RemoteCommand(object):
return thread
+@contextmanager
+def sftp_open(sftp, filename, mode='r'):
+ """Context manager that provides a file-like object over a SFTP channel
+
+ This provides compatibility with older Paramiko versions.
+ (In Paramiko 1.10+, file objects from `sftp.open` are directly usable as
+ context managers).
+ """
+ file = sftp.open(filename, mode)
+ try:
+ yield file
+ finally:
+ file.close()
+
+
class Host(object):
"""Representation of a remote IPA host"""
def __init__(self, domain, hostname, role, index, ip=None):
@@ -314,13 +330,13 @@ class Host(object):
def get_file_contents(self, filename):
"""Read the named remote file and return the contents as a string"""
self.log.debug('READ %s', filename)
- with self.sftp.open(filename) as f:
+ with sftp_open(self.sftp, filename) as f:
return f.read()
def put_file_contents(self, filename, contents):
"""Write the given string to the named remote file"""
self.log.info('WRITE %s', filename)
- with self.sftp.open(filename, 'w') as f:
+ with sftp_open(self.sftp, filename, 'w') as f:
f.write(contents)
def file_exists(self, filename):