Files
keystone/tests/Feature/EnvironmentDeploymentPlanTest.php

114 lines
4.3 KiB
PHP

<?php
use App\Actions\Environments\PlanEnvironmentDeployment;
use App\Enums\DeployPolicy;
use App\Enums\EnvironmentAttachmentRole;
use App\Enums\SchedulerMode;
use App\Enums\ServiceCategory;
use App\Enums\ServiceType;
use App\Models\Application;
use App\Models\Environment;
use App\Models\Organisation;
use App\Models\Service;
it('deploys only with-environment services and checks dependency attachments', function () {
$organisation = Organisation::factory()->create();
$application = Application::factory()->for($organisation)->create();
$environment = Environment::factory()->for($application)->create();
$web = Service::factory()->for($environment)->create([
'organisation_id' => $organisation->id,
'name' => 'web',
'category' => ServiceCategory::APPLICATION,
'type' => ServiceType::LARAVEL,
'version' => 'php-8.4',
'version_track' => 'php-8.4',
'driver_name' => 'laravel.php-8.4',
'deploy_policy' => DeployPolicy::WITH_ENVIRONMENT,
]);
$postgres = Service::factory()->for($environment)->create([
'organisation_id' => $organisation->id,
'name' => 'postgres',
'deploy_policy' => DeployPolicy::DEPENDENCY_ONLY,
]);
$environment->attachments()->create([
'service_id' => $postgres->id,
'role' => EnvironmentAttachmentRole::DATABASE,
'is_primary' => true,
]);
$plan = app(PlanEnvironmentDeployment::class)->execute($environment);
expect($plan->services)
->toHaveCount(1)
->and($plan->services[0]->is($web))->toBeTrue()
->and($plan->dependencies)->toHaveCount(1)
->and($plan->dependencies[0]->is($postgres))->toBeTrue();
});
it('blocks multi-server environment deployments when no registry exists', function () {
$organisation = Organisation::factory()->create();
$application = Application::factory()->for($organisation)->create();
$environment = Environment::factory()->for($application)->create();
Service::factory()->for($environment)->create([
'organisation_id' => $organisation->id,
'name' => 'web',
'category' => ServiceCategory::APPLICATION,
'type' => ServiceType::LARAVEL,
'version' => 'php-8.4',
'version_track' => 'php-8.4',
'driver_name' => 'laravel.php-8.4',
'deploy_policy' => DeployPolicy::WITH_ENVIRONMENT,
'desired_replicas' => 2,
]);
$plan = app(PlanEnvironmentDeployment::class)->execute($environment);
expect($plan->requiresRegistry)->toBeTrue();
});
it('warns about sync queues without creating worker services', function () {
$organisation = Organisation::factory()->create();
$application = Application::factory()->for($organisation)->create();
$environment = Environment::factory()->for($application)->create();
$environment->variables()->create([
'key' => 'QUEUE_CONNECTION',
'value' => 'sync',
'source' => 'user',
'overridable' => true,
]);
$plan = app(PlanEnvironmentDeployment::class)->execute($environment);
expect($plan->warnings)
->toContain('QUEUE_CONNECTION=sync is not recommended for deployed Laravel environments.');
});
it('blocks single scheduler mode when the scheduler target has multiple replicas', function () {
$organisation = Organisation::factory()->create();
$application = Application::factory()->for($organisation)->create();
$environment = Environment::factory()->for($application)->create([
'scheduler_enabled' => true,
'scheduler_mode' => SchedulerMode::SINGLE,
]);
$web = Service::factory()->for($environment)->create([
'organisation_id' => $organisation->id,
'name' => 'web',
'category' => ServiceCategory::APPLICATION,
'type' => ServiceType::LARAVEL,
'version' => 'php-8.4',
'version_track' => 'php-8.4',
'driver_name' => 'laravel.php-8.4',
'deploy_policy' => DeployPolicy::WITH_ENVIRONMENT,
'process_roles' => ['web', 'scheduler'],
'desired_replicas' => 2,
]);
$environment->forceFill(['scheduler_target_service_id' => $web->id])->save();
$plan = app(PlanEnvironmentDeployment::class)->execute($environment->refresh());
expect($plan->blockers)->toContain('Scheduler mode single requires the scheduler target service to run exactly one replica.');
});