test: Command transmission fallback handling

refs #950
This commit is contained in:
Johannes Meyer 2024-03-18 13:34:14 +01:00
parent 83af168936
commit a0bb146790
2 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,28 @@
<?php
namespace Tests\Icinga\Module\Icingadb\Lib;
use Icinga\Application\Config;
use Icinga\Data\ConfigObject;
use Icinga\Module\Icingadb\Command\IcingaApiCommand;
use Icinga\Module\Icingadb\Command\Transport\ApiCommandTransport;
use Icinga\Module\Icingadb\Command\Transport\CommandTransport;
use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
class StrikingCommandTransport extends CommandTransport
{
public static function getConfig(): Config
{
return Config::fromArray(['endpoint1' => ['host' => 'endpointA'], 'endpoint2' => ['host' => 'endpointB']]);
}
public static function createTransport(ConfigObject $config): ApiCommandTransport
{
return (new class extends ApiCommandTransport {
protected function sendCommand(IcingaApiCommand $command)
{
throw new CommandTransportException(sprintf('%s strikes!', $this->getHost()));
}
})->setHost($config->host);
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Tests\Icinga\Module\Icingadb\Command\Transport;
use Icinga\Module\Icingadb\Command\Object\AddCommentCommand;
use Icinga\Module\Icingadb\Command\Transport\CommandTransportException;
use Icinga\Module\Icingadb\Model\Host;
use PHPUnit\Framework\TestCase;
use Tests\Icinga\Module\Icingadb\Lib\StrikingCommandTransport;
class CommandTransportTest extends TestCase
{
public function testFallbackHandling()
{
$this->expectException(CommandTransportException::class);
$this->expectExceptionMessage('endpointB strikes!');
(new StrikingCommandTransport())->send(
(new AddCommentCommand())
->setExpireTime(42)
->setAuthor('GLaDOS')
->setComment('The cake is a lie')
->setObjects(new \CallbackFilterIterator(new \ArrayIterator([
(new Host())->setProperties(['name' => 'host1']),
(new Host())->setProperties(['name' => 'host2']),
]), function ($host) {
return $host->name === 'host2';
}))
);
}
public function testGeneratorsAreNotSupported()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Generators are not supported');
(new StrikingCommandTransport())->send(
(new AddCommentCommand())
->setExpireTime(42)
->setAuthor('GLaDOS')
->setComment('The cake is a lie')
->setObjects((function () {
yield (new Host())->setProperties(['name' => 'host1']);
yield (new Host())->setProperties(['name' => 'host2']);
})())
);
}
}