Fix `sshutils.put_file' to use sftp context

Change-Id: Ib6e35b7b1c33a8aa9be00489e03ecbb9c6380287
Implements: blueprint vm-workloads-framework
This commit is contained in:
Pavel Boldin 2015-04-23 23:23:23 +03:00
parent 6761385d37
commit 9d29f151de
2 changed files with 11 additions and 10 deletions

View File

@ -271,9 +271,8 @@ class SSH(object):
client = self._get_client()
sftp = client.open_sftp()
sftp.put(localpath, remotepath)
if mode is None:
mode = 0o777 & os.stat(localpath).st_mode
sftp.chmod(remotepath, mode)
sftp.close()
with client.open_sftp() as sftp:
sftp.put(localpath, remotepath)
if mode is None:
mode = 0o777 & os.stat(localpath).st_mode
sftp.chmod(remotepath, mode)

View File

@ -274,7 +274,8 @@ class SSHRunTestCase(test.TestCase):
@mock.patch("rally.common.sshutils.os.stat")
def test_put_file(self, mock_stat):
sftp = self.fake_client.open_sftp.return_value = mock.Mock()
sftp = self.fake_client.open_sftp.return_value = mock.MagicMock()
sftp.__enter__.return_value = sftp
mock_stat.return_value = os.stat_result([0o753] + [0] * 9)
@ -283,13 +284,14 @@ class SSHRunTestCase(test.TestCase):
sftp.put.assert_called_once_with("localfile", "remotefile")
mock_stat.assert_called_once_with("localfile")
sftp.chmod.assert_called_once_with("remotefile", 0o753)
sftp.close.assert_called_once_with()
sftp.__exit__.assert_called_once_with(None, None, None)
def test_put_file_mode(self):
sftp = self.fake_client.open_sftp.return_value = mock.Mock()
sftp = self.fake_client.open_sftp.return_value = mock.MagicMock()
sftp.__enter__.return_value = sftp
self.ssh.put_file("localfile", "remotefile", mode=0o753)
sftp.put.assert_called_once_with("localfile", "remotefile")
sftp.chmod.assert_called_once_with("remotefile", 0o753)
sftp.close.assert_called_once_with()
sftp.__exit__.assert_called_once_with(None, None, None)