def test_wait_for_unix_socket(tmpdir): addr = sockutil.UnixAddress(tmpdir.join("path")) # Socket was not created yet. start = time.monotonic() assert not sockutil.wait_for_socket(addr, 0.1) waited = time.monotonic() - start assert 0.1 <= waited < 0.2 sock = socket.socket(socket.AF_UNIX) with closing(sock): sock.bind(addr) # Socket bound but not listening yet. start = time.monotonic() assert not sockutil.wait_for_socket(addr, 0.1) waited = time.monotonic() - start assert 0.1 <= waited < 0.2 sock.listen(1) # Socket listening - should return immediately. assert sockutil.wait_for_socket(addr, 0.0) # Socket was closed - should return immediately. assert not sockutil.wait_for_socket(addr, 0.0)
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 sockutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not sockutil.wait_for_socket(addr, 0.0)
def test_run_unix(tmpdir): image = str(tmpdir.join("image")) sock = str(tmpdir.join("sock")) with io.open(image, "wb") as f: f.truncate(1024**2) addr = nbd.UnixAddress(sock) with qemu_nbd.run(image, "raw", addr): # The helper already waited for the NBD socket, not wait needed. assert sockutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not sockutil.wait_for_socket(addr, 0.0)
def srv(request, tmpdir_factory): tmp_dir = tmpdir_factory.mktemp("admin_tool_test") conf_file = tmp_dir.mkdir("conf.d").join("daemon.conf") random_port = testutil.random_tcp_port() conf = DAEMON_CONF.format(run_dir=str(tmp_dir), control_transport=request.param, control_port=random_port) conf_file.write(conf) log.info("Starting daemon with conf dir %s", tmp_dir) cmd = ["./ovirt-imageio", "--conf-dir", str(tmp_dir)] proc = subprocess.Popen(cmd) try: if request.param == "unix": socket = sockutil.UnixAddress(str(tmp_dir.join("sock"))) else: socket = sockutil.TCPAddress("localhost", random_port) if not sockutil.wait_for_socket(socket, 10): raise RuntimeError("Timeout waiting for {}".format(socket)) log.info("Daemon started with pid %s", proc.pid) yield str(tmp_dir) finally: log.info("Terminating daemon") proc.terminate()
def run(image, fmt, qmp_sock=None, start_cpu=True, shutdown_timeout=1): # NOTES: # - Let qemu pick default memory size, since on some platforms memory have # strange alignment. Here is a failure from ppc64le host: # qemu-kvm: Memory size 0x1000000 is not aligned to 256 MiB cmd = [ QEMU, # Use kvm if available, othrewise fallback to tcg. This allows running # qemu on Travis CI which does not support nested virtualization. "-nodefaults", "-machine", "accel=kvm:tcg", "-drive", "if=virtio,id=drive0,node-name=file0,file={},format={}".format( image, fmt), "-nographic", "-net", "none", "-monitor", "none", "-serial", "stdio", ] if qmp_sock: cmd.append("-qmp") cmd.append("unix:{},server,nowait".format(qmp_sock)) # Workaround for bug in qemu-4.0.0-rc0 on Fedora, failing to start VM # becuase initilizing real audio driver failed. # See https://bugzilla.redhat.com/1692047. if supports_audiodev(): cmd.append("-audiodev") cmd.append("none,id=1") if not start_cpu: cmd.append("-S") log.info("Starting qemu %s", cmd) p = subprocess.Popen(cmd, env=env(), stdin=subprocess.PIPE, stdout=subprocess.PIPE) try: if qmp_sock and not sockutil.wait_for_socket(qmp_sock, 1): raise RuntimeError("Timeout waiting for socket: %s" % qmp_sock) yield Guest(p) finally: log.info("Terminating qemu gracefully") p.terminate() try: p.wait(shutdown_timeout) except subprocess.TimeoutExpired: log.warning("Timeout terminating qemu - killing it") p.kill() p.wait() log.debug("qemu terminated with exit code %s", p.returncode)
def test_wait_for_tcp_socket(): sock = socket.socket() with closing(sock): sock.bind(("localhost", 0)) addr = sockutil.TCPAddress(*sock.getsockname()) # Socket bound but not listening yet. start = time.monotonic() assert not sockutil.wait_for_socket(addr, 0.1) waited = time.monotonic() - start assert 0.1 <= waited < 0.2 sock.listen(1) # Socket listening - should return immediately. assert sockutil.wait_for_socket(addr, 0.0) # Socket was closed - should return immediately. assert not sockutil.wait_for_socket(addr, 0.0)
def started_imageio(tmpdir, drop_privileges="true"): prepare_config(tmpdir, drop_privileges=drop_privileges) conf_dir = tmpdir.join("conf") cmd = ["./ovirt-imageio", "--conf-dir", str(conf_dir)] proc = subprocess.Popen(cmd) try: socket = sockutil.UnixAddress(str(tmpdir.join("run", "sock"))) if not sockutil.wait_for_socket(socket, 10): raise RuntimeError("Timeout waiting for {}".format(socket)) # Wait until server is listening - at this point it already dropped # privileges. if drop_privileges: cfg = config.load(str(conf_dir.join("conf.d", "daemon.conf"))) with http.ControlClient(cfg) as c: r = c.get("/tickets/no-such-ticket") r.read() assert r.status == 404 yield proc finally: proc.terminate()