318 lines
12 KiB
PHP
318 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Functional;
|
|
|
|
use App\Entity\Client;
|
|
use App\Entity\Image;
|
|
use App\Entity\ImageImageRepository;
|
|
use App\Entity\ImageRepository;
|
|
use App\Entity\OrganizationalUnit;
|
|
use App\Factory\UserFactory;
|
|
use App\Model\DeployMethodTypes;
|
|
use App\Model\ImageStatus;
|
|
use App\Model\OrganizationalUnitP2PModes;
|
|
use App\Model\OrganizationalUnitTypes;
|
|
use App\Model\UserGroupPermissions;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
|
|
|
/**
|
|
* La creación de la transmisión en el ogRepository es una operación por repositorio, no por cliente.
|
|
* Antes se lanzaba dentro del bucle de clientes, así que un despliegue de N equipos hacía N veces la
|
|
* misma petición (y esa petición tarda ~10s en real), de ahí que el tiempo creciera con el nº de PCs.
|
|
*
|
|
* @see \App\Controller\DeployImageAction::prepareRepositoryTransmissions()
|
|
*/
|
|
class DeployImageTorrentTest extends AbstractTest
|
|
{
|
|
private const ADMIN_USER = 'deploy-super-admin';
|
|
|
|
/** @var string[] URLs invocadas sobre el httpClient, en orden */
|
|
private array $requestedUrls = [];
|
|
|
|
/** Contadores para no repetir IPs, que son únicas en BD. */
|
|
private int $clientIpCounter = 0;
|
|
|
|
private int $repositoryIpCounter = 0;
|
|
|
|
private ?string $adminToken = null;
|
|
|
|
/** @var string[] Orden de "start:" (peticion lanzada) y "read:" (respuesta leida) */
|
|
private array $callOrder = [];
|
|
|
|
/** Permite que un test elija el mock que registra el orden de las llamadas. */
|
|
private bool $trackCallOrder = false;
|
|
|
|
private function getEntityManager(): EntityManagerInterface
|
|
{
|
|
return static::getContainer()->get('doctrine')->getManager();
|
|
}
|
|
|
|
/**
|
|
* Sustituye el cliente HTTP por un mock que registra cada URL invocada, para poder contar
|
|
* cuántas peticiones se hacen al ogRepository y cuántas a los ogAgent.
|
|
*/
|
|
private function mockHttpClient(): void
|
|
{
|
|
$this->requestedUrls = [];
|
|
|
|
$mock = new MockHttpClient(function (string $method, string $url): MockResponse {
|
|
$this->requestedUrls[] = $url;
|
|
|
|
if (str_contains($url, '/ogrepository/v1/')) {
|
|
return new MockResponse(
|
|
json_encode(['success' => true, 'details' => 'Tracker and Seeder serving image correctly']),
|
|
['response_headers' => ['content-type' => 'application/json']]
|
|
);
|
|
}
|
|
|
|
return new MockResponse(
|
|
json_encode(['job_id' => 'RestaurarImagen-'.substr(md5($url.count($this->requestedUrls)), 0, 8)]),
|
|
['response_headers' => ['content-type' => 'application/json']]
|
|
);
|
|
});
|
|
|
|
static::getContainer()->set('http_client', $mock);
|
|
}
|
|
|
|
/**
|
|
* Igual que mockHttpClient(), pero además anota el orden en que se crean las peticiones y en
|
|
* que se leen sus respuestas. El cuerpo se entrega con un generador, que MockHttpClient sólo
|
|
* consume cuando alguien lee la respuesta: así se distingue "lanzada" de "leída".
|
|
*/
|
|
private function mockHttpClientTrackingOrder(): void
|
|
{
|
|
$this->requestedUrls = [];
|
|
$this->callOrder = [];
|
|
|
|
$mock = new MockHttpClient(function (string $method, string $url): MockResponse {
|
|
$this->requestedUrls[] = $url;
|
|
|
|
$isAgent = str_contains($url, '/opengnsys/RestaurarImagen');
|
|
|
|
if ($isAgent) {
|
|
$this->callOrder[] = 'start:'.$url;
|
|
}
|
|
|
|
$body = function () use ($url, $isAgent): \Generator {
|
|
if ($isAgent) {
|
|
$this->callOrder[] = 'read:'.$url;
|
|
|
|
yield json_encode(['job_id' => 'job-'.substr(md5($url), 0, 8)]);
|
|
|
|
return;
|
|
}
|
|
|
|
yield json_encode(['success' => true, 'details' => 'Tracker and Seeder serving image correctly']);
|
|
};
|
|
|
|
return new MockResponse($body(), ['response_headers' => ['content-type' => 'application/json']]);
|
|
});
|
|
|
|
static::getContainer()->set('http_client', $mock);
|
|
}
|
|
|
|
/** @return array{image: ImageImageRepository, clients: Client[]} */
|
|
private function createDeployScenario(int $numberOfClients, ?ImageRepository $repository = null): array
|
|
{
|
|
$entityManager = $this->getEntityManager();
|
|
|
|
$organizationalUnit = new OrganizationalUnit();
|
|
$organizationalUnit->setName('deploy-ou-'.uniqid());
|
|
$organizationalUnit->setType(OrganizationalUnitTypes::CLASSROOM);
|
|
$entityManager->persist($organizationalUnit);
|
|
|
|
if ($repository === null) {
|
|
$repository = new ImageRepository();
|
|
$repository->setName('repo-'.uniqid());
|
|
$repository->setIp('10.200.0.'.(++$this->repositoryIpCounter));
|
|
$entityManager->persist($repository);
|
|
}
|
|
|
|
$image = new Image();
|
|
$image->setName('image-'.uniqid());
|
|
$image->setType('monolithic');
|
|
$image->setIsGlobal(false);
|
|
$entityManager->persist($image);
|
|
|
|
$imageImageRepository = new ImageImageRepository();
|
|
$imageImageRepository->setImage($image);
|
|
$imageImageRepository->setRepository($repository);
|
|
$imageImageRepository->setName($image->getName());
|
|
$imageImageRepository->setStatus(ImageStatus::SUCCESS);
|
|
$imageImageRepository->setImageFullsum(md5($image->getName()));
|
|
$entityManager->persist($imageImageRepository);
|
|
|
|
$clients = [];
|
|
for ($i = 0; $i < $numberOfClients; $i++) {
|
|
$client = new Client();
|
|
$client->setName('deploy-client-'.$i.'-'.uniqid());
|
|
$client->setMaintenance(false);
|
|
$client->setOrganizationalUnit($organizationalUnit);
|
|
$client->setIp('10.'.intdiv($this->clientIpCounter, 254).'.1.'.(($this->clientIpCounter++ % 254) + 1));
|
|
$client->setToken('token-'.$i);
|
|
$client->setRepository($repository);
|
|
$entityManager->persist($client);
|
|
|
|
$clients[] = $client;
|
|
}
|
|
|
|
$entityManager->flush();
|
|
|
|
return ['image' => $imageImageRepository, 'clients' => $clients];
|
|
}
|
|
|
|
/** Lanza el despliegue torrent y devuelve el código de respuesta. */
|
|
private function deployTorrent(ImageImageRepository $image, array $clients): int
|
|
{
|
|
if ($this->adminToken === null) {
|
|
UserFactory::createOne(['username' => self::ADMIN_USER, 'roles' => [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
|
|
$this->adminToken = $this->getToken([
|
|
'username' => self::ADMIN_USER,
|
|
'password' => UserFactory::PLAIN_PASSWORD,
|
|
]);
|
|
}
|
|
|
|
$token = $this->adminToken;
|
|
|
|
$clientIris = array_map(
|
|
static fn (Client $client): string => '/clients/'.$client->getUuid(),
|
|
$clients
|
|
);
|
|
|
|
$imageUuid = (string) $image->getUuid();
|
|
|
|
// El mock debe instalarse sobre el kernel que atenderá la petición: createClient() lo reinicia.
|
|
$apiClient = $this->createClientWithCredentials($token);
|
|
|
|
if ($this->trackCallOrder) {
|
|
$this->mockHttpClientTrackingOrder();
|
|
} else {
|
|
$this->mockHttpClient();
|
|
}
|
|
|
|
$apiClient->request('POST', '/image-image-repositories/'.$imageUuid.'/deploy-image', [
|
|
'json' => [
|
|
'type' => 'monolithic',
|
|
'method' => DeployMethodTypes::TORRENT,
|
|
'clients' => $clientIris,
|
|
'diskNumber' => 1,
|
|
'partitionNumber' => 1,
|
|
'p2pMode' => OrganizationalUnitP2PModes::P2P_MODE_PEER,
|
|
'p2pTime' => 0,
|
|
],
|
|
]);
|
|
|
|
return $apiClient->getResponse()->getStatusCode();
|
|
}
|
|
|
|
/** @return string[] */
|
|
private function repositoryRequests(): array
|
|
{
|
|
return array_values(array_filter(
|
|
$this->requestedUrls,
|
|
static fn (string $url): bool => str_contains($url, '/ogrepository/v1/')
|
|
));
|
|
}
|
|
|
|
/** @return string[] */
|
|
private function agentRequests(): array
|
|
{
|
|
return array_values(array_filter(
|
|
$this->requestedUrls,
|
|
static fn (string $url): bool => str_contains($url, '/opengnsys/RestaurarImagen')
|
|
));
|
|
}
|
|
|
|
/** Con 10 equipos solo debe crearse la transmisión una vez, y avisarse a los 10 ogAgent. */
|
|
public function testTorrentDeploymentCallsRepositoryOncePerDeployment(): void
|
|
{
|
|
$scenario = $this->createDeployScenario(10);
|
|
|
|
$statusCode = $this->deployTorrent($scenario['image'], $scenario['clients']);
|
|
$this->assertSame(200, $statusCode);
|
|
|
|
$this->assertCount(
|
|
1,
|
|
$this->repositoryRequests(),
|
|
'La transmisión p2p debe crearse una sola vez, no una por equipo'
|
|
);
|
|
$this->assertStringContainsString('/ogrepository/v1/p2p', $this->repositoryRequests()[0]);
|
|
$this->assertCount(10, $this->agentRequests(), 'Cada equipo debe recibir su orden de despliegue');
|
|
}
|
|
|
|
/** El número de llamadas al ogRepository no debe crecer con el número de equipos. */
|
|
public function testRepositoryCallsDoNotGrowWithClientCount(): void
|
|
{
|
|
$scenario = $this->createDeployScenario(3);
|
|
$this->deployTorrent($scenario['image'], $scenario['clients']);
|
|
$repositoryCallsWithThreeClients = count($this->repositoryRequests());
|
|
|
|
$scenario = $this->createDeployScenario(30);
|
|
$this->deployTorrent($scenario['image'], $scenario['clients']);
|
|
$repositoryCallsWithThirtyClients = count($this->repositoryRequests());
|
|
|
|
$this->assertSame(1, $repositoryCallsWithThreeClients);
|
|
$this->assertSame(
|
|
$repositoryCallsWithThreeClients,
|
|
$repositoryCallsWithThirtyClients,
|
|
'El coste en ogRepository debe ser constante, no proporcional al nº de equipos'
|
|
);
|
|
}
|
|
|
|
/** Si los equipos resuelven a repositorios distintos, se crea una transmisión en cada uno. */
|
|
public function testOneRepositoryCallPerDistinctRepository(): void
|
|
{
|
|
$entityManager = $this->getEntityManager();
|
|
|
|
$firstScenario = $this->createDeployScenario(4);
|
|
|
|
$secondRepository = new ImageRepository();
|
|
$secondRepository->setName('repo-second-'.uniqid());
|
|
$secondRepository->setIp('10.0.0.2');
|
|
$entityManager->persist($secondRepository);
|
|
$entityManager->flush();
|
|
|
|
// Cuatro equipos más, apuntando al segundo repositorio, sobre la misma imagen.
|
|
$secondScenario = $this->createDeployScenario(4, $secondRepository);
|
|
|
|
$clients = array_merge($firstScenario['clients'], $secondScenario['clients']);
|
|
|
|
$this->deployTorrent($firstScenario['image'], $clients);
|
|
|
|
$this->assertCount(2, $this->repositoryRequests(), 'Una transmisión por repositorio distinto');
|
|
$this->assertCount(8, $this->agentRequests());
|
|
}
|
|
|
|
/**
|
|
* Las N peticiones a los ogAgent deben lanzarse todas antes de leer la primera respuesta: es lo
|
|
* que permite que viajen en paralelo. Si se procesaran de una en una, el orden sería
|
|
* start/read/start/read... y el tiempo total la suma de todos los equipos.
|
|
*/
|
|
public function testAgentRequestsAreAllStartedBeforeReadingAnyResponse(): void
|
|
{
|
|
$this->trackCallOrder = true;
|
|
$scenario = $this->createDeployScenario(12);
|
|
|
|
$this->assertSame(200, $this->deployTorrent($scenario['image'], $scenario['clients']));
|
|
|
|
$starts = array_keys(array_filter(
|
|
$this->callOrder,
|
|
static fn (string $entry): bool => str_starts_with($entry, 'start:')
|
|
));
|
|
$reads = array_keys(array_filter(
|
|
$this->callOrder,
|
|
static fn (string $entry): bool => str_starts_with($entry, 'read:')
|
|
));
|
|
|
|
$this->assertCount(12, $starts);
|
|
$this->assertCount(12, $reads);
|
|
$this->assertLessThan(
|
|
min($reads),
|
|
max($starts),
|
|
'Todas las peticiones deben estar lanzadas antes de leer la primera respuesta'
|
|
);
|
|
}
|
|
}
|