Skip to content

Commit 57d2bab

Browse files
committed
tests: add basic rc-service tests
1 parent b7b38db commit 57d2bab

File tree

7 files changed

+195
-14
lines changed

7 files changed

+195
-14
lines changed

sh/meson.build

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ sh_config = [
2121
]
2222

2323
scripts_config = [
24-
'gendepends.sh.in',
2524
'openrc-run.sh.in',
2625
'openrc-user.sh.in',
26+
'gendepends.sh.in',
2727
's6-svscanboot.sh.in'
2828
]
2929

@@ -49,8 +49,12 @@ else
4949
]
5050
endif
5151

52-
install_data(sh,
53-
install_dir : rc_shdir)
52+
fs = import('fs')
53+
script_deps = []
54+
foreach file : sh
55+
script_deps += fs.copyfile(file, install: true, install_dir: rc_shdir)
56+
endforeach
57+
5458
foreach file : sh_config
5559
configure_file(input : file,
5660
output : '@BASENAME@',
@@ -60,10 +64,8 @@ endforeach
6064

6165
foreach file : scripts_config
6266
configure_file(input : file,
63-
output : '@BASENAME@',
64-
configuration : sh_conf_data,
65-
install_dir : rc_shdir,
66-
install_mode : 'rwxr-xr-x')
67+
output : '@BASENAME@', configuration : sh_conf_data,
68+
install_dir : rc_shdir, install_mode : 'rwxr-xr-x')
6769
endforeach
6870

6971
foreach file : scripts_config_os

