blib/lib/Test/Clustericious/Log.pm

filter: Statementstmt 90.4% 160 / 177 Branchbran 77.2% 51 / 66 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 95.2% 20 / 21 Podpod 100% 4 / 4 totaltotal 87.9% 241 / 274 CC 41 SCAR 22.8
CC
41
Cov
87%
_summary
20.0
import
20.0
log_like
6.0
CRAP 9.8
« blib/lib/Clustericious/Plugin/AutodataHandler.pm ↑ Summary blib/lib/Clustericious/Command/generate/app.pm »
1 package Test::Clustericious::Log;
2  
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Test2::Plugin::FauxHomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use File::Glob qw( bsd_glob );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Clustericious::Log ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 35 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 35 ▶use Test2::API qw( context );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 35 ▶use YAML::XS qw( Dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 35 our @EXPORT = qw( log_events log_context log_like log_unlike );
15 35 our %EXPORT_TAGS = ( all => \@EXPORT );
16  
17 # ABSTRACT: Clustericious logging in tests.
18 35 our $VERSION = '1.29'; # VERSION
19  
20  
21 sub log_events
22 34 ▶{
Subroutine log_events: called CC=1 SCAR=0.0
Pod documented
23 34 @{ Test::Clustericious::Log::Appender->new->{list} };
24 }
25  
26  
27 sub log_context (&)
28 10 ▶{
Subroutine log_context: called CC=4 SCAR=13.9
Pod documented
29 10 my($code) = @_;
30 10 my $old = Test::Clustericious::Log::Appender->new->{list};
31 10 local Test::Clustericious::Log::Appender->new->{list} = [];
32
33 10 my @ret;
34 my $ret;
35
36 10 ▶ if(wantarray)
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 3 7 10
elsif (defined wantarray) { } 1 6 7
37 {
38 3 @ret = $code->()
39 }
40 elsif(defined wantarray)
41 {
42 1 $ret = $code->();
43 }
44 else
45 {
46 6 $code->();
47 }
48
49 10 push @$old, @{ Test::Clustericious::Log::Appender->new->{list} };
50  
51 10 ▶ wantarray ? @ret : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 7 10
52 }
53  
54  
55 sub _event_match
56 78 ▶{
Subroutine _event_match: called CC=5 SCAR=16.1
57 78 my($pattern, $event) = @_;
58  
59 78 my $match = 1;
60 78 foreach my $key (keys %$pattern)
61 {
62 93 my $pattern = $pattern->{$key};
63 93 ▶ if(ref $pattern eq 'Regexp')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq 'Regexp') { } 56 37 93
64 {
65 56 ▶ $match = 0 unless $event->{$key} =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{$key} =~ /$pattern/ 47 9 56
66 }
67 else
68 {
69 37 ▶ $match = 0 unless $event->{$key} eq $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{$key} eq $pattern 18 19 37
70 }
71 }
72
73 78 $match;
74 }
75  
76  
77 sub log_like ($;$)
78 12 ▶{
Subroutine log_like: called CC=6 SCAR=17.9
Pod documented
79 12 my($pattern, $message) = @_;
80
81 12 ▶ $message ||= "log matches pattern";
Condition100%2/2
$message ||= "log matches pattern"
l!l
57
MC/DC100%1/1
$message ||= "log matches pattern"
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
82 12 ▶ $pattern = { message => $pattern } unless ref $pattern eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pattern eq "HASH" 8 4 12
83
84 12 my $ctx = context();
85 12 my $ok = 0;
86
87 12 foreach my $event (log_events)
88 {
89 55 ▶ if(_event_match($pattern, $event))
Branch: 1 branch
BranchTrueFalseTotal
if (_event_match($pattern, $event)) 11 44 55
90 {
91 11 $ok = 1;
92 11 last;
93 }
94 }
95
96 12 $ctx->ok($ok, $message);
97  
98 12 ▶ unless($ok)
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 11 12
99 {
100
101 1 $ctx->diag("None of the events matched the pattern:");
102 1 $ctx->diag(
103 Dump({
104 events => [log_events],
105 pattern => $pattern,
106 })
107 );
108 }
109
110 12 $ctx->release;
111
112 12 $ok;
113 }
114  
115 sub log_unlike ($;$)
116 8 ▶{
Subroutine log_unlike: called CC=6 SCAR=17.9
Pod documented
117 8 my($pattern, $message) = @_;
118
119 8 ▶ $message ||= "log does not match pattern";
Condition100%2/2
$message ||= "log does not match pattern"
l!l
44
MC/DC100%1/1
$message ||= "log does not match pattern"
$message
Truth table100%2/2
A
A= $message
Aresult
11
00
120 8 ▶ $pattern = { message => $pattern } unless ref $pattern eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pattern eq "HASH" 6 2 8
121  
122 8 my $ctx = context();
123 8 my @match;
124
125 8 foreach my $event (log_events)
126 {
127 23 ▶ if(_event_match($pattern, $event))
Branch: 1 branch
BranchTrueFalseTotal
if (_event_match($pattern, $event)) 3 20 23
128 {
129 3 push @match, $event;
130 }
131 }
132
133 8 $ctx->ok(!scalar @match, $message);
134
135 8 foreach my $match (@match)
136 {
137 3 $ctx->diag("This event matched, but should not have:");
138 3 $ctx->diag(
139 Dump({
140 event => $match,
141 pattern => $pattern,
142 })
143 );
144 }
145  
146 8 $ctx->release;
147
148 8 !scalar @match;
149 }
150  
151  
152 sub import
153 43 ▶{
Subroutine import: called CC=19 SCAR=30.0
154 43 my($class) = shift;
155  
156 # first caller wins
157 43 state $counter = 0;
158 43 ▶ if($counter++)
Branch: 1 branch
BranchTrueFalseTotal
if ($counter++) 9 34 43
159 {
160 9 my $caller = caller;
161 9 ▶ unless($caller eq 'Test::Clustericious::Cluster')
Branch: 1 branch
BranchTrueFalseTotal
unless ($caller eq "Test::Clustericious::Cluster") 0 9 9
162 {
163 0 my $ctx = context();
164 0 $ctx->diag("you must use Test::Clustericious::Log before Test::Clustericious::Cluster");
165 0 $ctx->release;
166 }
167 9 return;
168 }
169  
170 34 my $home = bsd_glob('~');
171 34 ▶ mkdir "$home/etc" unless -d "$home/etc";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$home/etc" 12 22 34
172 34 ▶ mkdir "$home/log" unless -d "$home/log";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$home/log" 34 0 34
173  
174 34 my $config = {
175 FileX => [ 'TRACE', 'FATAL' ],
176 NoteX => [ 'DEBUG', 'WARN' ],
177 DiagX => [ 'ERROR', 'FATAL' ],
178 TestX => [ 'TRACE', 'FATAL' ],
179 };
180  
181 34 my $args;
182 34 ▶ if(@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 34 34
183 {
184 0 die;
185 }
186 else
187 {
188 34 $args = { @_ };
189 }
190
191 34 foreach my $type (qw( file note diag ))
192 {
193 102 ▶ if(defined $args->{$type})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{$type}) 6 96 102
194 {
195 6 my $name = ucfirst($type) . 'X';
196 6 ▶ if($args->{$type} =~ /^(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)(..(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)|)$/)
Branch: 3 branches
BranchTrueFalseTotal
if ($args->{$type} =~ /^(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)(..(TRACE|DEBUG|INFO|WARN|ERROR|FATAL)|)$/) { } 2 4 6
elsif ($args->{$type} eq 'NONE') { } 3 1 4
elsif ($args->{$type} eq 'ALL') { } 1 0 1
197 {
198 2 my($min,$max) = ($1,$3);
199 2 ▶ $max = $min unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 1 1 2
200 2 $config->{$name} = [ $min, $max ];
201 }
202 elsif($args->{$type} eq 'NONE')
203 {
204 3 delete $config->{$name};
205 }
206 elsif($args->{$type} eq 'ALL')
207 {
208 1 $config->{$name} = [ 'TRACE', 'FATAL' ];
209 }
210 else
211 {
212 0 carp "illegal log range: " . $args->{$type};
213 }
214 }
215 }
216
217 34 open my $fh, '>', "$home/etc/log4perl.conf";
218  
219 34 print $fh "log4perl.rootLogger=TRACE, ";
220 34 ▶ print $fh "FileX, " if defined $config->{FileX};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"FileX"} 34 0 34
221 34 ▶ print $fh "NoteX, " if defined $config->{NoteX};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"NoteX"} 34 0 34
222 34 ▶ print $fh "DiagX, " if defined $config->{DiagX};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"DiagX"} 31 3 34
223 34 ▶ print $fh "TestX, " if defined $config->{TestX};
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"TestX"} 34 0 34
224 34 print $fh "\n";
225
226 34 ▶ while(my($appender, $levels) = each %$config)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$config) 133 34 167
227 {
228 133 my($min, $max) = @{ $levels };
229 133 print $fh "log4perl.filter.Match$appender = Log::Log4perl::Filter::LevelRange\n";
230 133 print $fh "log4perl.filter.Match$appender.LevelMin = $min\n";
231 133 print $fh "log4perl.filter.Match$appender.LevelMax = $max\n";
232 133 print $fh "log4perl.filter.Match$appender.AcceptOnMatch = true\n";
233 }
234
235 34 print $fh "log4perl.appender.FileX=Log::Log4perl::Appender::File\n";
236 34 print $fh "log4perl.appender.FileX.filename=$home/log/test.log\n";
237 34 print $fh "log4perl.appender.FileX.mode=append\n";
238 34 print $fh "log4perl.appender.FileX.layout=PatternLayout\n";
239 34 print $fh "log4perl.appender.FileX.layout.ConversionPattern=[%P %p{1} %rms] %F:%L %m%n\n";
240 34 print $fh "log4perl.appender.FileX.Filter=MatchFileX\n";
241  
242 34 print $fh "log4perl.appender.TestX=Test::Clustericious::Log::Appender\n";
243 34 print $fh "log4perl.appender.TestX.layout=PatternLayout\n";
244 34 print $fh "log4perl.appender.TestX.layout.ConversionPattern=%m\n";
245 34 print $fh "log4perl.appender.TestX.Filter=MatchTestX\n";
246
247 34 print $fh "log4perl.appender.NoteX=Log::Log4perl::Appender::TAP\n";
248 34 print $fh "log4perl.appender.NoteX.method=note\n";
249 34 print $fh "log4perl.appender.NoteX.layout=PatternLayout\n";
250 34 print $fh "log4perl.appender.NoteX.layout.ConversionPattern=%5p %m%n\n";
251 34 print $fh "log4perl.appender.NoteX.Filter=MatchNoteX\n";
252  
253 34 print $fh "log4perl.appender.DiagX=Log::Log4perl::Appender::TAP\n";
254 34 print $fh "log4perl.appender.DiagX.method=diag\n";
255 34 print $fh "log4perl.appender.DiagX.layout=PatternLayout\n";
256 34 print $fh "log4perl.appender.DiagX.layout.ConversionPattern=%5p %m%n\n";
257 34 print $fh "log4perl.appender.DiagX.Filter=MatchDiagX\n";
258
259 34 close $fh;
260  
261 34 ▶ if($args->{import})
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"import"}) 2 32 34
262 {
263 0 ▶ @_ = ($class, ref $args->{import} ? @{ $args->{import} } : ($args->{import}));
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'import'} ? : 0 2 2
264 2 goto &Exporter::import;
265 }
266  
267 }
268  
269 sub _summary
270 0 ▶{
Subroutine _summary: not called CC=4 SCAR=30.0
271 0 my($ctx, $real, $new) = @_;
272
273 0 my $home = bsd_glob('~');
274
275 0 my $hub = $ctx->hub;
276
277 0 ▶ if($hub->failed)
Branch: 1 branch
BranchTrueFalseTotal
if ($hub->failed) 0 0 0
278 {
279 0 ▶ if($ENV{CLUSTERICIOUS_LOG_SPEW_OFF})
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'CLUSTERICIOUS_LOG_SPEW_OFF'}) { } 0 0 0
elsif (-r "$home/log/test.log") { } 0 0 0
280 {
281 0 $ctx->diag("not spewing the entire log (unset CLUSTERICIOUS_LOG_SPEW_OFF to turn back on)");
282 }
283 elsif(-r "$home/log/test.log")
284 {
285 0 $ctx->diag("detailed log");
286 0 open my $fh, '<', "$home/log/test.log";
287 0 $ctx->diag(<$fh>);
288 0 close $fh;
289 }
290 else
291 {
292 0 $ctx->diag("no detailed log");
293 }
294 }
295 }
296  
297 package Test::Clustericious::Log::Appender;
298  
299 35 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
300 35 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
301 35 our @ISA = qw( Log::Log4perl::Appender );
302  
303 sub new
304 86 ▶{
Subroutine new: called CC=3 SCAR=11.0
305 86 my($class) = @_;
306
307 86 ▶ Carp::croak "not subclassable"
Branch: 1 branch
BranchTrueFalseTotal
unless $class eq __PACKAGE__ 0 86 86
308 unless $class eq __PACKAGE__;
309
310 86 state $self;
311
312 86 ▶ unless(defined $self)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 22 64 86
313 {
314 22 $self = bless { list => [] }, __PACKAGE__;
315 }
316
317 86 $self;
318 }
319  
320 sub log
321 179 ▶{
Subroutine log: called CC=1 SCAR=0.0
322 179 my($self, %args) = @_;
323
324 179 push @{ $self->{list} }, Storable::dclone(\%args);
325  
326 179 ();
327 }
328  
329 35 1;
330  
331 __END__
« blib/lib/Clustericious/Plugin/AutodataHandler.pm ↑ Summary blib/lib/Clustericious/Command/generate/app.pm »