def test_upload_a_directory(): # you can't "attempting to upload a directory raises an exception" with empty_local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): with pytest.raises(ValueError): upload(local_env["temp-dir"], remote_env["temp-dir"])
def test_upload_to_non_existant_remote_dir(): "intermediate non-existant directories in a remote path will be created." with local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): non_existant_dir = "does/not/exist" local_file = local_env["temp-files"]["small-file"] remote_file_name = os.path.basename(local_file) expected_remote_file = join(remote_env["temp-dir"], non_existant_dir, remote_file_name) upload(local_file, expected_remote_file) assert remote_file_exists(expected_remote_file)
def test_upload_to_extant_remote_file(): "the default policy is to overwrite files that exist." with empty_local_fixture(): with remote_fixture() as remote_env: with test_settings(): payload = b"foo" remote_file = remote_env["temp-files"]["small-file"] # just to illustrate an overwrite *is* happening assert remote_file_exists(remote_file) upload(BytesIO(payload), remote_file) result = remote('cat "%s"' % (remote_file, )) assert [payload.decode("utf-8")] == result["stdout"]
def test_run_script(): "a simple shell script can be uploaded and executed and the results accessible" with empty_local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): local_script = join(local_env["temp-dir"], "script.sh") open(local_script, "w").write(r"""#!/bin/bash echo "hello, world" """) remote_script = join(remote_env["temp-dir"], "script.sh") upload(local_script, remote_script) remote("chmod +x %s" % remote_script) with rcd(os.path.dirname(remote_script)): result = remote("./script.sh") assert ["hello, world"] == result["stdout"]
def test_upload_to_extant_remote_file_no_overwrite(): "the default policy of overwriting files can be disabled when `override` is set to `False`." with empty_local_fixture(): with remote_fixture() as remote_env: with test_settings(): payload = b"foo" remote_file = remote_env["temp-files"]["small-file"] assert remote_file_exists(remote_file) with pytest.raises(operations.NetworkError) as exc_info: upload(BytesIO(payload), remote_file, overwrite=False) expected_msg = ( "Remote file exists and 'overwrite' is set to 'False'. Refusing to write: %s" % (remote_file, )) assert expected_msg == str(exc_info.value)
def test_upload_file_to_root_dir(): "uploads a file as a regular user to a root-owned directory with `use_sudo`" with local_fixture() as local_env: with empty_remote_fixture() as remote_env: with test_settings(): remote_sudo('chown root:root -R "%s"' % remote_env["temp-dir"]) local_file_name = local_env["temp-files"]["small-file"] remote_file_name = join(remote_env["temp-dir"], basename(local_file_name)) # upload file to root-owned directory with pytest.raises(operations.NetworkError): upload(local_file_name, remote_file_name) upload(local_file_name, remote_file_name, use_sudo=True) assert remote_file_exists(remote_file_name, use_sudo=True)
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_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 workerfn(): upload(local_script, remote_script) remote("chmod +x %s" % remote_script) with rcd(os.path.dirname(remote_script)): return remote("./script.sh")