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

_______________________________________________
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs

Reply via email to