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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
## no critic (Modules::RequireExplicitPackage)
use strict;
use warnings;
use utf8;
use vars (qw($Self));
use URI::Escape();
my $HelperObject = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
# unregister other ticket handlers
$ConfigObject->Set(
Key => 'Ticket::EventModulePost',
Value => undef,
);
# register the generic interface test handler only
$ConfigObject->Set(
Key => 'Ticket::EventModulePost###9900-GenericInterface',
Value => {
Module => 'Kernel::GenericInterface::Event::Handler',
Event => '.*',
Transaction => 1,
},
);
$Self->Is(
$ConfigObject->Get('Ticket::EventModulePost')->{'9900-GenericInterface'}->{Module},
'Kernel::GenericInterface::Event::Handler',
"Event handler added to config",
);
my $Home = $ConfigObject->Get('Home');
my $Daemon = $Home . '/bin/otrs.Daemon.pl';
# get daemon status (stop if necessary to reload configuration with planner daemon disabled)
my $PreviousDaemonStatus = `perl $Daemon status`;
if ( !$PreviousDaemonStatus ) {
$Self->False(
1,
"Could not determine current daemon status!",
);
die "Could not determine current daemon status!";
}
if ( $PreviousDaemonStatus =~ m{Daemon running}i ) {
my $ResultMessage = system("perl $Daemon stop");
}
else {
$Self->True(
1,
"Daemon was already stopped.",
);
}
# Wait for slow systems
my $SleepTime = 120;
print "Waiting at most $SleepTime s until daemon stops\n";
ACTIVESLEEP:
for my $Seconds ( 1 .. $SleepTime ) {
my $DaemonStatus = `perl $Daemon status`;
if ( $DaemonStatus =~ m{Daemon not running}i ) {
last ACTIVESLEEP;
}
print "Sleeping for $Seconds seconds...\n";
sleep 1;
}
my $CurrentDaemonStatus = `perl $Daemon status`;
$Self->True(
int $CurrentDaemonStatus =~ m{Daemon not running}i,
"Daemon is not running",
);
if ( $CurrentDaemonStatus !~ m{Daemon not running}i ) {
die "Daemon could not be stopped.";
}
my $SchedulerDBObject = $Kernel::OM->Get('Kernel::System::Daemon::SchedulerDB');
# Remove scheduled tasks from DB, as they may interfere with tests run later.
my @AllTasks = $SchedulerDBObject->TaskList();
for my $Task (@AllTasks) {
my $Success = $SchedulerDBObject->TaskDelete(
TaskID => $Task->{TaskID},
);
$Self->True(
$Success,
"TaskDelete - Removed scheduled task $Task->{TaskID}",
);
}
my @Tests = (
{
Name => 'Synchronous event call',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 0,
},
],
},
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 1,
},
{
Name => 'Asynchronous event call via scheduler daemon',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 1,
},
],
},
},
},
},
Asynchronous => 1,
ValidID => 1,
Success => 1,
},
{
Name => 'Synchronous event call Filter Success',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 0,
Condition => {
Condition => {
ConditionLinking => 'and',
1 => {
Type => 'and',
Fields => {
Queue => {
Match => 'Raw',
Type => 'String',
},
},
},
},
},
},
],
},
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 1,
},
{
Name => 'Synchronous event call Filter Fail',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 0,
Condition => {
Condition => {
ConditionLinking => 'and',
1 => {
Type => 'and',
Fields => {
Queue => {
Match => 'Postmaster',
Type => 'String',
},
},
},
},
},
},
],
},
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 0,
},
{
Name => 'Asynchronous event call Filter Success',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 1,
Condition => {
Condition => {
ConditionLinking => 'and',
1 => {
Type => 'and',
Fields => {
Queue => {
Match => 'Raw',
Type => 'String',
},
},
},
},
},
},
],
},
},
},
},
Asynchronous => 1,
ValidID => 1,
Success => 1,
},
{
Name => 'Synchronous event call - Web service set to invalid',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketCreate',
Asynchronous => 0,
},
],
},
},
},
},
Asynchronous => 0,
ValidID => 2,
Success => 0,
},
# to add the Web Service is needed to have the Requester Transport and Type
{
Name => 'Synchronous event call - Empty Requester configuration',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => '',
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 0,
},
{
Name => 'Synchronous event call - Empty Invoker configuration',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 0,
},
{
Name => 'Synchronous event call - Invalid Invoker events (not a hash ref)',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => {
Event => 'TicketCreate',
Asynchronous => 0,
},
},
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 0,
},
{
Name => 'Synchronous event call - Different Event',
WebserviceConfig => {
Debugger => {
DebugThreshold => 'debug',
},
Requester => {
Transport => {
Type => 'HTTP::Test',
Config => {
Fail => 0,
},
},
Invoker => {
test_operation => {
Type => 'Test::TestSimple',
Events => [
{
Event => 'TicketMove',
Asynchronous => 0,
},
],
},
},
},
},
Asynchronous => 0,
ValidID => 1,
Success => 0,
},
);
# get needed objects
my $WebserviceObject = $Kernel::OM->Get('Kernel::System::GenericInterface::Webservice');
my $DebugLogObject = $Kernel::OM->Get('Kernel::System::GenericInterface::DebugLog');
my $TaskWorkerObject = $Kernel::OM->Get('Kernel::System::Daemon::DaemonModules::SchedulerTaskWorker');
my $RandomID = $HelperObject->GetRandomID();
for my $Test (@Tests) {
# add config
my $WebserviceID = $WebserviceObject->WebserviceAdd(
Config => $Test->{WebserviceConfig},
Name => "$Test->{Name} $RandomID",
ValidID => $Test->{ValidID},
UserID => 1,
);
$Self->IsNot(
$WebserviceID,
undef,
"$Test->{Name} WebserviceAdd() - should not be undef",
);
#
# Run actual test
#
my $TicketID;
# enclose in block because the events are executed in destructor of ticket object
{
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
$TicketID = $TicketObject->TicketCreate(
Title => 'Some Ticket Title',
Queue => 'Raw',
Lock => 'unlock',
Priority => '3 normal',
State => 'closed successful',
CustomerID => '123465',
CustomerUser => 'unittest@otrs.com',
OwnerID => 1,
UserID => 1,
);
$Self->True(
$TicketID,
"$Test->{Name} TicketCreate()",
);
$Kernel::OM->ObjectsDiscard(
Objects => ['Kernel::System::Ticket'],
);
}
# If this is asynchronous, wait for the daemon to handle the task
if ( $Test->{Asynchronous} ) {
local $SIG{CHLD} = "IGNORE";
# Wait for slow systems
my $SleepTime = 5;
print "Waiting at most $SleepTime s until tasks are registered\n";
ACTIVESLEEP:
for my $Seconds ( 1 .. $SleepTime ) {
my @List = $SchedulerDBObject->TaskList(
Type => 'GenericInterface',
);
last ACTIVESLEEP if scalar @List;
print "Sleeping for $Seconds seconds...\n";
sleep 1;
}
my $TotalWaitToExecute = 120;
# wait for daemon children to actually execute tasks
WAITEXECUTE:
for my $Wait ( 1 .. $TotalWaitToExecute ) {
print "Waiting for Daemon to execute tasks, $Wait seconds\n";
my $Success = $TaskWorkerObject->Run();
$TaskWorkerObject->_WorkerPIDsCheck();
$Self->True(
$Success,
'TaskWorker Run() - To execute current tasks, with true',
);
my @List = $SchedulerDBObject->TaskList(
Type => 'GenericInterface',
);
if ( scalar @List eq 0 ) {
$Self->True(
1,
"$Test->{Name} - all tasks are dropped from task list",
);
last WAITEXECUTE;
}
sleep 1;
next WAITEXECUTE if $Wait < $TotalWaitToExecute;
$Self->True(
0,
"$Test->{Name} - all tasks are not dropped from task list after $TotalWaitToExecute seconds!",
);
}
}
my $LogData = $DebugLogObject->LogSearch(
CommunicationType => 'Requester',
WebserviceID => $WebserviceID,
WithData => 1,
);
if ( $Test->{Success} ) {
$Self->Is(
scalar @{$LogData},
1,
"$Test->{Name} log data found",
);
$Self->Is(
ref $LogData->[0],
'HASH',
"$Test->{Name} log data found entry",
);
$Self->Is(
ref $LogData->[0]->{Data},
'ARRAY',
"$Test->{Name} log data found data entry",
);
$Self->Is(
scalar(
grep { $_->{Data} =~ m/'ResponseContent' \s+ => \s+ '.*?TicketID=$TicketID.*?'/smx }
@{ $LogData->[0]->{Data} }
),
1,
"$Test->{Name} event handler communication result data found ('ResponseContent' => 'TicketID=$TicketID')",
);
}
else {
$Self->Is(
scalar @{$LogData},
0,
"$Test->{Name} no log data found",
);
}
# delete config
my $Success = $WebserviceObject->WebserviceDelete(
ID => $WebserviceID,
UserID => 1,
);
$Self->True(
$Success,
"$Test->{Name} WebserviceDelete()",
);
}
my $TicketObject = $Kernel::OM->Get('Kernel::System::Ticket');
# cleanup ticket database
my @DeleteTicketList = $TicketObject->TicketSearch(
Result => 'ARRAY',
CustomerUserLogin => 'unittest@otrs.com',
UserID => 1,
);
for my $TicketID (@DeleteTicketList) {
$TicketObject->TicketDelete(
TicketID => $TicketID,
UserID => 1,
);
}
# start daemon if it was already running before this test
if ( $PreviousDaemonStatus =~ m{Daemon running}i ) {
my $Result = system("perl $Daemon start");
$Self->Is(
$Result,
0,
"Daemon start call returned successfully.",
);
# Wait for slow systems
my $SleepTime = 120;
print "Waiting at most $SleepTime s until daemon start\n";
ACTIVESLEEP:
for my $Seconds ( 1 .. $SleepTime ) {
my $DaemonStatus = `perl $Daemon status`;
if ( $DaemonStatus =~ m{Daemon running}i ) {
last ACTIVESLEEP;
}
print "Sleeping for $Seconds seconds...\n";
sleep 1;
}
}
$CurrentDaemonStatus = `perl $Daemon status`;
$Self->Is(
$CurrentDaemonStatus,
$PreviousDaemonStatus,
"Daemon has original state again.",
);
1;
|