@@ -36,6 +36,17 @@ summary() {
3636 fi
3737}
3838
39+ # ── Helpers ───────────────────────────────────────────────────────────────────
40+
41+ # Returns 0 if systemd is running AND the unit file is installed on disk.
42+ systemd_and_unit_available () {
43+ [ -d /run/systemd/system ] && command -v systemctl > /dev/null 2>&1 || return 1
44+ for path in " ${UNIT_FILE_PATHS[@]} " ; do
45+ [ -f " $path " ] && return 0
46+ done
47+ return 1
48+ }
49+
3950# ── Check functions ───────────────────────────────────────────────────────────
4051
4152check_binary_executable () {
@@ -61,10 +72,10 @@ check_webapp() {
6172 fail " Webapp directory missing: $WEBAPP_DIR "
6273 fi
6374 for app in client player; do
64- if find " $WEBAPP_DIR /$app " -name ' index.html' 2> /dev/null | grep -q . ; then
65- pass " Webapp $app contains index.html"
75+ if [ -f " $WEBAPP_DIR /$app / index.html" ] ; then
76+ pass " Webapp $app entry point exists: $WEBAPP_DIR / $app / index.html"
6677 else
67- fail " Webapp $app missing index.html"
78+ fail " Webapp $app entry point missing: $WEBAPP_DIR / $app / index.html"
6879 fi
6980 done
7081}
@@ -77,6 +88,16 @@ check_config_dir() {
7788 fi
7889}
7990
91+ check_config_dir_permissions () {
92+ local perms
93+ perms=$( stat -c ' %a' " $CONFIG_DIR " 2> /dev/null)
94+ if [ " $perms " = " 750" ]; then
95+ pass " Config directory has secure permissions ($perms ): $CONFIG_DIR "
96+ else
97+ fail " Config directory has insecure permissions ($perms , expected 750): $CONFIG_DIR "
98+ fi
99+ }
100+
80101check_binary_help () {
81102 HELP_OUTPUT=$( " $BINARY " --help 2>&1 ) && HELP_RC=$? || HELP_RC=$?
82103 if [ " $HELP_RC " -eq 0 ] || echo " $HELP_OUTPUT " | grep -qi ' gateway\|usage\|help' ; then
@@ -87,20 +108,6 @@ check_binary_help() {
87108}
88109
89110check_config_init () {
90- if [ ! -f " $CONFIG_FILE " ]; then
91- info " Config file not generated by postinst (expected without systemd)."
92- info " Running config initialization manually…"
93- CONFIG_INIT_LOG=$( mktemp)
94- if " $BINARY " --config-init-only > " $CONFIG_INIT_LOG " 2>&1 ; then
95- pass " Config initialization command succeeded"
96- else
97- echo " config-init-only output:"
98- cat " $CONFIG_INIT_LOG "
99- fail " Config initialization command failed"
100- fi
101- rm -f " $CONFIG_INIT_LOG "
102- fi
103-
104111 if [ -f " $CONFIG_FILE " ]; then
105112 pass " Default config file exists: $CONFIG_FILE "
106113 if python3 -c " import json; json.load(open('$CONFIG_FILE '))" 2> /dev/null; then
@@ -109,7 +116,7 @@ check_config_init() {
109116 fail " $( basename " $CONFIG_FILE " ) exists but is not valid JSON"
110117 fi
111118 else
112- fail " Default config file missing after initialization : $CONFIG_FILE "
119+ fail " Default config file missing after installation : $CONFIG_FILE "
113120 fi
114121}
115122
@@ -140,19 +147,145 @@ check_unit_file() {
140147 fi
141148}
142149
143- check_service_startup () {
144- info " [Best-effort] Checking service startup…"
145- warn " systemd service startup testing is best-effort in containers."
146- warn " Full service validation requires a real systemd environment."
147- if [ -d /run/systemd/system ]; then
148- info " systemd detected; attempting service start…"
149- if systemctl start devolutions-gateway 2>&1 ; then
150- pass " [Best-effort] Service started successfully"
151- systemctl status devolutions-gateway 2>&1 || true
150+ check_single_execstart () {
151+ local unit_file=" " count
152+ for path in " ${UNIT_FILE_PATHS[@]} " ; do
153+ if [ -f " $path " ]; then
154+ unit_file=" $path "
155+ break
156+ fi
157+ done
158+ if [ -z " $unit_file " ]; then
159+ warn " Skipping ExecStart check: no unit file found (check_unit_file already reported this)."
160+ return
161+ fi
162+ # Match only non-empty ExecStart= lines; bare 'ExecStart=' is a reset directive.
163+ count=$( grep -c ' ^ExecStart=[^[:space:]]' " $unit_file " 2> /dev/null || true)
164+ if [ " $count " -eq 1 ]; then
165+ pass " Service file has exactly one ExecStart directive"
166+ else
167+ fail " Service file has $count ExecStart directives (expected exactly 1)"
168+ fi
169+ }
170+
171+ check_config_file_permissions () {
172+ if [ ! -f " $CONFIG_FILE " ]; then
173+ fail " Config file not found, cannot check permissions: $CONFIG_FILE "
174+ return
175+ fi
176+ local perms
177+ perms=$( stat -c ' %a' " $CONFIG_FILE " 2> /dev/null)
178+ if [ " $perms " = " 600" ]; then
179+ pass " Config file has secure permissions ($perms ): $CONFIG_FILE "
180+ else
181+ fail " Config file has insecure permissions ($perms , expected 600): $CONFIG_FILE "
182+ fi
183+ }
184+
185+ check_provisioner_key () {
186+ info " Generating RSA-2048 provisioner key pair with openssl…"
187+ KEY_LOG=$( mktemp)
188+ if openssl genrsa -out " $CONFIG_DIR /provisioner.key" 2048 > " $KEY_LOG " 2>&1 \
189+ && openssl rsa -in " $CONFIG_DIR /provisioner.key" \
190+ -pubout -out " $CONFIG_DIR /provisioner.pem" >> " $KEY_LOG " 2>&1 ; then
191+ chmod 600 " $CONFIG_DIR /provisioner.key"
192+ pass " Provisioner key pair generated: $CONFIG_DIR /provisioner.pem"
193+ else
194+ echo " openssl output:"
195+ cat " $KEY_LOG "
196+ fail " Failed to generate provisioner key pair"
197+ fi
198+ rm -f " $KEY_LOG "
199+ }
200+
201+ check_service_health () {
202+ local health_url=" http://localhost:7171/jet/health"
203+ local gateway_pid=" "
204+ local gateway_log=" "
205+
206+ if systemd_and_unit_available; then
207+ info " systemd available — using systemctl start/stop"
208+ if ! systemctl start devolutions-gateway > /dev/null 2>&1 ; then
209+ fail " systemctl start devolutions-gateway failed"
210+ echo " Service logs:"
211+ journalctl -u devolutions-gateway --no-pager -n 50 2> /dev/null || true
212+ return
213+ fi
214+ else
215+ info " systemd not available — starting binary directly"
216+ gateway_log=$( mktemp)
217+ " $BINARY " 2> " $gateway_log " &
218+ gateway_pid=$!
219+ fi
220+
221+ # Wait for the service to be ready (up to 10 s).
222+ local i=0
223+ while [ " $i " -lt 10 ]; do
224+ curl -sf -H ' Accept: application/json' " $health_url " > /dev/null 2>&1 && break
225+ sleep 1
226+ i=$(( i + 1 ))
227+ done
228+
229+ local health_output health_rc
230+ health_output=$( curl -sf -H ' Accept: application/json' " $health_url " 2> /dev/null) && health_rc=$? || health_rc=$?
231+
232+ # Stop the service.
233+ if systemd_and_unit_available; then
234+ systemctl stop devolutions-gateway > /dev/null 2>&1 || true
235+ elif [ -n " $gateway_pid " ]; then
236+ kill " $gateway_pid " 2> /dev/null || true
237+ wait " $gateway_pid " 2> /dev/null || true
238+ fi
239+
240+ if [ " $health_rc " -eq 0 ]; then
241+ pass " Health endpoint responded: $health_output "
242+ # Verify the version field in the health response matches expected.
243+ local health_version
244+ health_version=$( python3 -c " import json,sys; d=json.load(sys.stdin); print(d.get('version',''))" <<< " $health_output" 2> /dev/null) || health_version=" "
245+ if [ -n " $health_version " ] && echo " $health_version " | grep -qF " $VERSION " ; then
246+ pass " Health response version ($health_version ) matches expected ($VERSION )"
247+ elif [ -n " $health_version " ]; then
248+ fail " Health response version ($health_version ) does not match expected ($VERSION )"
152249 else
153- warn " Service start failed (expected in some container environments)."
250+ warn " Could not extract version from health response"
251+ fi
252+ else
253+ fail " Health endpoint did not respond at $health_url after 10 s"
254+ if systemd_and_unit_available; then
255+ echo " Service logs:"
256+ journalctl -u devolutions-gateway --no-pager -n 50 2> /dev/null || true
257+ elif [ -n " $gateway_log " ] && [ -f " $gateway_log " ]; then
258+ echo " Gateway process output:"
259+ cat " $gateway_log "
154260 fi
261+ fi
262+
263+ [ -n " $gateway_log " ] && rm -f " $gateway_log "
264+ }
265+
266+ check_post_uninstall () {
267+ if [ ! -f " $BINARY " ]; then
268+ pass " Binary removed after uninstall"
269+ else
270+ fail " Binary still present after uninstall: $BINARY "
271+ fi
272+
273+ local unit_file_found=0
274+ for path in " ${UNIT_FILE_PATHS[@]} " ; do
275+ if [ -f " $path " ]; then
276+ unit_file_found=1
277+ break
278+ fi
279+ done
280+ if [ " $unit_file_found " -eq 0 ]; then
281+ pass " Unit file removed after uninstall"
282+ else
283+ fail " Unit file still present after uninstall"
284+ fi
285+
286+ if [ -d " $CONFIG_DIR " ]; then
287+ pass " Config directory preserved after uninstall: $CONFIG_DIR "
155288 else
156- info " No systemd detected; skipping service startup test. "
289+ fail " Config directory was removed after uninstall (should be preserved) "
157290 fi
158291}
0 commit comments