| 1 | package Test::Chimps::Server; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Test::Chimps::ReportCollection; | ||
| 7 | use Test::Chimps::Report; | ||
| 8 | use Test::Chimps::Server::Lister; | ||
| 9 | |||
| 10 | use Algorithm::TokenBucket; | ||
| 11 | use CGI::Carp qw<fatalsToBrowser>; | ||
| 12 | use CGI; | ||
| 13 | use Digest::MD5 qw<md5_hex>; | ||
| 14 | use Fcntl qw<:DEFAULT :flock>; | ||
| 15 | use File::Basename; | ||
| 16 | use File::Path; | ||
| 17 | use File::Spec; | ||
| 18 | use Jifty::DBI::Handle; | ||
| 19 | use Jifty::DBI::SchemaGenerator; | ||
| 20 | use Params::Validate qw<:all>; | ||
| 21 | use Storable qw<store_fd fd_retrieve nfreeze thaw>; | ||
| 22 | use Test::TAP::HTMLMatrix; | ||
| 23 | use Test::TAP::Model::Visual; | ||
| 24 | use YAML::Syck; | ||
| 25 | use DateTime; | ||
| 26 | |||
| 27 | use constant PROTO_VERSION => 0.2; | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | Test::Chimps::Server - Accept smoke report uploads and display smoke reports | ||
| 32 | |||
| 33 | =head1 SYNOPSIS | ||
| 34 | |||
| 35 | This module simplifies the process of running a smoke server. It | ||
| 36 | is meant to be used with Test::Chimps::Client. | ||
| 37 | |||
| 38 | use Test::Chimps::Server; | ||
| 39 | |||
| 40 | my $server = Test::Chimps::Server->new(base_dir => '/var/www/smokes'); | ||
| 41 | |||
| 42 | $server->handle_request; | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =head2 new ARGS | ||
| 47 | |||
| 48 | Creates a new Server object. ARGS is a hash whose valid keys are: | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item * base_dir | ||
| 53 | |||
| 54 | Mandatory. Base directory where report data will be stored. | ||
| 55 | |||
| 56 | =item * bucket_file | ||
| 57 | |||
| 58 | Name of bucket database file (see L<Algorithm::Bucket>). Defaults | ||
| 59 | to 'bucket.dat'. | ||
| 60 | |||
| 61 | =item * burst_rate | ||
| 62 | |||
| 63 | Burst upload rate allowed (see L<Algorithm::Bucket>). Defaults to | ||
| 64 | 5. | ||
| 65 | |||
| 66 | =item * database_dir | ||
| 67 | |||
| 68 | Directory under bsae_dir where the SQLite database will be stored. | ||
| 69 | Defaults to 'chimpsdb'. | ||
| 70 | |||
| 71 | =item * database_file | ||
| 72 | |||
| 73 | File under database_dir to use as the SQLite database. Defaults to | ||
| 74 | 'database'. | ||
| 75 | |||
| 76 | =item * list_template | ||
| 77 | |||
| 78 | Template filename under base_dir/template_dir to use for listing | ||
| 79 | smoke reports. Defaults to 'list.tmpl'. | ||
| 80 | |||
| 81 | =item * lister | ||
| 82 | |||
| 83 | An instance of L<Test::Chimps::Server::Lister> to use to list smoke | ||
| 84 | reports. You do not have to use this option unless you are | ||
| 85 | subclassing C<Lister>. | ||
| 86 | |||
| 87 | =item * max_rate | ||
| 88 | |||
| 89 | Maximum upload rate allowed (see L<Algorithm::Bucket>). Defaults | ||
| 90 | to 1/30. | ||
| 91 | |||
| 92 | =item * max_size | ||
| 93 | |||
| 94 | Maximum size of HTTP POST that will be accepted. Defaults to 3 | ||
| 95 | MiB. | ||
| 96 | |||
| 97 | =item * max_reports_per_subcategory | ||
| 98 | |||
| 99 | Maximum number of smokes allowed per category. Defaults to 5. | ||
| 100 | |||
| 101 | =item * template_dir | ||
| 102 | |||
| 103 | Directory under base_dir where html templates will be stored. | ||
| 104 | Defaults to 'templates'. | ||
| 105 | |||
| 106 | =item * variables_validation_spec | ||
| 107 | |||
| 108 | A hash reference of the form accepted by Params::Validate. If | ||
| 109 | supplied, this will be used to validate the report variables | ||
| 110 | submitted to the server. | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | use base qw/Class::Accessor/; | ||
| 117 | |||
| 118 | __PACKAGE__->mk_ro_accessors( | ||
| 119 | qw/base_dir bucket_file max_rate max_size | ||
| 120 | max_reports_per_subcategory database_dir database_file | ||
| 121 | template_dir list_template lister | ||
| 122 | variables_validation_spec handle/ | ||
| 123 | ); | ||
| 124 | |||
| 125 | sub new { | ||
| 126 | my $class = shift; | ||
| 127 | my $obj = bless {}, $class; | ||
| 128 | $obj->_init(@_); | ||
| 129 | return $obj; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub _init { | ||
| 133 | my $self = shift; | ||
| 134 | my %args = validate_with( | ||
| 135 | params => \@_, | ||
| 136 | called => 'The Test::Chimps::Server constructor', | ||
| 137 | spec => { | ||
| 138 | base_dir => { | ||
| 139 | type => SCALAR, | ||
| 140 | optional => 0 | ||
| 141 | }, | ||
| 142 | bucket_file => { | ||
| 143 | type => SCALAR, | ||
| 144 | default => 'bucket.dat', | ||
| 145 | optional => 1 | ||
| 146 | }, | ||
| 147 | burst_rate => { | ||
| 148 | type => SCALAR, | ||
| 149 | optional => 1, | ||
| 150 | default => 5, | ||
| 151 | callbacks => { | ||
| 152 | "greater than or equal to 0" => sub { $_[0] >= 0 } | ||
| 153 | } | ||
| 154 | }, | ||
| 155 | database_dir => { | ||
| 156 | type => SCALAR, | ||
| 157 | optional => 1, | ||
| 158 | default => 'chimpsdb' | ||
| 159 | }, | ||
| 160 | database_file => { | ||
| 161 | type => SCALAR, | ||
| 162 | optional => 1, | ||
| 163 | default => 'database' | ||
| 164 | }, | ||
| 165 | variables_validation_spec => { | ||
| 166 | type => HASHREF, | ||
| 167 | optional => 1 | ||
| 168 | }, | ||
| 169 | list_template => { | ||
| 170 | type => SCALAR, | ||
| 171 | optional => 1, | ||
| 172 | default => 'list.tmpl' | ||
| 173 | }, | ||
| 174 | lister => { | ||
| 175 | type => SCALAR, | ||
| 176 | isa => 'Test::Chimps::Server::Lister', | ||
| 177 | optional => 1 | ||
| 178 | }, | ||
| 179 | max_rate => { | ||
| 180 | type => SCALAR, | ||
| 181 | default => 1 / 30, | ||
| 182 | optional => 1, | ||
| 183 | callbacks => { | ||
| 184 | "greater than or equal to 0" => sub { $_[0] >= 0 } | ||
| 185 | } | ||
| 186 | }, | ||
| 187 | max_size => { | ||
| 188 | type => SCALAR, | ||
| 189 | default => 2**20 * 3.0, | ||
| 190 | optional => 1, | ||
| 191 | callbacks => { | ||
| 192 | "greater than or equal to 0" => sub { $_[0] >= 0 } | ||
| 193 | } | ||
| 194 | }, | ||
| 195 | max_reports_per_subcategory => { | ||
| 196 | type => SCALAR, | ||
| 197 | default => 5, | ||
| 198 | optional => 1, | ||
| 199 | callbacks => { | ||
| 200 | "greater than or equal to 0" => sub { $_[0] >= 0 } | ||
| 201 | } | ||
| 202 | }, | ||
| 203 | report_dir => { | ||
| 204 | type => SCALAR, | ||
| 205 | default => 'reports', | ||
| 206 | optional => 1 | ||
| 207 | }, | ||
| 208 | template_dir => { | ||
| 209 | type => SCALAR, | ||
| 210 | default => 'templates', | ||
| 211 | optional => 1 | ||
| 212 | } | ||
| 213 | } | ||
| 214 | ); | ||
| 215 | |||
| 216 | foreach my $key (keys %args) { | ||
| 217 | $self->{$key} = $args{$key}; | ||
| 218 | } | ||
| 219 | |||
| 220 | if (defined $self->variables_validation_spec) { | ||
| 221 | foreach my $var (keys %{$self->variables_validation_spec}) { | ||
| 222 | package Test::Chimps::Report::Schema; | ||
| 223 | column($var, type(is('text'))); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | my $dbdir = File::Spec->catdir($self->base_dir, | ||
| 228 | $self->database_dir); | ||
| 229 | if (! -e $dbdir) { | ||
| 230 | mkpath($dbdir); | ||
| 231 | } | ||
| 232 | |||
| 233 | my $dbname = File::Spec->catfile($dbdir, | ||
| 234 | $self->database_file); | ||
| 235 | $self->{handle} = Jifty::DBI::Handle->new(); | ||
| 236 | |||
| 237 | # create the table if the db doesn't exist. ripped out of | ||
| 238 | # Jifty::Script::Schema because this stuff should be in | ||
| 239 | # Jifty::DBI, but isn't | ||
| 240 | if (! -e $dbname) { | ||
| 241 | my $sg = Jifty::DBI::SchemaGenerator->new($self->handle); | ||
| 242 | $sg->add_model(Test::Chimps::Report->new(handle => $self->handle)); | ||
| 243 | |||
| 244 | $self->handle->connect(driver => 'SQLite', | ||
| 245 | database => $dbname); | ||
| 246 | # for non SQLite | ||
| 247 | # $self->handle->simple_query('CREATE DATABASE database'); | ||
| 248 | $self->handle->simple_query($_) for $sg->create_table_sql_statements; | ||
| 249 | } else { | ||
| 250 | $self->handle->connect(driver => 'SQLite', | ||
| 251 | database => $dbname); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | =head2 handle_request | ||
| 256 | |||
| 257 | Handles a single request. This function will either accept a | ||
| 258 | series of reports for upload or display report summaries. | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub handle_request { | ||
| 263 | my $self = shift; | ||
| 264 | |||
| 265 | my $cgi = CGI->new; | ||
| 266 | if ($cgi->param("upload")) { | ||
| 267 | $self->_process_upload($cgi); | ||
| 268 | } elsif ($cgi->param("id")) { | ||
| 269 | $self->_process_detail($cgi); | ||
| 270 | } else { | ||
| 271 | $self->_process_listing($cgi); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | sub _process_upload { | ||
| 276 | my $self = shift; | ||
| 277 | my $cgi = shift; | ||
| 278 | |||
| 279 | print $cgi->header("text/plain"); | ||
| 280 | $self->_limit_rate($cgi); | ||
| 281 | $self->_validate_params($cgi); | ||
| 282 | $self->_variables_validation_spec($cgi); | ||
| 283 | $self->_add_report($cgi); | ||
| 284 | |||
| 285 | print "ok"; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _limit_rate { | ||
| 289 | my $self = shift; | ||
| 290 | my $cgi = shift; | ||
| 291 | |||
| 292 | my $bucket_file = File::Spec->catfile($self->{base_dir}, | ||
| 293 | $self->{bucket_file}); | ||
| 294 | |||
| 295 | # Open the DB and lock it exclusively. See perldoc -q lock. | ||
| 296 | sysopen my $fh, $bucket_file, O_RDWR|O_CREAT | ||
| 297 | or die "Couldn't open \"$bucket_file\": $!\n"; | ||
| 298 | flock $fh, LOCK_EX | ||
| 299 | or die "Couldn't flock \"$bucket_file\": $!\n"; | ||
| 300 | |||
| 301 | my $data = eval { fd_retrieve $fh }; | ||
| 302 | $data ||= [$self->{max_rate}, $self->{burst_rate}]; | ||
| 303 | my $bucket = Algorithm::TokenBucket->new(@$data); | ||
| 304 | |||
| 305 | my $exit; | ||
| 306 | unless($bucket->conform(1)) { | ||
| 307 | print "Rate limiting -- please wait a bit and try again, thanks."; | ||
| 308 | $exit++; | ||
| 309 | } | ||
| 310 | $bucket->count(1); | ||
| 311 | |||
| 312 | seek $fh, 0, 0 or die "Couldn't rewind \"$bucket_file\": $!\n"; | ||
| 313 | truncate $fh, 0 or die "Couldn't truncate \"$bucket_file\": $!\n"; | ||
| 314 | |||
| 315 | store_fd [$bucket->state] => $fh or | ||
| 316 | croak "Couldn't serialize bucket to \"$bucket_file\": $!\n"; | ||
| 317 | |||
| 318 | exit if $exit; | ||
| 319 | } | ||
| 320 | |||
| 321 | sub _validate_params { | ||
| 322 | my $self = shift; | ||
| 323 | my $cgi = shift; | ||
| 324 | |||
| 325 | if(! $cgi->param("version") || | ||
| 326 | $cgi->param("version") != PROTO_VERSION) { | ||
| 327 | print "Protocol versions do not match!"; | ||
| 328 | exit; | ||
| 329 | } | ||
| 330 | |||
| 331 | if(! $cgi->param("model_structure")) { | ||
| 332 | print "No model structure given!"; | ||
| 333 | exit; | ||
| 334 | } | ||
| 335 | |||
| 336 | # uncompress_smoke(); | ||
| 337 | } | ||
| 338 | |||
| 339 | sub _variables_validation_spec { | ||
| 340 | my $self = shift; | ||
| 341 | my $cgi = shift; | ||
| 342 | |||
| 343 | if (defined $self->{variables_validation_spec}) { | ||
| 344 | my $report_variables = thaw($cgi->param('report_variables')); | ||
| 345 | eval { | ||
| 346 | validate(@{[%$report_variables]}, $self->{variables_validation_spec}); | ||
| 347 | }; | ||
| 348 | if (defined $@ && $@) { | ||
| 349 | # XXX: doesn't dump subroutines because we're using YAML::Syck | ||
| 350 | print "This server accepts specific report variables. It's validation ", | ||
| 351 | "string looks like this:\n", Dump($self->{variables_validation_spec}), | ||
| 352 | "\nYour report variables look like this:\n", $cgi->param('report_variables'); | ||
| 353 | exit; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | sub _add_report { | ||
| 359 | my $self = shift; | ||
| 360 | my $cgi = shift; | ||
| 361 | |||
| 362 | my $params = {}; | ||
| 363 | |||
| 364 | $params->{timestamp} = DateTime->from_epoch(epoch => time); | ||
| 365 | |||
| 366 | my $report_variables = thaw($cgi->param('report_variables')); | ||
| 367 | foreach my $var (keys %{$report_variables}) { | ||
| 368 | $params->{$var} = $report_variables->{$var}; | ||
| 369 | } | ||
| 370 | |||
| 371 | my $model = Test::TAP::Model::Visual->new_with_struct(thaw($cgi->param('model_structure'))); | ||
| 372 | |||
| 373 | foreach my $var ( | ||
| 374 | qw/total_ok | ||
| 375 | total_passed | ||
| 376 | total_nok | ||
| 377 | total_failed | ||
| 378 | total_percentage | ||
| 379 | total_ratio | ||
| 380 | total_seen | ||
| 381 | total_skipped | ||
| 382 | total_todo | ||
| 383 | total_unexpectedly_succeeded/ | ||
| 384 | ) | ||
| 385 | { | ||
| 386 | |||
| 387 | $params->{$var} = $model->$var; | ||
| 388 | } | ||
| 389 | |||
| 390 | $params->{model_structure} = thaw($cgi->param('model_structure')); | ||
| 391 | |||
| 392 | my $matrix = Test::TAP::HTMLMatrix->new($model, | ||
| 393 | Dump(thaw($cgi->param('report_variables')))); | ||
| 394 | $matrix->has_inline_css(1); | ||
| 395 | $params->{report_html} = $matrix->detail_html; | ||
| 396 | |||
| 397 | my $report = Test::Chimps::Report->new(handle => $self->handle); | ||
| 398 | |||
| 399 | $report->create(%$params) or croak "Couldn't add report to database: $!\n"; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _process_detail { | ||
| 403 | my $self = shift; | ||
| 404 | my $cgi = shift; | ||
| 405 | |||
| 406 | print $cgi->header; | ||
| 407 | |||
| 408 | my $id = $cgi->param("id"); | ||
| 409 | |||
| 410 | my $report = Test::Chimps::Report->new(handle => $self->handle); | ||
| 411 | $report->load($id); | ||
| 412 | |||
| 413 | print $report->report_html; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub _process_listing { | ||
| 417 | my $self = shift; | ||
| 418 | my $cgi = shift; | ||
| 419 | |||
| 420 | print $cgi->header(); | ||
| 421 | |||
| 422 | my $report_coll = Test::Chimps::ReportCollection->new(handle => $self->handle); | ||
| 423 | $report_coll->unlimit; | ||
| 424 | my @reports; | ||
| 425 | while (my $report = $report_coll->next) { | ||
| 426 | push @reports, $report; | ||
| 427 | } | ||
| 428 | |||
| 429 | my $lister; | ||
| 430 | if (defined $self->lister) { | ||
| 431 | $lister = $self->lister; | ||
| 432 | } else { | ||
| 433 | $lister = Test::Chimps::Server::Lister->new( | ||
| 434 | list_template => $self->list_template, | ||
| 435 | max_reports_per_subcategory => $self->max_reports_per_subcategory | ||
| 436 | ); | ||
| 437 | } | ||
| 438 | |||
| 439 | $lister->output_list(File::Spec->catdir($self->{base_dir}, | ||
| 440 | $self->{template_dir}), | ||
| 441 | \@reports, | ||
| 442 | $cgi); | ||
| 443 | |||
| 444 | } | ||
| 445 | |||
| 446 | =head1 AUTHOR | ||
| 447 | |||
| 448 | Zev Benjamin, C<< <zev at cpan.org> >> | ||
| 449 | |||
| 450 | =head1 BUGS | ||
| 451 | |||
| 452 | Please report any bugs or feature requests to | ||
| 453 | C<bug-test-chimps at rt.cpan.org>, or through the web interface at | ||
| 454 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Chimps>. | ||
| 455 | I will be notified, and then you'll automatically be notified of progress on | ||
| 456 | your bug as I make changes. | ||
| 457 | |||
| 458 | =head1 SUPPORT | ||
| 459 | |||
| 460 | You can find documentation for this module with the perldoc command. | ||
| 461 | |||
| 462 | perldoc Test::Chimps | ||
| 463 | |||
| 464 | You can also look for information at: | ||
| 465 | |||
| 466 | =over 4 | ||
| 467 | |||
| 468 | =item * Mailing list | ||
| 469 | |||
| 470 | Chimps has a mailman mailing list at | ||
| 471 | L<chimps@bestpractical.com>. You can subscribe via the web | ||
| 472 | interface at | ||
| 473 | L<http://lists.bestpractical.com/cgi-bin/mailman/listinfo/chimps>. | ||
| 474 | |||
| 475 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 476 | |||
| 477 | L<http://annocpan.org/dist/Test-Chimps> | ||
| 478 | |||
| 479 | =item * CPAN Ratings | ||
| 480 | |||
| 481 | L<http://cpanratings.perl.org/d/Test-Chimps> | ||
| 482 | |||
| 483 | =item * RT: CPAN's request tracker | ||
| 484 | |||
| 485 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Chimps> | ||
| 486 | |||
| 487 | =item * Search CPAN | ||
| 488 | |||
| 489 | L<http://search.cpan.org/dist/Test-Chimps> | ||
| 490 | |||
| 491 | =back | ||
| 492 | |||
| 493 | =head1 ACKNOWLEDGEMENTS | ||
| 494 | |||
| 495 | Some code in this distribution is based on smokeserv-server.pl from | ||
| 496 | the Pugs distribution. | ||
| 497 | |||
| 498 | =head1 COPYRIGHT & LICENSE | ||
| 499 | |||
| 500 | Copyright 2006 Best Practical Solutions. | ||
| 501 | Portions copyright 2005-2006 the Pugs project. | ||
| 502 | |||
| 503 | This program is free software; you can redistribute it and/or modify it | ||
| 504 | under the same terms as Perl itself. | ||
| 505 | |||
| 506 | =cut | ||
| 507 | |||
| 508 | 1; |