示例#1
0
def test_receive_flush(extra, dirty):
    size = 4096
    dst = memory.Backend("r+", b"a" * size)
    src = io.BytesIO(b"b" * size)
    op = ops.Receive(dst, src, size, **extra)
    op.run()
    assert dst.dirty == dirty
示例#2
0
def test_receive_unbuffered_stream_partial_content(user_file):
    chunks = [b"a" * 8192, b"b" * 42, b"c" * (8192 - 42)]
    src = util.UnbufferedStream(chunks)
    size = sum(len(c) for c in chunks)

    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, size + 1)
        with pytest.raises(errors.PartialContent):
            op.run()
示例#3
0
def test_receive_seek():
    dst = memory.Backend("r+", b"a" * 10)
    dst.seek(8)
    src = io.BytesIO(b"b" * 5)
    op = ops.Receive(dst, src, 5)
    op.run()
    dst.seek(0)
    b = bytearray(11)
    n = dst.readinto(b)
    assert n == 10
    assert b == b"bbbbbaaaaa\0"
示例#4
0
def test_receive_partial_content(user_file, offset, size):
    with io.open(user_file.path, "wb") as f:
        f.truncate(size + offset)

    src = io.BytesIO(b"x" * (size - 1))
    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, size, offset=offset)
        with pytest.raises(errors.PartialContent) as e:
            op.run()

    assert e.value.requested == size
    assert e.value.available == size - 1
示例#5
0
def test_receive_unbuffered_stream(user_file):
    chunks = [b"a" * 8192, b"b" * 42, b"c" * (8192 - 42)]
    src = util.UnbufferedStream(chunks)
    size = sum(len(c) for c in chunks)

    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, size)
        op.run()

    with io.open(user_file.path, "rb") as f:
        for c in chunks:
            assert f.read(len(c)) == c
        assert f.read() == b""
示例#6
0
def test_receive_no_size(user_file, offset, size):
    with io.open(user_file.path, "wb") as f:
        f.truncate(offset + size)

    src = io.BytesIO(b"x" * size)
    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, offset=offset)
        op.run()

    with io.open(user_file.path, "rb") as f:
        assert f.read(offset) == b"\0" * offset
        assert f.read(size) == src.getvalue()

        file_size = os.path.getsize(user_file.path)
        trailer = file_size - offset - size
        assert file_size % user_file.sector_size == 0
        assert f.read() == b"\0" * trailer
示例#7
0
def test_receive_inside(user_file, offset, size):
    trailer = 8192
    with io.open(user_file.path, "wb") as f:
        f.truncate(offset + size + trailer)

    src = io.BytesIO(b"x" * size)
    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, size, offset=offset)
        op.run()

    with io.open(user_file.path, "rb") as f:
        # Nothing is written before offset.
        assert f.read(offset) == b"\0" * offset

        # All data was written.
        assert f.read(size) == src.getvalue()

        # Nothing was written after offset + size, and file size is not
        # modified.
        assert f.read() == b"\0" * trailer
示例#8
0
def test_receive_new(user_file, offset, size, preallocated):
    with io.open(user_file.path, "wb") as f:
        if preallocated:
            f.truncate(offset + size)

    src = io.BytesIO(b"x" * size)
    with file.open(user_file.url, "r+") as dst:
        op = ops.Receive(dst, src, size, offset=offset)
        op.run()

    with io.open(user_file.path, "rb") as f:
        # Nothing is written before offset.
        assert f.read(offset) == b"\0" * offset

        # All data was written.
        assert f.read(size) == src.getvalue()

        # Writing to unaligned size align file size by padding zeroes.
        file_size = os.path.getsize(user_file.path)
        trailer = file_size - offset - size
        assert file_size % user_file.sector_size == 0
        assert f.read() == b"\0" * trailer
示例#9
0
    def put(self, req, resp, ticket_id):
        if not ticket_id:
            raise http.Error(http.BAD_REQUEST, "Ticket id is required")

        size = req.content_length
        if size is None:
            raise http.Error(
                http.BAD_REQUEST, "Content-Length header is required")

        offset = req.content_range.first if req.content_range else 0

        # For backward compatibility, we flush by default.
        flush = validate.enum(req.query, "flush", ("y", "n"), default="y")
        flush = (flush == "y")

        try:
            ticket = auth.authorize(ticket_id, "write")
        except errors.AuthorizationError as e:
            raise http.Error(http.FORBIDDEN, str(e))

        validate.allowed_range(offset, size, ticket)

        log.info(
            "[%s] WRITE size=%d offset=%d flush=%s ticket=%s",
            req.client_addr, size, offset, flush, ticket_id)

        op = ops.Receive(
            self._backend(req, ticket),
            req,
            size,
            offset=offset,
            flush=flush,
            buffersize=self.config.daemon.buffer_size,
            clock=req.clock)
        try:
            ticket.run(op)
        except errors.PartialContent as e:
            raise http.Error(http.BAD_REQUEST, str(e))
示例#10
0
def test_recv_repr():
    op = ops.Receive(None, None, 100, offset=42)
    rep = repr(op)
    assert "Receive" in rep
    assert "size=100 offset=42 buffersize=4096 done=0" in rep