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")
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()
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)
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
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") == ""
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)
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)
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)
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"
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()
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
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"
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"] == ""
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))
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
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)
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)
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")
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"])
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")
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") == ""
def test_init(self): c = Client(router=xenbus.XenGuestRouter(XenBusConnection())) assert isinstance(c.router.connection, XenBusConnection) assert not c.router.thread.is_alive()
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)
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")
def test_uncommitted_transaction(): with pytest.raises(PyXSError): with Client() as c: c.transaction()
def writer(): with Client() as other: other[b"/foo/bar"] = b"unexpected write"
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"])
def setup_function(f): try: with Client() as c: c.delete(b"/foo") except PyXSError: pass
def test_write_invalid(): with pytest.raises(InvalidPath): Client().write(b"INVALID%PATH!", b"baz")
def client(request): c = Client(router=Router(request.param())) try: yield c.__enter__() finally: c.__exit__(sys.exc_info())