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