Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teuthology: Add support for seek and sync in write_file #2010

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion teuthology/orchestra/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,22 +265,34 @@ def read_file(self, path, sudo=False, stdout=None,


def write_file(self, path, data, sudo=False, mode=None, owner=None,
mkdir=False, append=False):
mkdir=False, append=False, bs=None,
offset=None, sync=False):
"""
Write data to remote file

The data written in 512-byte blocks, provide `bs` to use bigger blocks.

:param path: file path on remote host
:param data: str, binary or fileobj to be written
:param sudo: use sudo to write file, defaults False
:param mode: set file mode bits if provided
:param owner: set file owner if provided
:param mkdir: preliminary create the file directory, defaults False
:param append: append data to the file, defaults False
:param bs: write up to N bytes at a time if provided, default is 512 in `dd`
:param offset: number of bs blocks to seek to in file, defaults 0
:param sync: sync file after write is complete if provided
"""
dd = 'sudo dd' if sudo else 'dd'
args = dd + ' of=' + path
if append:
args += ' conv=notrunc oflag=append'
if bs:
args += ' bs=' + str(bs)
if offset:
args += ' seek=' + str(offset)
if sync:
args += ' conv=sync'
kshtsk marked this conversation as resolved.
Show resolved Hide resolved
if mkdir:
mkdirp = 'sudo mkdir -p' if sudo else 'mkdir -p'
dirpath = os.path.dirname(path)
Expand Down
12 changes: 12 additions & 0 deletions teuthology/orchestra/test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,15 @@ def test_is_container(self):
rem2 = remote.Remote(name='[email protected]', ssh=self.m_ssh)
rem2._runner = m_run
assert not rem2.is_container

@patch("teuthology.orchestra.remote.Remote.run")
def test_write_file(self, m_run):
file = "fakefile"
contents = "fakecontents"
rem = remote.Remote(name='[email protected]', ssh=self.m_ssh)

remote.Remote.write_file(rem, file, contents, bs=1, offset=1024)
m_run.assert_called_with(args=f"set -ex\ndd of={file} bs=1 seek=1024", stdin=contents, quiet=True)

remote.Remote.write_file(rem, file, contents, sync=True)
m_run.assert_called_with(args=f"set -ex\ndd of={file} conv=sync", stdin=contents, quiet=True)