From a7f78d152fd0f3f134f738b520ecf14e363815e0 Mon Sep 17 00:00:00 2001 From: Zack Cerza Date: Wed, 18 May 2016 09:05:04 -0600 Subject: [PATCH] Add Remote._sftp_get_size() Signed-off-by: Zack Cerza --- teuthology/orchestra/remote.py | 7 +++++++ teuthology/orchestra/test/test_remote.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/teuthology/orchestra/remote.py b/teuthology/orchestra/remote.py index 668ba56ff8..39f2d05a4c 100644 --- a/teuthology/orchestra/remote.py +++ b/teuthology/orchestra/remote.py @@ -276,6 +276,13 @@ class Remote(object): sftp = self.ssh.open_sftp() return sftp.open(remote_path) + def _sftp_get_size(self, remote_path): + """ + Via _sftp_open_file, return the filesize in bytes + """ + with self._sftp_open_file(remote_path) as f: + return f.stat().st_size + def remove(self, path): self.run(args=['rm', '-fr', path]) diff --git a/teuthology/orchestra/test/test_remote.py b/teuthology/orchestra/test/test_remote.py index 612a0ca400..fd693ec059 100644 --- a/teuthology/orchestra/test/test_remote.py +++ b/teuthology/orchestra/test/test_remote.py @@ -163,3 +163,16 @@ class TestRemote(object): assert rem._sftp_open_file('x').stat().st_size == 42 with rem._sftp_open_file('x') as f: assert f == m_file_obj + + def test_sftp_get_size(self): + m_file_obj = MagicMock() + m_stat = Mock() + m_stat.st_size = 42 + m_file_obj.stat.return_value = m_stat + m_open = MagicMock() + m_open.return_value = m_file_obj + m_open.return_value.__enter__.return_value = m_file_obj + with patch.object(remote.Remote, '_sftp_open_file', new=m_open): + rem = remote.Remote(name='jdoe@xyzzy.example.com', ssh=self.m_ssh) + assert rem._sftp_get_size('/fake/file') == 42 + -- 2.39.5