Example #1
0
File: tests.py Project: brkt/pyxs
def test_mkdir():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        c.mkdir("/foo/bar")
        assert c.ls("/foo") == ["bar"]
        assert c.read("/foo/bar") == ""
Example #2
0
def test_is_domain_introduced(backend):
    c = Client(connection=backend())

    for domid in map(int, c.ls("/local/domain")):
        assert c.is_domain_introduced(domid)

    assert not c.is_domain_introduced(999)
Example #3
0
def test_transaction_context_manager():
    # b) transaction in progress -- expecting it to be commited on
    #    context manager exit.
    c = Client()
    with c.transaction():
        assert c.tx_id != 0

    assert c.tx_id == 0
Example #4
0
def test_write(backend):
    c = Client(connection=backend())

    c.write(b"/foo/bar", b"baz")
    assert c.read(b"/foo/bar") == b"baz"

    c[b"/foo/bar"] = b"boo"
    assert c[b"/foo/bar"] == b"boo"
Example #5
0
def test_is_domain_introduced():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        for domid in map(int, c.ls("/local/domain")):
           assert c.is_domain_introduced(domid)

        assert not c.is_domain_introduced(999)
Example #6
0
File: tests.py Project: brkt/pyxs
def test_is_domain_introduced():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        for domid in map(int, c.ls("/local/domain")):
            assert c.is_domain_introduced(domid)

        assert not c.is_domain_introduced(999)
Example #7
0
def test_write():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        c.write("/foo/bar", "baz")
        assert c.read("/foo/bar") == "baz"

        c["/foo/bar"] = "boo"
        assert c["/foo/bar"] == "boo"
Example #8
0
File: tests.py Project: brkt/pyxs
def test_write():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        c.write("/foo/bar", "baz")
        assert c.read("/foo/bar") == "baz"

        c["/foo/bar"] = "boo"
        assert c["/foo/bar"] == "boo"
Example #9
0
def test_rm(backend):
    c = Client(connection=backend())
    c.mkdir(b"/foo/bar")
    c.rm(b"/foo/bar")

    with pytest.raises(PyXSError):
        c.read(b"/foo/bar")

    c.read(b"/foo/bar", b"baz") == b"baz"  # using a default option.

    assert c.read(b"/foo") == b""
Example #10
0
def test_transaction_exception():
    try:
        with Client() as c:
            assert not c.exists(b"/foo/bar")
            c.transaction()
            c[b"/foo/bar"] = b"boo"
            raise ValueError
    except ValueError:
        pass

    with Client() as c:
        assert not c.exists(b"/foo/bar")
Example #11
0
def test_get_domain_path(backend):
    c = Client(connection=backend())

    # a) invalid domid.
    with pytest.raises(ValueError):
        c.get_domain_path(b"foo")

    # b) OK-case (note, that XenStored doesn't care if a domain
    #    actually exists, but according to the spec we shouldn't
    #    really count on a *valid* reply in that case).
    assert c.get_domain_path(0) == b"/local/domain/0"
    assert c.get_domain_path(999) == b"/local/domain/999"
Example #12
0
def test_client_read(backend):
    c = Client(connection=backend())

    # a) non-existant path.
    try:
        c.read(b"/foo/bar")
    except PyXSError as e:
        assert e.args[0] == errno.ENOENT

    # b) OK-case (`/local` is allways in place).
    assert c.read("/local") == b""
    assert c["/local"] == b""
Example #13
0
def test_client_ack():
    c = Client()

    # a) OK-case.
    c.connection.recv = lambda *args: Packet(Op.WRITE, b"OK\x00")
    c.ack(Op.WRITE, b"/foo", b"bar")

    # b) ... something went wrong.
    c.connection.recv = lambda *args: Packet(Op.WRITE, b"boo")

    with pytest.raises(PyXSError):
        c.ack(Op.WRITE, b"/foo", b"bar")
Example #14
0
def test_client_read():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        # a) non-existant path.
        try:
            c.read("/foo/bar")
        except PyXSError as e:
            assert e.args[0] is errno.ENOENT

        # b) OK-case (`/local` is allways in place).
        assert c.read("/local") == ""
        assert c["/local"] == ""
