add a wrapper for logrotate to fool the system logrotate's checkproc
[omd.git] / t / 20-package_thruk.t
blobb46ed5ee6fb7a222dc14f446e3825cda5a028f1c
1 #!/usr/bin/env perl
3 use warnings;
4 use strict;
5 use Test::More;
6 use Data::Dumper;
8 BEGIN {
9 use lib('t');
10 require TestUtils;
11 import TestUtils;
12 use FindBin;
13 use lib "$FindBin::Bin/lib/lib/perl5";
16 plan( tests => 2805 );
18 ##################################################
19 # create our test site
20 my $omd_bin = TestUtils::get_omd_bin();
21 my $site = TestUtils::create_test_site() or TestUtils::bail_out_clean("no further testing without site");
22 my $auth = 'OMD Monitoring Site '.$site.':omdadmin:omd';
23 my $host = "omd-".$site;
24 my $service = "Dummy+Service";
25 my $servicep= "Dummy Service";
27 # create test host/service
28 TestUtils::prepare_obj_config('t/data/omd/testconf1', '/omd/sites/'.$site.'/etc/nagios/conf.d', $site);
30 # decrease pnp interval
31 TestUtils::test_command({ cmd => "/usr/bin/env sed -i -e 's/^perfdata_file_processing_interval = 15/perfdata_file_processing_interval = 2/g' -e 's/^sleep_time = 15/sleep_time = 2/g' /opt/omd/sites/$site/etc/pnp4nagios/npcd.cfg" });
33 # set thruk as default
34 TestUtils::test_command({ cmd => $omd_bin." config $site set DEFAULT_GUI thruk" });
35 TestUtils::test_command({ cmd => $omd_bin." start $site" });
37 # set token for post requests
38 push @INC, '/omd/sites/'.$site.'/share/thruk/lib';
39 use_ok('Thruk::Utils::Cache');
40 use_ok('Thruk::Config');
41 set_test_user_token();
44 my $reports = [
46 'name' => 'Host',
47 'template' => 'sla_host.tt',
48 'params.sla' => 95,
49 'params.timeperiod' => 'last12months',
50 'params.host' => $host,
51 'params.breakdown' => 'months',
52 'params.unavailable' => [ 'down', 'unreachable' ],
53 'params.graph_min_sla' => 90,
54 'params.decimals' => 2,
55 'send_type_1' => 'month',
56 'send_day_1' => 1,
57 'week_day_1' => '',
58 'send_hour_1' => 0,
59 'send_minute_1' => 0,
63 ##################################################
64 # define some checks
65 my $tests = [
66 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 60 -H localhost -a omdadmin:omd -u /$site/thruk/side.html -e 200'", like => '/HTTP OK:/' },
67 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 20 -H localhost -u /$site/thruk -e 401'", like => '/HTTP OK:/' },
68 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 60 -H localhost -a omdadmin:omd -u /$site/thruk -e 301'", like => '/HTTP OK:/' },
69 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 60 -H localhost -a omdadmin:omd -u /$site/thruk/ -e 200'", like => '/HTTP OK:/' },
70 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 20 -H localhost -a omdadmin:omd -u \"/$site/thruk/cgi-bin/status.cgi?hostgroup=all&style=hostdetail\" -e 200 -r \"Host Status Details For All Host Groups\"'", like => '/HTTP OK:/' },
71 { cmd => "/bin/su - $site -c 'lib/nagios/plugins/check_http -t 20 -H localhost -a omdadmin:omd -u \"/$site/thruk/cgi-bin/tac.cgi\" -e 200 -r \"Logged in as <i>omdadmin<\/i>\"'", like => '/HTTP OK:/' },
72 { cmd => "/bin/su - $site -c './bin/thruk -l'", like => "/$site/" },
73 { cmd => "/bin/su - $site -c './bin/thruk -l --local'", like => "/$site/" },
74 { cmd => "/bin/su - $site -c './bin/naglint ./etc/nagios/conf.d/commands.cfg'", like => "/check_local_load/" },
77 my $own_tests = [
78 { cmd => "/bin/su - $site -c './etc/init.d/thruk restart'", like => '/\([\d\ ]+\)\ OK/' },
80 my $shared_tests = [
81 { cmd => "/bin/su - $site -c './etc/init.d/thruk restart'", like => '/only available for apaches/', exit => 1 },
84 for my $report (@{$reports}) {
85 my $args = [];
86 for my $key (keys %{$report}) {
87 for my $val (ref $report->{$key} eq 'ARRAY' ? @{$report->{$key}} : $report->{$key}) {
88 push @{$args}, $key.'='.$val;
91 push @{$tests}, (
92 { cmd => "/bin/su - $site -c './bin/thruk -A omdadmin \"/thruk/cgi-bin/reports2.cgi?action=save&report=9999&".join('&', @{$args})."\"'",
93 like => "/OK - report updated/" },
94 { cmd => "/bin/su - $site -c 'omd reload crontab'", like => [ '/OK/' ] },
95 { cmd => "/bin/su - $site -c '/usr/bin/crontab -l | grep -i thruk | grep -v cron.d'", like => [ '/9999/' ] },
96 { cmd => "/bin/su - $site -c './bin/thruk -a report=9999 --local'", like => [ '/%PDF\-1\.4/', '/%%EOF/' ] },
97 { cmd => "/bin/su - $site -c './bin/thruk -A omdadmin \"/thruk/cgi-bin/reports2.cgi?action=remove&report=9999\"'", like => '/OK - report removed/' },
102 my $urls = [
103 # static html pages
104 { url => "/thruk/side.html", like => '/<title>Thruk<\/title>/' },
105 { url => "", like => '/<title>Thruk<\/title>/' },
106 { url => "/thruk/index.html", like => '/<title>Thruk<\/title>/' },
107 { url => "/thruk/docs/index.html", like => '/<title>Documentation<\/title>/' },
108 { url => "/thruk/main.html", like => '/<title>Thruk Monitoring Webinterface<\/title>/' },
110 # availability
111 { url => '/thruk/cgi-bin/avail.cgi', 'like' => '/Availability Report/' },
112 { url => '/thruk/cgi-bin/avail.cgi?host='.$host.'&timeperiod=last7days&smon=1&sday=13&syear=2010&shour=0&smin=0&ssec=0&emon=1&eday=14&eyear=2010&ehour=24&emin=0&esec=0&rpttimeperiod=&assumeinitialstates=yes&assumestateretention=yes&assumestatesduringnotrunning=yes&includesoftstates=no&initialassumedservicestate=0&backtrack=4', 'like' => '/Availability Report/' },
114 # extinfo
115 { url => '/thruk/cgi-bin/extinfo.cgi?type=1&host='.$host },
116 { url => '/thruk/cgi-bin/extinfo.cgi?type=2&host='.$host.'&service='.$service },
118 # config
119 { url => '/thruk/cgi-bin/config.cgi', 'like' => '/Configuration/' },
120 { url => '/thruk/cgi-bin/config.cgi?type=hosts', 'like' => '/Configuration/' },
121 { url => '/thruk/cgi-bin/config.cgi?type=services', 'like' => '/Configuration/' },
123 # history
124 { url => '/thruk/cgi-bin/history.cgi', like => '/Alert History/' },
125 { url => '/thruk/cgi-bin/history.cgi?host=all', like => '/Alert History/' },
126 { url => '/thruk/cgi-bin/history.cgi?host='.$host, like => '/Alert History/' },
127 { url => '/thruk/cgi-bin/history.cgi?host='.$host.'&service='.$service, like => '/Alert History/' },
129 # notifications
130 { url => '/thruk/cgi-bin/notifications.cgi', like => '/All Hosts and Services/' },
132 # outages
133 { url => '/thruk/cgi-bin/outages.cgi', like => '/Network Outages/' },
135 # showlog
136 { url => '/thruk/cgi-bin/showlog.cgi', like => '/Event Log/' },
138 # status
139 { url => '/thruk/cgi-bin/status.cgi', like => '/Current Network Status/' },
140 { url => '/thruk/cgi-bin/status.cgi?host='.$host, like => '/Current Network Status/' },
142 # summary
143 { url => '/thruk/cgi-bin/summary.cgi', like => '/Alert Summary Report/' },
145 # tac
146 { url => '/thruk/cgi-bin/tac.cgi', like => '/Tactical Monitoring Overview/' },
148 # trends
149 { url => '/thruk/cgi-bin/trends.cgi?host='.$host.'&t1=1264820912&t2=1265425712&includesoftstates=no&assumestateretention=yes&assumeinitialstates=yes&assumestatesduringnotrunning=yes&initialassumedhoststate=0&backtrack=4', 'like' => '/Host and Service State Trends/' },
150 { url => '/thruk/cgi-bin/trends.cgi?host='.$host.'&service='.$service.'&t1=1264820912&t2=1265425712&includesoftstates=no&assumestateretention=yes&assumeinitialstates=yes&assumestatesduringnotrunning=yes&initialassumedservicestate=0&backtrack=4', 'like' => '/Host and Service State Trends/' },
152 # statusmap
153 { url => '/thruk/cgi-bin/statusmap.cgi?host=all', like => '/Network Map/' },
155 # minemap
156 { url => '/thruk/cgi-bin/minemap.cgi', like => '/Mine Map/' },
158 # conf tool
159 { url => '/thruk/cgi-bin/conf.cgi', like => '/Config Tool/' },
160 { url => '/thruk/cgi-bin/conf.cgi?sub=thruk', like => [ '/Config Tool/', '/title_prefix/', '/use_wait_feature/'] },
161 { url => '/thruk/cgi-bin/conf.cgi?sub=cgi', like => [ '/Config Tool/', '/show_context_help/', '/use_pending_states/' ] },
162 { url => '/thruk/cgi-bin/conf.cgi?sub=users', like => [ '/Config Tool/', '/select user to change/' ] },
163 { url => '/thruk/cgi-bin/conf.cgi?sub=users&action=change&data.username=omdadmin', like => [ '/Config Tool/', '/remove password/', '/authorized_for_all_services/' ] },
164 { url => '/thruk/cgi-bin/conf.cgi?sub=objects', like => [ '/Config Tool/', '/select host to change/' ] },
165 { url => '/thruk/cgi-bin/conf.cgi?sub=objects&apply=yes', like => [ '/Config Tool/', '/There are no pending changes to commit/' ] },
166 { url => '/thruk/cgi-bin/conf.cgi?sub=objects&type=host&data.name=generic-host', like => [ '/Config Tool/', '/Template:\s+generic\-host/', '/templates.cfg/' ], skip_html_links => 1 },
167 { url => '/thruk/cgi-bin/conf.cgi?sub=objects&action=browser', like => [ '/Config Tool/', '/commands.cfg/' ] },
169 # reporting
170 { url => '/thruk/cgi-bin/reports2.cgi', like => '/Reporting/' },
171 { url => '/thruk/cgi-bin/reports2.cgi', post => { 'action' => 'save', 'report' => '9999', 'name' => 'Service SLA Report for '.$host.' - '.$servicep, 'template' => 'sla_service.tt', 'params.sla' => 95, 'params.timeperiod' => 'last12months', 'params.host' => $host, 'params.service' => $servicep, 'params.breakdown' => 'months', 'params.unavailable' => ['critical', 'unknown' ], 'params.decimals' => 2, 'params.graph_min_sla' => 90 }, like => '/success_message/' },
172 { url => '/thruk/cgi-bin/reports2.cgi?report=9999&action=update' },
173 { url => '/thruk/cgi-bin/reports2.cgi', waitfor => 'reports2.cgi\?report=9999\&amp;refresh=0' },
174 { url => '/thruk/cgi-bin/reports2.cgi?report=9999', like => [ '/%PDF-1.4/', '/%%EOF/' ] },
175 { url => '/thruk/cgi-bin/reports2.cgi', post => { 'action' => 'remove', 'report' => 9999 }, like => '/report removed/' },
177 # recurring downtimes
178 { url => '/thruk/cgi-bin/extinfo.cgi', post => { 'type' => 6, 'recurring' => 'save', 'target' => 'host', 'host' => $host, 'comment' => 'automatic downtime', 'send_type_1' => 'month', 'send_day_1' => 1, 'week_day_1' => '', 'send_hour_1' => 0, 'send_minute_1' => 0, 'duration' => 120, 'childoptions' => 0, 'nr' => 999 }, like => '/recurring downtime saved/' },
179 { url => '/thruk/cgi-bin/extinfo.cgi', post => { 'type' => 6, 'target' => 'host', 'recurring' => 'remove', 'nr' => 999, 'host' => $host }, like => '/recurring downtime removed/' },
181 # usercontent examples
182 { url => '/thruk/usercontent/backgrounds/world.png', like => '/PNG/' },
185 my $own_urls = [
186 # business process
187 { url => '/thruk/cgi-bin/bp.cgi?action=new&bp_label=New Test Business Process', like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
188 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'commit', 'bp' => 1 }, like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
189 { url => '/thruk/cgi-bin/status.cgi', waitfor => 'New\ Test\ Business\ Process' },
190 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'remove', 'bp' => 1 }, skip_link_check => ['.cgi'] },
193 my $shared_urls = [
194 # business process
195 { url => '/thruk/cgi-bin/bp.cgi?action=new&bp_label=New Test Business Process', like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
196 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'commit', 'bp' => 1 }, like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
197 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'remove', 'bp' => 1 }, skip_link_check => ['.cgi'] },
200 my $cookie_urls = [
201 { url => '/thruk/cgi-bin/tac.cgi', like => '/Password/', unlike => [ '/internal server error/'] },
205 # complete the url
206 for my $url ( @{$urls}, @{$shared_urls}, @{$own_urls}, @{$cookie_urls} ) {
207 $url->{'url'} = "http://localhost/".$site.$url->{'url'};
208 $url->{'auth'} = $auth;
209 $url->{'unlike'} = [ '/internal server error/', '/"\/thruk\//', '/\'\/thruk\//' ] unless defined $url->{'unlike'};
210 if($url->{'post'}) {
211 $url->{'post'}->{'token'} = 'test';
215 for my $core (qw/nagios shinken icinga/) {
216 ##################################################
217 # run our tests
218 TestUtils::test_command({ cmd => $omd_bin." stop $site" });
219 TestUtils::test_command({ cmd => $omd_bin." config $site set CORE $core" });
220 TestUtils::test_command({ cmd => $omd_bin." config $site set APACHE_MODE own" });
221 TestUtils::test_command({ cmd => TestUtils::config('APACHE_INIT')." restart" });
222 TestUtils::test_command({ cmd => $omd_bin." start $site" }) or TestUtils::bail_out_clean("No need to test Thruk without proper startup");
223 TestUtils::wait_for_file("/omd/sites/$site/tmp/run/live") or TestUtils::bail_out_clean("No need to test Thruk without livestatus connection");
224 unlink("/omd/sites/$site/tmp/thruk/thruk.cache");
225 unlink("/omd/sites/$site/var/thruk/obj_retention.dat");
227 TestUtils::test_command({ cmd => "/bin/su - $site -c './bin/thruk -A omdadmin \"cmd.cgi?cmd_typ=7&cmd_mod=2&host=omd-$site&service=Dummy+Service&start_time=now&force_check=on&btnSubmit=Commit\" --local'", like => '/Command request successfully submitted/', errlike => '/cmd: COMMAND/' });
228 TestUtils::wait_for_file("/omd/sites/$site/var/pnp4nagios/perfdata/omd-$site/Dummy_Service_omd-dummy.rrd") or TestUtils::bail_out_clean("No need to test Thruk without working pnp");;
230 for my $test (@{$tests}) {
231 TestUtils::test_command($test);
233 for my $test (@{$own_tests}) {
234 TestUtils::test_command($test);
236 ##################################################
237 # and request some pages
238 for my $url ( @{$urls} ) {
239 TestUtils::test_url($url);
241 for my $url ( @{$own_urls} ) {
242 TestUtils::test_url($url);
245 ##################################################
246 # switch webserver to shared mode
247 TestUtils::test_command({ cmd => $omd_bin." stop $site" });
248 TestUtils::test_command({ cmd => $omd_bin." config $site set APACHE_MODE shared" });
249 TestUtils::test_command({ cmd => TestUtils::config('APACHE_INIT')." restart" });
250 TestUtils::test_command({ cmd => $omd_bin." start $site" });
252 ##################################################
253 # then run tests again
254 for my $test (@{$tests}) {
255 TestUtils::test_command($test);
257 for my $test (@{$shared_tests}) {
258 TestUtils::test_command($test);
260 ##################################################
261 # and request some pages
262 for my $url ( @{$urls} ) {
263 TestUtils::test_url($url);
265 for my $url ( @{$shared_urls} ) {
266 TestUtils::test_url($url);
269 my $log = "/omd/sites/$site/var/log/thruk.log";
270 my $tlog = '/tmp/thruk_test_error.log';
271 is(-f $log, 1, "log exists");
272 # grep out commands
273 `/bin/cat $log | /bin/grep -v 'cmd: COMMAND' | /bin/grep -v ' started ' | /bin/grep -v 'templates precompiled in' > $tlog 2>&1`;
274 is(-s $tlog, 0, "log is empty") or diag(Dumper(`cat $log`));
275 unlink($tlog);
278 ##################################################
279 # enable cookie auth
280 TestUtils::test_command({ cmd => $omd_bin." stop $site" });
281 TestUtils::test_command({ cmd => $omd_bin." config $site set CORE nagios" });
282 TestUtils::test_command({ cmd => $omd_bin." config $site set APACHE_MODE own" });
283 TestUtils::test_command({ cmd => $omd_bin." config $site set THRUK_COOKIE_AUTH on" });
284 TestUtils::test_command({ cmd => $omd_bin." start $site", like => '/OK/' });
285 TestUtils::test_command({ cmd => TestUtils::config('APACHE_INIT')." restart" });
286 TestUtils::test_command({ cmd => $omd_bin." start $site thruk", like => '/OK/' });
287 sleep(3);
288 TestUtils::test_command({ cmd => $omd_bin." status $site apache", like => '/running/' });
289 for my $url ( @{$cookie_urls} ) {
290 TestUtils::test_url($url);
293 ##################################################
294 # cleanup test site
295 TestUtils::test_command({ cmd => TestUtils::config('APACHE_INIT')." restart" });
296 TestUtils::remove_test_site($site);
299 ##################################################
300 sub set_test_user_token {
301 my $file = '/omd/sites/'.$site.'/var/thruk/token';
302 local $ENV{'CATALYST_CONFIG'} = '/omd/sites/'.$site.'/etc/thruk/';
303 my $config = Thruk::Config::get_config();
304 my $store = Thruk::Utils::Cache->new($file);
305 my $tokens = $store->get('token');
306 $tokens->{'omdadmin'} = { token => 'test', time => time() };
307 $store->set('token', $tokens);
308 `chown $site:$site $file`;
309 ok(-s $file, $file." exists") or TestUtils::bail_out_clean("no further testing without token");
310 return;