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;
137 my $err = $mail->send;
138 $self->add_errors('Mailer error #1', $err) if $err;
142 sub _prepare_report {
145 my $user = $self->_email_user;
146 my $template = Template->new({ 'INTERPOLATE' => 0,
152 return unless $template;
153 my $email_template = $self->config->{email_template};
154 my $filename = $email_template || ( (SL::DB::Default->get->templates || "templates/mails") . "/self_test/status_mail.txt" );
155 my $content_type = $filename =~ m/.html$/ ? 'text/html' : 'text/plain';
161 database => $::auth->client->{dbname},
162 client => $::auth->client->{name},
163 path => $FindBin::Bin,
164 errors => $self->errors,
168 $template->process($filename, \%params, \$output) || die $template->error;
170 return (\$output, $content_type);
175 $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
185 SL::BackgroundJob::SelfTest - pluggable self testing
189 use SL::BackgroundJob::SelfTest;
190 SL::BackgroundJob::SelfTest->new->run;;