Imported Upstream version 3.3.3
[debian/amanda] / packaging / common / test_sh_libs.sh
index 22c4fb6cf87351bc12d0ed0e58b42917419a29af..265fb0640f5be352aabb73b8bf04005052a9b9cb 100755 (executable)
 # SHUNIT_* and SHU_* are shunit2 variables
 # test_* should only be used by test functions.
 # All other variables will probably be used by pre/post/common functions.
-
-# shunit2 assumes _inc is, but we might run from a separate builddir. So try to
-# use srcdir, as defined by automake, or otherwise `pwd`.
+case $1 in
+    DEBUG) DEBUG=True
+        shift
+        ;;
+    *) DEBUG= ;;
+esac
+export DEBUG
+
+ex_from=`dirname $0`
+
+# shunit2 assumes shunit_inc, but we might run from a separate
+# builddir. So try to use srcdir, as defined by automake, or
+# otherwise `pwd`.
 SHUNIT_INC="${srcdir=`pwd`}/packaging/common"; export SHUNIT_INC
-
 TMPDIR=`pwd`/shunit-test; export TMPDIR
 amanda_user=test_amandabackup; export amanda_user
 amanda_group=test_disk; export amanda_group
@@ -31,6 +40,7 @@ wanted_shell='/bin/false'; export wanted_shell
 dist=Fedora; export dist
 SYSCONFDIR=$TMPDIR/etc; export SYSCONFDIR
 SBINDIR=$TMPDIR/sbin; export SBINDIR
+encoder=`{ command -v base64 2>/dev/null; } || { command -v uuencode 2>/dev/null; }`
 # Don't potentially conflict with a real value...
 deb_uid=63000; export deb_uid
 
@@ -42,16 +52,11 @@ test_cleanup_files="$TMPDIR"; export test_cleanup_files
 # shunit2 is sourced.
 mkdir -p ${TMPDIR} || exit 1
 {
-    LOGFILE=`
-       (umask 077 && mktemp "$TMPDIR/test-log.XXXX") 2> /dev/null
-       ` &&
-       test -f "$LOGFILE"
+    LOGFILE="$TMPDIR/test_sh_libs.log"
+    (umask 077 && touch $LOGFILE)
 } || {
-    LOGFILE=$TMPDIR/test-log.$$.$RANDOM
-    (umask 077 && touch "$LOGFILE")
-} || {
-       echo "Unable to create log file!"
-       exit 1
+    echo "Unable to create log file!"
+    exit 1
 }
 export LOGFILE
 
