Imported Upstream version 3.2.0
[debian/amanda] / installcheck / Amanda_Process.pl
1 # Copyright (c) 2008, 2009, 2010 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. Mathilda Ave., Suite 300
17 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
18
19 use Test::More tests => 11;
20 use File::Path;
21 use strict;
22 use warnings;
23
24 use lib "@amperldir@";
25 use Installcheck;
26 use Installcheck::Config;
27 use Amanda::Paths;
28 use Amanda::Debug;
29 use Amanda::Config qw( :init :getconf config_dir_relative );
30 use Amanda::Process;
31
32 # set up debugging so debug output doesn't interfere with test results
33 Amanda::Debug::dbopen("installcheck");
34 Installcheck::log_test_output();
35
36 # and disable Debug's die() and warn() overrides
37 Amanda::Debug::disable_die_override();
38
39 my $Amanda_process = Amanda::Process->new(0);
40
41 $Amanda_process->load_ps_table();
42
43 like($Amanda_process->{pstable}->{$$}, qr/(Amanda_Process|perl)/,
44    "find program name for perl script");
45 is($Amanda_process->{ppid}->{$$}, getppid,
46    "load_ps_table get correct ppid for Amanda_Process");
47
48 #override works done by load_ps_table, override pstable
49 $Amanda_process->{pstable} = {
50         1     => "init",
51         1001  => "bash",
52         30072 => "amdump",
53         30093 => "driver",
54         30099 => "taper",
55         30100 => "dumper",
56         30101 => "dumper",
57         30102 => "foobar",
58         30103 => "dumper",
59         30104 => "dumper",
60         30527 => "chunker",
61         30538 => "gzip",
62         30539 => "gzip",
63 };
64
65 #override works done by load_ps_table, override ppid
66 $Amanda_process->{ppid} = {
67         1     => 1,
68         1001  => 1,
69         30072 => 1001,
70         30093 => 30072,
71         30099 => 30093,
72         30100 => 30093,
73         30101 => 30093,
74         30102 => 1,
75         30103 => 30093,
76         30104 => 30093,
77         30527 => 30093,
78         30538 => 30100,
79         30539 => 30100,
80 };
81
82 #create a log file
83 my $log_filename = "$Installcheck::TMP/Amanda_Logfile_test.log";
84 open my $logfile, ">", $log_filename or die("Could not create temporary log file '$log_filename': $!");
85 print $logfile <<LOGFILE;
86 INFO amdump amdump pid 30072
87 INFO driver driver pid 30093
88 INFO planner planner pid 30092
89 INFO dumper dumper pid 30100
90 INFO taper taper pid 30099
91 INFO dumper dumper pid 30103
92 INFO dumper dumper pid 30104
93 INFO dumper dumper pid 30101
94 INFO planner pid-done 30092
95 INFO chunker chunker pid 30475
96 INFO dumper gzip pid 30500
97 INFO dumper gzip pid 30501
98 INFO dumper pid-done 30500
99 INFO chunker pid-done 30475
100 INFO dumper pid-done 30501
101 INFO chunker chunker pid 30527
102 LOGFILE
103 close $logfile;
104
105 #parse the log file
106 $Amanda_process->scan_log($log_filename);
107 is_deeply($Amanda_process->{pids},
108         {30072 => "amdump",
109          30093 => "driver",
110          30099 => "taper",
111          30100 => "dumper",
112          30101 => "dumper",
113          30103 => "dumper",
114          30104 => "dumper",
115          30527 => "chunker"},
116         "scan_log works");
117 is($Amanda_process->{master_pname}, "amdump",
118    "master_name is set to 'amdump'");
119 is($Amanda_process->{master_pid}, "30072",
120    "master_pid is set to '30072'");
121
122 $Amanda_process->add_child();
123 is_deeply($Amanda_process->{amprocess},
124         {30072 => 30072,
125          30093 => 30093,
126          30099 => 30099,
127          30100 => 30100,
128          30101 => 30101,
129          30103 => 30103,
130          30104 => 30104,
131          30527 => 30527,
132          30538 => 30538,
133          30539 => 30539},
134         "add_child add the 2 gzip process");
135
136 is($Amanda_process->process_alive(30100, "dumper"), 1,
137    "process_alive return if pname match");
138 is($Amanda_process->process_alive(30100, "driver"), '',
139    "process_alive return '' if pname doesn't match");
140 is($Amanda_process->process_alive(30100), 1,
141    "process_alive return 1 without pname for amanda process");
142 is($Amanda_process->process_alive(30102), 1,
143    "process_alive return 1 without pname for any process");
144 is($Amanda_process->process_alive(30105), '',
145    "process_alive return '' if the process is dead");