Example #15
0
File: tests.py Project: brkt/pyxs
def test_client_read():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        # a) non-existant path.
        try:
            c.read("/foo/bar")
        except PyXSError as e:
            assert e.args[0] is errno.ENOENT

        # b) OK-case (`/local` is allways in place).
        assert c.read("/local") == ""
        assert c["/local"] == ""
Example #16
0
def test_init():
    # a) UnixSocketConnection
    c = Client()
    assert c.tx_id == 0
    assert isinstance(c.router.connection, UnixSocketConnection)
    assert not c.router.thread.is_alive()

    c = Client(unix_socket_path="/var/run/xenstored/socket")
    assert isinstance(c.router.connection, UnixSocketConnection)
    assert not c.router.thread.is_alive()

    # b) XenBusConnection
    c = Client(xen_bus_path="/dev/xen/xenbus")
    assert isinstance(c.router.connection, XenBusConnection)
    assert not c.router.thread.is_alive()
Example #17
0
def test_client_init():
    # a) UnixSocketConnection
    c = Client()
    assert c.tx_id is 0
    assert not c.events
    assert isinstance(c.connection, UnixSocketConnection)
    assert c.connection.fd is None

    c = Client(unix_socket_path="/var/run/xenstored/socket")
    assert isinstance(c.connection, UnixSocketConnection)
    assert c.connection.fd is None

    # b) XenBusConnection
    c = Client(xen_bus_path="/proc/xen/xenbus")
    assert isinstance(c.connection, XenBusConnection)
    assert c.connection.fd is None
Example #18
0
def test_execute_command_invalid_tx_id():
    with Client() as c:
        monkeypatch_router(c, Packet(Op.READ, b"/local" + NUL,
                                     rq_id=0, tx_id=42))

        with pytest.raises(UnexpectedPacket):
            c.execute_command(Op.READ, b"/local" + NUL)
Example #19
0
def test_execute_command_error():
    with Client() as c:
        with pytest.raises(PyXSError):
            c.execute_command(Op.READ, b"/unexisting/path" + NUL)

        with pytest.raises(PyXSError):
            c.execute_command(-42, b"/unexisting/path" + NUL)
Example #20
0
def nova_agent_listen(server_type, server_os, notify, server_init):
    log.info('Starting actions for {0}'.format(server_type.__name__))
    log.info('Checking for existence of /dev/xen/xenbus')

    send_notification = True
    notify_init = False

    if os.path.exists('/dev/xen/xenbus'):
        with Client(router=XENBUS_ROUTER) as xenbus_client:
            check_provider(utils.get_provider(client=xenbus_client))
            while True:
                notify_init = action(server_os, client=xenbus_client)
                if send_notification and notify_init:
                    log.info('Sending notification startup is complete')
                    utils.send_notification(server_init, notify)
                    send_notification = False

                time.sleep(1)
    else:
        check_provider(utils.get_provider())
        while True:
            notify_init = action(server_os)
            if send_notification and notify_init:
                log.info('Sending notification startup is complete')
                utils.send_notification(server_init, notify)
                send_notification = False

            time.sleep(1)
Example #21
0
File: tests.py Project: brkt/pyxs
def test_client_ack():
    c = Client()

    # a) OK-case.
    c.connection.recv = lambda *args: Packet(Op.WRITE, "OK\x00")

    try:
        c.ack(Op.WRITE, "/foo", "bar")
    except PyXSError as e:
        pytest.fail("No error should've been raised, got: {0}".format(e))

    # b) ... something went wrong.
    c.connection.recv = lambda *args: Packet(Op.WRITE, "boo")

    with pytest.raises(PyXSError):
        c.ack(Op.WRITE, "/foo", "bar")
Example #22
0
def test_context_manager():
    # a) no transaction is running
    c = Client()
    assert not c.router.thread.is_alive()

    with c:
        assert c.router.thread.is_alive()

    assert not c.router.thread.is_alive()
