Example #1
0
def test_full_backup_single_image(tmpdir, fmt, transport):
    chunk_size = 1024**2
    disk_size = 5 * chunk_size

    # Create disk
    disk = str(tmpdir.join("disk." + fmt))
    subprocess.check_call([
        "qemu-img",
        "create",
        "-f",
        fmt,
        disk,
        str(disk_size),
    ])

    # Pupulate disk with data.
    with qemu_nbd.open(disk, fmt) as d:
        for i in range(0, disk_size, chunk_size):
            d.write(i, b"%d\n" % i)
        d.flush()

    if transport == "unix":
        sock = nbd.UnixAddress(tmpdir.join("sock"))
    else:
        sock = nbd.TCPAddress("localhost", testutil.random_tcp_port())

    # Start full backup and copy the data, veifying what we read.
    with backup.full_backup(tmpdir, disk, fmt, sock):
        with nbd.Client(sock, "sda") as c:
            log.debug("Backing up data with nbd client")
            for i in range(0, disk_size, chunk_size):
                data = c.read(i, chunk_size)
                assert data.startswith(b"%d\n\0" % i)
def test_run_tcp(tmpfile):
    with io.open(tmpfile, "r+b") as f:
        f.truncate(1024**2)

    addr = nbd.TCPAddress("localhost", testutil.random_tcp_port())

    with qemu_nbd.run(tmpfile, "raw", addr):
        # The helper already waited for the NBD socket, not wait needed.
        assert testutil.wait_for_socket(addr, 0.0)

    # The socket must be closed, no wait needed.
    assert not testutil.wait_for_socket(addr, 0.0)
Example #3
0
def test_open_tcp(tmpdir, url_template, export):
    image = str(tmpdir.join("image"))
    with open(image, "wb") as f:
        f.truncate(1024**3)

    sock = nbd.TCPAddress(u"localhost", testutil.random_tcp_port())
    url = url_template.format(port=sock.port)

    log.debug("Trying url=%r export=%r", url, export)
    with qemu_nbd.run(image, "raw", sock, export_name=export):
        with nbd.open(urlparse(url)) as c:
            assert c.export_size == 1024**3
Example #4
0
def test_get_nbd_backend(tmpdir, nbd_server, transport):
    if transport == "unix":
        nbd_server.sock = nbd.UnixAddress(tmpdir.join("sock"))
    else:
        nbd_server.sock = nbd.TCPAddress("localhost",
                                         testutil.random_tcp_port())
    nbd_server.start()

    ticket = Ticket("test", nbd_server.url)
    req = Request()
    b = backends.get(req, ticket)

    assert b.name == "nbd"
Example #5
0
def test_full_backup(tmpdir, fmt, transport):
    disk_size = 1024**2
    disk_part = disk_size // 4
    disk = str(tmpdir.join("disk." + fmt))
    backup_disk = str(tmpdir.join("backup.raw"))

    # Create disk
    subprocess.check_call([
        "qemu-img",
        "create",
        "-f",
        fmt,
        disk,
        str(disk_size),
    ])

    # Pupulate disk with data.
    with qemu_nbd.open(disk, fmt) as d:
        for i in range(0, disk_size, disk_part):
            data = b"%d\n" % i
            d.write(i, data.ljust(512))
        d.flush()

    if transport == "unix":
        nbd_sock = nbd.UnixAddress(tmpdir.join("nbd.sock"))
    else:
        nbd_sock = nbd.TCPAddress("localhost", testutil.random_tcp_port())

    # Backup using qemu-img convert.
    with backup.full_backup(tmpdir, disk, fmt, nbd_sock):
        log.debug("Backing up image with qemu-img")
        subprocess.check_call([
            "qemu-img",
            "convert",
            "-p",
            "-f",
            "raw",
            "-O",
            "raw",
            nbd_sock.url("sda"),
            backup_disk,
        ])

    # Compare source and backup disks.
    with qemu_nbd.open(disk, fmt, read_only=True) as d, \
            io.open(backup_disk, "rb") as b:
        for i in range(0, disk_size, disk_part):
            b.seek(i)
            assert d.read(i, 512) == b.read(512)
Example #6
0
def test_full_backup_handshake(tmpdir, fmt, transport):
    image = str(tmpdir.join("image"))
    subprocess.check_call(["qemu-img", "create", "-f", fmt, image, "1g"])

    if transport == "unix":
        sock = nbd.UnixAddress(tmpdir.join("sock"))
    else:
        sock = nbd.TCPAddress("localhost", testutil.random_tcp_port())

    with backup.full_backup(tmpdir, image, fmt, sock):
        with nbd.Client(sock, "sda") as c:
            # TODO: test transmission_flags?
            assert c.export_size == 1024**3
            assert c.minimum_block_size == 1
            assert c.preferred_block_size == 4096
            assert c.maximum_block_size == 32 * 1024**2
            assert c.base_allocation
