-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestUtils.pm
503 lines (449 loc) · 18.8 KB
/
TestUtils.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env perl
package TestUtils;
#########################
# Test Utils
#########################
use strict;
use Data::Dumper;
use Test::More;
use URI::Escape;
use Encode qw/decode_utf8/;
use Thruk::Utils;
use Thruk::Utils::External;
use Catalyst::Test 'Thruk';
my $use_html_lint = 0;
eval {
require HTML::Lint;
$use_html_lint = 1;
};
#########################
sub get_test_servicegroup {
my $request = _request('/thruk/cgi-bin/status.cgi?servicegroup=all&style=overview');
ok( $request->is_success, 'get_test_servicegroup() needs a proper config page' ) or diag(Dumper($request));
my $page = $request->content;
my $group;
if($page =~ m/extinfo\.cgi\?type=8&servicegroup=(.*?)'>(.*?)<\/a>/) {
$group = $1;
}
isnt($group, undef, "got a servicegroup from config.cgi") or bail_out_req('got no test servicegroup, cannot test.', $request);
return($group);
}
#########################
sub get_test_hostgroup {
my $request = _request('/thruk/cgi-bin/status.cgi?hostgroup=all&style=overview');
ok( $request->is_success, 'get_test_hostgroup() needs a proper config page' ) or diag(Dumper($request));
my $page = $request->content;
my $group;
if($page =~ m/'extinfo\.cgi\?type=5&hostgroup=(.*?)'>(.*?)<\/a>/) {
$group = $1;
}
isnt($group, undef, "got a hostgroup from config.cgi") or bail_out_req('got no test hostgroup, cannot test.', $request);
return($group);
}
#########################
sub get_test_user {
my $request = _request('/thruk/cgi-bin/status.cgi?hostgroup=all&style=hostdetail');
ok( $request->is_success, 'get_test_user() needs a proper config page' ) or diag(Dumper($request));
my $page = $request->content;
my $user;
if($page =~ m/Logged in as <i>(.*?)<\/i>/) {
$user = $1;
}
isnt($user, undef, "got a user from config.cgi") or bail_out_req('got no test user, cannot test.', $request);
return($user);
}
#########################
sub get_test_service {
my $backend = shift;
my $request = _request('/thruk/cgi-bin/status.cgi?host=all'.(defined $backend ? '&backend='.$backend : ''));
ok( $request->is_success, 'get_test_service() needs a proper status page' ) or diag(Dumper($request));
my $page = $request->content;
my($host,$service);
if($page =~ m/extinfo\.cgi\?type=2&host=(.*?)&service=(.*?)&/) {
$host = $1;
$service = $2;
}
isnt($host, undef, "got a host from status.cgi") or bail_out_req('got no test host, cannot test.', $request);
isnt($service, undef, "got a service from status.cgi") or bail_out_req('got no test service, cannot test.', $request);
$service = uri_unescape($service);
$host = uri_unescape($host);
return($host, $service);
}
#########################
sub get_test_timeperiod {
my $request = _request('/thruk/cgi-bin/config.cgi?type=timeperiods');
ok( $request->is_success, 'get_test_timeperiod() needs a proper config page' ) or diag(Dumper($request));
my $page = $request->content;
my $timeperiod;
if($page =~ m/id="timeperiod_.*?">\s*<td\ class='dataOdd'>([^<]+)<\/td>/gmx) {
$timeperiod = $1;
}
isnt($timeperiod, undef, "got a timeperiod from config.cgi") or bail_out_req('got no test config, cannot test.', $request);
return($timeperiod);
}
#########################
sub test_page {
my(%opts) = @_;
my $return = {};
my $opts = _set_test_page_defaults(\%opts);
ok($opts->{'url'}, $opts->{'url'});
my $request = _request($opts->{'url'}, $opts->{'startup_to_url'});
if(defined $opts->{'follow'}) {
my $redirects = 0;
while(my $location = $request->{'_headers'}->{'location'}) {
if($location !~ m/^(http|\/)/gmx) { $location = _relative_url($location, $request->base()->as_string()); }
$request = _request($location);
$redirects++;
last if $redirects > 10;
}
ok( $redirects < 10, 'Redirect succeed after '.$redirects.' hops' ) or bail_out_req('too many redirects', $request);
}
if(!defined $opts->{'fail_message_ok'}) {
if($request->content =~ m/<span\ class="fail_message">([^<]+)<\/span>/mx) {
fail('Request '.$opts->{'url'}.' had error message: '.$1);
}
}
$return->{'content'} = $request->content;
if($request->is_redirect and $request->{'_headers'}->{'location'} =~ m/cgi\-bin\/job\.cgi\?job=(.*)$/) {
# is it a background job page?
wait_for_job($1);
my $location = $request->{'_headers'}->{'location'};
$request = _request($location);
$return->{'content'} = $request->content;
if($request->is_error) {
fail('Request '.$location.' should succeed');
bail_out_req('request failed', $request);
}
}
elsif(defined $opts->{'fail'}) {
ok( $request->is_error, 'Request '.$opts->{'url'}.' should fail' );
}
elsif(defined $opts->{'redirect'}) {
ok( $request->is_redirect, 'Request '.$opts->{'url'}.' should redirect' ) or diag(Dumper($request));
if(defined $opts->{'location'}) {
if(defined $request->{'_headers'}->{'location'}) {
like($request->{'_headers'}->{'location'}, qr/$opts->{'location'}/, "Content should redirect: ".$opts->{'location'});
} else {
fail('no redirect header found');
}
}
}
elsif(defined $return->{'content'} and $return->{'content'} =~ m/cgi\-bin\/job\.cgi\?job=(.*)$/) {
# is it a background job page?
wait_for_job($1);
my $location = "/thruk/cgi-bin/job.cgi?job=".$1;
$request = _request($location);
$return->{'content'} = $request->content;
if($request->is_error) {
fail('Request '.$location.' should succeed');
bail_out_req('request failed', $request);
}
} else {
ok( $request->is_success, 'Request '.$opts->{'url'}.' should succeed' ) or bail_out_req('request failed', $request);
}
# text that should appear
if(defined $opts->{'like'}) {
if(ref $opts->{'like'} eq '') {
like($return->{'content'}, qr/$opts->{'like'}/, "Content should contain: ".$opts->{'like'}) or diag($opts->{'url'});
} elsif(ref $opts->{'like'} eq 'ARRAY') {
for my $like (@{$opts->{'like'}}) {
like($return->{'content'}, qr/$like/, "Content should contain: ".$like) or diag($opts->{'url'});
}
}
}
# text that shouldn't appear
if(defined $opts->{'unlike'}) {
if(ref $opts->{'unlike'} eq '') {
unlike($return->{'content'}, qr/$opts->{'unlike'}/, "Content should not contain: ".$opts->{'unlike'}) or diag($opts->{'url'});
} elsif(ref $opts->{'unlike'} eq 'ARRAY') {
for my $unlike (@{$opts->{'unlike'}}) {
unlike($return->{'content'}, qr/$unlike/, "Content should not contain: ".$unlike) or diag($opts->{'url'});
}
}
}
# test the content type
$return->{'content_type'} = $request->header('Content-Type');
my $content_type = $request->header('Content-Type');
if(defined $opts->{'content_type'}) {
is($return->{'content_type'}, $opts->{'content_type'}, 'Content-Type should be: '.$opts->{'content_type'}) or diag($opts->{'url'});
}
# memory usage
SKIP: {
skip "skipped memory check", 1 unless defined $ENV{'TEST_AUTHOR'};
open(my $ph, '-|', "ps -p $$ -o rss") or die("ps failed: $!");
while(my $line = <$ph>) {
if($line =~ m/(\d+)/) {
my $rsize = sprintf("%.2f", $1/1024);
ok($rsize < 1024, 'resident size ('.$rsize.'MB) higher than 1024MB on '.$opts->{'url'});
}
}
close($ph);
}
# html valitidy
if($content_type =~ 'text\/html' and !$request->is_redirect) {
like($return->{'content'}, '/<html[^>]*>/i', 'html page has html section');
like($return->{'content'}, '/<!doctype/i', 'html page has doctype');
}
SKIP: {
if($content_type =~ 'text\/html' and (!defined $opts->{'skip_html_lint'} or $opts->{'skip_html_lint'} == 0)) {
if($use_html_lint == 0) {
skip "no HTML::Lint installed", 2;
}
my $lint = new HTML::Lint;
isa_ok( $lint, "HTML::Lint" );
# will result in "Parsing of undecoded UTF-8 will give garbage when decoding entities..." otherwise
my $content = decode_utf8($return->{'content'});
$lint->parse($content);
my @errors = $lint->errors;
@errors = diag_lint_errors_and_remove_some_exceptions($lint);
is( scalar @errors, 0, "No errors found in HTML" );
$lint->clear_errors();
}
}
# check for missing images / css or js
if($content_type =~ 'text\/html') {
my @matches1 = $return->{'content'} =~ m/\s+(src|href)='(.+?)'/gi;
my @matches2 = $return->{'content'} =~ m/\s+(src|href)="(.+?)"/gi;
my $links_to_check;
my $x=0;
for my $match (@matches1, @matches2) {
$x++;
next if $x%2==1;
next if $match =~ m/^http/;
next if $match =~ m/^ssh/;
next if $match =~ m/^mailto:/;
next if $match =~ m/^(#|'|")/;
next if $match =~ m/^\/thruk\/cgi\-bin/;
next if $match =~ m/^\w+\.cgi/;
next if $match =~ m/^javascript:/;
next if $match =~ m/^'\+\w+\+'$/ and defined $ENV{'CATALYST_SERVER'};
next if $match =~ m|^/thruk/frame\.html| and defined $ENV{'CATALYST_SERVER'};
next if $match =~ m/"\s*\+\s*icon\s*\+\s*"/;
next if $match =~ m/\/"\+/;
$match =~ s/"\s*\+\s*url_prefix\s*\+\s*"/\//gmx;
$match =~ s/"\s*\+\s*theme\s*\+\s*"/Thruk/gmx;
$links_to_check->{$match} = 1;
}
my $errors = 0;
for my $test_url (keys %{$links_to_check}) {
next if $test_url =~ m/\/pnp4nagios\//mx;
if($test_url !~ m/^(http|\/)/gmx) { $test_url = _relative_url($test_url, $request->base()->as_string()); }
my $request = _request($test_url);
if($request->is_redirect) {
my $redirects = 0;
while(my $location = $request->{'_headers'}->{'location'}) {
if($location !~ m/^(http|\/)/gmx) { $location = _relative_url($location, $request->base()->as_string()); }
$request = _request($location);
$redirects++;
last if $redirects > 10;
}
}
unless($request->is_success) {
$errors++;
diag("'$test_url' is missing, status: ".$request->code);
}
}
is( $errors, 0, 'All stylesheets, images and javascript exist' );
}
return $return;
}
#########################
sub diag_lint_errors_and_remove_some_exceptions {
my $lint = shift;
my @return;
for my $error ( $lint->errors ) {
my $err_str = $error->as_string;
next if $err_str =~ m/<IMG\ SRC=".*command.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*warning.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*unknown.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*critical.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*flapping.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*recovery.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*restart.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*start.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*icon_minimize\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*right\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*left\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*right\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*up\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*down\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*down\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*json\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*waiting\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*downtime\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*info\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*problem\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*criticity_\d\.png">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*stop\.gif">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*\/conf\/images\/obj_.*">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC=".*\/logos\/.*">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/<IMG\ SRC="[^"]*\.cgi[^"]*">\ tag\ has\ no\ HEIGHT\ and\ WIDTH\ attributes/imx;
next if $err_str =~ m/Unknown\ attribute\ "data\-\w+"\ for\ tag/imx;
next if $err_str =~ m/Invalid\ character.*should\ be\ written\ as/imx;
next if $err_str =~ m/Unknown\ attribute\ "placeholder"\ for\ tag\ <input>/imx;
diag($error->as_string."\n");
push @return, $error;
}
return @return;
}
#########################
sub get_themes {
my @themes = @{Thruk->config->{'View::TT'}->{'PRE_DEFINE'}->{'themes'}};
return @themes;
}
#########################
sub get_c {
my($res, $c) = ctx_request('/thruk/side.html');
return $c;
}
#########################
sub get_user {
my $c = get_c();
my ($uid, $groups) = Thruk::Utils::get_user($c);
my $user = getpwuid($uid);
return $user;
}
#########################
sub wait_for_job {
my $job = shift;
alarm(60);
while(Thruk::Utils::External::_is_running('./var/jobs/'.$job)) {
sleep(1);
}
is(Thruk::Utils::External::_is_running('./var/jobs/'.$job), 0, 'job is finished');
alarm(0);
return;
}
#########################
=head2 test_command
execute a test command
needs test hash
{
cmd => command line to execute
exit => expected exit code
like => (list of) regular expressions which have to match stdout
errlike => (list of) regular expressions which have to match stderr, default: empty
sleep => time to wait after executing the command
}
=cut
sub test_command {
my $test = shift;
my($rc, $stderr) = ( -1, '') ;
my $return = 1;
require Test::Cmd;
Test::Cmd->import();
# run the command
isnt($test->{'cmd'}, undef, "running cmd: ".$test->{'cmd'}) or $return = 0;
my($prg,$arg) = split(/\s+/, $test->{'cmd'}, 2);
my $t = Test::Cmd->new(prog => $prg, workdir => '') or die($!);
alarm(300);
eval {
local $SIG{ALRM} = sub { die "timeout on cmd: ".$test->{'cmd'}."\n" };
$t->run(args => $arg, stdin => $test->{'stdin'});
$rc = $?>>8;
};
if($@) {
$stderr = $@;
} else {
$stderr = $t->stderr;
}
alarm(0);
# exit code?
$test->{'exit'} = 0 unless exists $test->{'exit'};
if(defined $test->{'exit'} and $test->{'exit'} != -1) {
ok($rc == $test->{'exit'}, "exit code: ".$rc." == ".$test->{'exit'}) or do { diag("command failed with rc: ".$rc." - ".$t->stdout); $return = 0 };
}
# matches on stdout?
if(defined $test->{'like'}) {
for my $expr (ref $test->{'like'} eq 'ARRAY' ? @{$test->{'like'}} : $test->{'like'} ) {
like($t->stdout, $expr, "stdout like ".$expr) or do { diag("\ncmd: '".$test->{'cmd'}."' failed\n"); $return = 0 };
}
}
# matches on stderr?
$test->{'errlike'} = '/^\s*$/' unless exists $test->{'errlike'};
if(defined $test->{'errlike'}) {
for my $expr (ref $test->{'errlike'} eq 'ARRAY' ? @{$test->{'errlike'}} : $test->{'errlike'} ) {
like($stderr, $expr, "stderr like ".$expr) or do { diag("\ncmd: '".$test->{'cmd'}."' failed"); $return = 0 };
}
}
# sleep after the command?
if(defined $test->{'sleep'}) {
ok(sleep($test->{'sleep'}), "slept $test->{'sleep'} seconds") or do { $return = 0 };
}
# set some values
$test->{'stdout'} = $t->stdout;
$test->{'stderr'} = $t->stderr;
$test->{'exit'} = $rc;
return $return;
}
#########################
sub make_test_hash {
my $data = shift;
my $test = shift || {};
if(ref $data eq '') {
$test->{'url'} = $data;
} else {
for my $key (%{$data}) {
$test->{$key} = $data->{$key};
}
}
return $test;
}
#########################
sub _relative_url {
my($location, $url) = @_;
my $newloc = $url;
$newloc =~ s/^(.*\/).*$/$1/gmx;
$newloc .= $location;
while($newloc =~ s|/[^\/]+/\.\./|/|gmx) {}
return $newloc;
}
#########################
sub _request {
my($url, $start_to) = @_;
my $request = request($url);
if($request->is_redirect and $request->{'_headers'}->{'location'} =~ m/\/startup\.html\?(.*)$/) {
my $link = $1;
$link =~ s/^wait\#//mx;
#diag("starting up... ".$link);
is($link, $start_to, "startup url points to: ".$link) if defined $start_to;
# startup fcgid
my $r = request('/thruk/cgi-bin/remote.cgi');
#diag("startup request:");
#diag(Dumper($r));
fail("startup failed: ".Dumper($r)) unless $r->is_success;
fail("startup failed, no pid: ".Dumper($r)) unless -f '/var/cache/thruk/thruk.pid';
sleep(3);
$request = request($link);
#diag("original request:");
#diag(Dumper($request));
}
return $request;
}
#########################
sub _set_test_page_defaults {
my($opts) = @_;
if(!exists $opts->{'unlike'}) {
$opts->{'unlike'} = [ 'internal server error', 'HASH', 'ARRAY' ];
}
return $opts;
}
#########################
sub bail_out_req {
my($msg, $req) = @_;
my $page = $req->content;
my $error = "";
if($page =~ m/<!--error:(.*?):error-->/smx) {
$error = $1;
diag(Dumper($msg));
diag(Dumper($error));
BAIL_OUT($msg);
}
diag(Dumper($msg));
diag(Dumper($req));
BAIL_OUT($msg);
return;
}
#########################
1;
__END__