line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: ReportPlugin.pm,v 1.10 2003/09/05 19:32:18 m_ilya Exp $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package HTTP::WebTest::ReportPlugin; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
HTTP::WebTest::ReportPlugin - Subclass for HTTP::WebTest report plugins. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Not applicable. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
This is a subclass of L. |
16
|
|
|
|
|
|
|
L report plugin classes can inherit from this |
17
|
|
|
|
|
|
|
class. It handles some test parameters common to report plugins by |
18
|
|
|
|
|
|
|
providing implementation of the method C. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=cut |
21
|
|
|
|
|
|
|
|
22
|
11
|
|
|
11
|
|
1215
|
use strict; |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
374
|
|
23
|
|
|
|
|
|
|
|
24
|
11
|
|
|
11
|
|
11120
|
use Net::SMTP; |
|
11
|
|
|
|
|
8339729
|
|
|
11
|
|
|
|
|
1340
|
|
25
|
|
|
|
|
|
|
|
26
|
11
|
|
|
11
|
|
822
|
use HTTP::WebTest::Utils qw(make_access_method); |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
805
|
|
27
|
|
|
|
|
|
|
|
28
|
11
|
|
|
11
|
|
67
|
use base qw(HTTP::WebTest::Plugin); |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
21437
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 TEST PARAMETERS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=for pod_merge copy params |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 output_ref |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
I |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
A reference to a scalar that accumulates text of test report. If this |
39
|
|
|
|
|
|
|
test parameter is specified then value of test parameter C is |
40
|
|
|
|
|
|
|
ignore. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
This parameter can be used only when passing the test parameters |
43
|
|
|
|
|
|
|
as arguments from a calling Perl script. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 fh_out |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
I |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
A filehandle (or anything else that supports C) to use for test |
50
|
|
|
|
|
|
|
report output. This parameter is ignored if test parameter |
51
|
|
|
|
|
|
|
C is specified also. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
This parameter can be used only when passing the test parameters |
54
|
|
|
|
|
|
|
as arguments from a calling Perl script. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 mail |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
I |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Option to e-mail output to one or more addresses specified by |
61
|
|
|
|
|
|
|
C test parameter. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 mail_success_subject |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
I |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Sets C header for test report e-mails when all tests are |
68
|
|
|
|
|
|
|
passed successfully. In this string some character sequences have |
69
|
|
|
|
|
|
|
special meaning (see C parameter for their |
70
|
|
|
|
|
|
|
description). |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head3 Default Value |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
C |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 mail_failure_subject |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
I |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Sets C header for test report e-mails when some tests |
81
|
|
|
|
|
|
|
fail. In this string some character sequences have special meaning: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=over 4 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item %f |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
the number of failed tests |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=item %s |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
the number of successful tests |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item %t |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
the total number of tests |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item %% |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
replaced with single C<%> |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=back |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head3 Default Value |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
C |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head2 mail_addresses |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
I |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
A list of e-mail addresses where report will be send (if sending |
112
|
|
|
|
|
|
|
report is enabled with C test parameter). |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=over 4 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item * all |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Send e-mail containing test results. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item * errors |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Send e-mail only if one or more tests fails. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * no |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Do not send e-mail. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head3 Default value |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
C |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=back |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 mail_server |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
I |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Fully-qualified name of of the mail server (e.g., mailhost.mycompany.com). |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head3 Default value |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
C |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 mail_from |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
I |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Sets From: header for test report e-mails. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head3 Default Value |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Name of user under which test script runs. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# declare some supported test params |
157
|
|
|
|
|
|
|
sub param_types { |
158
|
1026
|
|
|
1026
|
1
|
45444
|
return q(output_ref stringref |
159
|
|
|
|
|
|
|
fh_out anything |
160
|
|
|
|
|
|
|
mail_addresses list('scalar','...') |
161
|
|
|
|
|
|
|
mail scalar |
162
|
|
|
|
|
|
|
mail_server scalar |
163
|
|
|
|
|
|
|
mail_from scalar |
164
|
|
|
|
|
|
|
test_name scalar |
165
|
|
|
|
|
|
|
mail_success_subject scalar |
166
|
|
|
|
|
|
|
mail_failure_subject scalar); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 CLASS METHODS |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=cut |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 test_output () |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head3 Returns |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Returns a reference to buffer that stores copy of test output. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
*test_output = make_access_method('TEST_OUTPUT', sub { my $s = ''; \$s } ); |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 print (@array) |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Prints data in <@array> either into string (if test parameter |
186
|
|
|
|
|
|
|
C is set) or to some filehandle (if test parameter C |
187
|
|
|
|
|
|
|
is set) or to standard output. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Also stores this data into buffer accessible via method C. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub print { |
194
|
422
|
|
|
422
|
1
|
588
|
my $self = shift; |
195
|
|
|
|
|
|
|
|
196
|
422
|
|
|
|
|
1432
|
$self->global_validate_params(qw(output_ref fh_out)); |
197
|
|
|
|
|
|
|
|
198
|
422
|
|
|
|
|
1500
|
my $output_ref = $self->global_test_param('output_ref'); |
199
|
422
|
|
|
|
|
1507
|
my $fh_out = $self->global_test_param('fh_out'); |
200
|
|
|
|
|
|
|
|
201
|
422
|
|
|
|
|
1597
|
my $text = join '', @_; |
202
|
|
|
|
|
|
|
|
203
|
422
|
|
|
|
|
701
|
${$self->test_output} .= $text; |
|
422
|
|
|
|
|
1661
|
|
204
|
|
|
|
|
|
|
|
205
|
422
|
100
|
|
|
|
982
|
if(defined $output_ref) { |
|
|
50
|
|
|
|
|
|
206
|
417
|
|
|
|
|
458
|
${$output_ref} .= $text; |
|
417
|
|
|
|
|
2626
|
|
207
|
|
|
|
|
|
|
} elsif(defined $fh_out) { |
208
|
5
|
|
|
|
|
33
|
print $fh_out $text; |
209
|
|
|
|
|
|
|
} else { |
210
|
0
|
|
|
|
|
0
|
print $text; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head2 start_tests () |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
This method is called by L at the beginning |
217
|
|
|
|
|
|
|
of the test run. Its implementation in this class initializes the |
218
|
|
|
|
|
|
|
output buffer for the test report. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
If you redefine this method in a subclass, be sure to call |
221
|
|
|
|
|
|
|
the superclass method in the new method: |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub start_tests { |
224
|
|
|
|
|
|
|
my $self = shift; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
$self->SUPER::start_tests; |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# your code here |
229
|
|
|
|
|
|
|
.... |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=cut |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sub start_tests { |
235
|
68
|
|
|
68
|
1
|
310
|
my $self = shift; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
# reset temporary output storage |
238
|
68
|
|
|
|
|
409
|
$self->test_output(undef); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 end_tests () |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
This method is called by L at the end of |
244
|
|
|
|
|
|
|
a test run. Its implementation in this class e-mails the test report |
245
|
|
|
|
|
|
|
according test parameters C. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
If you redefine this method in subclass be sure to call |
248
|
|
|
|
|
|
|
the superclass method in the new method: |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
sub end_tests { |
251
|
|
|
|
|
|
|
my $self = shift; |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# your code here |
254
|
|
|
|
|
|
|
.... |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
$self->SUPER::end_tests; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub end_tests { |
262
|
64
|
|
|
64
|
1
|
124
|
my $self = shift; |
263
|
|
|
|
|
|
|
|
264
|
64
|
50
|
|
|
|
364
|
if($self->_email_report_is_expected) { |
265
|
0
|
|
|
|
|
0
|
$self->_send_email_report; |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
# check if we need to mail report |
270
|
|
|
|
|
|
|
sub _email_report_is_expected { |
271
|
69
|
|
|
69
|
|
434
|
my $self = shift; |
272
|
|
|
|
|
|
|
|
273
|
69
|
|
|
|
|
348
|
$self->global_validate_params(qw(mail)); |
274
|
|
|
|
|
|
|
|
275
|
69
|
|
|
|
|
276
|
my $mail = $self->global_test_param('mail'); |
276
|
|
|
|
|
|
|
|
277
|
69
|
100
|
|
|
|
1419
|
return unless defined $mail; |
278
|
4
|
50
|
66
|
|
|
18
|
return unless $mail eq 'all' or $mail eq 'errors'; |
279
|
4
|
100
|
100
|
|
|
18
|
return if $mail eq 'errors' and $self->webtest->have_succeed; |
280
|
|
|
|
|
|
|
|
281
|
3
|
|
|
|
|
65
|
return 1; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
# sends test report on email |
285
|
|
|
|
|
|
|
sub _send_email_report { |
286
|
7
|
|
|
7
|
|
15690
|
my $self = shift; |
287
|
|
|
|
|
|
|
|
288
|
7
|
|
|
|
|
32
|
$self->global_validate_params(qw(mail_addresses mail_server mail_from)); |
289
|
|
|
|
|
|
|
|
290
|
7
|
|
|
|
|
22
|
my $mail_addresses = $self->global_test_param('mail_addresses'); |
291
|
7
|
|
|
|
|
23
|
my $mail_server = $self->global_test_param('mail_server', 'localhost'); |
292
|
7
|
|
|
|
|
23
|
my $mail_from = $self->global_test_param('mail_from'); |
293
|
|
|
|
|
|
|
|
294
|
7
|
|
|
|
|
26
|
my $smtp = Net::SMTP->new($mail_server); |
295
|
7
|
50
|
|
|
|
30
|
die "HTTP::WebTest: Can't create Net::SMTP object" |
296
|
|
|
|
|
|
|
unless defined $smtp; |
297
|
|
|
|
|
|
|
|
298
|
7
|
|
50
|
|
|
1719
|
my $from = $mail_from || getlogin() || getpwuid($<) || 'nobody'; |
299
|
|
|
|
|
|
|
|
300
|
7
|
|
|
|
|
28
|
$self->_smtp_cmd($smtp, 'mail', $from); |
301
|
7
|
|
|
|
|
18
|
$self->_smtp_cmd($smtp, 'to', @$mail_addresses); |
302
|
7
|
|
|
|
|
17
|
$self->_smtp_cmd($smtp, 'data'); |
303
|
7
|
|
|
|
|
26
|
$self->_smtp_cmd($smtp, 'datasend', "From: $from\n"); |
304
|
|
|
|
|
|
|
{ |
305
|
7
|
|
|
|
|
9
|
my $mail_addresses = join ', ', @$mail_addresses; |
|
7
|
|
|
|
|
14
|
|
306
|
7
|
|
|
|
|
20
|
$self->_smtp_cmd($smtp, 'datasend', "To: $mail_addresses\n"); |
307
|
|
|
|
|
|
|
} |
308
|
7
|
|
|
|
|
23
|
$self->_smtp_cmd($smtp, 'datasend', |
309
|
|
|
|
|
|
|
'Subject: ' . $self->_subject_header . "\n"); |
310
|
7
|
|
|
|
|
21
|
$self->_smtp_cmd($smtp, 'datasend', "\n"); |
311
|
7
|
|
|
|
|
11
|
$self->_smtp_cmd($smtp, 'datasend', ${$self->test_output}); |
|
7
|
|
|
|
|
24
|
|
312
|
7
|
|
|
|
|
18
|
$self->_smtp_cmd($smtp, 'dataend'); |
313
|
7
|
|
|
|
|
18
|
$self->_smtp_cmd($smtp, 'quit'); |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# returns value of subject header for email report |
317
|
|
|
|
|
|
|
sub _subject_header { |
318
|
7
|
|
|
7
|
|
7
|
my $self = shift; |
319
|
|
|
|
|
|
|
|
320
|
7
|
|
|
|
|
21
|
$self->global_validate_params(qw(mail_success_subject mail_failure_subject)); |
321
|
|
|
|
|
|
|
|
322
|
7
|
|
|
|
|
55
|
my $success_subject |
323
|
|
|
|
|
|
|
= $self->global_test_param('mail_success_subject', |
324
|
|
|
|
|
|
|
'Web tests succeeded'); |
325
|
7
|
|
|
|
|
25
|
my $fail_subject |
326
|
|
|
|
|
|
|
= $self->global_test_param('mail_failure_subject', |
327
|
|
|
|
|
|
|
'WEB TESTS FAILED! FOUND %f ERROR(S)'); |
328
|
|
|
|
|
|
|
|
329
|
7
|
|
|
|
|
20
|
my %replace = ('f' => $self->webtest->num_fail, |
330
|
|
|
|
|
|
|
's' => $self->webtest->num_succeed, |
331
|
|
|
|
|
|
|
't' => ($self->webtest->num_fail + |
332
|
|
|
|
|
|
|
$self->webtest->num_succeed), |
333
|
|
|
|
|
|
|
'%' => '%' |
334
|
|
|
|
|
|
|
); |
335
|
|
|
|
|
|
|
|
336
|
7
|
100
|
|
|
|
334
|
my $subject = ($self->webtest->have_succeed ? |
337
|
|
|
|
|
|
|
$success_subject : |
338
|
|
|
|
|
|
|
$fail_subject); |
339
|
|
|
|
|
|
|
|
340
|
7
|
50
|
|
|
|
327
|
$subject =~ s/%(.)/exists $replace{$1} ? $replace{$1} : '%' . $1/ge; |
|
21
|
|
|
|
|
93
|
|
341
|
|
|
|
|
|
|
|
342
|
7
|
|
|
|
|
38
|
return $subject; |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
# simple helper method that automates error handling |
346
|
|
|
|
|
|
|
sub _smtp_cmd { |
347
|
70
|
|
|
70
|
|
79
|
my $self = shift; |
348
|
70
|
|
|
|
|
91
|
my $smtp = shift; |
349
|
70
|
|
|
|
|
74
|
my $cmd = shift; |
350
|
|
|
|
|
|
|
|
351
|
70
|
|
|
|
|
303
|
my $ret = $smtp->$cmd(@_); |
352
|
|
|
|
|
|
|
|
353
|
70
|
50
|
|
|
|
3325
|
unless($ret) { |
354
|
0
|
|
|
|
|
|
my $msg = $smtp->message; |
355
|
0
|
|
|
|
|
|
die "HTTP::WebTest: mail error for command $cmd: $msg"; |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=head1 COPYRIGHT |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
Copyright (c) 2001-2003 Ilya Martynov. All rights reserved. |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
364
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head1 SEE ALSO |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
L |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
L |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
L |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
L |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
=cut |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
1; |