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