src/openrc-run/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
executable('openrc-run', 'openrc-run.c',
1+
openrc_run = executable('openrc-run', 'openrc-run.c',
22
dependencies: [rc, einfo, shared, audit_dep, dl_dep, pam_dep, pam_misc_dep, selinux_dep, util_dep, crypt_dep],
33
include_directories: incdir,
44
install: true,

src/rc-service/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
executable('rc-service', 'rc-service.c',
1+
rc_service = executable('rc-service', 'rc-service.c',
22
include_directories: incdir,
33
dependencies: [rc, einfo, shared],
44
install: true,

test/units/meson.build

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
is_older_than = find_program('check-is-older-than.sh')
2-
sh_yesno = find_program('check-sh-yesno.sh')
3-
4-
test('is_older_than', is_older_than, env : test_env)
5-
test('sh_yesno', sh_yesno, env : test_env)
1+
test('is_older_than', find_program('check-is-older-than.sh'), env: test_env)
2+
test('sh_yesno', find_program('check-sh-yesno.sh'), env: test_env)
3+
test('rc-service', find_program('rc-service.sh'),
4+
env: [test_env, 'SUBDIR=' + meson.current_source_dir()],
5+
workdir: meson.project_source_root(),
6+
args: [ rc_service, openrc_run ],
7+
depends: script_deps)

test/units/rc-service.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
. "$SUBDIR/setup-root.sh"
6+
rc_service=${1?}
7+
openrc_run=${2?}
8+
cp "$RC_LIBEXECDIR/sh/openrc-run.sh" "$root/run/openrc/openrc-run.sh"
9+
cp "$RC_LIBEXECDIR/sh/gendepends.sh" "$root/run/openrc/gendepends.sh"
10+
chmod +x "$root/run/openrc/openrc-run.sh"
11+
chmod +x "$root/run/openrc/gendepends.sh"
12+
13+
rc_service() {
14+
$rc_service "$@" 2>&1
15+
}
16+
17+
in_state() {
18+
state=${1?}
19+
shift
20+
for svc; do
21+
test "$(readlink "$RC_SVCDIR/$state/$svc")" = "$root/etc/init.d/$svc"
22+
done
23+
}
24+
25+
mkservice() {
26+
service="$root/etc/init.d/$1"
27+
cat > "$service" <<-EOF
28+
#!$SOURCE_ROOT/${openrc_run}
29+
depend() {
30+
${2-:;}
31+
}
32+
start() {
33+
${3-sleep 0.1}
34+
}
35+
EOF
36+
chmod +x "$service"
37+
}
38+
39+
mkservice nya
40+
mkservice foo "need nya"
41+
mkservice mew "want foo"
42+
43+
addrunlevel() {
44+
ln -s "$root/etc/init.d/${1?}" "$root/etc/runlevel/${2?}/$1"
45+
}
46+
47+
rc_service nya start
48+
in_state started nya
49+
50+
rc_service nya stop
51+
( ! in_state started nya )
52+
53+
rc_service foo start
54+
in_state started foo nya
55+
56+
rc_service foo stop
57+
in_state started nya
58+
( ! in_state started foo )
59+
60+
rc_service foo start
61+
in_state started foo nya
62+
63+
rc_service nya stop
64+
( ! in_state started foo nya )
65+
66+
rc_service mew start
67+
in_state started foo nya mew
68+
69+
rc_service nya stop
70+
rc_service mew stop
71+
72+
( ! in_state started foo nya mew )

test/units/rc-update.sh

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
. ${1?}
6+
rc_update=${2?}
7+
8+
rc_update() {
9+
"$rc_update" --root "$root" "$@" 2>&1
10+
}
11+
12+
in_runlevel() {
13+
test "$(readlink "$root/etc/runlevels/${1?}/${2?}")" = "$root/etc/init.d/$2"
14+
}
15+
16+
stacked() {
17+
test -d "$root/etc/runlevels/${1?}/${2?}"
18+
}
19+
20+
# non-existent
21+
( ! rc_update add nya )
22+
( ! rc_update -s add nya )
23+
( ! rc_update del nya )
24+
( ! rc_update -s del nya )
25+
26+
cat > "$root/etc/init.d/nya" <<-EOF
27+
#!/sbin/openrc-run
28+
start() {
29+
:;
30+
}
31+
EOF
32+
33+
# non-executable
34+
( ! rc_update add nya )
35+
( ! rc_update -s add nya )
36+
37+
chmod +x "$root/etc/init.d/nya"
38+
39+
# implicit runlevel
40+
rc_update add nya
41+
in_runlevel default nya
42+
43+
rc_update del nya
44+
( ! in_runlevel default nya)
45+
# not in the runlevel
46+
( ! rc_update del nya )
47+
48+
# explicit runlevel
49+
rc_update add nya default
50+
in_runlevel default nya
51+
# not in the specified runlevel
52+
( ! rc_update del nya boop )
53+
54+
# stacking
55+
rc_update -s add boop
56+
stacked default boop
57+
58+
rc_update -s del boop
59+
( ! stacked boop nya )
60+
61+
rc_update -s add boop default
62+
stacked default boop
63+
64+
rc_update -s del boop
65+
( ! stacked boop default )
66+
( ! rc_update -s del boop )
67+
68+
# show
69+
rc_update add nya
70+
rc_update | grep -qE "^\s*nya"
71+
rc_update show | grep -qE "^\s*nya"
72+
rc_update show default | grep -qE "^\s*nya"
73+
rc_update del nya
74+
( ! rc_update show | grep -qE "^\s*nya" )
75+
( ! rc_update show default | grep -qE "^\s*nya" )
76+
77+
exit 0

test/units/setup-root.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
root=$(mktemp -d --tmpdir rc.XXXXXX)
2+
#trap "rm -r $root" EXIT
3+
4+
sysdir="$root/etc"
5+
export RC_LIBEXECDIR="$BUILD_ROOT"
6+
export RC_SCRIPTDIRS="$sysdir"
7+
export RC_SVCDIR="$root/run/openrc"
8+
mkdir -p "$RC_SVCDIR"
9+
echo "default" > "$RC_SVCDIR/softlevel"
10+
11+
for dir in init.d conf.d runlevels; do
12+
mkdir -p "$sysdir/$dir"
13+
done
14+
for dir in sysinit boot default boop shutdown; do
15+
mkdir -p "$sysdir/runlevels/$dir"
16+
done
17+
18+
for dir in daemons exclusive failed hotplugged inactive init.d \
19+
options scheduled started starting stopping tmp wasinactive; do
20+
mkdir -p "$root/run/openrc/$dir"
21+
done
22+
23+
setup_path() {
24+
local IFS=:
25+
export PATH="$PATH:$*"
26+
}
27+
28+
setup_path "$BUILD_ROOT"/src/*

0 commit comments

Comments
 (0)