Beispiel #1
0
def test_check_remote_files():
    "check that remote files can be found (or not)"
    with remote_fixture() as remote_env:
        with test_settings():
            file_that_exists = join(remote_env["temp-files"]["small-file"])
            file_that_does_not_exist = join(remote_env["temp-dir"],
                                            "doesnot.exist")
            assert remote_file_exists(file_that_exists)
            assert not remote_file_exists(file_that_does_not_exist)
Beispiel #2
0
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)
Beispiel #3
0
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"]
Beispiel #4
0
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)
Beispiel #5
0
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
Beispiel #6
0
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)
Beispiel #7
0
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
Beispiel #8
0
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
Beispiel #9
0
 def workerfn():
     with state.settings() as env:
         return remote_file_exists(env["remote_file"], use_sudo=True)