@@ -79,7 +84,7 @@ oneTimeSetUp() {
 }
 
 oneTimeTearDown() {
-    if [ ${__shunit_assertsFailed} -eq 0 ]; then
+    if [ ${__shunit_assertsFailed} -eq 0 ] && [ ! "${DEBUG}" ]; then
        rm -rf $test_cleanup_files
     else
        echo "Check ${test_cleanup_files} for logs and error info."
@@ -114,6 +119,12 @@ fi
 # Source our mock utils.
 . ${SHUNIT_INC}/mock_utils.sh
 
+log_tail_no_stamp() {
+    # This strips the date off of a log line so only the message is
+    # compared.
+    LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
+}
+
 ######################################
 # Common functions
 
@@ -122,13 +133,13 @@ fi
 test___logger() {
     # Write a line to the log, test that it got there.
     TEST_MSG="test___logger message"
-    LOG_LINE="`date +'%b %e %Y %T'`: ${TEST_MSG}"
+    LOG_LINE="`date +'%b %d %Y %T'`: ${TEST_MSG}"
     # It's important for the log messages to be quoted, or funny stuff happens.
     logger "${TEST_MSG}"
     assertEquals "logger() return code" 0 $?
-    LOG_TAIL=`tail -1 ${LOGFILE}`
+    log_tail_no_stamp
     assertEquals "logger() did not write <${LOG_LINE}> " \
-       "${LOG_LINE}" "${LOG_TAIL}"
+       "${TEST_MSG}" "${LOG_TAIL}"
     # Leave this outside the unit test framework.  if the logger is
     # broken we must exit.
     if [ ! `grep -c "${LOG_LINE}" ${LOGFILE}` = "1" ]; then
@@ -142,6 +153,10 @@ test__log_output_of() {
     TEST_MSG="test__log_output_of message"
     log_output_of echo "${TEST_MSG}"
     assertEquals "log_output_of()" 0 $?
+    LOG_LINE="echo: ${TEST_MSG}"
+    log_tail_no_stamp
+    assertEquals "log_output_of() message should be: " \
+        "${LOG_LINE}" "${LOG_TAIL}"
     COUNT=`grep -c "${TEST_MSG}" ${LOGFILE}`
     assertEquals "log_output_of(): incorrect content in log" \
        1 ${COUNT}
@@ -272,7 +287,7 @@ test_reload_xinetd() {
     # Might need init script.
     if [ "$IAmRoot" ]; then
         startSkipping
-        echo "test_install_smf: skipped"
+        echo "test_reload_xinetd: skipped"
         return
     elif [ ! -f "${SYSCONFDIR}/init.d/xinetd" ]; then
         mv ${MOCKDIR}/xinetd ${SYSCONFDIR}/init.d
@@ -284,6 +299,12 @@ test_reload_xinetd() {
     touch ${MOCKDIR}/success
     reload_xinetd "reload"
     assertEquals "reload_xinetd" 0 $?
+    # Test no argument
+    reload_xinetd
+    assertEquals "reload_xinetd should attempt to reload" 0 $?
+    flags=`cat ${mock_xinetd_flags}`
+    assertEquals "xinetd_flags should contain: " \
+        "xinetd args: reload" "${flags}"
     # Test failure
     rm ${MOCKDIR}/success
     reload_xinetd "reload"
@@ -292,7 +313,7 @@ test_reload_xinetd() {
     assertEquals "reload_xinetd should try to restart." 0 $?
     reload_xinetd "restart"
     assertEquals "restart should fail." 1 $?
-    tail -3 ${LOGFILE}|grep "Restarting xinetd" >/dev/null
+    tail -3 ${LOGFILE}|grep "restarting xinetd" >/dev/null
     assertEquals "Should log attempt to restart" 0 $?
 }
 
@@ -339,7 +360,7 @@ test_check_user_group_missing() {
         echo $os > ${MOCKDIR}/id_os
         check_user_group "abracadabra"
         assertNotEquals "'check_user group abracadabra' should not be found:" 0 $?
-        LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
+        log_tail_no_stamp
         assertEquals "check_user_group should write" \
             "User's primary group 'abracadabra' does not exist" \
             "${LOG_TAIL}"
@@ -467,6 +488,20 @@ test_create_user() {
     assertEquals "create_user()" 0 $?
 }
 
+test_add_profiles() {
+    # Solaris only, but testing using mock usermod will run if not root
+    # on any system.
+    logger "test_add_profiles"
+    [ "$IAmRoot" ] && startSkipping
+    add_profiles "Profile foo,Profile bar"
+    assertEquals "add_profiles should succeed" 0 $?
+    flags=`cat ${mock_usermod_flags}`
+    assertEquals "usermod_flags should contain:" \
+        "usermod args: -P \"Profile foo,Profile bar\" ${amanda_user}" \
+        "${flags}"
+
+}
+
 test_add_group_check_parameters_logs() {
     rm -f ${MOCKDIR}/groupadd_rc ${MOCKDIR}/num_groups
     # Return codes are integers.
@@ -474,7 +509,7 @@ test_add_group_check_parameters_logs() {
     # Test that first parameter is required.
     add_group
     assertEquals "add_group without a group should fail." 1 $?
-    LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
+    log_tail_no_stamp
     assertEquals "add_group should write" \
         "Error: first argument was not a group to add." \
         "${LOG_TAIL}"
@@ -582,23 +617,86 @@ test_check_amandates() {
        "`cat $mock_chmod_flags`"
 }
 
-test_create_gnupg() {
+test_a_create_gnupg() {
+    # We need to impose some order on a few tests because some functions
+    # rely on others.  Tests are sorted alphabetically.  Insert a letter after
+    # "test_" to impose order on particular tests
     logger "test_create_gnupg"
     create_gnupg
     assertEquals "create_gnupg" 0 $?
     assertTrue "[ -d ${AMANDAHOMEDIR}/.gnupg ]"
+    # Dir exists
+    create_gnupg
+    assertEquals "create_gnupg dir existing" 0 $?
+}
+
+test_a_get_random_lines() {
+    logger "test_get_random_lines"
+    get_random_lines > ${TMPDIR}/lines
+    assertEquals "get_random_lines" 1 $?
+    get_random_lines 1 > ${TMPDIR}/lines
+    assertEquals "get_random_lines 1" 0 $?
+    assertEquals "get_random_lines 1 output" 1 "`sed -n '$=' ${TMPDIR}/lines`"
+    get_random_lines 20 > ${TMPDIR}/lines
+    assertEquals "get_random_lines 20 output" 20 "`sed -n '$=' ${TMPDIR}/lines`"
+}
+
+test_b_create_ampassphrase() {
+    rm -f ${AMANDAHOMEDIR}/.am_passphrase
+    logger "test_create_ampassphrase"
+    create_ampassphrase
+    assertEquals "create_ampassphrase" 0 $?
+    assertSame \
+        "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.am_passphrase" \
+        "`cat $mock_chown_flags`"
+    assertSame \
+        "chmod args: 0600 ${AMANDAHOMEDIR}/.am_passphrase" \
+        "`cat $mock_chmod_flags`"
+    # When .am_passphrase exists.
+    create_ampassphrase
+    log_tail_no_stamp
+    assertSame \
+        "Info: ${AMANDAHOMEDIR}/.am_passphrase already exists." \
+        "${LOG_TAIL}"
+    rm ${AMANDAHOMEDIR}/.am_passphrase
+}
+
+test_b_create_amkey() {
+    logger "test_create_amkey"
+    # Missing .am_passphrase
+    [ -f ${AMANDAHOMEDIR}/.am_passphrase ] && rm ${AMANDAHOMEDIR}/.am_passphrase
+    create_amkey
+    assertEquals "create_amkey" 1 $?
+    log_tail_no_stamp
+    assertSame \
+        "Error: ${AMANDAHOMEDIR}/.am_passphrase is missing, can't create amcrypt key." \
+        "${LOG_TAIL}"
+    # Need .am_passphrase. Ignore these test errors if get_random_lines or
+    # create_gnupg tests failed.
+    get_random_lines 1 > ${AMANDAHOMEDIR}/.am_passphrase
+    create_amkey
+    assertEquals "create_amkey" 0 $?
+    # Test with existing key
+    create_amkey
+    assertEquals "create_amkey" 0 $?
+    log_tail_no_stamp
+    assertSame \
+        "Info: Encryption key '${AMANDAHOMEDIR}/.gnupg/am_key.gpg' already exists." \
+        "${LOG_TAIL}"
+    # make sure unencrypted am_key is not hanging around
+    assertFalse "[ -f ${AMANDAHOMEDIR}/.gnupg/am_key ]"
+
 }
 
 test_check_gnupg() {
     logger "test_check_gnupg"
     check_gnupg
     assertEquals "check_gnupg" 0 $?
-    [ "$IAmRoot" ] && { startSkipping; echo "test_check_gnupg: skipped"; }
     assertSame \
-       "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.gnupg" \
+       "chown args: -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.gnupg" \
        "`cat $mock_chown_flags`"
     assertSame \
-       "chmod args: 700 ${AMANDAHOMEDIR}/.gnupg" \
+       "chmod args: -R u=rwX,go= ${AMANDAHOMEDIR}/.gnupg" \
        "`cat $mock_chmod_flags`"
 }
 
@@ -704,7 +802,7 @@ test_install_client_conf() {
     esac
 }
 
-#TODO: create_ampassphrase, create_amtmp
+#TODO: create_amtmp
 
 ######################################
 #TODO: post_rm_functions