scripts: Improve error handling for 50.samba testparm failure
authorMartin Schwenke <martin@meltin.net>
Thu, 30 Jul 2015 06:49:35 +0000 (16:49 +1000)
committerMartin Schwenke <martin@meltin.net>
Wed, 24 Feb 2016 05:14:36 +0000 (16:14 +1100)
Also add tests.  Update testparm stub to fake error and timeout.  Add
timeout stub.

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
(Imported from commit 7d04778c82a8f657b6ba0173c29529fa03ab7a25)

config/events.d/50.samba
tests/eventscripts/50.samba.monitor.110.sh [new file with mode: 0755]
tests/eventscripts/50.samba.monitor.111.sh [new file with mode: 0755]
tests/eventscripts/50.samba.monitor.112.sh [new file with mode: 0755]
tests/eventscripts/50.samba.monitor.113.sh [new file with mode: 0755]
tests/eventscripts/stubs/testparm
tests/eventscripts/stubs/timeout [new file with mode: 0755]

index 4b53cba01be6f9736a25a2509df692755fc7b73a..1742ff19249f4ad5ef5461ec299b1ece031dbb1f 100755 (executable)
@@ -78,19 +78,42 @@ testparm_foreground_update ()
 {
     _timeout="$1"
 
-    if ! _out=$(timeout $_timeout testparm -v -s 2>/dev/null) ; then
-       if [ -f "$smbconf_cache" ] ; then
-           echo "WARNING: smb.conf cache update failed - using old cache file"
-           return 1
-       else
-           die "ERROR: smb.conf cache create failed"
-       fi
-    fi
-
+    # No need to remove these temporary files, since there are only 2
+    # of them.
+    _out="${smbconf_cache}.out"
+    _err="${smbconf_cache}.err"
+
+    timeout $_timeout testparm -v -s >"$_out" 2>"$_err"
+    case $? in
+       0) : ;;
+       124)
+           if [ -f "$smbconf_cache" ] ; then
+               echo "WARNING: smb.conf cache update timed out - using old cache file"
+               return 1
+           else
+               echo "ERROR: smb.conf cache create failed - testparm command timed out"
+               exit 1
+           fi
+           ;;
+       *)
+           if [ -f "$smbconf_cache" ] ; then
+               echo "WARNING: smb.conf cache update failed - using old cache file"
+               cat "$_err"
+               return 1
+           else
+               echo "ERROR: smb.conf cache create failed - testparm failed with:"
+               cat "$_err"
+               exit 1
+           fi
+    esac
+
+    # Only using $$ here to avoid a collision.  This is written into
+    # CTDB's own state directory so there is no real need for a secure
+    # temporary file.
     _tmpfile="${smbconf_cache}.$$"
     # Patterns to exclude...
-    pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='    
-    echo "$_out" | grep -Ev "$pat" >"$_tmpfile"
+    _pat='^[[:space:]]+(registry[[:space:]]+shares|include|copy|winbind[[:space:]]+separator)[[:space:]]+='
+    grep -Ev "$_pat" <"$_out" >"$_tmpfile"
     mv "$_tmpfile" "$smbconf_cache" # atomic
 
     return 0
diff --git a/tests/eventscripts/50.samba.monitor.110.sh b/tests/eventscripts/50.samba.monitor.110.sh
new file mode 100755 (executable)
index 0000000..2151933
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "testparm fails"
+
+setup_samba
+
+export FAKE_TESTPARM_FAIL="yes"
+required_result 1 <<EOF
+ERROR: smb.conf cache create failed - testparm failed with:
+Load smb config files from ${CTDB_ETCDIR}/samba/smb.conf
+rlimit_max: increasing rlimit_max (2048) to minimum Windows limit (16384)
+Processing section "[1_existing]"
+Processing section "[2_existing]"
+Processing section "[3_existing]"
+Loaded services file OK.
+WARNING: 'workgroup' and 'netbios name' must differ.
+EOF
+simple_test
diff --git a/tests/eventscripts/50.samba.monitor.111.sh b/tests/eventscripts/50.samba.monitor.111.sh
new file mode 100755 (executable)
index 0000000..53f2f67
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "testparm fails on 2nd time through"
+
+setup_samba
+
+ok_null
+simple_test
+
+export FAKE_TESTPARM_FAIL="yes"
+required_result 1 <<EOF
+WARNING: smb.conf cache update failed - using old cache file
+Load smb config files from ${CTDB_ETCDIR}/samba/smb.conf
+rlimit_max: increasing rlimit_max (2048) to minimum Windows limit (16384)
+Processing section "[1_existing]"
+Processing section "[2_existing]"
+Processing section "[3_existing]"
+Loaded services file OK.
+WARNING: 'workgroup' and 'netbios name' must differ.
+
+Failed to set smb ports
+EOF
+simple_test
diff --git a/tests/eventscripts/50.samba.monitor.112.sh b/tests/eventscripts/50.samba.monitor.112.sh
new file mode 100755 (executable)
index 0000000..a7a487f
--- /dev/null
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "testparm times out"
+
+setup_samba
+
+export FAKE_TIMEOUT="yes"
+required_result 1 <<EOF
+ERROR: smb.conf cache create failed - testparm command timed out
+EOF
+simple_test
diff --git a/tests/eventscripts/50.samba.monitor.113.sh b/tests/eventscripts/50.samba.monitor.113.sh
new file mode 100755 (executable)
index 0000000..56bd86d
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+. "${TEST_SCRIPTS_DIR}/unit.sh"
+
+define_test "testparm times out on 2nd time through"
+
+setup_samba
+
+ok_null
+simple_test
+
+export FAKE_TIMEOUT="yes"
+ok <<EOF
+WARNING: smb.conf cache update timed out - using old cache file
+EOF
+simple_test
index aac5b18110540874599a4c00874cd29f78cb3a3a..692db45a49d8c9129bc0023d0f454ca508727227 100755 (executable)
@@ -6,8 +6,42 @@ not_implemented ()
     exit 2
 }
 
+error ()
+{
+    cat >&2 <<EOF
+Load smb config files from ${CTDB_ETCDIR}/samba/smb.conf
+rlimit_max: increasing rlimit_max (2048) to minimum Windows limit (16384)
+EOF
+
+    for i in $FAKE_SHARES ; do
+       bi=$(basename "$i")
+       echo "Processing section \"[${bi}]\""
+    done >&2
+
+    cat >&2 <<EOF
+Loaded services file OK.
+WARNING: 'workgroup' and 'netbios name' must differ.
+
+EOF
+
+    exit 1
+}
+
+timeout ()
+{
+    echo "$0: INTERNAL ERROR - timeout stub should avoid this" >&2
+}
+
+if [ -n "$FAKE_TESTPARM_FAIL" ] ; then
+    error
+fi
+
+if [ -n "$FAKE_TIMEOUT" ] ; then
+    timeout
+fi
+
 # Ensure that testparm always uses our canned configuration instead of
-# the global one, unless some other file is specified. 
+# the global one, unless some other file is specified.
 
 file=""
 parameter=""
diff --git a/tests/eventscripts/stubs/timeout b/tests/eventscripts/stubs/timeout
new file mode 100755 (executable)
index 0000000..1ddce53
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+if [ -n "$FAKE_TIMEOUT" ] ; then
+    exit 124
+else
+    shift 1
+    exec "$@"
+fi