add functions to manipulate ENABLED_SERVICES
Editing ENABLED_SERVICES directly can get tricky when
the user wants to disable something. This patch includes
two new functions for adding or removing services
safely, and a third (for completeness) to clear the
settings entirely before adding a minimal set of
services.
It also moves the logic for dealing with "negated"
services into a function so it can be tested and
applied by the new functions for manipulating
ENABLED_SERVICES.
Change-Id: I88f205f3666b86e6f0b6a94e0ec32a26c4bc6873
Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
diff --git a/tests/functions.sh b/tests/functions.sh
index e436ed9..f111a48 100755
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -143,3 +143,99 @@
fi
rm test.ini
+
+# Enabling/disabling services
+
+echo "Testing enable_service()"
+
+function test_enable_service() {
+ local start="$1"
+ local add="$2"
+ local finish="$3"
+
+ ENABLED_SERVICES="$start"
+ enable_service $add
+ if [ "$ENABLED_SERVICES" = "$finish" ]
+ then
+ echo "OK: $start + $add -> $ENABLED_SERVICES"
+ else
+ echo "changing $start to $finish with $add failed: $ENABLED_SERVICES"
+ fi
+}
+
+test_enable_service '' a 'a'
+test_enable_service 'a' b 'a,b'
+test_enable_service 'a,b' c 'a,b,c'
+test_enable_service 'a,b' c 'a,b,c'
+test_enable_service 'a,b,' c 'a,b,c'
+test_enable_service 'a,b' c,d 'a,b,c,d'
+test_enable_service 'a,b' "c d" 'a,b,c,d'
+test_enable_service 'a,b,c' c 'a,b,c'
+
+test_enable_service 'a,b,-c' c 'a,b'
+test_enable_service 'a,b,c' -c 'a,b'
+
+function test_disable_service() {
+ local start="$1"
+ local del="$2"
+ local finish="$3"
+
+ ENABLED_SERVICES="$start"
+ disable_service "$del"
+ if [ "$ENABLED_SERVICES" = "$finish" ]
+ then
+ echo "OK: $start - $del -> $ENABLED_SERVICES"
+ else
+ echo "changing $start to $finish with $del failed: $ENABLED_SERVICES"
+ fi
+}
+
+echo "Testing disable_service()"
+test_disable_service 'a,b,c' a 'b,c'
+test_disable_service 'a,b,c' b 'a,c'
+test_disable_service 'a,b,c' c 'a,b'
+
+test_disable_service 'a,b,c' a 'b,c'
+test_disable_service 'b,c' b 'c'
+test_disable_service 'c' c ''
+test_disable_service '' d ''
+
+test_disable_service 'a,b,c,' c 'a,b'
+test_disable_service 'a,b' c 'a,b'
+
+
+echo "Testing disable_all_services()"
+ENABLED_SERVICES=a,b,c
+disable_all_services
+
+if [[ -z "$ENABLED_SERVICES" ]]
+then
+ echo "OK"
+else
+ echo "disabling all services FAILED: $ENABLED_SERVICES"
+fi
+
+echo "Testing disable_negated_services()"
+
+
+function test_disable_negated_services() {
+ local start="$1"
+ local finish="$2"
+
+ ENABLED_SERVICES="$start"
+ disable_negated_services
+ if [ "$ENABLED_SERVICES" = "$finish" ]
+ then
+ echo "OK: $start + $add -> $ENABLED_SERVICES"
+ else
+ echo "changing $start to $finish failed: $ENABLED_SERVICES"
+ fi
+}
+
+test_disable_negated_services '-a' ''
+test_disable_negated_services '-a,a' ''
+test_disable_negated_services '-a,-a' ''
+test_disable_negated_services 'a,-a' ''
+test_disable_negated_services 'b,a,-a' 'b'
+test_disable_negated_services 'a,b,-a' 'b'
+test_disable_negated_services 'a,-a,b' 'b'