8737290ce6a3171ac5693002b1a1cbc49ab1d681
[debian/amanda] / installcheck / Amanda_Debug.pl
1 # Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License version 2 as published
5 # by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful, but
8 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
10 # for more details.
11 #
12 # You should have received a copy of the GNU General Public License along
13 # with this program; if not, write to the Free Software Foundation, Inc.,
14 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 #
16 # Contact information: Zmanda Inc, 465 S Mathlida Ave, Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 9;
20 use File::Path;
21 use strict;
22
23 use lib "@amperldir@";
24 use Amanda::Debug;
25
26 ## most failures of the debug module will just kill the process, so
27 ## the main goal of this test script is just to make it to the end :)
28
29 my $fh;
30 my $debug_text;
31 my $pid;
32 my $kid;
33
34 # set up debugging so debug output doesn't interfere with test results
35 Amanda::Debug::dbopen("installcheck");
36 Amanda::Debug::dbrename("TESTCONF", "installcheck");
37
38 # and disable Debug's die() and warn() overrides
39 Amanda::Debug::disable_die_override();
40
41 my $debug_fd = Amanda::Debug::dbfd();
42 ok($debug_fd, "dbfd() returns something nonzero");
43
44 my $debug_file = Amanda::Debug::dbfn();
45 ok(-f $debug_file, "dbfn() returns a filename that exists");
46
47 Amanda::Debug::debug('debug message');
48 Amanda::Debug::info('info message');
49 Amanda::Debug::message('message message');
50 Amanda::Debug::warning('warning message');
51
52 Amanda::Debug::dbclose();
53
54 open ($fh, "<", $debug_file);
55 $debug_text = do { local $/; <$fh> };
56 close($fh);
57
58 like($debug_text, qr/debug message/, "debug message is in debug log file");
59 like($debug_text, qr/info message/, "info message is in debug log file");
60 like($debug_text, qr/message message/, "message message is in debug log file");
61 like($debug_text, qr/warning message/, "warning message is in debug log file");
62
63 Amanda::Debug::dbreopen($debug_file, "oops, one more thing");
64 Amanda::Debug::dbclose();
65
66 open ($fh, "<", $debug_file);
67 $debug_text = do { local $/; <$fh> };
68 close($fh);
69
70 like($debug_text, qr/warning message/, "dbreopen doesn't erase existing contents");
71 like($debug_text, qr/oops, one more thing/, "dbreopen adds 'notation' to the debug log");
72
73 Amanda::Debug::dbreopen($debug_file, "I've still got more stuff to test");
74
75 # fork a child to call error()
76 $pid = open($kid, "-|");
77 die "Can't fork: $!" unless defined($pid);
78 if (!$pid) {
79     $Amanda::Debug::erroutput_type = 0; # don't spew to stderr, too, please
80     Amanda::Debug::critical("morituri te salutamus");
81     exit 1; # just in case
82 }
83 close $kid;
84 waitpid $pid, 0;
85
86 # just hope this works -- Perl makes it very difficult to write to fd 2!
87 Amanda::Debug::debug_dup_stderr_to_debug();
88 Amanda::Debug::dbclose();
89
90 open ($fh, "<", $debug_file);
91 $debug_text = do { local $/; <$fh> };
92 close($fh);
93
94 like($debug_text, qr/morituri te salutamus/, "critical() writes its message to the debug log");