Skip to content

Commit

Permalink
IH-583 Create tests for the standalone implementations of systemd fun…
Browse files Browse the repository at this point in the history
…ctions

Add new cram tests that exercise Daemon.systemd_notify

Signed-off-by: Andrii Sultanov <[email protected]>
  • Loading branch information
last-genius committed Jun 21, 2024
1 parent 062ab83 commit 7e74dad
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(executable
(modes exe)
(name test_systemd)
(libraries xapi-stdext-unix))

(cram
(deps test_systemd.exe))
50 changes: 50 additions & 0 deletions ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.ml
Original file line number Diff line number Diff line change
@@ -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"
Empty file.
34 changes: 34 additions & 0 deletions ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/test/test_systemd.t
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7e74dad

Please sign in to comment.