diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2022-05-19 13:04:37 +0200 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2022-05-19 13:04:37 +0200 |
commit | 8885850cab6cecf83d65ff7233677827e16762fa (patch) | |
tree | df7fa2f9aebfb1a3dc40facd7a63cf3922fcefa1 | |
parent | bc01f011019828ffda721ba2078ee75f216e4ae7 (diff) | |
download | alpine-conf-8885850cab6cecf83d65ff7233677827e16762fa.zip |
tests: add fake rc-update binary
and add tests for it to verify that it actually works as expected
We can use this for the other tests later
-rwxr-xr-x | tests/bin/rc-update | 67 | ||||
-rwxr-xr-x | tests/fake_rc_update_test | 99 |
2 files changed, 166 insertions, 0 deletions
diff --git a/tests/bin/rc-update b/tests/bin/rc-update new file mode 100755 index 0000000..cb7ee36 --- /dev/null +++ b/tests/bin/rc-update @@ -0,0 +1,67 @@ +#!/bin/sh + +prog=$0 +usage() { + echo "usage: rc-update [--quiet] add|del|delete SVC RUNLEVEL" + exit $1 +} + +msg() { + if ! [ -n "$quiet" ]; then + echo "$@" + fi +} + +OPTS=$(getopt -l quiet,help -o "qh" -n $prog -- "$@") || usage "1" >&2 + +quiet= +eval set -- "$OPTS" +for opt; do + case "$opt" in + -h|--help) + usage 0 + ;; + --quiet) + quiet=1 + ;; + --) + shift + break + ;; + *) usage "1" >&2 + ;; + esac + shift +done + +cmd="$1" +svc="$2" +runlevel="${3:-default}" + +if [ -z "$cmd" ] || [ -z "$svc" ]; then + usage "1" >&2 +fi + +case "$cmd" in + add) + if [ -L "$ROOT"/etc/runlevels/$runlevel/$svc ]; then + msg " * rc-update: $svc already installed in runlevel \`$runlevel'; skipping" + exit 0 + fi + mkdir -p "$ROOT"/etc/runlevels/$runlevel + ln -sf ../../init.d/$svc "$ROOT"/etc/runlevels/$runlevel/$svc + msg " * service $svc added to runlevel $runlevel" + ;; + del|delete) + if ! [ -L "$ROOT"/etc/runlevels/$runlevel/$svc ]; then + msg " * rc-update: service \`$svc' is not in the runlevel \`$runlevel'" >&2 + exit 1 + fi + rm "$ROOT"/etc/runlevels/$runlevel/$svc + msg " * service foo removed from runlevel $runlevel" + ;; + *) usage "1" >&2 + ;; +esac + + diff --git a/tests/fake_rc_update_test b/tests/fake_rc_update_test new file mode 100755 index 0000000..2a359c5 --- /dev/null +++ b/tests/fake_rc_update_test @@ -0,0 +1,99 @@ +#!/usr/bin/env atf-sh + +. $(atf_get_srcdir)/test_env.sh +init_tests \ + rc_update_usage \ + rc_update_add \ + rc_update_add_quiet \ + rc_update_del \ + rc_update_del_quiet \ + rc_update_runlevel + +rc_update_usage_body() { + test_usage rc-update +} + +rc_update_add_body() { + init_env + atf_check -s exit:0 \ + -o match:"service foo added to runlevel default" \ + -e empty \ + rc-update add foo + test -L etc/runlevels/default/foo || atf_fail "symlink not created" + + atf_check -s exit:0 \ + -o match:"foo already installed" \ + -e empty \ + rc-update add foo +} + +rc_update_add_quiet_body() { + init_env + atf_check -s exit:0 \ + -o empty \ + -e empty \ + rc-update add --quiet foo + test -L etc/runlevels/default/foo || atf_fail "symlink not created" + + atf_check -s exit:0 \ + -o empty \ + -e empty \ + rc-update add --quiet foo +} + +rc_update_del_body() { + init_env + rc-update --quiet add foo + atf_check -s exit:0 \ + -o match:"service foo removed from runlevel default" \ + -e empty \ + rc-update del foo + + if test -L etc/runlevels/default/foo; then + atf_fail "symlink not removed" + fi + + atf_check -s exit:1 \ + -o empty \ + -e match:"is not in the runlevel" \ + rc-update del foo +} + +rc_update_del_quiet_body() { + init_env + rc-update --quiet add foo + atf_check -s exit:0 \ + -o empty \ + -e empty \ + rc-update del --quiet foo + + if test -L etc/runlevels/default/foo; then + atf_fail "symlink not removed" + fi + + atf_check -s exit:1 \ + -o empty \ + -e empty \ + rc-update del --quiet foo +} + +rc_update_runlevel_body() { + init_env + + atf_check -s exit:0 \ + -o empty \ + -e empty \ + rc-update add --quiet foo sysinit + + test -L etc/runlevels/sysinit/foo || atf_fail "symlink not created in sysinit" + + atf_check -s exit:0 \ + -o match:"service foo removed from runlevel sysinit" \ + -e empty \ + rc-update delete foo sysinit + + if test -L etc/runlevels/sysinit/foo; then + atf_fail "symlink not removed" + fi +} + |