Example #23
0
def test_client_context_manager():
    # a) no transaction is running
    c = Client()
    assert c.connection.fd is None

    with c:
        assert c.connection.fd

    assert c.connection.fd is None

    # b) transaction in progress -- expecting it to be commited on
    #    context manager exit.
    c = Client(transaction=True)

    with c:
        assert c.tx_id is not 0

    assert c.tx_id is 0
Example #24
0
File: tests.py Project: brkt/pyxs
def test_rm():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())
        c.mkdir("/foo/bar")

        try:
            c.rm("/foo/bar")
        except PyXSError as e:
            pytest.fail("No error should've been raised, got: {0}".format(e))

        with pytest.raises(PyXSError):
            c.read("/foo/bar")

        try:
            c.read("/foo/bar", "baz") == "baz"  # using a default option.
        except PyXSError:
            pytest.fail("No error should've been raised, got: {0}".format(e))

        assert c.read("/foo") == ""
Example #25
0
def test_ls():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())
        c.mkdir("/foo/bar")

        # a) OK-case.
        assert c.ls("/foo") == ["bar"]
        assert c.ls("/foo/bar") == []

        # b) directory doesn't exist.
        try:
            c.ls("/path/to/something")
        except PyXSError as e:
            assert e.args[0] is errno.ENOENT
Example #26
0
def test_mkdir():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        c.mkdir("/foo/bar")
        assert c.ls("/foo") == ["bar"]
        assert c.read("/foo/bar") == ""
Example #27
0
def test_header_decode_error():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        # a) The following packet's header cannot be decoded to UTF-8, but
        #    we still need to handle it somehow.
        p = Packet(11, "/foo", rq_id=0, tx_id=128)

        try:
            c.connection.send(p)
        except UnicodeDecodeError as e:
            pytest.fail("No error should've been raised, got: {0}"
                        .format(e))
Example #28
0
def nova_agent_listen(server_type, server_os):
    log.info('Setting lock on file')
    create_lock_file()
    log.info('Starting actions for {0}...'.format(server_type.__name__))
    log.info('Checking for existence of /dev/xen/xenbus')
    if os.path.exists('/dev/xen/xenbus'):
        with Client(router=XENBUS_ROUTER) as xenbus_client:
            while True:
                action(server_os, client=xenbus_client)
                time.sleep(1)
    else:
        while True:
            action(server_os)
            time.sleep(1)
Example #29
0
def test_ls(backend):
    c = Client(connection=backend())
    c.mkdir(b"/foo/bar")

    # a) OK-case.
    assert c.ls(b"/foo") == [b"bar"]
    assert c.ls(b"/foo/bar") == []

    # b) directory doesn't exist.
    try:
        c.ls(b"/path/to/something")
    except PyXSError as e:
        assert e.args[0] == errno.ENOENT
Example #30
0
def nova_agent_listen(server_type, server_os):
    log.info('Starting actions for {0}'.format(server_type.__name__))
    log.info('Checking for existence of /dev/xen/xenbus')
    if os.path.exists('/dev/xen/xenbus'):
        with Client(router=XENBUS_ROUTER) as xenbus_client:
            check_provider(utils.get_provider(client=xenbus_client))
            while True:
                action(server_os, client=xenbus_client)
                notify_ready()
                time.sleep(1)
    else:
        check_provider(utils.get_provider())
        while True:
            action(server_os)
            notify_ready()
            time.sleep(1)
Example #31
0
File: tests.py Project: brkt/pyxs
def test_ls():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())
        c.mkdir("/foo/bar")

        # a) OK-case.
        assert c.ls("/foo") == ["bar"]
        assert c.ls("/foo/bar") == []

        # b) directory doesn't exist.
        try:
            c.ls("/path/to/something")
        except PyXSError as e:
            assert e.args[0] is errno.ENOENT
Example #32
0
def test_get_domain_path():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())

        # a) invalid domid.
        with pytest.raises(ValueError):
            c.get_domain_path("foo")

        # b) OK-case (note, that XenStored doesn't care if a domain
        #    actually exists, but according to the spec we shouldn't
        #    really count on a *valid* reply in that case).
        assert c.get_domain_path(0) == "/local/domain/0"
        assert c.get_domain_path(999) == "/local/domain/999"
