1 package SL::BackgroundJob::SelfTest;
 
   5 use parent qw(SL::BackgroundJob::Base);
 
   9 use TAP::Parser::Aggregator;
 
  16 use Rose::Object::MakeMethods::Generic (
 
  19    'add_modules' => { interface => 'add', hash_key => 'modules' },
 
  21    'add_errors'  => { interface => 'add', hash_key => 'errors' },
 
  23    'add_full_diag'  => { interface => 'add', hash_key => 'full_diag' },
 
  26    qw(diag tester config aggreg),
 
  31   $_[0]->create_standard_job('20 2 * * *'); # every day at 2:20 am
 
  37   $self->config($::lx_office_conf{self_test} || {});
 
  39   $self->tester(Test::Builder->new);
 
  40   $self->aggreg(TAP::Parser::Aggregator->new);
 
  42   $self->modules(split /\s+/, $self->config->{modules});
 
  49   return 1 unless $self->modules;
 
  51   foreach my $module ($self->modules) {
 
  52     $self->run_module($module);
 
  56     sprintf "SelfTest status: %s, passed: %s, failed: %s, unexpectedly succeeded: %s",
 
  57              $self->aggreg->get_status,
 
  58              $self->aggreg->passed,
 
  59              $self->aggreg->failed,
 
  60              $self->aggreg->todo_passed,
 
  63   if (!$self->aggreg->all_passed || $self->config->{send_email_on_success}) {
 
  71   my ($self, $module) = @_;
 
  73   # TAP usually prints out to STDOUT and STDERR, capture those for TAP::Parser
 
  76   $self->tester->output        (\$output);
 
  77   $self->tester->failure_output(\$output);
 
  78   $self->tester->todo_output   (\$output);
 
  80   # sanitize module name;
 
  81   # this allows unicode package names, which are known to be buggy in 5.10, you should avoid them
 
  82   $module =~ s/[^\w:]//g;
 
  83   $module = "SL::BackgroundJob::SelfTest::$module";
 
  86   (my $file = $module) =~ s|::|/|g;
 
  88     require $file . '.pm';
 
  90   } or $self->add_errors($::locale->text('Could not load class #1 (#2): "#3"', $module, $file, $@)) && return;
 
  93     my $worker = $module->new;
 
  94     $worker->tester($self->tester);
 
  98   } or $self->add_errors($::locale->text('Could not load class #1, #2', $module, $@)) && return;
 
 100   $self->add_full_diag($output);
 
 101   $self->{diag_per_module}{$module} = $output;
 
 103   my $parser = TAP::Parser->new({ tap => $output});
 
 106   $self->aggreg->add($module => $parser);
 
 110   $_[0]{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $_[0]->config->{send_email_to});
 
 116   return if !$self->config || !$self->config->{send_email_to};
 
 118   my $user  = $self->_email_user;
 
 119   my $email = $user ? $user->get_config_value('email') : undef;
 
 121   return unless $email;
 
 123   my ($output, $content_type) = $self->_prepare_report;
 
 125   my $mail              = Mailer->new(charset => $::locale->is_utf8 ? 'UTF-8' : Common->DEFAULT_CHARSET );
 
 126   $mail->{from}         = $self->config->{email_from};
 
 127   $mail->{to}           = $email;
 
 128   $mail->{subject}      = $self->config->{email_subject};
 
 129   $mail->{content_type} = $content_type;
 
 130   $mail->{message}      = $$output;
 
 135 sub _prepare_report {
 
 138   my $user = $self->_email_user;
 
 139   my $template = Template->new({ 'INTERPOLATE' => 0,
 
 145   return unless $template;
 
 146   my $email_template = $self->config->{email_template};
 
 147   my $filename       = $email_template || ( ($user->get_config_value('templates') || "templates/mails") . "/self_test/status_mail.txt" );
 
 148   my $content_type   = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
 
 154     database => $::myconfig{dbname},
 
 155     path     => $FindBin::Bin,
 
 159   $template->process($filename, \%params, \$output) || die $template->error;
 
 161   return (\$output, $content_type);
 
 166   $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
 
 176 SL::BackgroundJob::TelfTests - pluggable self testing
 
 180   use SL::BackgroundJob::SelfTests;
 
 181   SL::BackgroundJob::SelfTests->new->run;;