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 module_nr),
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 $self->module_nr(($self->module_nr || 0) + 1);
95 (my $file = $module) =~ s|::|/|g;
97 require $file . '.pm';
99 } or $self->add_errors($::locale->text('Could not load class #1 (#2): "#3"', $module, $file, $@)) && return;
102 $self->tester->subtest($module => sub {
106 } or $self->add_errors($::locale->text('Could not load class #1, #2', $module, $@)) && return;
108 $self->add_full_diag($output);
109 $self->{diag_per_module}{$self->module_nr . ': ' . $module} = $output;
111 my $parser = TAP::Parser->new({ tap => $output});
114 $self->aggreg->add($module => $parser);
118 $_[0]{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $_[0]->config->{send_email_to});
124 return if !$self->config || !$self->config->{send_email_to};
126 my $user = $self->_email_user;
127 my $email = $user ? $user->get_config_value('email') : undef;
129 return unless $email;
131 my ($output, $content_type) = $self->_prepare_report;
133 my $mail = Mailer->new;
134 $mail->{from} = $self->config->{email_from};
135 $mail->{to} = $email;
136 $mail->{subject} = $self->config->{email_subject};
137 $mail->{content_type} = $content_type;
138 $mail->{message} = $$output;
140 my $err = $mail->send;
141 $self->add_errors('Mailer error #1', $err) if $err;
145 sub _prepare_report {
148 my $template = Template->new({ 'INTERPOLATE' => 0,
154 return unless $template;
155 my $email_template = $self->config->{email_template};
156 my $filename = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/self_test/status_mail.txt" );
157 my $content_type = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
163 database => $::auth->client->{dbname},
164 client => $::auth->client->{name},
165 path => $FindBin::Bin,
166 errors => $self->errors,
170 $template->process($filename, \%params, \$output) || die $template->error;
172 return (\$output, $content_type);
177 $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
187 SL::BackgroundJob::SelfTest - pluggable self testing
191 use SL::BackgroundJob::SelfTest;
192 SL::BackgroundJob::SelfTest->new->run;;