Example #33
0
def test_watches():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())
        c.write("/foo/bar", "baz")
        m = c.monitor()
        m.watch("/foo/bar", "boo")

        # a) we receive the first event immediately, so `wait()` doesn't
        #    block.
        assert m.wait() == ("/foo/bar", "boo")

        # b) before the second call we have to make sure someone
        #    will change the path being watched.
        Timer(.5, lambda: c.write("/foo/bar", "baz")).run()
        assert m.wait() == ("/foo/bar", "boo")

        # c) changing a children of the watched path triggers watch
        #    event as well.
        Timer(.5, lambda: c.write("/foo/bar/baz", "???")).run()
        assert m.wait() == ("/foo/bar/baz", "boo")
Example #34
0
def test_client_ack():
    c = Client()

    # a) OK-case.
    c.connection.recv = lambda *args: Packet(Op.WRITE, "OK\x00")

    try:
        c.ack(Op.WRITE, "/foo", "bar")
    except PyXSError as e:
        pytest.fail("No error should've been raised, got: {0}"
                    .format(e))

    # b) ... something went wrong.
    c.connection.recv = lambda *args: Packet(Op.WRITE, "boo")

    with pytest.raises(PyXSError):
        c.ack(Op.WRITE, "/foo", "bar")
Example #35
0
def test_watches(backend):
    c = Client(connection=backend())
    c.write(b"/foo/bar", b"baz")
    m = c.monitor()
    m.watch(b"/foo/bar", b"boo")

    # a) we receive the first event immediately, so `wait()` doesn't
    #    block.
    assert m.wait() == (b"/foo/bar", b"boo")

    # b) before the second call we have to make sure someone
    #    will change the path being watched.
    Timer(.5, lambda: c.write(b"/foo/bar", b"baz")).run()
    assert m.wait() == (b"/foo/bar", b"boo")

    # c) changing a children of the watched path triggers watch
    #    event as well.
    Timer(.5, lambda: c.write(b"/foo/bar/baz", b"???")).run()
    assert m.wait() == (b"/foo/bar/baz", b"boo")
Example #36
0
def test_write_invalid():
    with pytest.raises(InvalidPath):
        Client().write(b"INVALID%PATH!", b"baz")
Example #37
0
def test_set_perms_invalid():
    with pytest.raises(InvalidPath):
        Client().set_perms(b"INVALID%PATH!", [])

    with pytest.raises(InvalidPermission):
        Client().set_perms(b"/foo/bar", [b"z"])
Example #38
0
File: tests.py Project: brkt/pyxs
def test_permissions():
    for backend in [UnixSocketConnection, XenBusConnection]:
        c = Client(connection=backend())
        c.rm("/foo")
        c.mkdir("/foo/bar")

        # a) checking default permissions -- full access.
        assert c.get_permissions("/foo/bar") == ["n0"]

        # b) setting new permissions, and making sure it worked.
        c.set_permissions("/foo/bar", ["b0"])
        assert c.get_permissions("/foo/bar") == ["b0"]

        # c) conflicting permissions -- XenStore doesn't care.
        c.set_permissions("/foo/bar", ["b0", "n0", "r0"])
        assert c.get_permissions("/foo/bar") == ["b0", "n0", "r0"]

        # d) invalid permission format.
        with pytest.raises(InvalidPermission):
            c.set_permissions("/foo/bar", ["x0"])
Example #39
0
 def writer():
     with Client() as other:
         other[b"/foo/bar"] = b"unexpected write"
Example #40
0
def client(request):
    c = Client(router=Router(request.param()))
    try:
        yield c.__enter__()
    finally:
        c.__exit__(sys.exc_info())
Example #41
0
def test_check_path(op):
    with pytest.raises(InvalidPath):
        getattr(Client(), op)(b"INVALID%PATH!")
