91 lines
3.6 KiB
PHP
91 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Functional;
|
|
|
|
use App\Controller\OgAgent\CreateImageAction;
|
|
use App\Controller\OgAgent\RunScriptAction;
|
|
use App\Controller\OgRepository\Git\CreateRepositoryAction;
|
|
use App\Controller\OgRepository\Git\SshKeyAction;
|
|
use App\Dto\Input\CommandExecuteInput;
|
|
use App\Dto\Output\ClientOutput;
|
|
use App\Entity\Client;
|
|
use App\Entity\Trace;
|
|
use App\Factory\ClientFactory;
|
|
use App\Model\CommandTypes;
|
|
use App\Model\TraceStatus;
|
|
use App\Service\CreatePartitionService;
|
|
use App\Service\Trace\CreateService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
/**
|
|
* Reproduce el bug reportado: si el cliente está ocupado (el agente responde
|
|
* 409) y el usuario marcó "encolar tarea", ejecutar un script debía crear
|
|
* una traza PENDING para que opengnsys:execute-pending-traces la reintentase
|
|
* al terminar la tarea anterior. Antes del fix, esta rama ignoraba el flag
|
|
* "queue" por completo y siempre creaba una traza BUSY (que el comando de
|
|
* reintento nunca recoge, porque solo busca traces en PENDING).
|
|
*/
|
|
class RunScriptQueueTest extends AbstractTest
|
|
{
|
|
private function buildRunScriptAction(MockHttpClient $httpClient): RunScriptAction
|
|
{
|
|
$container = static::getContainer();
|
|
|
|
return new RunScriptAction(
|
|
$container->get(EntityManagerInterface::class),
|
|
$httpClient,
|
|
$container->get(CreatePartitionService::class),
|
|
$container->get(LoggerInterface::class),
|
|
$container->get(CreateService::class),
|
|
$container->get(CreateRepositoryAction::class),
|
|
$container->get(SshKeyAction::class),
|
|
'false',
|
|
);
|
|
}
|
|
|
|
public function testBusyClientWithQueueCreatesPendingTraceNotBusy(): void
|
|
{
|
|
/** @var Client $client */
|
|
$client = ClientFactory::createOne(['ip' => '127.0.0.1', 'token' => 'fake-token'])->_real();
|
|
|
|
$mockHttpClient = new MockHttpClient(fn() => new MockResponse('{"message":"busy"}', ['http_code' => 409]));
|
|
|
|
$input = new CommandExecuteInput();
|
|
$input->clients = [new ClientOutput($client)];
|
|
$input->script = 'echo hello';
|
|
$input->queue = true;
|
|
|
|
$this->buildRunScriptAction($mockHttpClient)->__invoke($input);
|
|
|
|
$trace = static::getContainer()->get('doctrine')->getRepository(Trace::class)
|
|
->findOneBy(['client' => $client, 'command' => CommandTypes::RUN_SCRIPT]);
|
|
|
|
$this->assertNotNull($trace, 'Debe haberse creado una traza para el script');
|
|
$this->assertSame(TraceStatus::PENDING, $trace->getStatus(), 'Con "encolar" marcado, la traza debe quedar PENDING (para que el reintento la recoja), no BUSY');
|
|
}
|
|
|
|
public function testBusyClientWithoutQueueCreatesBusyTrace(): void
|
|
{
|
|
/** @var Client $client */
|
|
$client = ClientFactory::createOne(['ip' => '127.0.0.1', 'token' => 'fake-token'])->_real();
|
|
|
|
$mockHttpClient = new MockHttpClient(fn() => new MockResponse('{"message":"busy"}', ['http_code' => 409]));
|
|
|
|
$input = new CommandExecuteInput();
|
|
$input->clients = [new ClientOutput($client)];
|
|
$input->script = 'echo hello';
|
|
$input->queue = false;
|
|
|
|
$this->buildRunScriptAction($mockHttpClient)->__invoke($input);
|
|
|
|
$trace = static::getContainer()->get('doctrine')->getRepository(Trace::class)
|
|
->findOneBy(['client' => $client, 'command' => CommandTypes::RUN_SCRIPT]);
|
|
|
|
$this->assertNotNull($trace);
|
|
$this->assertSame(TraceStatus::BUSY, $trace->getStatus(), 'Sin "encolar", debe mantenerse el comportamiento previo (traza BUSY, sin reintento)');
|
|
}
|
|
}
|