summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rwxr-xr-xtests/bin/rc-service107
-rwxr-xr-xtests/fake_rc_service_test187
2 files changed, 294 insertions, 0 deletions
diff --git a/tests/bin/rc-service b/tests/bin/rc-service
new file mode 100755
index 0000000..f6b570e
--- /dev/null
+++ b/tests/bin/rc-service
@@ -0,0 +1,107 @@
+#!/bin/sh
+
+prog=$0
+usage() {
+ echo "usage: rc-service [OPTS] SERVICE [ACTION]"
+ exit $1
+}
+
+msg() {
+ if ! [ -n "$quiet" ]; then
+ echo "$@"
+ fi
+}
+
+OPTS=$(getopt -l help,exists,ifexists,ifstarted,ifstopped,verbose,quiet \
+ -o "heiNsSvq" -n $prog -- "$@") || usage "1" >&2
+
+# -e, --exists <arg> tests if the service exists or not
+# -i, --ifexists if the service exists run the command
+# -N, --ifnotstarted if the service is not started run the command
+# -s, --ifstarted if the service is started run the command
+# -S, --ifstopped if the service is stopped run the command
+# -v, --verbose Run verbosely
+# -q, --quiet Run quietly (repeat to suppress errors)
+
+eval set -- "$OPTS"
+for opt; do
+ case "$opt" in
+ -h|--help)
+ usage 0
+ ;;
+ --exists)
+ exists=1
+ ;;
+ --ifexists)
+ ifexists=1
+ ;;
+ --ifstarted)
+ ifstarted=1
+ ;;
+ --ifstopped)
+ ifstopped=1
+ ;;
+ --verbose)
+ verbose=1
+ ;;
+ --quiet)
+ quiet=1
+ ;;
+ --)
+ shift
+ break
+ ;;
+ esac
+ shift
+done
+
+svc="$1"
+action="$2"
+
+if [ -z "$svc" ]; then
+ usage "1" >&2
+fi
+
+if [ -z "$action" ] && [ -z "$exists" ]; then
+ usage "1" >&2
+fi
+
+if [ -n "$exists" ]; then
+ test -e "$ROOT"/etc/init.d/$svc
+ exit
+fi
+
+if [ -n "$ifstarted" ] && ! [ -e "$ROOT"/run/started/$svc ]; then
+ exit 0
+fi
+
+if [ -n "$ifstopped" ] && [ -e "$ROOT"/run/started/$svc ]; then
+ exit 0
+fi
+
+case "$action" in
+ start)
+ if [ -e "$ROOT"/run/started/$svc ]; then
+ ! [ -n "$ifstopped" ] && msg " * WARNING: $svc has already been started" >&2
+ exit 0
+ fi
+
+ msg " * Starting $svc ... [ok]"
+ mkdir -p "$ROOT"/run/started
+ touch "$ROOT"/run/started/$svc
+ ;;
+ stop)
+ if ! [ -e "$ROOT"/run/started/$svc ]; then
+ msg " * WARNING: $svc is already stopped" >&2
+ exit 0
+ fi
+
+ msg "* Stopping $svc ... [ok]"
+ rm -f "$ROOT"/run/started/$svc
+ ;;
+ restart)
+ $prog --ifstarted $svc stop && $prog $svc start
+ ;;
+ *) usage 1
+esac
+
diff --git a/tests/fake_rc_service_test b/tests/fake_rc_service_test
new file mode 100755
index 0000000..d62bbe9
--- /dev/null
+++ b/tests/fake_rc_service_test
@@ -0,0 +1,187 @@
+#!/usr/bin/env atf-sh
+
+. $(atf_get_srcdir)/test_env.sh
+init_tests \
+ rc_service_usage \
+ rc_service_not_exists \
+ rc_service_exists \
+ rc_service_start \
+ rc_service_start_quiet \
+ rc_service_already_started \
+ rc_service_stop \
+ rc_service_stop_quiet \
+ rc_service_already_stopped \
+ rc_service_ifstarted_stop \
+ rc_service_ifstarted_start \
+ rc_service_ifstopped_start \
+ rc_service_ifstopped_stop \
+ rc_service_restart_stopped \
+ rc_service_restart_started
+
+test_started() {
+ test -e run/started/$1 || atf_fail "$1 was not started"
+}
+
+test_stopped() {
+ ! test -e run/started/$1 || atf_fail "$1 was not stopped"
+}
+
+rc_service_usage_body() {
+ test_usage rc-service
+}
+
+rc_service_not_exists_body() {
+ init_env
+ atf_check -s exit:1 \
+ -o empty \
+ -e empty \
+ rc-service --exist foo
+}
+
+rc_service_exists_body() {
+ init_env
+ mkdir -p etc/init.d
+ touch etc/init.d/foo
+
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --exist foo
+}
+
+rc_service_start_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o match:"Starting foo" \
+ -e empty \
+ rc-service foo start
+ test_started foo
+}
+
+rc_service_start_quiet_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --quiet foo start
+ test_started foo
+}
+
+rc_service_already_started_body() {
+ init_env
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o empty \
+ -e match:"WARNING: foo has already been started" \
+ rc-service foo start
+
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --quiet foo start
+}
+
+rc_service_stop_body() {
+ init_env
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o match:"Stopping foo" \
+ -e empty \
+ rc-service foo stop
+ test_stopped foo
+}
+
+rc_service_stop_quiet_body() {
+ init_env
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --quiet foo stop
+ test_stopped foo
+}
+
+rc_service_already_stopped_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o empty \
+ -e match:"WARNING: foo is already stopped" \
+ rc-service foo stop
+
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --quiet foo stop
+}
+
+rc_service_ifstarted_stop_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --ifstarted foo stop
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o match:"Stopping foo" \
+ -e empty \
+ rc-service --ifstarted foo stop
+}
+
+rc_service_ifstarted_start_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --ifstarted foo start
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o empty \
+ -e match:"WARNING: foo has already been started" \
+ rc-service --ifstarted foo start
+}
+
+rc_service_ifstopped_start_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o match:"Starting foo" \
+ -e empty \
+ rc-service --ifstopped foo start
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --ifstopped foo start
+}
+
+rc_service_ifstopped_stop_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o empty \
+ -e match:"WARNING: foo is already stopped" \
+ rc-service --ifstopped foo stop
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o empty \
+ -e empty \
+ rc-service --ifstopped foo stop
+}
+
+rc_service_restart_started_body() {
+ init_env
+ rc-service --quiet foo start
+ atf_check -s exit:0 \
+ -o match:"Stopping foo" \
+ -o match:"Starting foo" \
+ -e empty \
+ rc-service foo restart
+ test_started foo
+}
+
+rc_service_restart_stopped_body() {
+ init_env
+ atf_check -s exit:0 \
+ -o match:"Starting foo" \
+ -e empty \
+ rc-service foo restart
+ test_started foo
+}
+