def test_download_file_owned_by_root(): "a file owned by root can be downloaded by the regular user if 'use_sudo' is True" with empty_local_fixture() as local_env: with remote_fixture() as remote_env: with test_settings(): # create a root-only file on remote machine remote_file_name = remote_env["temp-files"]["small-file"] file_contents = "root users only!\n" remote_sudo('printf "%s" > "%s"' % (file_contents, remote_file_name)) remote_sudo('chmod 600 "%s"' % remote_file_name) remote_sudo('chown root:root "%s"' % remote_file_name) local_file_name = join(local_env["temp-dir"], basename(remote_file_name)) # ensure remote root-only file cannot be downloaded by regular user. # in this case we own the directory but the file is owned by root. with pytest.raises(operations.NetworkError): download(remote_file_name, local_file_name) # download remote root-only file as regular user download(remote_file_name, local_file_name, use_sudo=True) assert os.path.exists(local_file_name) assert file_contents == open(local_file_name, "r").read()
def test_download_an_obvious_directory(): # you can't """attempting to download an obvious directory (trailing slash /) raises an exception. Its possible as both parallel-ssh and paramiko use SFTP, but not supported.""" with empty_local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): # ensure we're dealing with an obvious directory remote_dir = "%s/" % remote_env["temp-dir"].rstrip("/") with pytest.raises(ValueError): download(remote_dir, local_env["temp-dir"])
def test_download_a_directory(): # you can't "attempting to download a directory raises an exception." # its possible as both parallel-ssh and paramiko use SFTP, but unsupported in threadbare. with empty_local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): # it becomes ambiguous if remote path is a file or a directory remote_dir = remote_env["temp-dir"] assert not remote_dir.endswith("/") with pytest.raises(ValueError): download(remote_dir, local_env["temp-dir"])
def test_download_to_extant_local_file_no_overwrite(): "the default policy of overwriting files can be disabled when `override` is set to `False`." with local_fixture() as local_env: with remote_fixture() as remote_env: with test_settings(): local_file = local_env["temp-files"]["small-file"] remote_file = remote_env["temp-files"]["medium-file"] with pytest.raises(operations.NetworkError) as exc_info: download(remote_file, local_file, overwrite=False) expected_msg = ( "Local file exists and 'overwrite' is set to 'False'. Refusing to write: %s" % (local_file, )) assert expected_msg == str(exc_info.value)
def test_download_to_extant_local_file(): "the default policy is to overwrite files that exist." with local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): local_file = local_env["temp-files"]["small-file"] assert os.path.exists(local_file) payload = "foo" remote_file = join(remote_env["temp-dir"], "foo.file") remote('printf %s > "%s"' % (payload, remote_file)) assert remote_file_exists(remote_file) download(remote_file, local_file) result = open(local_file, "r").read() assert payload == result
def test_upload_and_download_a_file_using_byte_buffers(): """contents of a BytesIO buffer can be uploaded to a remote file, and the contents of a remote file can be downloaded to a BytesIO buffer""" with empty_remote_fixture() as remote_env: with test_settings(quiet=True): payload = b"foo-bar-baz" uploadable_unicode_buffer = BytesIO(payload) remote_file_name = join(remote_env["temp-dir"], "bytes-test") upload(uploadable_unicode_buffer, remote_file_name) assert remote_file_exists(remote_file_name) result = remote('cat "%s"' % remote_file_name) assert result["succeeded"] assert result["stdout"] == [payload.decode()] download_unicode_buffer = BytesIO() download(remote_file_name, download_unicode_buffer) assert download_unicode_buffer.getvalue() == payload
def test_download_a_file_to_a_directory(): "a file can be downloaded to a directory and the name of the remote file will be used as the destination file" with empty_local_fixture() as local_env: with remote_fixture() as remote_env: with test_settings(): local_dir = local_env["temp-dir"] remote_file = remote_env["temp-files"]["small-file"] expected_local_file = join(local_dir, basename(remote_file)) new_local_file = download(remote_file, local_dir) assert os.path.exists(expected_local_file) assert expected_local_file == new_local_file
def test_download_a_file_to_a_relative_directory(): "relative destinations are expanded to full paths before downloading" with remote_fixture() as remote_env: with empty_local_fixture() as local_env: with test_settings(): with lcd(local_env["temp-dir"]): remote_file = remote_env["temp-files"]["small-file"] expected_local_file = join(local_env["temp-dir"], basename(remote_file)) relative_dir = "." new_local_file = download(remote_file, relative_dir) assert expected_local_file == new_local_file assert os.path.exists(expected_local_file)
def _test_upload_and_download_a_file(transfer_protocol): """write a local file, upload it to the remote server, modify it remotely, download it, modify it locally, assert it's contents are as expected""" with empty_local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(transfer_protocol=transfer_protocol): LOG.debug("modifying local file ...") local_file_name = join(local_env["temp-dir"], "foo") local('printf "foo" > %s' % local_file_name) LOG.debug("uploading file ...") remote_file_name = join(remote_env["temp-dir"], "foobar") upload(local_file_name, remote_file_name) # verify contents assert remote_file_exists(remote_file_name) assert remote("cat %s" % remote_file_name)["stdout"] == ["foo"] LOG.debug("modifying remote file ...") remote('printf "bar" >> %s' % remote_file_name) # verify contents assert remote("cat %s" % remote_file_name)["stdout"] == ["foobar"] LOG.debug("downloading file ...") new_local_file_name = join(local_env["temp-dir"], "foobarbaz") download(remote_file_name, new_local_file_name) # verify contents assert open(new_local_file_name, "r").read() == "foobar" LOG.debug("modifying local file (again) ...") local('printf "baz" >> %s' % new_local_file_name) LOG.debug("testing local file ...") data = open(new_local_file_name, "r").read() assert "foobarbaz" == data
def test_download_a_file_to_a_non_existant_dir(): """downloading a file to directory that does not exist will see that directory structure created. note: scp and sftp as used by parallel-ssh appear to do this out of the box but rsync will not. this ensures behaviour is consistent across all transfer-protocols.""" with remote_fixture() as remote_env: with empty_local_fixture() as local_env: with test_settings(): with lcd(local_env["temp-dir"]): remote_file = remote_env["temp-files"]["small-file"] non_existant_dir = "does/not/exist" expected_local_file = join( local_env["temp-dir"], non_existant_dir, os.path.basename(remote_file), ) new_local_file = download(remote_file, expected_local_file) assert expected_local_file == new_local_file assert os.path.exists(expected_local_file)