/usr/share/otrs/Kernel/System/UnitTest.pm is in otrs2 6.0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | # --
# Copyright (C) 2001-2018 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::System::UnitTest;
use strict;
use warnings;
use File::stat;
use Storable();
use Term::ANSIColor();
use Kernel::System::VariableCheck qw(IsHashRefWithData IsArrayRefWithData);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Encode',
'Kernel::System::JSON',
'Kernel::System::Main',
'Kernel::System::SupportDataCollector',
'Kernel::System::WebUserAgent',
);
=head1 NAME
Kernel::System::UnitTest - functions to run all or some OTRS unit tests
=head1 PUBLIC INTERFACE
=head2 new()
create unit test object. Do not use it directly, instead use:
my $UnitTestObject = $Kernel::OM->Get('Kernel::System::UnitTest');
=cut
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {};
bless( $Self, $Type );
$Self->{Debug} = $Param{Debug} || 0;
$Self->{ANSI} = $Param{ANSI};
return $Self;
}
=head2 Run()
run all or some tests located in C<scripts/test/**/*.t> and print the result.
$UnitTestObject->Run(
Name => ['JSON', 'User'], # optional, execute certain test files only
Directory => 'Selenium', # optional, execute tests in subdirectory
Verbose => 1, # optional (default 0), only show result details for all tests, not just failing
SubmitURL => $URL, # optional, send results to unit test result server
SubmitAuth => '0abc86125f0fd37baae' # optional authentication string for unit test result server
SubmitResultAsExitCode => 1, # optional, specify if exit code should not indicate if tests were ok/not ok, but if submission was successful instead
JobID => 12, # optional job ID for unit test submission to server
Scenario => 'OTRS 6 git', # optional scenario identifier for unit test submission to server
PostTestScripts => ['...'], # Script(s) to execute after a test has been run.
# You can specify %File%, %TestOk% and %TestNotOk% as dynamic arguments.
PreSubmitScripts => ['...'], # Script(s) to execute after all tests have been executed
# and the results are about to be sent to the server.
);
Please note that the individual test files are not executed in the main process,
but instead in separate forked child processes which are controlled by L<Kernel::System::UnitTest::Driver>.
Their results will be transmitted to the main process via a local file.
=cut
sub Run {
my ( $Self, %Param ) = @_;
$Self->{Verbose} = $Param{Verbose};
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
my $Product = $ConfigObject->Get('Product') . " " . $ConfigObject->Get('Version');
my $Home = $ConfigObject->Get('Home');
my $Directory = "$Home/scripts/test";
if ( $Param{Directory} ) {
$Directory .= "/$Param{Directory}";
$Directory =~ s/\.//g;
}
my @TestsToExecute = @{ $Param{Tests} // [] };
# Use non-overridden time() function.
my $StartTime = CORE::time; ## no critic;
my @Files = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $Directory,
Filter => '*.t',
Recursive => 1,
);
FILE:
for my $File (@Files) {
# check if only some tests are requested
if ( @TestsToExecute && !grep { $File =~ /\/\Q$_\E\.t$/smx } @TestsToExecute ) {
next FILE;
}
$Self->_HandleFile(
PostTestScripts => $Param{PostTestScripts},
File => $File,
);
}
# Use non-overridden time() function.
my $Duration = CORE::time - $StartTime; ## no critic
my $Host = $ConfigObject->Get('FQDN');
print "=====================================================================\n";
print $Self->_Color( 'yellow', $Host ) . " ran tests in " . $Self->_Color( 'yellow', "${Duration}s" );
print " for " . $Self->_Color( 'yellow', $Product ) . "\n";
if ( $Self->{TestCountNotOk} ) {
print $Self->_Color( 'red', "$Self->{TestCountNotOk} tests failed.\n" );
print " FailedTests:\n";
FAILEDFILE:
for my $FailedFile ( @{ $Self->{NotOkInfo} || [] } ) {
my ( $File, @Tests ) = @{ $FailedFile || [] };
next FAILEDFILE if !@Tests;
print sprintf " %s #%s\n", $File, join ", ", @Tests;
}
}
elsif ( $Self->{TestCountOk} ) {
print $Self->_Color( 'green', "All $Self->{TestCountOk} tests passed.\n" );
}
else {
print $Self->_Color( 'yellow', "No tests executed.\n" );
}
if ( $Param{SubmitURL} ) {
for my $PreSubmitScript ( @{ $Param{PreSubmitScripts} // [] } ) {
system "$PreSubmitScript";
}
my $SubmitResultSuccess = $Self->_SubmitResults(
%Param,
StartTime => $StartTime,
Duration => $Duration,
);
if ( $Param{SubmitResultAsExitCode} ) {
return $SubmitResultSuccess ? 1 : 0;
}
}
return $Self->{TestCountNotOk} ? 0 : 1;
}
=begin Internal:
=cut
sub _HandleFile {
my ( $Self, %Param ) = @_;
my $ResultDataFile = $Kernel::OM->Get('Kernel::Config')->Get('Home') . '/var/tmp/UnitTest.dump';
unlink $ResultDataFile;
# Create a child process.
my $PID = fork;
# Could not create child.
if ( $PID < 0 ) {
$Self->{ResultData}->{ $Param{File} } = { TestNotOk => 1 };
$Self->{TestCountNotOk} += 1;
print $Self->_Color( 'red', "Could not create child process for $Param{File}.\n" );
return;
}
# We're in the child process.
if ( !$PID ) {
my $Driver = $Kernel::OM->Create(
'Kernel::System::UnitTest::Driver',
ObjectParams => {
Verbose => $Self->{Verbose},
ANSI => $Self->{ANSI},
},
);
$Driver->Run( File => $Param{File} );
exit 0;
}
# Wait for child process to finish.
waitpid( $PID, 0 );
my $ResultData = Storable::retrieve($ResultDataFile);
if ( !$ResultData ) {
$ResultData->{TestNotOk}++;
}
$Self->{ResultData}->{ $Param{File} } = $ResultData;
$Self->{TestCountOk} += $ResultData->{TestOk} // 0;
$Self->{TestCountNotOk} += $ResultData->{TestNotOk} // 0;
$Self->{NotOkInfo} //= [];
if ( $ResultData->{NotOkInfo} ) {
# Cut out from result data hash, as we don't need to send this to the server.
push @{ $Self->{NotOkInfo} }, [ $Param{File}, @{ delete $ResultData->{NotOkInfo} } ];
}
for my $PostTestScript ( @{ $Param{PostTestScripts} // [] } ) {
my $Commandline = $PostTestScript;
$Commandline =~ s{%File%}{$Param{File}}ismxg;
$Commandline =~ s{%TestOk%}{$ResultData->{TestOk} // 0}iesmxg;
$Commandline =~ s{%TestNotOk%}{$ResultData->{TestNotOk} // 0}iesmxg;
system $Commandline;
}
return 1;
}
sub _SubmitResults {
my ( $Self, %Param ) = @_;
my %SupportData = $Kernel::OM->Get('Kernel::System::SupportDataCollector')->Collect();
die "Could not collect SupportData.\n" if !$SupportData{Success};
# Limit number of screenshots in the result data, since it can grow very large.
# Allow only up to 25 screenshots per submission (average size of 80kb per screenshot for a total of 2MB).
my $ScreenshotCountLimit = 25;
my $ScreenshotCount = 0;
RESULT:
for my $Result ( sort keys %{ $Self->{ResultData} } ) {
next RESULT if !IsHashRefWithData( $Self->{ResultData}->{$Result}->{Results} );
TEST:
for my $Test ( sort keys %{ $Self->{ResultData}->{$Result}->{Results} } ) {
next TEST if !IsArrayRefWithData( $Self->{ResultData}->{$Result}->{Results}->{$Test}->{Screenshots} );
# Get number of screenshots in this test. Note that this key is an array, and we support multiple
# screenshots per one test.
my $TestScreenshotCount = scalar @{ $Self->{ResultData}->{$Result}->{Results}->{$Test}->{Screenshots} };
# Check if number of screenshots for this result breaks the limit.
if ( $ScreenshotCount + $TestScreenshotCount > $ScreenshotCountLimit ) {
my $ScreenshotCountRemaining = $ScreenshotCountLimit - $ScreenshotCount;
# Allow only remaining number of screenshots.
if ( $ScreenshotCountRemaining > 0 ) {
@{ $Self->{ResultData}->{$Result}->{Results}->{$Test}->{Screenshots} }
= @{ $Self->{ResultData}->{$Result}->{Results}->{$Test}->{Screenshots} }[
0,
$ScreenshotCountRemaining
];
$ScreenshotCount = $ScreenshotCountLimit;
}
# Remove all screenshots.
else {
delete $Self->{ResultData}->{$Result}->{Results}->{$Test}->{Screenshots};
}
# Include message about removal of screenshots.
$Self->{ResultData}->{$Result}->{Results}->{$Test}->{Message}
.= ' (Additional screenshots have been omitted from the report because of size constraint.)';
next TEST;
}
$ScreenshotCount += $TestScreenshotCount;
}
}
my %SubmitData = (
Auth => $Param{SubmitAuth} // '',
JobID => $Param{JobID} // '',
Scenario => $Param{Scenario} // '',
Meta => {
StartTime => $Param{StartTime},
Duration => $Param{Duration},
TestOk => $Self->{TestCountOk},
TestNotOk => $Self->{TestCountNotOk},
},
SupportData => $SupportData{Result},
Results => $Self->{ResultData},
);
print "=====================================================================\n";
print "Sending results to " . $Self->_Color( 'yellow', $Param{SubmitURL} ) . " ...\n";
# Flush possible output log files to be able to submit them.
*STDOUT->flush();
*STDERR->flush();
# Limit attachment sizes to 2MB in total.
my $AttachmentCount = scalar @{ $Param{AttachmentPath} // [] };
my $AttachmentsSize = 1024 * 1024 * 2;
ATTACHMENT_PATH:
for my $AttachmentPath ( @{ $Param{AttachmentPath} // [] } ) {
my $FileHandle;
my $Content;
if ( !open $FileHandle, '<:encoding(UTF-8)', $AttachmentPath ) { ## no-critic
print $Self->_Color( 'red', "Could not open file $AttachmentPath, skipping.\n" );
next ATTACHMENT_PATH;
}
# Read only allocated size of file to try to avoid out of memory error.
if ( !read $FileHandle, $Content, $AttachmentsSize / $AttachmentCount ) { ## no-critic
print $Self->_Color( 'red', "Could not read file $AttachmentPath, skipping.\n" );
close $FileHandle;
next ATTACHMENT_PATH;
}
my $Stat = stat($AttachmentPath);
if ( !$Stat ) {
print $Self->_Color( 'red', "Cannot stat file $AttachmentPath, skipping.\n" );
close $FileHandle;
next ATTACHMENT_PATH;
}
# If file size exceeds the limit, include message about shortening at the end.
if ( $Stat->size() > $AttachmentsSize / $AttachmentCount ) {
$Content .= "\nThis file has been shortened because of size constraint.";
}
close $FileHandle;
$SubmitData{Attachments}->{$AttachmentPath} = $Content;
}
my $JSONObject = $Kernel::OM->Get('Kernel::System::JSON');
# Perform web service request and get response.
my %Response = $Kernel::OM->Get('Kernel::System::WebUserAgent')->Request(
Type => 'POST',
URL => $Param{SubmitURL},
Data => {
Action => 'PublicCIMaster',
Subaction => 'TestResults',
RequestData => $JSONObject->Encode(
Data => \%SubmitData,
),
},
);
if ( $Response{Status} ne '200 OK' ) {
print $Self->_Color( 'red', "Submission to server failed (status code '$Response{Status}').\n" );
return;
}
if ( !$Response{Content} ) {
print $Self->_Color( 'red', "Submission to server failed (no response).\n" );
return;
}
$Kernel::OM->Get('Kernel::System::Encode')->EncodeInput(
$Response{Content},
);
my $ResponseData = $JSONObject->Decode(
Data => ${ $Response{Content} },
);
if ( !$ResponseData ) {
print $Self->_Color( 'red', "Submission to server failed (invalid response).\n" );
return;
}
if ( !$ResponseData->{Success} && $ResponseData->{ErrorMessage} ) {
print $Self->_Color(
'red',
"Submission to server failed (error message '$ResponseData->{ErrorMessage}').\n"
);
return;
}
print $Self->_Color( 'green', "Submission was successful.\n" );
return 1;
}
=head2 _Color()
this will color the given text (see L<Term::ANSIColor::color()>) if
ANSI output is available and active, otherwise the text stays unchanged.
my $PossiblyColoredText = $CommandObject->_Color('green', $Text);
=cut
sub _Color {
my ( $Self, $Color, $Text ) = @_;
return $Text if !$Self->{ANSI};
return Term::ANSIColor::color($Color) . $Text . Term::ANSIColor::color('reset');
}
1;
=end Internal:
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<http://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.
=cut
|