Example #42
0
def test_mkdir(backend):
    c = Client(connection=backend())

    c.mkdir(b"/foo/bar")
    assert c.ls(b"/foo") == [b"bar"]
    assert c.read(b"/foo/bar") == b""
Example #43
0
File: tests.py Project: brkt/pyxs
def test_client_execute_command():
    c = Client()
    c.execute_command(Op.WRITE, "/foo/bar", "baz")

    # a) arguments contain invalid characters.
    with pytest.raises(ValueError):
        c.execute_command(Op.DEBUG, "\x07foo")

    # b) command validator fails.
    c.COMMAND_VALIDATORS[Op.DEBUG] = lambda *args: False
    with pytest.raises(ValueError):
        c.execute_command(Op.DEBUG, "foo")
    c.COMMAND_VALIDATORS.pop(Op.DEBUG)

    # c) ``Packet`` constructor fails.
    with pytest.raises(InvalidPayload):
        c.execute_command(Op.WRITE, "/foo/bar", "baz" * 4096)

    # d) XenStore returned an error code.
    with pytest.raises(PyXSError):
        c.execute_command(Op.READ, "/path/to/something")

    _old_recv = c.connection.recv
    # e) XenStore returns a packet with invalid operation in the header.
    c.connection.recv = lambda *args: Packet(Op.DEBUG, "boo")
    with pytest.raises(UnexpectedPacket):
        c.execute_command(Op.READ, "/foo/bar")
    c.connection.recv = _old_recv

    # d) XenStore returns a packet with invalid transaction id in the
    #    header.
    c.connection.recv = lambda *args: Packet(Op.READ, "boo", tx_id=42)
    with pytest.raises(UnexpectedPacket):
        c.execute_command(Op.READ, "/foo/bar")
    c.connection.recv = _old_recv

    # e) ... and a hack for ``XenBusConnection``
    c = Client(connection=XenBusConnection())
    c.connection.recv = lambda *args: Packet(Op.READ, "boo", tx_id=42)
    try:
        c.execute_command(Op.READ, "/foo/bar")
    except UnexpectedPacket as e:
        pytest.fail("No error should've been raised, got: {0}".format(e))
    c.connection.recv = _old_recv

    # f) Got a WATCH_EVENT instead of an expected packet type, making
    #    sure it's queued properly.
    def recv(*args):
        if hasattr(recv, "called"):
            return Packet(Op.READ, "boo")
        else:
            recv.called = True
            return Packet(Op.WATCH_EVENT, "boo")

    c.connection.recv = recv

    try:
        c.execute_command(Op.READ, "/foo/bar")
    except UnexpectedPacket as e:
        pytest.fail("No error should've been raised, got: {0}".format(e))
    else:
        assert len(c.events) is 1
        assert c.events[0] == Packet(Op.WATCH_EVENT, "boo")

    c.connection.recv = _old_recv

    # Cleaning up.
    with Client() as c:
        c.execute_command(Op.RM, "/foo/bar")
Example #44
0
def test_uncommitted_transaction():
    with pytest.raises(PyXSError):
        with Client() as c:
            c.transaction()
Example #45
0
 def test_init(self):
     c = Client(router=xenbus.XenGuestRouter(XenBusConnection()))
     assert isinstance(c.router.connection, XenBusConnection)
     assert not c.router.thread.is_alive()
Example #46
0
def setup_function(f):
    try:
        with Client() as c:
            c.delete(b"/foo")
    except PyXSError:
        pass
Example #47
0
def test_execute_command_invalid_characters():
    with Client() as c:
        c.execute_command(Op.WRITE, b"/foo/bar" + NUL, b"baz")

        with pytest.raises(ValueError):
            c.execute_command(Op.DEBUG, b"\x07foo" + NUL)
Example #48
0
def test_close_idempotent():
    c = Client()
    c.connect()
    c.close()
    c.close()
Example #49
0
def test_check_watch_path(op):
    with pytest.raises(InvalidPath):
        getattr(Client().monitor(), op)(b"INVALID%PATH", b"token")

    with pytest.raises(InvalidPath):
        getattr(Client().monitor(), op)(b"@arbitraryPath", b"token")