Merge branch 'exit-code'

This commit is contained in:
Jerome Charaoui 2021-01-11 22:27:58 -05:00
commit cb7e5aa4e0
4 changed files with 68 additions and 13 deletions

View File

@ -111,6 +111,18 @@ root@srchost# ssh-copy-id \-i /root/.ssh/id_dsa.pub backup@desthost
.TP .TP
Now, you should be able to ssh from user 'root' on srchost to user 'backup' on desthost without specifying a password. When prompted for a password by ssh-keygen, just leave it blank by hitting return. The "wizard" \fBninjahelper(1)\fP will walk you through these steps. Now, you should be able to ssh from user 'root' on srchost to user 'backup' on desthost without specifying a password. When prompted for a password by ssh-keygen, just leave it blank by hitting return. The "wizard" \fBninjahelper(1)\fP will walk you through these steps.
.SH EXIT VALUES
.PD 0
\fB0\fP success
.br
\fB1\fP errors or warnings emitted during backup
.br
\fB2\fP one or more backup actions failed
.br
\fB3\fP syntax or usage error
.br
.PD
.SH FILES .SH FILES
.PD 0 .PD 0
\fB/usr/sbin/backupninja\fP main script \fB/usr/sbin/backupninja\fP main script

View File

@ -375,8 +375,8 @@ function process_action() {
[ $debug ] && colorize "$a" [ $debug ] && colorize "$a"
done done
) )
retcode=$? retcode=${PIPESTATUS[0]}
# ^^^^^^^^ we have a problem! we can't grab the return code "$?". grrr. debug "handler returned exit code $retcode"
echo_debug_msg=0 echo_debug_msg=0
else else
@ -468,10 +468,9 @@ while [ $# -ge 1 ]; do
;; ;;
*) *)
debug=1 debug=1
echo "Unknown option $1" error "Unknown option $1"
fatal "Unknown option $1"
usage usage
exit exit 3
;; ;;
esac esac
shift shift
@ -554,7 +553,7 @@ fi
if [ "$UID" != "0" ]; then if [ "$UID" != "0" ]; then
echo "`basename $0` can only be run as root" echo "`basename $0` can only be run as root"
exit 1 exit 3
fi fi
## Process each configuration file ## Process each configuration file
@ -648,3 +647,11 @@ if [ -n "$reporthost" ]; then
debug "send $logfile to $reportuser@$reporthost:$reportdirectory" debug "send $logfile to $reportuser@$reporthost:$reportdirectory"
rsync -qt $logfile $reportuser@$reporthost:$reportdirectory rsync -qt $logfile $reportuser@$reporthost:$reportdirectory
fi fi
# return exit code
[ $halts == 0 ] || exit 2
[ $fatals == 0 ] || exit 2
[ $errors == 0 ] || exit 1
[ "$reportwarning" == "yes" -a $warnings != 0 ] && exit 1
exit 0

View File

