1 package SL::BackgroundJob::SelfTest;
 
   5 use parent qw(SL::BackgroundJob::Base);
 
   9 use TAP::Parser::Aggregator;
 
  17 use Rose::Object::MakeMethods::Generic (
 
  20    'add_modules' => { interface => 'add', hash_key => 'modules' },
 
  22    'add_errors'  => { interface => 'add', hash_key => 'errors' },
 
  24    'add_full_diag'  => { interface => 'add', hash_key => 'full_diag' },
 
  27    qw(diag tester config aggreg),
 
  32   $_[0]->create_standard_job('20 2 * * *'); # every day at 2:20 am
 
  38   $self->config($::lx_office_conf{self_test} || {});
 
  40   $self->tester(Test::Builder->new);
 
  41   $self->tester->reset; # stupid Test::Builder mplementation uses class variables
 
  42   $self->aggreg(TAP::Parser::Aggregator->new);
 
  44   $self->modules(split /\s+/, $self->config->{modules});
 
  51   return 1 unless $self->modules;
 
  53   foreach my $module ($self->modules) {
 
  54     $self->run_module($module);
 
  58     sprintf "SelfTest status: %s, passed: %s, failed: %s, unexpectedly succeeded: %s",
 
  59              $self->aggreg->get_status,
 
  60              $self->aggreg->passed,
 
  61              $self->aggreg->failed,
 
  62              $self->aggreg->todo_passed,
 
  65   if (!$self->aggreg->all_passed || $self->config->{send_email_on_success}) {
 
  73   my ($self, $module) = @_;
 
  75   # TAP usually prints out to STDOUT and STDERR, capture those for TAP::Parser
 
  78   $self->tester->output        (\$output);
 
  79   $self->tester->failure_output(\$output);
 
  80   $self->tester->todo_output   (\$output);
 
  82   # sanitize module name;
 
  83   # this allows unicode package names, which are known to be buggy in 5.10, you should avoid them
 
  84   $module =~ s/[^\w:]//g;
 
  85   $module = "SL::BackgroundJob::SelfTest::$module";
 
  88   (my $file = $module) =~ s|::|/|g;
 
  90     require $file . '.pm';
 
  92   } or $self->add_errors($::locale->text('Could not load class #1 (#2): "#3"', $module, $file, $@)) && return;
 
  95     my $worker = $module->new;
 
  96     $worker->tester($self->tester);
 
 100   } or $self->add_errors($::locale->text('Could not load class #1, #2', $module, $@)) && return;
 
 102   $self->add_full_diag($output);
 
 103   $self->{diag_per_module}{$module} = $output;
 
 105   my $parser = TAP::Parser->new({ tap => $output});
 
 108   $self->aggreg->add($module => $parser);
 
 112   $_[0]{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $_[0]->config->{send_email_to});
 
 118   return if !$self->config || !$self->config->{send_email_to};
 
 120   my $user  = $self->_email_user;
 
 121   my $email = $user ? $user->get_config_value('email') : undef;
 
 123   return unless $email;
 
 125   my ($output, $content_type) = $self->_prepare_report;
 
 127   my $mail              = Mailer->new;
 
 128   $mail->{from}         = $self->config->{email_from};
 
 129   $mail->{to}           = $email;
 
 130   $mail->{subject}      = $self->config->{email_subject};
 
 131   $mail->{content_type} = $content_type;
 
 132   $mail->{message}      = $$output;
 
 137 sub _prepare_report {
 
 140   my $user = $self->_email_user;
 
 141   my $template = Template->new({ 'INTERPOLATE' => 0,
 
 147   return unless $template;
 
 148   my $email_template = $self->config->{email_template};
 
 149   my $filename       = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/self_test/status_mail.txt" );
 
 150   my $content_type   = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
 
 156     database => $::myconfig{dbname},
 
 157     path     => $FindBin::Bin,
 
 161   $template->process($filename, \%params, \$output) || die $template->error;
 
 163   return (\$output, $content_type);
 
 168   $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
 
 178 SL::BackgroundJob::SelfTest - pluggable self testing
 
 182   use SL::BackgroundJob::SelfTest;
 
 183   SL::BackgroundJob::SelfTest->new->run;;