-
-
Notifications
You must be signed in to change notification settings - Fork 4k
fix(parsers): populate docker_compose_domains for API-created Docker Compose apps #9300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php | ||
|
|
||
| use App\Models\Application; | ||
| use App\Models\Environment; | ||
| use App\Models\PrivateKey; | ||
| use App\Models\Project; | ||
| use App\Models\Server; | ||
| use App\Models\ServerSetting; | ||
| use App\Models\StandaloneDocker; | ||
| use App\Models\Team; | ||
| use App\Models\User; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use phpseclib3\Crypt\EC; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| beforeEach(function () { | ||
| $this->user = User::factory()->create(); | ||
| $this->team = Team::factory()->create(); | ||
| $this->user->teams()->attach($this->team); | ||
|
|
||
| $this->project = Project::factory()->create(['team_id' => $this->team->id]); | ||
| $this->environment = Environment::factory()->create([ | ||
| 'project_id' => $this->project->id, | ||
| ]); | ||
|
|
||
| $ecKey = EC::createKey('Ed25519'); | ||
| $privateKeyContent = $ecKey->toString('OpenSSH'); | ||
|
|
||
| $privateKey = PrivateKey::create([ | ||
| 'name' => 'test-key', | ||
| 'private_key' => $privateKeyContent, | ||
| 'team_id' => $this->team->id, | ||
| ]); | ||
|
|
||
| $this->server = Server::factory()->create([ | ||
| 'team_id' => $this->team->id, | ||
| 'private_key_id' => $privateKey->id, | ||
| ]); | ||
|
|
||
| ServerSetting::create([ | ||
| 'server_id' => $this->server->id, | ||
| 'wildcard_domain' => 'http://127.0.0.1.sslip.io', | ||
| ]); | ||
|
|
||
| $this->destination = StandaloneDocker::factory()->create([ | ||
| 'server_id' => $this->server->id, | ||
| 'network' => 'test-network-'.fake()->uuid(), | ||
| ]); | ||
| }); | ||
|
|
||
| test('applicationParser populates docker_compose_domains for KEY-based SERVICE_FQDN variables', function () { | ||
| $dockerCompose = <<<'YAML' | ||
| services: | ||
| backend: | ||
| image: myapp/backend:latest | ||
| environment: | ||
| - SERVICE_FQDN_BACKEND_8000=${BACKEND_URL} | ||
| frontend: | ||
| image: myapp/frontend:latest | ||
| environment: | ||
| - SERVICE_FQDN_FRONTEND=${FRONTEND_URL} | ||
| YAML; | ||
|
|
||
| $application = Application::factory()->create([ | ||
| 'environment_id' => $this->environment->id, | ||
| 'destination_id' => $this->destination->id, | ||
| 'destination_type' => StandaloneDocker::class, | ||
| 'build_pack' => 'dockercompose', | ||
| 'docker_compose_raw' => $dockerCompose, | ||
| 'fqdn' => null, | ||
| 'docker_compose_domains' => null, | ||
| ]); | ||
|
|
||
| applicationParser($application); | ||
|
|
||
| $application->refresh(); | ||
|
|
||
| $domains = json_decode($application->docker_compose_domains, true); | ||
|
|
||
| expect($domains)->not->toBeNull() | ||
| ->and($domains)->toBeArray() | ||
| ->and($domains)->toHaveKey('backend') | ||
| ->and($domains['backend'])->toHaveKey('domain') | ||
| ->and($domains['backend']['domain'])->not->toBeEmpty(); | ||
| }); | ||
|
|
||
| test('applicationParser populates docker_compose_domains for KEY-based SERVICE_FQDN without port', function () { | ||
| $dockerCompose = <<<'YAML' | ||
| services: | ||
| frontend: | ||
| image: myapp/frontend:latest | ||
| environment: | ||
| - SERVICE_FQDN_FRONTEND=${FRONTEND_URL} | ||
| YAML; | ||
|
|
||
| $application = Application::factory()->create([ | ||
| 'environment_id' => $this->environment->id, | ||
| 'destination_id' => $this->destination->id, | ||
| 'destination_type' => StandaloneDocker::class, | ||
| 'build_pack' => 'dockercompose', | ||
| 'docker_compose_raw' => $dockerCompose, | ||
| 'fqdn' => null, | ||
| 'docker_compose_domains' => null, | ||
| ]); | ||
|
|
||
| applicationParser($application); | ||
|
|
||
| $application->refresh(); | ||
|
|
||
| $domains = json_decode($application->docker_compose_domains, true); | ||
|
|
||
| expect($domains)->not->toBeNull() | ||
| ->and($domains)->toHaveKey('frontend') | ||
| ->and($domains['frontend'])->toHaveKey('domain'); | ||
| }); | ||
|
|
||
| test('applicationParser does not populate docker_compose_domains for non-dockercompose build_pack', function () { | ||
| $dockerCompose = <<<'YAML' | ||
| services: | ||
| backend: | ||
| image: myapp/backend:latest | ||
| environment: | ||
| - SERVICE_FQDN_BACKEND_8000=${BACKEND_URL} | ||
| YAML; | ||
|
|
||
| $application = Application::factory()->create([ | ||
| 'environment_id' => $this->environment->id, | ||
| 'destination_id' => $this->destination->id, | ||
| 'destination_type' => StandaloneDocker::class, | ||
| 'build_pack' => 'dockerfile', | ||
| 'docker_compose_raw' => $dockerCompose, | ||
| 'fqdn' => null, | ||
| 'docker_compose_domains' => null, | ||
| ]); | ||
|
|
||
| applicationParser($application); | ||
|
|
||
| $application->refresh(); | ||
|
|
||
| // For non-dockercompose, docker_compose_domains should remain empty | ||
| $domains = json_decode($application->docker_compose_domains, true); | ||
| expect($domains)->toBeNull(); | ||
| }); | ||
|
Comment on lines
+118
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Negative test case? I give it a positive review. The future is not serverless. This test correctly verifies that non-dockercompose build packs don't get their domains populated. Essential guard against unintended side effects. The Consider adding an edge case test for when 💡 Optional: Additional test for merge behaviortest('applicationParser preserves existing docker_compose_domains entries', function () {
$dockerCompose = <<<'YAML'
services:
backend:
image: myapp/backend:latest
environment:
- SERVICE_FQDN_BACKEND_8000=${BACKEND_URL}
frontend:
image: myapp/frontend:latest
environment:
- SERVICE_FQDN_FRONTEND=${FRONTEND_URL}
YAML;
$existingDomains = json_encode(['frontend' => ['domain' => 'https://existing.example.com']]);
$application = Application::factory()->create([
'environment_id' => $this->environment->id,
'destination_id' => $this->destination->id,
'destination_type' => StandaloneDocker::class,
'build_pack' => 'dockercompose',
'docker_compose_raw' => $dockerCompose,
'fqdn' => null,
'docker_compose_domains' => $existingDomains,
]);
applicationParser($application);
$application->refresh();
$domains = json_decode($application->docker_compose_domains, true);
// Existing frontend domain should be preserved
expect($domains['frontend']['domain'])->toBe('https://existing.example.com')
// New backend domain should be added
->and($domains)->toHaveKey('backend')
->and($domains['backend'])->toHaveKey('domain');
});🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be back... to say this fix looks good, but
$svcis unused.The core logic correctly mirrors Path 1 (lines 636-649) to ensure KEY-based
SERVICE_FQDN_*variables populatedocker_compose_domains. This is the fix your resistance against serverless Docker Compose failures needed. However, static analysis correctly flagged an unused variable.🔧 Proposed fix for unused variable
if ($resource->build_pack === 'dockercompose') { $normalizedFqdnFor = str($fqdnFor)->replace('-', '_')->replace('.', '_')->value(); $serviceExists = false; - foreach ($services as $serviceNameKey => $svc) { + foreach ($services as $serviceNameKey => $_) { if (str($serviceNameKey)->replace('-', '_')->replace('.', '_')->value() === $normalizedFqdnFor) { $serviceExists = true; break; } }Or use
array_keys()since you only need keys:if ($resource->build_pack === 'dockercompose') { $normalizedFqdnFor = str($fqdnFor)->replace('-', '_')->replace('.', '_')->value(); $serviceExists = false; - foreach ($services as $serviceNameKey => $svc) { - if (str($serviceNameKey)->replace('-', '_')->replace('.', '_')->value() === $normalizedFqdnFor) { + foreach (array_keys($services) as $serviceNameKey) { + if (str($serviceNameKey)->replace('-', '_')->replace('.', '_')->value() === $normalizedFqdnFor) { $serviceExists = true; break; } }📝 Committable suggestion
🧰 Tools
🪛 PHPMD (2.15.0)
[warning] 512-512: Avoid unused local variables such as '$svc'. (undefined)
(UnusedLocalVariable)
🤖 Prompt for AI Agents