SelfTests
[kivitendo-erp.git] / SL / BackgroundJob / SelfTest.pm
1 package SL::BackgroundJob::SelfTest;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use Test::Builder;
8 use TAP::Parser;
9 use TAP::Parser::Aggregator;
10 use Sys::Hostname;
11 use FindBin;
12
13 use SL::DB::AuthUser;
14 use SL::Common;
15
16 use Rose::Object::MakeMethods::Generic (
17   array => [
18    'modules'     => {},
19    'add_modules' => { interface => 'add', hash_key => 'modules' },
20    'errors'      => {},
21    'add_errors'  => { interface => 'add', hash_key => 'errors' },
22    'full_diag'      => {},
23    'add_full_diag'  => { interface => 'add', hash_key => 'full_diag' },
24   ],
25   scalar => [
26    qw(diag tester config aggreg),
27   ],
28 );
29
30 sub create_job {
31   $_[0]->create_standard_job('20 2 * * *'); # every day at 2:20 am
32 }
33
34 sub setup {
35   my ($self) = @_;
36
37   $self->config($::lx_office_conf{self_test} || {});
38
39   $self->tester(Test::Builder->new);
40   $self->aggreg(TAP::Parser::Aggregator->new);
41
42   $self->modules(split /\s+/, $self->config->{modules});
43 }
44
45 sub run {
46   my $self        = shift;
47   $self->setup;
48
49   return 1 unless $self->modules;
50
51   foreach my $module ($self->modules) {
52     $self->run_module($module);
53   }
54
55   $self->log(
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,
61   );
62
63   if (!$self->aggreg->all_passed || $self->config->{send_email_on_success}) {
64     $self->_send_email;
65   }
66
67   return 1;
68 }
69
70 sub run_module {
71   my ($self, $module) = @_;
72
73   # TAP usually prints out to STDOUT and STDERR, capture those for TAP::Parser
74   my $output;
75
76   $self->tester->output        (\$output);
77   $self->tester->failure_output(\$output);
78   $self->tester->todo_output   (\$output);
79
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";
84
85   # try to load module;
86   (my $file = $module) =~ s|::|/|g;
87   eval {
88     require $file . '.pm';
89     1
90   } or $self->add_errors($::locale->text('Could not load class #1 (#2): "#3"', $module, $file, $@)) && return;
91
92   eval {
93     my $worker = $module->new;
94     $worker->tester($self->tester);
95
96     $worker->run;
97     1;
98   } or $self->add_errors($::locale->text('Could not load class #1, #2', $module, $@)) && return;
99
100   $self->add_full_diag($output);
101   $self->{diag_per_module}{$module} = $output;
102
103   my $parser = TAP::Parser->new({ tap => $output});
104   $parser->run;
105
106   $self->aggreg->add($module => $parser);
107 }
108
109 sub _email_user {
110   $_[0]{email_user} ||= SL::DB::Manager::AuthUser->find_by(login => $_[0]->config->{send_email_to});
111 }
112
113 sub _send_email {
114   my ($self) = @_;
115
116   return if !$self->config || !$self->config->{send_email_to};
117
118   my $user  = $self->_email_user;
119   my $email = $user ? $user->get_config_value('email') : undef;
120
121   return unless $email;
122
123   my ($output, $content_type) = $self->_prepare_report;
124
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;
131
132   $mail->send;
133 }
134
135 sub _prepare_report {
136   my ($self) = @_;
137
138   my $user = $self->_email_user;
139   my $template = Template->new({ 'INTERPOLATE' => 0,
140                                  'EVAL_PERL'   => 0,
141                                  'ABSOLUTE'    => 1,
142                                  'CACHE_SIZE'  => 0,
143                                });
144
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';
149
150
151   my %params = (
152     SELF     => $self,
153     host     => hostname,
154     database => $::myconfig{dbname},
155     path     => $FindBin::Bin,
156   );
157
158   my $output;
159   $template->process($filename, \%params, \$output) || die $template->error;
160
161   return (\$output, $content_type);
162 }
163
164 sub log {
165   my $self = shift;
166   $::lxdebug->message(0, "[" . __PACKAGE__ . "] @_") if $self->config->{log_to_file};
167 }
168
169
170 1;
171
172 __END__
173
174 =head1 NAME
175
176 SL::BackgroundJob::TelfTests - pluggable self testing
177
178 =head1 SYNOPSIS
179
180   use SL::BackgroundJob::SelfTests;
181   SL::BackgroundJob::SelfTests->new->run;;
182
183 =head1 DESCRIPTION
184
185
186
187 =head1 FUNCTIONS
188
189 =head1 BUGS
190
191 =head1 AUTHOR
192
193 =cut