summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2025-08-30 21:14:24 +0200
committerAnna (navi) Figueiredo Gomes <navi@vlhl.dev>2025-12-09 00:06:26 +0100
commit50e420d5f6c2e220428a2512b7f42c6a10650e79 (patch)
tree350ff4b6db18fd974e21a12a6fdfa43d9b82742a
parentb39a0127ab706d6d52b1291309df324e6b23fcad (diff)
tests: add basic rc-service testsorigin/tests
-rw-r--r--sh/meson.build16
-rw-r--r--src/openrc-run/meson.build2
-rw-r--r--src/rc-service/meson.build2
-rw-r--r--test/units/meson.build12
-rwxr-xr-xtest/units/rc-service.sh72
-rwxr-xr-xtest/units/rc-update.sh77
-rwxr-xr-xtest/units/setup-root.sh28
7 files changed, 195 insertions, 14 deletions
diff --git a/sh/meson.build b/sh/meson.build
index a229600f..1b014420 100644
--- a/sh/meson.build
+++ b/sh/meson.build
@@ -21,9 +21,9 @@ sh_config = [
]
scripts_config = [
- 'gendepends.sh.in',
'openrc-run.sh.in',
'openrc-user.sh.in',
+ 'gendepends.sh.in',
's6-svscanboot.sh.in'
]
@@ -49,8 +49,12 @@ else
]
endif
-install_data(sh,
- install_dir : rc_shdir)
+fs = import('fs')
+script_deps = []
+foreach file : sh
+ script_deps += fs.copyfile(file, install: true, install_dir: rc_shdir)
+endforeach
+
foreach file : sh_config
configure_file(input : file,
output : '@BASENAME@',
@@ -60,10 +64,8 @@ endforeach
foreach file : scripts_config
configure_file(input : file,
- output : '@BASENAME@',
- configuration : sh_conf_data,
- install_dir : rc_shdir,
- install_mode : 'rwxr-xr-x')
+ output : '@BASENAME@', configuration : sh_conf_data,
+ install_dir : rc_shdir, install_mode : 'rwxr-xr-x')
endforeach
foreach file : scripts_config_os
diff --git a/src/openrc-run/meson.build b/src/openrc-run/meson.build
index c2fac9d8..412c5e9c 100644
--- a/src/openrc-run/meson.build
+++ b/src/openrc-run/meson.build
@@ -1,4 +1,4 @@
-executable('openrc-run', 'openrc-run.c',
+openrc_run = executable('openrc-run', 'openrc-run.c',
dependencies: [rc, einfo, shared, audit_dep, dl_dep, pam_dep, pam_misc_dep, selinux_dep, util_dep, crypt_dep],
include_directories: incdir,
install: true,
diff --git a/src/rc-service/meson.build b/src/rc-service/meson.build
index 71c49fbe..211169ac 100644
--- a/src/rc-service/meson.build
+++ b/src/rc-service/meson.build
@@ -1,4 +1,4 @@
-executable('rc-service', 'rc-service.c',
+rc_service = executable('rc-service', 'rc-service.c',
include_directories: incdir,
dependencies: [rc, einfo, shared],
install: true,
diff --git a/test/units/meson.build b/test/units/meson.build
index 23c2758a..4d1cb8a3 100644
--- a/test/units/meson.build
+++ b/test/units/meson.build
@@ -1,5 +1,7 @@
-is_older_than = find_program('check-is-older-than.sh')
-sh_yesno = find_program('check-sh-yesno.sh')
-
-test('is_older_than', is_older_than, env : test_env)
-test('sh_yesno', sh_yesno, env : test_env)
+test('is_older_than', find_program('check-is-older-than.sh'), env: test_env)
+test('sh_yesno', find_program('check-sh-yesno.sh'), env: test_env)
+test('rc-service', find_program('rc-service.sh'),
+ env: [test_env, 'SUBDIR=' + meson.current_source_dir()],
+ workdir: meson.project_source_root(),
+ args: [ rc_service, openrc_run ],
+ depends: script_deps)
diff --git a/test/units/rc-service.sh b/test/units/rc-service.sh
new file mode 100755
index 00000000..e0cd90a8
--- /dev/null
+++ b/test/units/rc-service.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+set -e
+
+. "$SUBDIR/setup-root.sh"
+rc_service=${1?}
+openrc_run=${2?}
+cp "$RC_LIBEXECDIR/sh/openrc-run.sh" "$root/run/openrc/openrc-run.sh"
+cp "$RC_LIBEXECDIR/sh/gendepends.sh" "$root/run/openrc/gendepends.sh"
+chmod +x "$root/run/openrc/openrc-run.sh"
+chmod +x "$root/run/openrc/gendepends.sh"
+
+rc_service() {
+ $rc_service "$@" 2>&1
+}
+
+in_state() {
+ state=${1?}
+ shift
+ for svc; do
+ test "$(readlink "$RC_SVCDIR/$state/$svc")" = "$root/etc/init.d/$svc"
+ done
+}
+
+mkservice() {
+ service="$root/etc/init.d/$1"
+ cat > "$service" <<-EOF
+ #!$SOURCE_ROOT/${openrc_run}
+ depend() {
+ ${2-:;}
+ }
+ start() {
+ ${3-sleep 0.1}
+ }
+ EOF
+ chmod +x "$service"
+}
+
+mkservice nya
+mkservice foo "need nya"
+mkservice mew "want foo"
+
+addrunlevel() {
+ ln -s "$root/etc/init.d/${1?}" "$root/etc/runlevel/${2?}/$1"
+}
+
+rc_service nya start
+in_state started nya
+
+rc_service nya stop
+( ! in_state started nya )
+
+rc_service foo start
+in_state started foo nya
+
+rc_service foo stop
+in_state started nya
+( ! in_state started foo )
+
+rc_service foo start
+in_state started foo nya
+
+rc_service nya stop
+( ! in_state started foo nya )
+
+rc_service mew start
+in_state started foo nya mew
+
+rc_service nya stop
+rc_service mew stop
+
+( ! in_state started foo nya mew )
diff --git a/test/units/rc-update.sh b/test/units/rc-update.sh
new file mode 100755
index 00000000..d7437351
--- /dev/null
+++ b/test/units/rc-update.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+set -ex
+
+. ${1?}
+rc_update=${2?}
+
+rc_update() {
+ "$rc_update" --root "$root" "$@" 2>&1
+}
+
+in_runlevel() {
+ test "$(readlink "$root/etc/runlevels/${1?}/${2?}")" = "$root/etc/init.d/$2"
+}
+
+stacked() {
+ test -d "$root/etc/runlevels/${1?}/${2?}"
+}
+
+# non-existent
+( ! rc_update add nya )
+( ! rc_update -s add nya )
+( ! rc_update del nya )
+( ! rc_update -s del nya )
+
+cat > "$root/etc/init.d/nya" <<-EOF
+#!/sbin/openrc-run
+start() {
+ :;
+}
+EOF
+
+# non-executable
+( ! rc_update add nya )
+( ! rc_update -s add nya )
+
+chmod +x "$root/etc/init.d/nya"
+
+# implicit runlevel
+rc_update add nya
+in_runlevel default nya
+
+rc_update del nya
+( ! in_runlevel default nya)
+# not in the runlevel
+( ! rc_update del nya )
+
+# explicit runlevel
+rc_update add nya default
+in_runlevel default nya
+# not in the specified runlevel
+( ! rc_update del nya boop )
+
+# stacking
+rc_update -s add boop
+stacked default boop
+
+rc_update -s del boop
+( ! stacked boop nya )
+
+rc_update -s add boop default
+stacked default boop
+
+rc_update -s del boop
+( ! stacked boop default )
+( ! rc_update -s del boop )
+
+# show
+rc_update add nya
+rc_update | grep -qE "^\s*nya"
+rc_update show | grep -qE "^\s*nya"
+rc_update show default | grep -qE "^\s*nya"
+rc_update del nya
+( ! rc_update show | grep -qE "^\s*nya" )
+( ! rc_update show default | grep -qE "^\s*nya" )
+
+exit 0
diff --git a/test/units/setup-root.sh b/test/units/setup-root.sh
new file mode 100755
index 00000000..6545cb5d
--- /dev/null
+++ b/test/units/setup-root.sh
@@ -0,0 +1,28 @@
+root=$(mktemp -d --tmpdir rc.XXXXXX)
+#trap "rm -r $root" EXIT
+
+sysdir="$root/etc"
+export RC_LIBEXECDIR="$BUILD_ROOT"
+export RC_PATH="$sysdir"
+export RC_SVCDIR="$root/run/openrc"
+mkdir -p "$RC_SVCDIR"
+echo "default" > "$RC_SVCDIR/softlevel"
+
+for dir in init.d conf.d runlevels; do
+ mkdir -p "$sysdir/$dir"
+done
+for dir in sysinit boot default boop shutdown; do
+ mkdir -p "$sysdir/runlevels/$dir"
+done
+
+for dir in daemons exclusive failed hotplugged inactive init.d \
+ options scheduled started starting stopping tmp wasinactive; do
+ mkdir -p "$root/run/openrc/$dir"
+done
+
+setup_path() {
+ local IFS=:
+ export PATH="$PATH:$*"
+}
+
+setup_path "$BUILD_ROOT"/src/*