def test_wait_for_unix_socket(tmpdir): addr = nbd.UnixAddress(tmpdir.join("path")) # Socket was not created yet. start = time.time() assert not nbdutil.wait_for_socket(addr, 0.1) waited = time.time() - 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.time() assert not nbdutil.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 nbdutil.wait_for_socket(addr, 0.0) # Socket was closed - should return immediately. assert not nbdutil.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 nbdutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not nbdutil.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 nbdutil.wait_for_socket(addr, 0.0) # The socket must be closed, no wait needed. assert not nbdutil.wait_for_socket(addr, 0.0)
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 nbdutil.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 = nbd.TCPAddress(*sock.getsockname()) # Socket bound but not listening yet. start = time.time() assert not nbdutil.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 nbdutil.wait_for_socket(addr, 0.0) # Socket was closed - should return immediately. assert not nbdutil.wait_for_socket(addr, 0.0)