check_logfiles: 3.7.4.1
[omd.git] / t / 20-package_thruk.t
blob1c273e0534fca7e140073366be03e34bfd8c3b5f
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 => 1474 );
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/' },
81 for my $report (@{$reports}) {
82 my $args = [];
83 for my $key (keys %{$report}) {
84 for my $val (ref $report->{$key} eq 'ARRAY' ? @{$report->{$key}} : $report->{$key}) {
85 push @{$args}, $key.'='.$val;
88 push @{$tests}, (
89 { cmd => "/bin/su - $site -c './bin/thruk -A omdadmin \"/thruk/cgi-bin/reports2.cgi?action=save&report=9999&".join('&', @{$args})."\"'",
90 like => "/OK - report updated/" },
91 { cmd => "/bin/su - $site -c 'omd reload crontab'", like => [ '/OK/' ] },
92 { cmd => "/bin/su - $site -c '/usr/bin/crontab -l | grep -i thruk | grep -v cron.d'", like => [ '/9999/' ] },
93 { cmd => "/bin/su - $site -c './bin/thruk -a report=9999 --local'", like => [ '/%PDF\-1\.4/', '/%%EOF/' ] },
94 { cmd => "/bin/su - $site -c './bin/thruk -A omdadmin \"/thruk/cgi-bin/reports2.cgi?action=remove&report=9999\"'", like => '/OK - report removed/' },
99 my $urls = [
100 # static html pages
101 { url => "/thruk/side.html", like => '/<title>Thruk<\/title>/' },
102 { url => "", like => '/<title>Thruk<\/title>/' },
103 { url => "/thruk/index.html", like => '/<title>Thruk<\/title>/' },
104 { url => "/thruk/startup.html", like => '/<title>Thruk Monitoring Webinterface<\/title>/' },
105 { url => "/thruk/docs/index.html", like => '/<title>Documentation<\/title>/' },
106 { url => "/thruk/main.html", like => '/<title>Thruk Monitoring Webinterface<\/title>/' },
108 # availability
109 { url => '/thruk/cgi-bin/avail.cgi', 'like' => '/Availability Report/' },
110 { 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/' },
112 # extinfo
113 { url => '/thruk/cgi-bin/extinfo.cgi?type=1&host='.$host },
114 { url => '/thruk/cgi-bin/extinfo.cgi?type=2&host='.$host.'&service='.$service },
116 # config
117 { url => '/thruk/cgi-bin/config.cgi', 'like' => '/Configuration/' },
118 { url => '/thruk/cgi-bin/config.cgi?type=hosts', 'like' => '/Configuration/' },
119 { url => '/thruk/cgi-bin/config.cgi?type=services', 'like' => '/Configuration/' },
121 # history
122 { url => '/thruk/cgi-bin/history.cgi', like => '/Alert History/' },
123 { url => '/thruk/cgi-bin/history.cgi?host=all', like => '/Alert History/' },
124 { url => '/thruk/cgi-bin/history.cgi?host='.$host, like => '/Alert History/' },
125 { url => '/thruk/cgi-bin/history.cgi?host='.$host.'&service='.$service, like => '/Alert History/' },
127 # notifications
128 { url => '/thruk/cgi-bin/notifications.cgi', like => '/All Hosts and Services/' },
130 # outages
131 { url => '/thruk/cgi-bin/outages.cgi', like => '/Network Outages/' },
133 # showlog
134 { url => '/thruk/cgi-bin/showlog.cgi', like => '/Event Log/' },
136 # status
137 { url => '/thruk/cgi-bin/status.cgi', like => '/Current Network Status/' },
138 { url => '/thruk/cgi-bin/status.cgi?host='.$host, like => '/Current Network Status/' },
140 # summary
141 { url => '/thruk/cgi-bin/summary.cgi', like => '/Alert Summary Report/' },
143 # tac
144 { url => '/thruk/cgi-bin/tac.cgi', like => '/Tactical Monitoring Overview/' },
146 # trends
147 { 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/' },
148 { 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/' },
150 # statusmap
151 { url => '/thruk/cgi-bin/statusmap.cgi?host=all', like => '/Network Map/' },
153 # minemap
154 { url => '/thruk/cgi-bin/minemap.cgi', like => '/Mine Map/' },
156 # conf tool
157 { url => '/thruk/cgi-bin/conf.cgi', like => '/Config Tool/' },
158 { url => '/thruk/cgi-bin/conf.cgi?sub=thruk', like => [ '/Config Tool/', '/title_prefix/', '/use_wait_feature/'] },
159 { url => '/thruk/cgi-bin/conf.cgi?sub=cgi', like => [ '/Config Tool/', '/show_context_help/', '/use_pending_states/' ] },
160 { url => '/thruk/cgi-bin/conf.cgi?sub=users', like => [ '/Config Tool/', '/select user to change/' ] },
161 { url => '/thruk/cgi-bin/conf.cgi?sub=users&action=change&data.username=omdadmin', like => [ '/Config Tool/', '/remove password/', '/authorized_for_all_services/' ] },
162 { url => '/thruk/cgi-bin/conf.cgi?sub=objects', like => [ '/Config Tool/', '/select host to change/' ] },
163 { url => '/thruk/cgi-bin/conf.cgi?sub=objects&apply=yes', like => [ '/Config Tool/', '/There are no pending changes to commit/' ] },
164 { 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 },
165 { url => '/thruk/cgi-bin/conf.cgi?sub=objects&action=browser', like => [ '/Config Tool/', '/commands.cfg/' ] },
167 # reporting
168 { url => '/thruk/cgi-bin/reports2.cgi', like => '/Reporting/' },
169 { 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/' },
170 { url => '/thruk/cgi-bin/reports2.cgi?report=9999&action=update' },
171 { url => '/thruk/cgi-bin/reports2.cgi', waitfor => 'reports2.cgi\?report=9999\&amp;refresh=0' },
172 { url => '/thruk/cgi-bin/reports2.cgi?report=9999', like => [ '/%PDF-1.4/', '/%%EOF/' ] },
173 { url => '/thruk/cgi-bin/reports2.cgi', post => { 'action' => 'remove', 'report' => 9999 }, like => '/report removed/' },
175 # recurring downtimes
176 { 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/' },
177 { url => '/thruk/cgi-bin/extinfo.cgi', post => { 'type' => 6, 'target' => 'host', 'recurring' => 'remove', 'nr' => 999, 'host' => $host }, like => '/recurring downtime removed/' },
179 # usercontent examples
180 { url => '/thruk/usercontent/backgrounds/world.png', like => '/PNG/' },
183 my $own_urls = [
184 # business process
185 { url => '/thruk/cgi-bin/bp.cgi?action=new&bp_label=New Test Business Process', like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
186 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'commit', 'bp' => 1 }, like => '/New Test Business Process/', skip_link_check => ['.cgi'] },
187 { url => '/thruk/cgi-bin/status.cgi', waitfor => 'New\ Test\ Business\ Process' },
188 { url => '/thruk/cgi-bin/bp.cgi', post => { 'action' => 'remove', 'bp' => 1 }, skip_link_check => ['.cgi'] },
191 my $cookie_urls = [
192 { url => '/thruk/cgi-bin/tac.cgi', like => '/Password/', unlike => [ '/internal server error/'] },
196 # complete the url
197 for my $url ( @{$urls}, @{$own_urls}, @{$cookie_urls} ) {
198 $url->{'url'} = "http://localhost/".$site.$url->{'url'};
199 $url->{'auth'} = $auth;
200 $url->{'unlike'} = [ '/internal server error/', '/"\/thruk\//', '/\'\/thruk\//' ] unless defined $url->{'unlike'};
201 if($url->{'post'}) {
202 $url->{'post'}->{'token'} = 'test';
206 for my $core (qw/nagios shinken icinga/) {
207 ##################################################
208 # run our tests
209 TestUtils::test_command({ cmd => $omd_bin." stop $site" });
210 TestUtils::test_command({ cmd => $omd_bin." config $site set CORE $core" });
211 TestUtils::test_command({ cmd => $omd_bin." config $site set APACHE_MODE own" });
212 TestUtils::restart_system_apache();
213 unlink("/omd/sites/$site/tmp/run/live");
214 TestUtils::test_command({ cmd => $omd_bin." start $site" }) or TestUtils::bail_out_clean("No need to test Thruk without proper startup");
215 TestUtils::wait_for_file("/omd/sites/$site/tmp/run/live") or TestUtils::bail_out_clean("No need to test Thruk without livestatus connection");
216 unlink("/omd/sites/$site/tmp/thruk/thruk.cache");
217 unlink("/omd/sites/$site/var/thruk/obj_retention.dat");
219 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/' });
220 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");;
222 for my $test (@{$tests}) {
223 TestUtils::test_command($test);
225 for my $test (@{$own_tests}) {
226 TestUtils::test_command($test);
228 ##################################################
229 # and request some pages
230 for my $url ( @{$urls} ) {
231 TestUtils::test_url($url);
233 for my $url ( @{$own_urls} ) {
234 TestUtils::test_url($url);
237 my $log = "/omd/sites/$site/var/log/thruk.log";
238 my $tlog = '/tmp/thruk_test_error.log';
239 is(-f $log, 1, "log exists");
240 # grep out commands
241 `/bin/cat $log | /bin/grep -v 'cmd: COMMAND' | /bin/grep -v ' started ' | /bin/grep -v 'templates precompiled in' > $tlog 2>&1`;
242 is(-s $tlog, 0, "log is empty") or diag(Dumper(`cat $log`));
243 unlink($tlog);
246 ##################################################
247 # enable cookie auth
248 TestUtils::test_command({ cmd => $omd_bin." stop $site" });
249 TestUtils::test_command({ cmd => $omd_bin." config $site set CORE nagios" });
250 TestUtils::test_command({ cmd => $omd_bin." config $site set APACHE_MODE own" });
251 TestUtils::test_command({ cmd => $omd_bin." config $site set THRUK_COOKIE_AUTH on" });
252 unlink("/omd/sites/$site/tmp/run/live");
253 TestUtils::test_command({ cmd => $omd_bin." start $site", like => '/OK/' });
254 TestUtils::wait_for_file("/omd/sites/$site/tmp/run/live") or TestUtils::bail_out_clean("No need to test Thruk without livestatus connection");
255 TestUtils::restart_system_apache();
256 TestUtils::test_command({ cmd => $omd_bin." start $site thruk", like => '/OK/' });
257 sleep(3);
258 TestUtils::test_command({ cmd => $omd_bin." status $site apache", like => '/running/' });
259 for my $url ( @{$cookie_urls} ) {
260 TestUtils::test_url($url);
263 ##################################################
264 # cleanup test site
265 TestUtils::restart_system_apache();
266 TestUtils::remove_test_site($site);
269 ##################################################
270 sub set_test_user_token {
271 my $file = '/omd/sites/'.$site.'/var/thruk/token';
272 local $ENV{'THRUK_CONFIG'} = '/omd/sites/'.$site.'/etc/thruk/';
273 my $config = Thruk::Config::get_config();
274 my $store = Thruk::Utils::Cache->new($file);
275 my $tokens = $store->get('token');
276 $tokens->{'omdadmin'} = { token => 'test', time => time() };
277 $store->set('token', $tokens);
278 `chown $site:$site $file`;
279 ok(-s $file, $file." exists") or TestUtils::bail_out_clean("no further testing without token");
280 return;