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