Richard W.M. Jones
2022-Nov-04 12:45 UTC
[Libguestfs] [PATCH libnbd 0/2] ocaml: Simplify the sockaddr test
Not as bad as I thought it would be! Rich.
Richard W.M. Jones
2022-Nov-04 12:45 UTC
[Libguestfs] [PATCH libnbd 1/2] python: Rename the aio-connect test
This is a test, but it had a confusing name, so rename it to test-*.sh. Updates: commit b8d0dd8e8d15219161dad9d029ece0b49fcbb565 --- python/Makefile.am | 4 ++-- .../{python-aio-connect-unix.sh => test-aio-connect-unix.sh} | 0 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/python/Makefile.am b/python/Makefile.am index f51d40e16a..f1e701d03c 100644 --- a/python/Makefile.am +++ b/python/Makefile.am @@ -36,7 +36,7 @@ EXTRA_DIST = \ $(generator_built) \ nbdsh.py \ pycodestyle.sh \ - python-aio-connect-unix.sh \ + test-aio-connect-unix.sh \ $(srcdir)/t/*.py \ $(NULL) @@ -85,8 +85,8 @@ TESTS_ENVIRONMENT = \ $(NULL) LOG_COMPILER = $(top_builddir)/run TESTS += \ - python-aio-connect-unix.sh \ run-python-tests \ + test-aio-connect-unix.sh \ $(NULL) endif HAVE_NBDKIT diff --git a/python/python-aio-connect-unix.sh b/python/test-aio-connect-unix.sh similarity index 100% rename from python/python-aio-connect-unix.sh rename to python/test-aio-connect-unix.sh -- 2.37.0.rc2
Richard W.M. Jones
2022-Nov-04 12:45 UTC
[Libguestfs] [PATCH libnbd 2/2] ocaml: Simplify the sockaddr test
The sockaddr test added in commit 50500aade9 ("ocaml: Implement sockaddr type") was quite complicated because it had to run nbdkit as a subprocess, manage the socket, check for a PID file etc. The simpler way of doing this is to make it work like the Python test (python/test-aio-connect-unix.sh) where we run nbdkit -U - ... --run './test $unixsocket' The straightforward way would be to a separate shell script etc to ocaml/tests/Makefile.am:TESTS which is orthogonal to how the existing tests work. So instead make the test exec nbdkit when called first time (without the $unixsocket parameter) and then run the test when called the second time. Updates: commit 50500aade9f32899eddd2a7b89ae5c596674f95c --- ocaml/tests/test_580_aio_connect.ml | 69 +++++++++++++---------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/ocaml/tests/test_580_aio_connect.ml b/ocaml/tests/test_580_aio_connect.ml index 43c6fd0f7a..5a94ac0c9b 100644 --- a/ocaml/tests/test_580_aio_connect.ml +++ b/ocaml/tests/test_580_aio_connect.ml @@ -17,51 +17,42 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *) +(* This test is unusual because we want to run it under nbdkit + * rather than having the test run nbdkit as a subprocess. + * + * Therefore we detect if a $unixsocket parameter is passed + * in Sys.argv. If not then we exec nbdkit: + * nbdkit -U - ... --run '$argv0 \$unixsocket' + * If the $unixsocket parameter is present then we run the test. + *) + open Unix open Printf let () - let nbd = NBD.create () in + match Array.length Sys.argv with + | 1 -> (* exec nbdkit *) + let argv0 = Sys.argv.(0) in + let runcmd = sprintf "%s $unixsocket" (Filename.quote argv0) in + execvp "nbdkit" [| "nbdkit"; "-U"; "-"; "--exit-with-parent"; + "memory"; "size=512"; + "--run"; runcmd |] - (* Unlike other tests, we're going to run nbdkit as a subprocess - * by hand and have it listening on a randomly named socket - * that we create. - *) - let sock = Filename.temp_file "580-" ".sock" in - unlink sock; - let pidfile = Filename.temp_file "580-" ".pid" in - unlink pidfile; - let cmd - sprintf "nbdkit -U %s -P %s --exit-with-parent memory size=512 &" - (Filename.quote sock) (Filename.quote pidfile) in - if Sys.command cmd <> 0 then - failwith "nbdkit command failed"; - let rec loop i - if i > 60 then - failwith "nbdkit subcommand did not start up"; - if not (Sys.file_exists pidfile) then ( - sleep 1; - loop (i+1) - ) - in - loop 0; + | 2 -> (* run the test *) + let unixsocket = Sys.argv.(1) in + let nbd = NBD.create () in - (* Connect to the subprocess using a Unix.sockaddr. *) - let sa = ADDR_UNIX sock in - NBD.aio_connect nbd sa; - while NBD.aio_is_connecting nbd do - ignore (NBD.poll nbd 1) - done; - assert (NBD.aio_is_ready nbd); - NBD.close nbd; + (* Connect to the subprocess using a Unix.sockaddr. *) + let sa = ADDR_UNIX unixsocket in + NBD.aio_connect nbd sa; + while NBD.aio_is_connecting nbd do + ignore (NBD.poll nbd 1) + done; + assert (NBD.aio_is_ready nbd); + assert (NBD.get_size nbd = 512_L); + NBD.close nbd - (* Kill the nbdkit subprocess. *) - let chan = open_in pidfile in - let pid = int_of_string (input_line chan) in - kill pid Sys.sigterm; - - (* Clean up files. *) - unlink sock; - unlink pidfile + | _ -> + failwith "unexpected test parameters" let () = Gc.compact () -- 2.37.0.rc2