Imported Upstream version 3.3.2
[debian/amanda] / packaging / common / test_sh_libs.sh
1 #!/bin/sh
2 # This script is uses ShUnit to test packaging/common/*.  Currently
3 # it only tests the functions as they perform for rpm and deb building.
4 # As support is added for other styles of package, these tests should be
5 # updated.
6
7 #######################
8 # If EXECUTING WITH ROOT ACCESS SCRIPT DELETES AND CREATES USERS.
9 # EXECUTE WITH CAUTION.
10 #######################
11
12 #set -x
13
14 # Note on conflicting variable names:
15 # SHUNIT_* and SHU_* are shunit2 variables
16 # test_* should only be used by test functions.
17 # All other variables will probably be used by pre/post/common functions.
18
19 # shunit2 assumes _inc is, but we might run from a separate builddir. So try to
20 # use srcdir, as defined by automake, or otherwise `pwd`.
21 SHUNIT_INC="${srcdir=`pwd`}/packaging/common"; export SHUNIT_INC
22
23 TMPDIR=`pwd`/shunit-test; export TMPDIR
24 amanda_user=test_amandabackup; export amanda_user
25 amanda_group=test_disk; export amanda_group
26 sup_group="test_tape"; export sup_group
27 AMANDAHOMEDIR=$TMPDIR/amanda; export AMANDAHOMEDIR
28 AMANDATES=$AMANDAHOMEDIR/amandates; export AMANDATES
29 os=`uname`; export os
30 wanted_shell='/bin/false'; export wanted_shell
31 dist=Fedora; export dist
32 SYSCONFDIR=$TMPDIR/etc; export SYSCONFDIR
33 SBINDIR=$TMPDIR/sbin; export SBINDIR
34 # Don't potentially conflict with a real value...
35 deb_uid=63000; export deb_uid
36
37
38 # Append stuff to this when you make temp files if they are outside TMPDIR
39 test_cleanup_files="$TMPDIR"; export test_cleanup_files
40
41 # This can't be part of one-time setup, because TMPDIR must exist before
42 # shunit2 is sourced.
43 mkdir -p ${TMPDIR} || exit 1
44 {
45     LOGFILE=`
46         (umask 077 && mktemp "$TMPDIR/test-log.XXXX") 2> /dev/null
47         ` &&
48         test -f "$LOGFILE"
49 } || {
50     LOGFILE=$TMPDIR/test-log.$$.$RANDOM
51     (umask 077 && touch "$LOGFILE")
52 } || {
53         echo "Unable to create log file!"
54         exit 1
55 }
56 export LOGFILE
57
58 oneTimeSetUp() {
59     msg_prefix="oneTimeSetUp: "
60     if [ "$IAmRoot" ]; then
61         # Delete an existing user.
62         echo "$msg_prefix attempting to delete an existing user."
63         userdel -f -r ${amanda_user}
64         # If the user exists, our tests will act wrong, so exit
65         grep "${amanda_user}" ${SYSCONFDIR}/passwd &> /dev/null || { \
66             echo "$msg_prefix ${amanda_user} still exists, or no ${SYSCONFDIR}/passwd.";
67             exit 1; }
68         groupdel ${amanda_group} || exit 1
69         groupdel ${sup_group} || exit 1
70     else
71         echo "Not root, cleanup skipped"
72     fi
73     # Make all directories which might be needed.
74     mkdir -p ${AMANDAHOMEDIR}/example
75     if [ ! "$IAmRoot" ]; then
76         mkdir -p ${SYSCONFDIR}/xinetd.d
77         mkdir -p ${SYSCONFDIR}/init.d
78     fi
79 }
80
81 oneTimeTearDown() {
82     if [ ${__shunit_assertsFailed} -eq 0 ]; then
83         rm -rf $test_cleanup_files
84     else
85         echo "Check ${test_cleanup_files} for logs and error info."
86     fi
87 }
88
89 test_cleanup_files="$LOGFILE ${test_cleanup_files}"
90
91 # shows syntax errors before the tests are run.
92 . ${SHUNIT_INC}/common_functions.sh
93 . ${SHUNIT_INC}/pre_inst_functions.sh
94 . ${SHUNIT_INC}/post_inst_functions.sh
95 . ${SHUNIT_INC}/post_rm_functions.sh
96
97 # Use this to skip root requiring tests.
98 # id -u is not portable to solaris.
99 tester_id=`id | sed 's/gid=\([0-9]*\).*/\1/'`
100 if [ "$tester_id" = "0" ]; then
101     # Same as SHUNIT_TRUE, but we can't use that yet...
102     IAmRoot=0
103 else
104     IAmRoot=
105 fi
106
107 # CAUTION: using real values if we are root.
108 if [ "$IAmRoot" ]; then
109     amanda_group=disk; export amanda_group
110     SYSCONFDIR=/etc; export SYSCONFDIR
111     SBINDDIR=/sbin; export SBINDDIR
112 fi
113
114 # Source our mock utils.
115 . ${SHUNIT_INC}/mock_utils.sh
116
117 ######################################
118 # Common functions
119
120 # shUnit reorders tests alphabetically.  We need the logger and cleanup tests
121 # to run first.
122 test___logger() {
123     # Write a line to the log, test that it got there.
124     TEST_MSG="test___logger message"
125     LOG_LINE="`date +'%b %e %Y %T'`: ${TEST_MSG}"
126     # It's important for the log messages to be quoted, or funny stuff happens.
127     logger "${TEST_MSG}"
128     assertEquals "logger() return code" 0 $?
129     LOG_TAIL=`tail -1 ${LOGFILE}`
130     assertEquals "logger() did not write <${LOG_LINE}> " \
131         "${LOG_LINE}" "${LOG_TAIL}"
132     # Leave this outside the unit test framework.  if the logger is
133     # broken we must exit.
134     if [ ! `grep -c "${LOG_LINE}" ${LOGFILE}` = "1" ]; then
135         echo "error: logger(): Incorrect content in ${LOGFILE}: " `cat ${LOGFILE}`
136         exit 1
137     fi
138 }
139
140 test__log_output_of() {
141     # Use log_output_of to append to the log
142     TEST_MSG="test__log_output_of message"
143     log_output_of echo "${TEST_MSG}"
144     assertEquals "log_output_of()" 0 $?
145     COUNT=`grep -c "${TEST_MSG}" ${LOGFILE}`
146     assertEquals "log_output_of(): incorrect content in log" \
147         1 ${COUNT}
148     # Leave this outside the unit test framework.  if the logger is
149     # broken we must exit.
150     if [ ! ${COUNT} = '1' ]; then
151         echo "error: log_output_of(): Incorrect content in ${LOGFILE}: " `cat ${LOGFILE}`
152         exit 1
153     fi
154 }
155
156
157 # NOTE: check_xinetd and check_inetd tests end up simply duplicating the
158 # code of the function itself
159
160 test_check_smf() {
161     logger "test_check_smf"
162     [ "$os" = "SunOS" ] || { startSkipping; echo "test_check_smf: skipped"; }
163     # Test existing service
164     touch ${MOCKDIR}/prexisting_service
165     check_smf
166     assertEquals "check_smf preexisting services" 0 $?
167     rm ${MOCKDIR}/prexisting_service
168     check_smf
169     assertEquals "check_smf no amanda service" 1 $?
170 }
171
172 test_check_superserver_running() {
173     logger "test_check_superserver_running"
174     [ "$IAmRoot" ] && { startSkipping; echo "test_check_superserver_running: skipped"; }
175     # Test the positive cases
176     # running changes the output of mocked ps
177     touch ${MOCKDIR}/running
178     check_superserver_running inetd
179     assertEquals "check_superserver_running inetd" 0 $?
180     assertSame "ps args: -e" "`cat $mock_ps_flags`"
181     if [ `uname` = 'Darwin' ]; then
182         [ "$IAmRoot" ] && startSkipping
183         check_superserver_running launchd
184         assertEquals "check_superserver_running launchd" 0 $?
185         assertSame "ps args: aux" "`cat $mock_ps_flags`"
186         endSkipping
187     else
188         endSkipping
189         check_superserver_running launchd
190         assertEquals 2 $?
191     fi
192     # Test the negative case, skip if root
193     [ "$IAmRoot" ] && startSkipping
194     rm ${MOCKDIR}/running
195     check_superserver_running inetd
196     assertEquals "check_superserver_running inetd returned 0 when inetd was not running" 1 $?
197     assertSame "ps args: -e" "`cat $mock_ps_flags`"
198     check_superserver_running xinetd
199     assertNotEquals "check_superserver_running xinetd incorrectly returned 0" \
200          0 $?
201     assertSame "ps args: -e" "`cat $mock_ps_flags`"
202 }
203
204 test_backup_xinetd() {
205     logger "test_backup_xinetd"
206     touch ${SYSCONFDIR}/xinetd.d/amandaserver
207     backup_xinetd "amandaserver"
208     assertEquals "backup_xinetd returns 0" 0 $?
209     assertTrue "[ -f ${AMANDAHOMEDIR}/example/*.amandaserver.orig ]"
210 }
211
212 test_backup_inetd() {
213     logger "test_backup_inetd"
214     case $os in
215         SunOS) inetd_dir=${SYSCONFDIR}/inet ;;
216         *) inetd_dir=${SYSCONFDIR} ;;
217     esac
218     [ -d "${inetd_dir}" ] || mkdir -p ${inetd_dir}
219     touch ${inetd_dir}/inetd.conf
220     echo "amanda foo/bar/baz  amandad" >> ${inetd_dir}/inetd.conf
221     backup_inetd
222     assertEquals "backup_inetd returns 0" 0 $?
223     assertTrue "[ -f ${AMANDAHOMEDIR}/example/inetd.orig ]"
224 }
225
226 test_backup_smf() {
227     logger "test_backup_smf"
228     :
229     # TODO: how to mock this?
230 }
231
232 test_install_xinetd() {
233     logger "test_install_xinetd"
234     if [ "$os" = "SunOS" ] ; then
235         # Solaris has install_xinetd_sol
236         startSkipping
237         echo "test_install_xinetd: skipped"
238         return
239     fi
240     # Test success:
241     touch ${MOCKDIR}/success
242     touch ${AMANDAHOMEDIR}/example/xinetd.amandaserver
243     install_xinetd "amandaserver"
244     assertEquals "install_xinetd returns 0" 0 $?
245     # Test "install" failure
246     rm ${MOCKDIR}/success
247     install_xinetd "amandaserver"
248     assertEquals "install_xinetd returns 1" 1 $?
249 }
250
251 test_install_inetd() {
252     logger "test_install_inetd"
253     case $os in
254         SunOS) inetd_dir=${BASEDIR}/${SYSCONFDIR}/inet ;;
255         *) inetd_dir=${SYSCONFDIR} ;;
256     esac
257     [  -f ${inetd_dir}/inetd.conf ] || touch ${inetd_dir}/inetd.conf
258     test_inetd_entry='amanda foo/bar/baz  amandad'
259     if [ ! -f ${AMANDAHOMEDIR}/example/inetd.conf ]; then
260         echo "${test_inetd_entry}" > ${AMANDAHOMEDIR}/example/inetd.conf.amandaserver
261     fi
262     install_inetd amandaserver
263     assertEquals "install_inetd returns 0" 0 $?
264     assertSame "${test_inetd_entry}" "`tail -1 ${inetd_dir}/inetd.conf`"
265 }
266
267 # TODO: test_install_smf() {
268 # Needs mocks for: inetconv, inetadm, svcadm.
269
270 test_reload_xinetd() {
271     logger "test_reload_xinetd"
272     # Might need init script.
273     if [ "$IAmRoot" ]; then
274         startSkipping
275         echo "test_install_smf: skipped"
276         return
277     elif [ ! -f "${SYSCONFDIR}/init.d/xinetd" ]; then
278         mv ${MOCKDIR}/xinetd ${SYSCONFDIR}/init.d
279     fi
280     # Test bad argument
281     reload_xinetd foo
282     assertEquals "reload_xinetd should reject argument 'foo'" 1 $?
283     # Test success
284     touch ${MOCKDIR}/success
285     reload_xinetd "reload"
286     assertEquals "reload_xinetd" 0 $?
287     # Test failure
288     rm ${MOCKDIR}/success
289     reload_xinetd "reload"
290     assertEquals "reload_xinetd" 1 $?
291     tail -4 ${LOGFILE}|grep "\<xinetd.*Attempting restart" >/dev/null
292     assertEquals "reload_xinetd should try to restart." 0 $?
293     reload_xinetd "restart"
294     assertEquals "restart should fail." 1 $?
295     tail -3 ${LOGFILE}|grep "Restarting xinetd" >/dev/null
296     assertEquals "Should log attempt to restart" 0 $?
297 }
298
299 test_reload_inetd() {
300     logger "test_reload_inetd"
301     # Test bad argument
302     # Might need init script.
303     if [ ! "$IAmRoot" ]; then
304         if [ ! -f "${SYSCONFDIR}/init.d/inetd" ]; then
305             mv ${MOCKDIR}/inetd ${SYSCONFDIR}/init.d
306         fi
307     fi
308     # Test bad argument
309     reload_inetd foo
310     assertEquals "reload_inetd should reject argument 'foo' (return 1):" 1 $?
311     # Test success
312     touch ${MOCKDIR}/success
313     reload_inetd
314     assertEquals "reload_inetd" 0 $?
315     # Test failure
316     rm ${MOCKDIR}/success
317     reload_inetd "reload"
318     assertEquals "reload_inetd" 1 $?
319     tail -4 ${LOGFILE}|grep "\<inetd.*Attempting restart" >/dev/null
320     assertEquals "reload_inetd should try to restart." 0 $?
321     reload_inetd "restart"
322     assertEquals "restart should fail." 1 $?
323     tail -3 ${LOGFILE}|grep "Restarting inetd" >/dev/null
324     assertEquals "Should log attempt to restart" 0 $?
325 }
326
327
328 ######################################
329 # pre_install_functions
330
331 test_check_user_group_missing() {
332     logger "test_check_user_group_missing no param"
333     check_user_group
334     assertNotEquals "'check_user_group' should fail" 0 $?
335     logger "test_check_user_group_missing missing group"
336     [ ! "$IAmRoot" ] && rm -f ${SYSCONFDIR}/group
337     touch ${SYSCONFDIR}/group
338     for os in linux osx solaris; do
339         echo $os > ${MOCKDIR}/id_os
340         check_user_group "abracadabra"
341         assertNotEquals "'check_user group abracadabra' should not be found:" 0 $?
342         LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
343         assertEquals "check_user_group should write" \
344             "User's primary group 'abracadabra' does not exist" \
345             "${LOG_TAIL}"
346     done
347 }
348
349 good_group_entry="${amanda_group}:x:100:"
350 export good_group_entry
351 test_check_user_group_exists() {
352     logger "test_check_user_group user and group exist"
353     touch ${MOCKDIR}/id_exists
354     touch ${SYSCONFDIR}/group
355     # Non-root adds and entry to the mock group file
356     [ ! "$IAmRoot" ] && echo $good_group_entry > ${SYSCONFDIR}/group
357     for os in linux osx solaris; do
358         echo $os > ${MOCKDIR}/id_os
359
360         # Case 1: Amanda_user is correct.
361         echo ${amanda_group} > ${MOCKDIR}/id_group
362         check_user_group "${amanda_group}"
363         assertEquals "'check_user_group ${amanda_group}': id returns correct groupname" \
364             0 $?
365
366         # Case 2: Amanda_user is not a member of the the correct primary group.
367         rm ${MOCKDIR}/id_group
368         check_user_group "${amanda_group}"
369         assertEquals "'check_user_group ${amanda_group}' when not a member" 1 $?
370     done
371 }
372
373 test_check_user_supplemental_group_missing() {
374     logger "test_check_user_supplemental_group missing"
375     [ ! "$IAmRoot" ] && echo $good_group_entry > ${SYSCONFDIR}/group
376     for os in linux osx solaris; do
377         echo $os > ${MOCKDIR}/id_os
378         check_user_supplemental_group ${sup_group}
379         assertEquals "'check_user supplemental-group ${sup_group}' when group missing" \
380         1 $?
381     done
382 }
383
384 missing_group_member="${sup_group}:x:105:nobody"
385 export missing_group_member
386 good_sup_group_entry="${missing_group_member},${amanda_user}"
387 export good_sup_group_entry
388
389 test_check_user_supplemental_group_exists() {
390     logger "test_check_user_supplemental_group exists"
391     [ ! "$IAmRoot" ] && echo $missing_group_member > ${SYSCONFDIR}/group
392     check_user_supplemental_group ${sup_group}
393     assertEquals "'check_user_supplemental_group ${sup_group}' when amanda_user is not a member" \
394         1 $?
395
396     [ ! "$IAmRoot" ] && echo ${good_sup_group_entry} > ${SYSCONFDIR}/group
397     check_user_supplemental_group ${sup_group}
398     assertEquals "'check_user_supplemental_group ${sup_group}' with correct membership" \
399         0 $?
400 }
401 test_check_user_shell() {
402     logger "test_check_user_shell"
403     if [ ! "$IAmRoot" ]; then
404         echo "${good_passwd_entry}" > ${SYSCONFDIR}/passwd
405     fi
406     # Case 1: Provide a matching shell
407     check_user_shell "/bin/bash"
408     assertEquals "check_user_shell /bin/bash (matching)" 0 $?
409     # Case 2: Provide a non-matching shell. 
410     check_user_shell "/bin/ksh"
411     assertEquals "check_user_shell /bin/ksh (not matching)" 1 $?
412 }
413
414 test_check_user_homedir() {
415     logger 'test_check_user_homedir'
416     if [ ! "$IAmRoot" ]; then
417         echo "${good_passwd_entry}" > ${SYSCONFDIR}/passwd
418     fi
419     # Case 1: Assume amanda_user is correct.
420     check_user_homedir "${AMANDAHOMEDIR}"
421     assertEquals "check_user_homedir ${AMANDAHOMEDIR}" 0 $?
422     # Case 2: Provide an incorrect homedir
423     check_user_homedir "/tmp"
424     assertEquals "check_user_homedir /tmp" 1 $?
425 }
426
427 test_check_user_uid() {
428     echo "${amanda_group}" > ${MOCKDIR}/id_group
429     touch ${MOCKDIR}/id_exists
430     logger 'test_check_user_uid'
431     for os in linux osx solaris; do
432         echo $os > ${MOCKDIR}/id_os
433         check_user_uid
434         assertEquals "check_user_uid without a uid" 1 $?
435         logger 'test_check_user_uid wrong id'
436         check_user_uid 123
437         assertEquals "check_user_uid uids don't match" 1 $?
438         logger 'test_check_user_uid correct id'
439         check_user_uid ${deb_uid}
440     done
441
442 }
443 test_check_homedir_dir_missing() {
444     logger "test_check_homedir_dir_missing"
445     # First make sure the dir is missing
446     rm -rf ${AMANDAHOMEDIR}
447     check_homedir
448     assertNotEquals "check_homedir returned 0, but homedir did not exist" 0 $?
449 }
450
451 # passwd file entry for Linux systems, maybe others.  UID is correct
452 # for Debian as well.
453 good_passwd_entry="${amanda_user}:x:${mock_deb_uid}:6::${AMANDAHOMEDIR}:/bin/bash"
454 export good_passwd_entry
455 test_create_user() {
456     logger "test_create_user"
457     if [ ! "$IAmRoot" ]; then
458         startSkipping
459         echo "test_create_user: Creating mock passwd file."
460         echo "$good_passwd_entry" > ${SYSCONFDIR}/passwd
461         echo "test_create_user: tests skipped."
462         #TODO: mock useradd.
463         return
464     fi
465     # Case 1: create_user should succeed.
466     create_user
467     assertEquals "create_user()" 0 $?
468 }
469
470 test_add_group_check_parameters_logs() {
471     rm -f ${MOCKDIR}/groupadd_rc ${MOCKDIR}/num_groups
472     # Return codes are integers.
473     printf '%i' 0 > ${MOCKDIR}/groupadd_rc
474     # Test that first parameter is required.
475     add_group
476     assertEquals "add_group without a group should fail." 1 $?
477     LOG_TAIL=`tail -1 ${LOGFILE}|cut -d " " -f 5-`
478     assertEquals "add_group should write" \
479         "Error: first argument was not a group to add." \
480         "${LOG_TAIL}"
481 }
482
483 test_add_group_group_ok() {
484     # groupadd created group
485     printf '%i' 0 > ${MOCKDIR}/groupadd_rc
486     echo '${amanda_user} : prev_grp1' > ${MOCKDIR}/groups_output
487     add_group twinkle
488     assertEquals "add_group group ok" 0 $?
489     flags=`cat ${mock_usermod_flags}`
490     assertEquals "usermod_flags" \
491         "usermod args: -G prev_grp1,twinkle ${amanda_user}" \
492         "${flags}"
493
494     # Make sure supplemental groups are preserved when adding groups to an
495     # existing account
496     echo '${amanda_user} : prev_grp1 prev_grp2' > ${MOCKDIR}/groups_output
497     printf '%i' 1 > ${MOCKDIR}/num_groups
498     add_group twinkle
499     assertEquals "add_group group ok" 0 $?
500     flags=`cat ${mock_usermod_flags}`
501     assertEquals "usermod_flags should contain:" \
502         "usermod args: -G prev_grp1,prev_grp2,twinkle ${amanda_user}" \
503         "${flags}"
504 }
505
506 test_create_homedir() {
507     logger "test_create_homedir"
508     rm -rf ${AMANDAHOMEDIR}
509     create_homedir
510     assertEquals "create_homedir returns 0" 0 $?
511     assertTrue "${AMANDAHOMEDIR} did not get created" "[ -d ${AMANDAHOMEDIR} ]"
512     if [ "$IAmRoot" ]; then
513         # Check real owner
514         real_owner=`ls -ld $AMANDAHOMEDIR | awk '{ print $3":"$4; }'`
515         assertSame "${amanda_user}:${amanda_group}" "$real_owner"
516     else
517         assertSame \
518             "chown args: -R ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}" \
519             "`cat $mock_chown_flags`"
520     fi
521     # A second run should succeed too.
522     create_homedir    
523     assertEquals "When homedir exists, create_homedir returns 0:" 0 $?
524     # Later tests use ${AMANDAHOMEDIR}, so leave the dir there.
525 }
526
527 test_check_homedir_dir_existing() {
528     logger "test_check_homedir_dir_existing"
529     # Create the dir (example needed for other tests), then check again.
530     mkdir -p ${AMANDAHOMEDIR}/example
531     check_homedir
532     assertEquals "Homedir exists; check_homedir" 0 $?
533 }
534
535 test_create_logdir() {
536     logger "test_create_logdir"
537     # The logdir variable for the shell libs, not shunit2.
538     LOGDIR=$TMPDIR/amanda_log; export LOGDIR
539     rm -rf ${LOGDIR}
540     rm -rf ${LOGDIR}.save
541     create_logdir
542     assertEquals "create_logdir clean system" 0 $?
543     if [ -n "$IAmRoot" ]; then
544         real_owner=`ls -ld $LOGDIR | awk '{ print $3":"$4; }'`
545         assertSame "${amanda_user}:${amanda_group}" "$real_owner"
546     else
547         assertSame \
548             "chown args: -R ${amanda_user}:${amanda_group} ${LOGDIR}"\
549             "`cat $mock_chown_flags`"
550     fi
551     assertTrue "${LOGDIR} exists" "[ -d ${LOGDIR} ]"
552     # What happens if logdir is a file?
553     rm -rf ${LOGDIR}
554     touch ${LOGDIR}
555     create_logdir
556     assertEquals "create_logdir" 0 $?
557     assertTrue "${LOGDIR} exists" "[ -d ${LOGDIR} ]"
558     assertTrue "${LOGDIR}/amanda_log.save backup exists" \
559         "[ -f ${LOGDIR}/amanda_log.save ]"
560 }
561
562 test_create_amandates() {
563     logger "test_create_amandates"
564     rm -f ${AMANDATES}
565     create_amandates
566     assertEquals "create_amandates" 0 $?
567     assertTrue "[ -f ${AMANDATES} ]"
568 }
569
570 test_check_amandates() {
571     logger "test_check_amandates"
572     touch $mock_chown_flags
573     touch $mock_chmod_flags
574     check_amandates
575     assertEquals "check_amandates" 0 $?
576     [ "$IAmRoot" ] && { startSkipping; echo "test_check_amandates: skipped"; }
577     assertSame \
578         "chown args: ${amanda_user}:${amanda_group} ${AMANDATES}" \
579         "`cat $mock_chown_flags`"
580     assertSame \
581         "chmod args: 0640 ${AMANDATES}" \
582         "`cat $mock_chmod_flags`"
583 }
584
585 test_create_gnupg() {
586     logger "test_create_gnupg"
587     create_gnupg
588     assertEquals "create_gnupg" 0 $?
589     assertTrue "[ -d ${AMANDAHOMEDIR}/.gnupg ]"
590 }
591
592 test_check_gnupg() {
593     logger "test_check_gnupg"
594     check_gnupg
595     assertEquals "check_gnupg" 0 $?
596     [ "$IAmRoot" ] && { startSkipping; echo "test_check_gnupg: skipped"; }
597     assertSame \
598         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.gnupg" \
599         "`cat $mock_chown_flags`"
600     assertSame \
601         "chmod args: 700 ${AMANDAHOMEDIR}/.gnupg" \
602         "`cat $mock_chmod_flags`"
603 }
604
605 test_create_amandahosts() {
606     logger "test_create_amandahosts"
607     create_amandahosts
608     assertEquals "create_amandahosts:" 0 $?
609     assertEquals "${AMANDAHOMEDIR}/.amandahosts exists:" 0 $?
610     assertEquals "create_amandahosts" 0 $?
611     assertTrue "[ -f ${AMANDAHOMEDIR}/.amandahosts ]"
612 }
613
614 test_check_amandahosts_entry() {
615     logger "test_check_amandahosts_entry"
616     if [ -f ${AMANDAHOMEDIR}/.amandahosts ]; then
617         check_amandahosts_entry root amindexd amidxtaped
618         assertEquals "check_amandahosts_entry root amindexd amidxtaped" \
619             0 $?
620     else
621         echo "test_check_amandahosts_entry: ${AMANDAHOMEDIR}/.amandahosts missing.  test skipped"
622         startSkipping
623     fi
624 }
625
626 test_check_amandahosts_perm() {
627     logger "test_check_amandahosts_perm"
628     check_amandahosts_perms
629     assertEquals "check_amandahosts_perms" 0 $?
630     [ "$IAmRoot" ] && { startSkipping; echo "test_check_amandahosts_perm: skipped"; }
631     assertSame \
632         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.amandahosts" \
633         "`cat $mock_chown_flags`"
634     assertSame \
635         "chmod args: 0600 ${AMANDAHOMEDIR}/.amandahosts" \
636         "`cat $mock_chmod_flags`"
637 }
638
639 test_create_ssh_key() {
640     logger "test_create_ssh_key"
641     keydir=${AMANDAHOMEDIR}/.ssh
642     rm -rf ${keydir}
643     create_ssh_key server
644     assertEquals "create_ssh_key" 0 $?
645     assertTrue "[ -f ${keydir}/id_rsa_amdump ]"
646     [ "$IAmRoot" ] && { startSkipping; echo "test_create_ssh_key: skipped"; }
647     assertSame \
648         "chown args: ${amanda_user}:${amanda_group} ${keydir} ${keydir}/id_rsa_amdump ${keydir}/id_rsa_amdump.pub" \
649         "`cat $mock_chown_flags`"
650     # Chmod is called twice, but we only get the 2nd invocation.
651     assertSame \
652         "chmod args: 0600 ${keydir}/id_rsa_amdump ${keydir}/id_rsa_amdump.pub" \
653         "`cat $mock_chmod_flags`"
654     endSkipping
655
656     rm -rf ${keydir}
657     # What happens if .ssh is a file?
658     touch ${AMANDAHOMEDIR}/.ssh
659     create_ssh_key client
660     assertEquals "create_ssh_key" 0 $?
661     assertTrue "[ -f ${keydir}.save ]"
662     assertTrue "[ -f ${keydir}/id_rsa_amrecover ]"
663 }
664
665 test_create_profile() {
666     logger "test_create_profile"
667     rm -f ${AMANDAHOMEDIR}/.profile
668     create_profile
669     assertEquals "create_profile" 0 $?
670     assertTrue "[ -f ${AMANDAHOMEDIR}/.profile ]"
671 }
672
673 test_check_profile() {
674     logger "test_check_profile"
675     [ -f "${AMANDAHOMEDIR}/.profile" ] || touch ${AMANDAHOMEDIR}/.profile
676     check_profile
677     assertEquals "check_profile" 0 $?
678     assertTrue "[ -s ${AMANDAHOMEDIR}/.profile ]"
679     [ "$IAmRoot" ] && { startSkipping; echo "test_check_profile: skipped"; }
680     assertSame \
681         "chown args: ${amanda_user}:${amanda_group} ${AMANDAHOMEDIR}/.profile" \
682         "`cat $mock_chown_flags`"
683     assertSame \
684         "chmod args: 0640 ${AMANDAHOMEDIR}/.profile" \
685         "`cat $mock_chmod_flags`"
686 }
687
688 test_install_client_conf() {
689     logger "test_install_client_conf"
690     # Test success
691     touch ${MOCKDIR}/success
692     install_client_conf
693     assertEquals "install_client_conf" 0 $?
694     prefix="install args:"
695     inst_files="${AMANDAHOMEDIR}/example/amanda-client.conf ${SYSCONFDIR}/amanda/"
696     case $os in
697       SunOS) assertSame \
698         "`cat $mock_install_flags`" \
699         "${prefix} -m 0600 -u ${amanda_user} -g ${amanda_group} ${inst_files}"
700       ;;
701       *) assertSame \
702         "`cat $mock_install_flags`" \
703         "${prefix} -m 0600 -o ${amanda_user} -g ${amanda_group} ${inst_files}"
704     esac
705 }
706
707 #TODO: create_ampassphrase, create_amtmp
708
709 ######################################
710 #TODO: post_rm_functions
711
712 # Run a single test, or let shunit run all tests
713 if [ $# -gt 0 ]; then
714     echo $1
715     SPECIFIC_TESTS="$*"
716     suite() {
717         suite_addTest test___logger
718         suite_addTest test__log_output_of
719         for t in $SPECIFIC_TESTS; do
720             suite_addTest $t
721         done
722     }
723 fi
724
725 # Importing shunit2 triggers test enumeration, so must happen after
726 # all tests are defined.
727 . ${SHUNIT_INC}/shunit2
728
729 echo "shunit2 log is: ${LOGFILE}"
730 echo "mockdir is: ${MOCKDIR}"