1 package SL::BackgroundJob::SelfTest;
 
   5 use parent qw(SL::BackgroundJob::Base);
 
   9 use TAP::Parser::Aggregator;
 
  16 use SL::Locale::String qw(t8);
 
  19 use Rose::Object::MakeMethods::Generic (
 
  22    'add_modules' => { interface => 'add', hash_key => 'modules' },
 
  24    'add_errors'  => { interface => 'add', hash_key => 'errors' },
 
  26    'add_full_diag'  => { interface => 'add', hash_key => 'full_diag' },
 
  29    qw(diag tester config aggreg),
 
  34   $_[0]->create_standard_job('20 2 * * *'); # every day at 2:20 am
 
  40   $self->config($::lx_office_conf{self_test} || {});
 
  42   $self->tester(Test::Builder->new);
 
  43   $self->tester->reset; # stupid Test::Builder mplementation uses class variables
 
  44   $self->aggreg(TAP::Parser::Aggregator->new);
 
  46   $self->modules(split /\s+/, $self->config->{modules});
 
  53   return 1 unless $self->modules;
 
  55   foreach my $module ($self->modules) {
 
  56     $self->run_module($module);
 
  60     sprintf "SelfTest status: %s, passed: %s, failed: %s, unexpectedly succeeded: %s",
 
  61              $self->aggreg->get_status,
 
  62              $self->aggreg->passed,
 
  63              $self->aggreg->failed,
 
  64              $self->aggreg->todo_passed,
 
  66   # if (!$self->aggreg->all_passed || $self->config->{send_email_on_success}) {
 
  67   # all_passed is not set or calculated (anymore). it is safe to check only for probs or errors
 
  68   if ($self->aggreg->failed || $self->config->{send_email_on_success}) {
 
  72   croak t8("Unsuccessfully executed:" . join ("\n", $self->errors)) if $self->errors;
 
  77   my ($self, $module) = @_;
 
  79   # TAP usually prints out to STDOUT and STDERR, capture those for TAP::Parser
 
  82   $self->tester->output        (\$output);
 
  83   $self->tester->failure_output(\$output);
 
  84   $self->tester->todo_output   (\$output);
 
  86   # sanitize module name;
 
  87   # this allows unicode package names, which are known to be buggy in 5.10, you should avoid them
 
  88   $module =~ s/[^\w:]//g;
 
  89   $module = "SL::BackgroundJob::SelfTest::$module";
 
  92   (my $file = $module) =~ s|::|/|g;
 
  94     require $file . '.pm';
 
  96   } or $self->add_errors($::locale->text('Could not load class #1 (#2): "#3"', $module, $file, $@)) && return;
 
  99     $self->tester->subtest($module => sub {
 
 103   } or $self->add_errors($::locale->text('Could not load class #1, #2', $module, $@)) && return;
 
 105   $self->add_full_diag($output);
 
 106   $self->{diag_per_module}{$module} = $output;
 
 108   my $parser = TAP::Parser->new({ tap => $output});
 
 111   $self->aggreg->add($module => $parser);
 
 115   $_[0]{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $_[0]->config->{send_email_to});
 
 121   return if !$self->config || !$self->config->{send_email_to};
 
 123   my $user  = $self->_email_user;
 
 124   my $email = $user ? $user->get_config_value('email') : undef;
 
 126   return unless $email;
 
 128   my ($output, $content_type) = $self->_prepare_report;
 
 130   my $mail              = Mailer->new;
 
 131   $mail->{from}         = $self->config->{email_from};
 
 132   $mail->{to}           = $email;
 
 133   $mail->{subject}      = $self->config->{email_subject};
 
 134   $mail->{content_type} = $content_type;
 
 135   $mail->{message}      = $$output;
 
 140 sub _prepare_report {
 
 143   my $user = $self->_email_user;
 
 144   my $template = Template->new({ 'INTERPOLATE' => 0,
 
 150   return unless $template;
 
 151   my $email_template = $self->config->{email_template};
 
 152   my $filename       = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/self_test/status_mail.txt" );
 
 153   my $content_type   = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
 
 159     database => $::auth->client->{dbname},
 
 160     client   => $::auth->client->{name},
 
 161     path     => $FindBin::Bin,
 
 162     errors   => $self->errors,
 
 166   $template->process($filename, \%params, \$output) || die $template->error;
 
 168   return (\$output, $content_type);
 
 173   $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
 
 183 SL::BackgroundJob::SelfTest - pluggable self testing
 
 187   use SL::BackgroundJob::SelfTest;
 
 188   SL::BackgroundJob::SelfTest->new->run;;