@ -14,8 +14,14 @@ create_test_action() {
@test "general: error thrown on bad command-line option" { @test "general: error thrown on bad command-line option" {
run backupninja --foo run backupninja --foo
[ "$status" -eq 3 ]
echo "${lines[0]}" | grep -q "Error: Unknown option --foo"
}
@test "general: error thrown on attempt to run as non-root user" {
run sudo -u vagrant backupninja -f "${BATS_TMPDIR}/backupninja.conf"
[ "$status" -eq 2 ] [ "$status" -eq 2 ]
[ "${lines[0]}" = "Unknown option --foo" ] [ "${lines[1]}" = "backupninja can only be run as root" ]
} }
@test "general: logfile is created" { @test "general: logfile is created" {
@ -65,7 +71,6 @@ create_test_action() {
@test "reports: error report is mailed" { @test "reports: error report is mailed" {
create_test_action fatal test_error create_test_action fatal test_error
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
sleep 0.1 sleep 0.1
grep -q "\*failed\* -- ${BATS_TMPDIR}/backup.d/test.sh" /var/mail/vagrant grep -q "\*failed\* -- ${BATS_TMPDIR}/backup.d/test.sh" /var/mail/vagrant
} }
@ -75,7 +80,6 @@ create_test_action() {
setconfig backupninja.conf reportsuccess no setconfig backupninja.conf reportsuccess no
setconfig backupninja.conf reportwarning yes setconfig backupninja.conf reportwarning yes
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
sleep 0.1 sleep 0.1
grep -q "Warning: test_warning" /var/mail/vagrant grep -q "Warning: test_warning" /var/mail/vagrant
} }
@ -84,7 +88,6 @@ create_test_action() {
create_test_action create_test_action
setconfig backupninja.conf reportsuccess yes setconfig backupninja.conf reportsuccess yes
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
sleep 0.1 sleep 0.1
grep -q "success -- ${BATS_TMPDIR}/backup.d/test.sh" /var/mail/vagrant grep -q "success -- ${BATS_TMPDIR}/backup.d/test.sh" /var/mail/vagrant
} }
@ -94,7 +97,6 @@ create_test_action() {
setconfig backupninja.conf reportsuccess yes setconfig backupninja.conf reportsuccess yes
setconfig backupninja.conf reportinfo yes setconfig backupninja.conf reportinfo yes
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
sleep 0.1 sleep 0.1
grep -q "Info: test_info" /var/mail/vagrant grep -q "Info: test_info" /var/mail/vagrant
} }
@ -105,7 +107,6 @@ create_test_action() {
setconfig backupninja.conf reportsuccess yes setconfig backupninja.conf reportsuccess yes
setconfig backupninja.conf reportspace yes setconfig backupninja.conf reportspace yes
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh" run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
sleep 0.1 sleep 0.1
grep -q "/dev/sda1" /var/mail/vagrant grep -q "/dev/sda1" /var/mail/vagrant
} }
@ -174,4 +175,40 @@ create_test_action() {
grep -q "Debug: skipping ${BATS_TMPDIR}/backup.d/test.sh because current time does not match 21 at 09:00" "${BATS_TMPDIR}/log/backupninja.log" grep -q "Debug: skipping ${BATS_TMPDIR}/backup.d/test.sh because current time does not match 21 at 09:00" "${BATS_TMPDIR}/log/backupninja.log"
} }
@test "exit code: rc=2 when halt error raised in handler" {
create_test_action halt test_halt
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 2 ]
}
@test "exit code: rc=2 when fatal error raised in handler" {
create_test_action fatal test_fatal
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 2 ]
}
@test "exit code: rc=1 when error raised in handler" {
create_test_action error test_error
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 1 ]
}
@test "exit code: rc=1 when warning raised in handler and reportwarning=yes" {
create_test_action warning test_warning
setconfig backupninja.conf reportwarning yes
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 1 ]
}
@test "exit code: rc=0 when warning raised in handler and reportwarning=no" {
create_test_action warning test_warning
setconfig backupninja.conf reportwarning no
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
}
@test "exit code: rc=0 when no warnings/errors raised in handler" {
create_test_action "true"
run backupninja --now -f "${BATS_TMPDIR}/backupninja.conf" --run "${BATS_TMPDIR}/backup.d/test.sh"
[ "$status" -eq 0 ]
}

View File

@ -176,7 +176,6 @@ runaction() {
if [ -f "${BATS_TMPDIR}/backup.d/${ACTIONFILE}" ]; then if [ -f "${BATS_TMPDIR}/backup.d/${ACTIONFILE}" ]; then
[ -f "${BATS_TMPDIR}/log/backupninja.log" ] && rm -f "${BATS_TMPDIR}/log/backupninja.log" [ -f "${BATS_TMPDIR}/log/backupninja.log" ] && rm -f "${BATS_TMPDIR}/log/backupninja.log"
run backupninja -f "${BATS_TMPDIR}/backupninja.conf" $TEST --debug --now --run "${BATS_TMPDIR}/backup.d/${ACTIONFILE}" run backupninja -f "${BATS_TMPDIR}/backupninja.conf" $TEST --debug --now --run "${BATS_TMPDIR}/backup.d/${ACTIONFILE}"
[ "$status" -eq 0 ]
else else
echo "action file not found: ${BATS_TMPDIR}/backup.d/${ACTIONFILE}" echo "action file not found: ${BATS_TMPDIR}/backup.d/${ACTIONFILE}"
false false