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 24f9bda commit f1f9f51
Show file tree
Hide file tree
Showing 4 changed files with 88 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))
47 changes: 47 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,47 @@
let _ =
let module Daemon = Xapi_stdext_unix.Unixext.Daemon in
match Array.to_list Sys.argv with
| _ :: "notify" :: _ -> (
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
)
| _ :: "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
)
| _ :: "booted" :: _ ->
if Daemon.systemd_booted () then (
print_endline "Booted with systemd" ;
exit 0
) else (
print_endline "Booted without systemd" ;
exit 2
)
| _ ->
Printf.printf
"Usage:\n\
%s notify - test notification function\n\
%s booted - test booted function" Sys.argv.(0) Sys.argv.(0) ;
exit 3
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 f1f9f51

Please sign in to comment.