diff --git a/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/dune b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/dune new file mode 100644 index 00000000000..41db37828e9 --- /dev/null +++ b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/dune @@ -0,0 +1,7 @@ +(executable + (modes exe) + (name test_systemd) + (libraries xapi-stdext-unix)) + +(cram + (deps test_systemd.exe)) diff --git a/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.ml b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.ml new file mode 100644 index 00000000000..39a0a94a153 --- /dev/null +++ b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.ml @@ -0,0 +1,50 @@ +let _ = + let module Daemon = Xapi_stdext_unix.Unixext.Daemon in + let notify_test () = + if Daemon.systemd_notify Daemon.State.Ready then + exit 0 + else + match Sys.getenv_opt "NOTIFY_SOCKET" with + | Some _ -> + exit 4 + | None -> + print_endline "NOTIFY_SOCKET not set, notification couldn't be sent" ; + exit 1 + in + let server () = + let temp_path = + match Sys.getenv_opt "NOTIFY_SOCKET" with Some a -> a | None -> exit 4 + in + let socket_path = + if String.starts_with ~prefix:"@" temp_path then ( + print_endline temp_path ; + "\x00" ^ String.sub temp_path 1 (String.length temp_path - 1) + ) else + temp_path + in + Unix.( + let sock = socket PF_UNIX SOCK_DGRAM 0 ~cloexec:true in + bind sock (ADDR_UNIX socket_path) ; + let b = Bytes.create 1024 in + let i, _ = recvfrom sock b 0 1024 [] in + print_endline (Bytes.sub_string b 0 i) ; + close sock + ) + in + let booted_test () = + if Daemon.systemd_booted () then ( + print_endline "Booted with systemd" ; + exit 0 + ) else ( + print_endline "Booted without systemd" ; + exit 2 + ) + in + Arg.parse + [ + ("--notify", Arg.Unit notify_test, "Test systemd_notify function") + ; ("--server", Arg.Unit server, "Listen for the notifications") + ; ("--booted", Arg.Unit booted_test, "Test systemd_booted function") + ] + (fun _ -> raise (Arg.Bad "Specify a valid option")) + "Unit test for the Daemon module in Xapi_stdext_unix\nUsage:\n" diff --git a/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.mli b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.mli new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.t b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.t new file mode 100644 index 00000000000..33b39dc277c --- /dev/null +++ b/ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.t @@ -0,0 +1,34 @@ +== Testing no-ops + $ unset NOTIFY_SOCKET + $ ./test_systemd.exe --notify + NOTIFY_SOCKET not set, notification couldn't be sent + [1] + +== Use abstract sockets + $ export NOTIFY_SOCKET="@systemd.socket"; echo "$NOTIFY_SOCKET" + @systemd.socket + $ ./test_systemd.exe --server & + @systemd.socket + READY=1 + $ sleep 1 + $ ./test_systemd.exe --notify + +== Use socket files + $ export TMPDIR=${TMPDIR:-/tmp} + $ export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-$TMPDIR} + $ export NOTIFY_SOCKET="${XDG_RUNTIME_DIR}/systemd.socket" + $ rm -f "$NOTIFY_SOCKET" + $ ./test_systemd.exe --server & + READY=1 + $ sleep 1 + $ test -S "$NOTIFY_SOCKET" + $ ./test_systemd.exe --notify + +== Currently not run tests because of insufficient permissions +== in cram to be manipulating this file +$ mv /run/systemd/system /run/systemd/system.old +$ ./test_systemd.exe booted +Booted without systemd +$ mv /run/systemd/system.old /run/systemd/system +$ ./test_systemd.exe booted +Booted with systemd