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