Example #7
0
def test_wait_for_tcp_socket():
    sock = socket.socket()
    with closing(sock):
        sock.bind(("localhost", 0))
        addr = nbd.TCPAddress(*sock.getsockname())

        # Socket bound but not listening yet.
        start = time.time()
        assert not testutil.wait_for_socket(addr, 0.1)
        waited = time.time() - start
        assert 0.1 <= waited < 0.2

        sock.listen(1)

        # Socket listening - should return immediately.
        assert testutil.wait_for_socket(addr, 0.0)

    # Socket was closed - should return immediately.
    assert not testutil.wait_for_socket(addr, 0.0)
Example #8
0
def test_full_backup_complete_chain(tmpdir, transport):
    depth = 3
    chunk_size = 1024**2
    disk_size = depth * chunk_size

    for i in range(depth):
        # Create disk based on previous one.
        disk = str(tmpdir.join("disk.%d" % i))
        cmd = ["qemu-img", "create", "-f", "qcow2"]

        if i > 0:
            cmd.append("-b")
            cmd.append("disk.%d" % (i - 1))

        cmd.append(disk)
        cmd.append(str(disk_size))

        subprocess.check_call(cmd)

        # This data can be read only from this disk.
        with qemu_nbd.open(disk, "qcow2") as d:
            d.write(i * chunk_size, b"%d\n" % i)
            d.flush()

    if transport == "unix":
        sock = nbd.UnixAddress(tmpdir.join("sock"))
    else:
        sock = nbd.TCPAddress("localhost", testutil.random_tcp_port())

    # Start full backup and copy the data, veifying what we read.
    with backup.full_backup(tmpdir, disk, "qcow2", sock):
        with nbd.Client(sock, "sda") as c:
            log.debug("Backing up data with nbd client")
            for i in range(depth):
                # Every chunk comes from different image.
                data = c.read(i * chunk_size, chunk_size)
                assert data.startswith(b"%d\n\0" % i)
from six.moves import urllib_parse

import pytest

from ovirt_imageio_common import nbd
from ovirt_imageio_common.compat import subprocess

from . import qemu_nbd
from . import testutil


@pytest.mark.parametrize("addr,export,url", [
    (nbd.UnixAddress("/sock"), None, "nbd:unix:/sock"),
    (nbd.UnixAddress("/sock"), "", "nbd:unix:/sock"),
    (nbd.UnixAddress("/sock"), "sda", "nbd:unix:/sock:exportname=sda"),
    (nbd.TCPAddress("host", 10900), None, "nbd:host:10900"),
    (nbd.TCPAddress("host", 10900), "", "nbd:host:10900"),
    (nbd.TCPAddress("host", 10900), "sdb", "nbd:host:10900:exportname=sdb"),
])
def test_server_url(addr, export, url):
    srv = qemu_nbd.Server("image", "raw", addr, export_name=export)
    assert srv.url == urllib_parse.urlparse(url)


@pytest.mark.parametrize("fmt", ["raw", "qcow2"])
def test_open(tmpdir, fmt):
    disk = str(tmpdir.join("disk." + fmt))

    subprocess.check_call([
        "qemu-img",
        "create",
Example #10
0
def test_tcp_address_invalid(host, port):
    with pytest.raises(ValueError):
        nbd.TCPAddress(host, port)
Example #11
0
from . import backup
from . import testutil

log = logging.getLogger("test")

# Addresses


@pytest.mark.parametrize(
    "addr,export,url",
    [
        # Note: We get Unicode when parsing ticket JSON.
        (nbd.UnixAddress(u"/sock"), None, u"nbd:unix:/sock"),
        (nbd.UnixAddress(u"/sock"), u"", u"nbd:unix:/sock"),
        (nbd.UnixAddress(u"/sock"), u"sda", u"nbd:unix:/sock:exportname=sda"),
        (nbd.TCPAddress(u"host", 10900), None, u"nbd:host:10900"),
        (nbd.TCPAddress(u"host", 10900), u"", u"nbd:host:10900"),
        (nbd.TCPAddress(u"host",
                        10900), u"sdb", u"nbd:host:10900:exportname=sdb"),
    ])
def test_url(addr, export, url):
    assert addr.url(export) == url


@pytest.mark.parametrize("host,port", [
    (u"localhost", u"42"),
    (42, 42),
])
def test_tcp_address_invalid(host, port):
    with pytest.raises(ValueError):
        nbd.TCPAddress(host, port)