コード例 #1
0
def test_debugging_interface(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        assert b.readable()
        assert b.writable()
        assert b.sparse
        assert b.name == "nbd"
コード例 #2
0
def test_dirty(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        # backend created clean
        assert not b.dirty

        # write and zero dirty the backend
        b.write(b"01234")
        assert b.dirty

        b.flush()
        assert not b.dirty

        b.zero(5)
        assert b.dirty

        b.flush()
        assert not b.dirty

        # readinto, seek do not affect dirty.
        b.seek(0)
        assert not b.dirty

        with closing(util.aligned_buffer(10)) as buf:
            b.readinto(buf)
        assert not b.dirty
コード例 #3
0
def test_debugging_interface(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        assert b.readable()
        assert b.writable()
        assert b.sparse
        assert b.name == "nbd"
コード例 #4
0
def test_size(nbd_server, fmt):
    nbd_server.fmt = fmt
    subprocess.check_call(
        ["qemu-img", "create", "-f", fmt, nbd_server.image, "150m"])
    nbd_server.start()
    with nbd.open(nbd_server.url, "r") as b:
        assert b.size() == 150 * 1024**2
コード例 #5
0
def test_dirty(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        # backend created clean
        assert not b.dirty

        # write and zero dirty the backend
        b.write(b"01234")
        assert b.dirty

        b.flush()
        assert not b.dirty

        b.zero(5)
        assert b.dirty

        b.flush()
        assert not b.dirty

        # readinto, seek do not affect dirty.
        b.seek(0)
        assert not b.dirty

        with closing(util.aligned_buffer(10)) as buf:
            b.readinto(buf)
        assert not b.dirty
コード例 #6
0
def test_open_read_larger_buffer(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r") as b:
        # buffer larger than remaining data
        buffer_length = 10
        data_length = 5
        with closing(util.aligned_buffer(buffer_length)) as buf:
            b.seek(b._client.export_size - data_length)
            assert b.readinto(buf) == data_length
        b.flush()
コード例 #7
0
def test_open_read_larger_buffer(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r") as b:
        # buffer larger than remaining data
        buffer_length = 10
        data_length = 5
        with closing(util.aligned_buffer(buffer_length)) as buf:
            b.seek(b._client.export_size - data_length)
            assert b.readinto(buf) == data_length
        b.flush()
コード例 #8
0
def test_zero_middle(nbd_server, sparse):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+", sparse=sparse) as b:
        # nbd backend is always sparse.
        assert b.sparse

        b.write(b"xxxxxxxxxxxx")
        b.seek(4)
        assert b.zero(4) == 4

        with closing(util.aligned_buffer(12)) as buf:
            b.seek(0)
            assert b.readinto(buf) == 12
            assert buf[:] == b"xxxx\x00\x00\x00\x00xxxx"
コード例 #9
0
def test_write_to_length(nbd_server, length):
    # Create a sparse file for fastest reading.
    with io.open(nbd_server.image, "wb") as f:
        f.truncate(33 * 1024**2)

    nbd_server.start()

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r") as b:
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == length
        assert dst.tell() == length
コード例 #10
0
def test_write_to_length(nbd_server, length):
    # Create a sparse file for fastest reading.
    with io.open(nbd_server.image, "wb") as f:
        f.truncate(33 * 1024**2)

    nbd_server.start()

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r") as b:
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == length
        assert dst.tell() == length
コード例 #11
0
def test_zero_middle(nbd_server, sparse):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+", sparse=sparse) as b:
        # nbd backend is always sparse.
        assert b.sparse

        b.write(b"xxxxxxxxxxxx")
        b.seek(4)
        assert b.zero(4) == 4

        with closing(util.aligned_buffer(12)) as buf:
            b.seek(0)
            assert b.readinto(buf) == 12
            assert buf[:] == b"xxxx\x00\x00\x00\x00xxxx"
コード例 #12
0
def test_write_to_offset(nbd_server, offset):
    with io.open(nbd_server.image, "wb") as f:
        f.truncate(16 * 1024)

    nbd_server.start()
    length = 4096

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r") as b:
        b.seek(offset)
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == offset + length
        assert dst.tell() == length
コード例 #13
0
def test_close(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        pass

    # Closing twice does not do anything.
    b.close()

    # But other operations should fail now with:
    #     socket.error: Bad file descriptor
    with pytest.raises(IOError):
        b.write("more")
    with pytest.raises(IOError):
        with closing(util.aligned_buffer(100)) as buf:
            b.readinto(buf)
コード例 #14
0
def test_close(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        pass

    # Closing twice does not do anything.
    b.close()

    # But other operations should fail now with:
    #     socket.error: Bad file descriptor
    with pytest.raises(IOError):
        b.write("more")
    with pytest.raises(IOError):
        with closing(util.aligned_buffer(100)) as buf:
            b.readinto(buf)
コード例 #15
0
def test_write_to_offset(nbd_server, offset):
    with io.open(nbd_server.image, "wb") as f:
        f.truncate(16 * 1024)

    nbd_server.start()
    length = 4096

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r") as b:
        b.seek(offset)
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == offset + length
        assert dst.tell() == length
コード例 #16
0
def test_open_writeonly(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "w") as b:
        assert not b.readable()
        assert b.writable()

        data = b"data"
        b.write(data)
        assert b.tell() == len(data)

        with pytest.raises(IOError):
            with closing(util.aligned_buffer(100)) as buf:
                b.readinto(buf)

        dst = io.BytesIO()
        with pytest.raises(IOError):
            b.write_to(dst, 100)

        b.flush()
コード例 #17
0
def test_open_read_write(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        assert b.readable()
        assert b.writable()

        data = b"data"
        b.write(data)
        assert b.tell() == len(data)

        b.zero(4)
        size = len(data) + 4
        assert b.tell() == size

        with closing(util.aligned_buffer(size)) as buf:
            b.seek(0)
            assert b.readinto(buf) == size
            assert buf[:] == data + b"\0" * 4
        b.flush()
コード例 #18
0
def test_open_writeonly(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "w") as b:
        assert not b.readable()
        assert b.writable()

        data = b"data"
        b.write(data)
        assert b.tell() == len(data)

        with pytest.raises(IOError):
            with closing(util.aligned_buffer(100)) as buf:
                b.readinto(buf)

        dst = io.BytesIO()
        with pytest.raises(IOError):
            b.write_to(dst, 100)

        b.flush()
コード例 #19
0
def test_open_read_write(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        assert b.readable()
        assert b.writable()

        data = b"data"
        b.write(data)
        assert b.tell() == len(data)

        b.zero(4)
        size = len(data) + 4
        assert b.tell() == size

        with closing(util.aligned_buffer(size)) as buf:
            b.seek(0)
            assert b.readinto(buf) == size
            assert buf[:] == data + b"\0" * 4
        b.flush()
コード例 #20
0
def test_write_to_buffer_size(nbd_server, buffer_size):
    length = 16 * 1024

    with io.open(nbd_server.image, "wb") as f:
        f.truncate(length)
        for i in range(0, length, 1024):
            f.seek(i)
            f.write(b"%d\n" % i)

    nbd_server.start()

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r", buffer_size=buffer_size) as b:
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == length

    with io.open(nbd_server.image, "rb") as f:
        assert f.read() == dst.getvalue()
コード例 #21
0
def test_write_to_buffer_size(nbd_server, buffer_size):
    length = 16 * 1024

    with io.open(nbd_server.image, "wb") as f:
        f.truncate(length)
        for i in range(0, length, 1024):
            f.seek(i)
            f.write(b"%d\n" % i)

    nbd_server.start()

    dst = io.BytesIO()
    with nbd.open(nbd_server.url, "r", buffer_size=buffer_size) as b:
        n = b.write_to(dst, length)

        assert n == length
        assert b.tell() == length

    with io.open(nbd_server.image, "rb") as f:
        assert f.read() == dst.getvalue()
コード例 #22
0
def test_open_readonly(nbd_server):
    nbd_server.read_only = True
    nbd_server.start()
    with nbd.open(nbd_server.url, "r") as b:
        assert b.readable()
        assert not b.writable()

        with pytest.raises(IOError):
            b.write(b"data")
        assert b.tell() == 0

        with pytest.raises(IOError):
            b.zero(4)
        assert b.tell() == 0

        with closing(util.aligned_buffer(100)) as buf:
            buf.write(b"x" * 100)
            assert b.readinto(buf) == len(buf)
            assert buf[:] == b"\0" * len(buf)

        b.flush()
コード例 #23
0
def test_open_readonly(nbd_server):
    nbd_server.read_only = True
    nbd_server.start()
    with nbd.open(nbd_server.url, "r") as b:
        assert b.readable()
        assert not b.writable()

        with pytest.raises(IOError):
            b.write(b"data")
        assert b.tell() == 0

        with pytest.raises(IOError):
            b.zero(4)
        assert b.tell() == 0

        with closing(util.aligned_buffer(100)) as buf:
            buf.write(b"x" * 100)
            assert b.readinto(buf) == len(buf)
            assert buf[:] == b"\0" * len(buf)

        b.flush()
コード例 #24
0
def test_invalid_mode(nbd_server):
    nbd_server.start()
    with pytest.raises(ValueError):
        nbd.open(nbd_server.url, "invalid")
コード例 #25
0
def test_context_manager(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        b.write(b"data")
    with pytest.raises(IOError):
        b.write("more")
コード例 #26
0
def test_context_manager(nbd_server):
    nbd_server.start()
    with nbd.open(nbd_server.url, "r+") as b:
        b.write(b"data")
    with pytest.raises(IOError):
        b.write("more")
コード例 #27
0
def test_invalid_mode(nbd_server):
    nbd_server.start()
    with pytest.raises(ValueError):
        nbd.open(nbd_server.url, "invalid")