Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

2.9 KB
Raw Blame
<?php

use App\Models\Application;
use App\Models\Server;
use App\Models\StandaloneDocker;
use Illuminate\Support\Facades\Process;
use Symfony\Component\Yaml\Yaml;
use Tests\TestCase;

uses(TestCase::class);

it('parses valid Docker Compose files with more than 128 collection aliases', function () {
    Process::fake();

    $services = collect(range(1, 129))
        ->mapWithKeys(fn (int $index): array => ["service-{$index}" => [
            '<<' => '*defaults',
        ]])
        ->map(fn (array $service, string $name): string => "  {$name}:\n    <<: {$service['<<']}\n")
        ->implode('');

    $application = new Application;
    $application->docker_compose_raw = "x-defaults: &defaults\n  image: alpine:latest\nservices:\n{$services}";

    $server = new Server;
    $server->ip = '127.0.0.1';
    $server->user = 'root';

    $destination = new StandaloneDocker;
    $destination->setRelation('server', $server);
    $application->setRelation('destination', $destination);

    $application->oldRawParser();

    $parsedCompose = Yaml::parse($application->docker_compose_raw);

    expect(data_get($parsedCompose, 'services'))->toHaveCount(129)
        ->and(data_get($parsedCompose, 'services.service-129.image'))->toBe('alpine:latest')
        ->and(data_get($parsedCompose, 'services.service-129.labels'))->toContain('coolify.managed=true');
});

it('keeps a finite collection alias limit for Docker Compose files', function () {
    $services = collect(range(1, Application::MAX_DOCKER_COMPOSE_COLLECTION_ALIASES + 1))
        ->map(fn (int $index): string => "  service-{$index}:\n    <<: *defaults\n")
        ->implode('');

    $application = new Application;
    $application->docker_compose_raw = "x-defaults: &defaults\n  image: alpine:latest\nservices:\n{$services}";

    $application->oldRawParser();
})->throws(RuntimeException::class, 'Maximum number of collection aliases (256) exceeded');

it('accepts Docker Compose files with more than 128 collection aliases during the safety check', function () {
    $services = collect(range(1, 129))
        ->map(fn (int $index): string => "  service-{$index}:\n    <<: *defaults\n")
        ->implode('');

    validateDockerComposeForInjection("x-defaults: &defaults\n  image: alpine:latest\nservices:\n{$services}");

    expect(parseDockerComposeYaml("x-defaults: &defaults\n  image: alpine:latest\nservices:\n{$services}"))
        ->toHaveKey('services.service-129.image', 'alpine:latest');
});

it('keeps a finite collection alias limit during the Docker Compose safety check', function () {
    $services = collect(range(1, Application::MAX_DOCKER_COMPOSE_COLLECTION_ALIASES + 1))
        ->map(fn (int $index): string => "  service-{$index}:\n    <<: *defaults\n")
        ->implode('');

    validateDockerComposeForInjection("x-defaults: &defaults\n  image: alpine:latest\nservices:\n{$services}");
})->throws(Exception::class, 'Maximum number of collection aliases (256) exceeded');