feat(tests): add comprehensive tests for ContainerStatusAggregator and serverStatus accessor

- Introduced tests for ContainerStatusAggregator to validate status aggregation logic across various container states.
- Implemented tests to ensure serverStatus accessor correctly checks server infrastructure health without being affected by container status.
- Updated ExcludeFromHealthCheckTest to verify excluded status handling in various components.
- Removed obsolete PushServerUpdateJobStatusAggregationTest as its functionality is covered elsewhere.
- Updated version number for sentinel to 0.0.17 in versions.json.
This commit is contained in:
Andras Bacsai
2025-11-20 17:31:07 +01:00
parent 70fb4c6869
commit ae6eef3cdb
23 changed files with 1590 additions and 912 deletions
@@ -0,0 +1,223 @@
<?php
/**
* Unit tests to verify consistent handling of all-excluded containers
* across PushServerUpdateJob, GetContainersStatus, and ComplexStatusCheck.
*
* These tests verify the fix for issue where different code paths handled
* all-excluded containers inconsistently:
* - PushServerUpdateJob (Sentinel, ~30s) previously skipped updates
* - GetContainersStatus (SSH, ~1min) previously skipped updates
* - ComplexStatusCheck (Multi-server) correctly calculated :excluded status
*
* After this fix, all three paths now calculate and return :excluded status
* consistently, preventing status drift and UI inconsistencies.
*/
it('ensures CalculatesExcludedStatus trait exists with required methods', function () {
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
// Verify trait has both status calculation methods
expect($traitFile)
->toContain('trait CalculatesExcludedStatus')
->toContain('protected function calculateExcludedStatus(Collection $containers, Collection $excludedContainers): string')
->toContain('protected function calculateExcludedStatusFromStrings(Collection $containerStatuses): string')
->toContain('protected function getExcludedContainersFromDockerCompose(?string $dockerComposeRaw): Collection');
});
it('ensures ComplexStatusCheck uses CalculatesExcludedStatus trait', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
// Verify trait is used
expect($complexStatusCheckFile)
->toContain('use App\Traits\CalculatesExcludedStatus;')
->toContain('use CalculatesExcludedStatus;');
// Verify it uses the trait method instead of inline code
expect($complexStatusCheckFile)
->toContain('return $this->calculateExcludedStatus($containers, $excludedContainers);');
// Verify it uses the trait helper for excluded containers
expect($complexStatusCheckFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
});
it('ensures PushServerUpdateJob uses CalculatesExcludedStatus trait', function () {
$pushServerUpdateJobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify trait is used
expect($pushServerUpdateJobFile)
->toContain('use App\Traits\CalculatesExcludedStatus;')
->toContain('use CalculatesExcludedStatus;');
// Verify it calculates excluded status instead of skipping (old behavior: continue)
expect($pushServerUpdateJobFile)
->toContain('// If all containers are excluded, calculate status from excluded containers')
->toContain('$aggregatedStatus = $this->calculateExcludedStatusFromStrings($containerStatuses);');
// Verify it uses the trait helper for excluded containers
expect($pushServerUpdateJobFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
});
it('ensures PushServerUpdateJob calculates excluded status for applications', function () {
$pushServerUpdateJobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// In aggregateMultiContainerStatuses, verify the all-excluded scenario
// calculates status and updates the application
expect($pushServerUpdateJobFile)
->toContain('if ($relevantStatuses->isEmpty()) {')
->toContain('$aggregatedStatus = $this->calculateExcludedStatusFromStrings($containerStatuses);')
->toContain('if ($aggregatedStatus && $application->status !== $aggregatedStatus) {')
->toContain('$application->status = $aggregatedStatus;')
->toContain('$application->save();');
});
it('ensures PushServerUpdateJob calculates excluded status for services', function () {
$pushServerUpdateJobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Count occurrences - should appear twice (once for applications, once for services)
$calculateExcludedCount = substr_count(
$pushServerUpdateJobFile,
'$aggregatedStatus = $this->calculateExcludedStatusFromStrings($containerStatuses);'
);
expect($calculateExcludedCount)->toBe(2, 'Should calculate excluded status for both applications and services');
});
it('ensures GetContainersStatus uses CalculatesExcludedStatus trait', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Verify trait is used
expect($getContainersStatusFile)
->toContain('use App\Traits\CalculatesExcludedStatus;')
->toContain('use CalculatesExcludedStatus;');
// Verify it calculates excluded status instead of returning null
expect($getContainersStatusFile)
->toContain('// If all containers are excluded, calculate status from excluded containers')
->toContain('return $this->calculateExcludedStatusFromStrings($containerStatuses);');
// Verify it uses the trait helper for excluded containers
expect($getContainersStatusFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
});
it('ensures GetContainersStatus calculates excluded status for applications', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// In aggregateApplicationStatus, verify the all-excluded scenario returns status
expect($getContainersStatusFile)
->toContain('if ($relevantStatuses->isEmpty()) {')
->toContain('return $this->calculateExcludedStatusFromStrings($containerStatuses);');
});
it('ensures GetContainersStatus calculates excluded status for services', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// In aggregateServiceContainerStatuses, verify the all-excluded scenario updates status
expect($getContainersStatusFile)
->toContain('$aggregatedStatus = $this->calculateExcludedStatusFromStrings($containerStatuses);')
->toContain('if ($aggregatedStatus) {')
->toContain('$statusFromDb = $subResource->status;')
->toContain("if (\$statusFromDb !== \$aggregatedStatus) {\n \$subResource->update(['status' => \$aggregatedStatus]);");
});
it('ensures excluded status format is consistent across all paths', function () {
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
// Trait now delegates to ContainerStatusAggregator and uses appendExcludedSuffix helper
expect($traitFile)
->toContain('use App\\Services\\ContainerStatusAggregator;')
->toContain('$aggregator = new ContainerStatusAggregator;')
->toContain('private function appendExcludedSuffix(string $status): string');
// Check that appendExcludedSuffix returns consistent colon format with :excluded suffix
expect($traitFile)
->toContain("return 'degraded:excluded';")
->toContain("return 'paused:excluded';")
->toContain("return 'starting:excluded';")
->toContain("return 'exited:excluded';")
->toContain('return "$status:excluded";'); // For running:healthy:excluded, running:unhealthy:excluded, etc.
});
it('ensures all three paths check for exclude_from_hc flag consistently', function () {
// All three should use the trait helper method
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
$pushServerUpdateJobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
expect($complexStatusCheckFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
expect($pushServerUpdateJobFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
expect($getContainersStatusFile)
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
// The trait method should check both exclude_from_hc and restart: no
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
expect($traitFile)
->toContain('$excludeFromHc = data_get($serviceConfig, \'exclude_from_hc\', false);')
->toContain('$restartPolicy = data_get($serviceConfig, \'restart\', \'always\');')
->toContain('if ($excludeFromHc || $restartPolicy === \'no\') {');
});
it('ensures calculateExcludedStatus uses ContainerStatusAggregator', function () {
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
// Check that the trait uses ContainerStatusAggregator service instead of duplicating logic
expect($traitFile)
->toContain('protected function calculateExcludedStatus(Collection $containers, Collection $excludedContainers): string')
->toContain('use App\Services\ContainerStatusAggregator;')
->toContain('$aggregator = new ContainerStatusAggregator;')
->toContain('$aggregator->aggregateFromContainers($excludedOnly)');
// Check that it has appendExcludedSuffix helper for all states
expect($traitFile)
->toContain('private function appendExcludedSuffix(string $status): string')
->toContain("return 'degraded:excluded';")
->toContain("return 'paused:excluded';")
->toContain("return 'starting:excluded';")
->toContain("return 'exited:excluded';")
->toContain('return "$status:excluded";'); // For running:healthy:excluded
});
it('ensures calculateExcludedStatusFromStrings uses ContainerStatusAggregator', function () {
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
// Check that the trait uses ContainerStatusAggregator service instead of duplicating logic
expect($traitFile)
->toContain('protected function calculateExcludedStatusFromStrings(Collection $containerStatuses): string')
->toContain('use App\Services\ContainerStatusAggregator;')
->toContain('$aggregator = new ContainerStatusAggregator;')
->toContain('$aggregator->aggregateFromStrings($containerStatuses)');
// Check that it has appendExcludedSuffix helper for all states
expect($traitFile)
->toContain('private function appendExcludedSuffix(string $status): string')
->toContain("return 'degraded:excluded';")
->toContain("return 'paused:excluded';")
->toContain("return 'starting:excluded';")
->toContain("return 'exited:excluded';")
->toContain('return "$status:excluded";'); // For running:healthy:excluded
});
it('verifies no code path skips update when all containers excluded', function () {
$pushServerUpdateJobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// These patterns should NOT exist anymore (old behavior that caused drift)
expect($pushServerUpdateJobFile)
->not->toContain("// If all containers are excluded, don't update status");
expect($getContainersStatusFile)
->not->toContain("// If all containers are excluded, don't update status");
// Instead, both should calculate excluded status
expect($pushServerUpdateJobFile)
->toContain('// If all containers are excluded, calculate status from excluded containers');
expect($getContainersStatusFile)
->toContain('// If all containers are excluded, calculate status from excluded containers');
});
+113 -111
View File
@@ -58,30 +58,25 @@ it('does not mark containers as unhealthy when health status is missing', functi
// We can't easily test the private aggregateContainerStatuses method directly,
// but we can verify that the code doesn't default to 'unhealthy'
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify the fix: health status should not default to 'unhealthy'
expect($complexStatusCheckFile)
expect($aggregatorFile)
->not->toContain("data_get(\$container, 'State.Health.Status', 'unhealthy')")
->toContain("data_get(\$container, 'State.Health.Status')");
// Verify the health check logic for non-excluded containers
expect($complexStatusCheckFile)
->toContain('if ($containerHealth === \'unhealthy\') {');
// Verify the health check logic
expect($aggregatorFile)
->toContain('if ($health === \'unhealthy\') {');
});
it('only marks containers as unhealthy when health status explicitly equals unhealthy', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// For non-excluded containers (line ~108)
expect($complexStatusCheckFile)
->toContain('if ($containerHealth === \'unhealthy\') {')
// Verify the service checks for explicit 'unhealthy' status
expect($aggregatorFile)
->toContain('if ($health === \'unhealthy\') {')
->toContain('$hasUnhealthy = true;');
// For excluded containers (line ~145)
expect($complexStatusCheckFile)
->toContain('if ($containerHealth === \'unhealthy\') {')
->toContain('$excludedHasUnhealthy = true;');
});
it('handles missing health status correctly in GetContainersStatus', function () {
@@ -92,13 +87,14 @@ it('handles missing health status correctly in GetContainersStatus', function ()
->not->toContain("data_get(\$container, 'State.Health.Status', 'unhealthy')")
->toContain("data_get(\$container, 'State.Health.Status')");
// Verify it uses 'unknown' when health status is missing
// Verify it uses 'unknown' when health status is missing (now using colon format)
expect($getContainersStatusFile)
->toContain('$healthSuffix = $containerHealth ?? \'unknown\';');
->toContain('$healthSuffix = $containerHealth ?? \'unknown\';')
->toContain('ContainerStatusAggregator'); // Uses the service
});
it('treats containers with running status and no healthcheck as not unhealthy', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// The logic should be:
// 1. Get health status (may be null)
@@ -106,67 +102,65 @@ it('treats containers with running status and no healthcheck as not unhealthy',
// 3. Don't mark as unhealthy if health status is null/missing
// Verify the condition explicitly checks for unhealthy
expect($complexStatusCheckFile)
->toContain('if ($containerHealth === \'unhealthy\')');
expect($aggregatorFile)
->toContain('if ($health === \'unhealthy\')');
// Verify this check is done for running containers
expect($complexStatusCheckFile)
->toContain('} elseif ($containerStatus === \'running\') {')
expect($aggregatorFile)
->toContain('} elseif ($state === \'running\') {')
->toContain('$hasRunning = true;');
});
it('tracks unknown health state in aggregation', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// State machine logic now in ContainerStatusAggregator service
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify that $hasUnknown tracking variable exists
expect($getContainersStatusFile)
// Verify that $hasUnknown tracking variable exists in the service
expect($aggregatorFile)
->toContain('$hasUnknown = false;');
// Verify that unknown state is detected in status parsing
expect($getContainersStatusFile)
->toContain("if (str(\$status)->contains('unknown')) {")
expect($aggregatorFile)
->toContain("str(\$status)->contains('unknown')")
->toContain('$hasUnknown = true;');
});
it('preserves unknown health state in aggregated status with correct priority', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// State machine logic now in ContainerStatusAggregator service (using colon format)
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify three-way priority in aggregation:
// 1. Unhealthy (highest priority)
// 2. Unknown (medium priority)
// 3. Healthy (only when all explicitly healthy)
expect($getContainersStatusFile)
expect($aggregatorFile)
->toContain('if ($hasUnhealthy) {')
->toContain("return 'running (unhealthy)';")
->toContain("return 'running:unhealthy';")
->toContain('} elseif ($hasUnknown) {')
->toContain("return 'running (unknown)';")
->toContain("return 'running:unknown';")
->toContain('} else {')
->toContain("return 'running (healthy)';");
->toContain("return 'running:healthy';");
});
it('tracks unknown health state in ComplexStatusCheck for multi-server applications', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
it('tracks unknown health state in ContainerStatusAggregator for all applications', function () {
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify that $hasUnknown tracking variable exists
expect($complexStatusCheckFile)
expect($aggregatorFile)
->toContain('$hasUnknown = false;');
// Verify that unknown state is detected when containerHealth is null
expect($complexStatusCheckFile)
->toContain('} elseif ($containerHealth === null) {')
// Verify that unknown state is detected when health is null or 'starting'
expect($aggregatorFile)
->toContain('} elseif (is_null($health) || $health === \'starting\') {')
->toContain('$hasUnknown = true;');
// Verify excluded containers also track unknown
expect($complexStatusCheckFile)
->toContain('$excludedHasUnknown = false;');
});
it('preserves unknown health state in ComplexStatusCheck aggregated status', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
it('preserves unknown health state in ContainerStatusAggregator aggregated status', function () {
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify three-way priority for non-excluded containers
expect($complexStatusCheckFile)
// Verify three-way priority for running containers in the service
expect($aggregatorFile)
->toContain('if ($hasUnhealthy) {')
->toContain("return 'running:unhealthy';")
->toContain('} elseif ($hasUnknown) {')
@@ -174,114 +168,115 @@ it('preserves unknown health state in ComplexStatusCheck aggregated status', fun
->toContain('} else {')
->toContain("return 'running:healthy';");
// Verify three-way priority for excluded containers
// Verify ComplexStatusCheck delegates to the service
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
expect($complexStatusCheckFile)
->toContain('if ($excludedHasUnhealthy) {')
->toContain("return 'running:unhealthy:excluded';")
->toContain('} elseif ($excludedHasUnknown) {')
->toContain("return 'running:unknown:excluded';")
->toContain("return 'running:healthy:excluded';");
->toContain('use App\\Services\\ContainerStatusAggregator;')
->toContain('$aggregator = new ContainerStatusAggregator;')
->toContain('$aggregator->aggregateFromContainers($relevantContainers);');
});
it('preserves unknown health state in Service model aggregation', function () {
$serviceFile = file_get_contents(__DIR__.'/../../app/Models/Service.php');
// Verify unknown is handled in non-excluded applications
// Verify unknown is handled correctly
expect($serviceFile)
->toContain("} elseif (\$health->value() === 'unknown') {")
->toContain("if (\$complexHealth !== 'unhealthy') {")
->toContain("\$complexHealth = 'unknown';");
->toContain("if (\$aggregateHealth !== 'unhealthy') {")
->toContain("\$aggregateHealth = 'unknown';");
// The pattern should appear 4 times (non-excluded apps, non-excluded databases,
// excluded apps, excluded databases)
// The pattern should appear at least once (Service model has different aggregation logic than ContainerStatusAggregator)
$unknownCount = substr_count($serviceFile, "} elseif (\$health->value() === 'unknown') {");
expect($unknownCount)->toBe(4);
expect($unknownCount)->toBeGreaterThan(0);
});
it('handles starting state (created/starting) in GetContainersStatus', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// State machine logic now in ContainerStatusAggregator service
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify tracking variable exists
expect($getContainersStatusFile)
expect($aggregatorFile)
->toContain('$hasStarting = false;');
// Verify detection for created/starting states
expect($getContainersStatusFile)
->toContain("} elseif (str(\$status)->contains('created') || str(\$status)->contains('starting')) {")
expect($aggregatorFile)
->toContain("str(\$status)->contains('created') || str(\$status)->contains('starting')")
->toContain('$hasStarting = true;');
// Verify aggregation returns starting status
expect($getContainersStatusFile)
// Verify aggregation returns starting status (colon format)
expect($aggregatorFile)
->toContain('if ($hasStarting) {')
->toContain("return 'starting (unknown)';");
->toContain("return 'starting:unknown';");
});
it('handles paused state in GetContainersStatus', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// State machine logic now in ContainerStatusAggregator service
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify tracking variable exists
expect($getContainersStatusFile)
expect($aggregatorFile)
->toContain('$hasPaused = false;');
// Verify detection for paused state
expect($getContainersStatusFile)
->toContain("} elseif (str(\$status)->contains('paused')) {")
expect($aggregatorFile)
->toContain("str(\$status)->contains('paused')")
->toContain('$hasPaused = true;');
// Verify aggregation returns paused status
expect($getContainersStatusFile)
// Verify aggregation returns paused status (colon format)
expect($aggregatorFile)
->toContain('if ($hasPaused) {')
->toContain("return 'paused (unknown)';");
->toContain("return 'paused:unknown';");
});
it('handles dead/removing states in GetContainersStatus', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// State machine logic now in ContainerStatusAggregator service
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify tracking variable exists
expect($getContainersStatusFile)
expect($aggregatorFile)
->toContain('$hasDead = false;');
// Verify detection for dead/removing states
expect($getContainersStatusFile)
->toContain("} elseif (str(\$status)->contains('dead') || str(\$status)->contains('removing')) {")
expect($aggregatorFile)
->toContain("str(\$status)->contains('dead') || str(\$status)->contains('removing')")
->toContain('$hasDead = true;');
// Verify aggregation returns degraded status
expect($getContainersStatusFile)
// Verify aggregation returns degraded status (colon format)
expect($aggregatorFile)
->toContain('if ($hasDead) {')
->toContain("return 'degraded (unhealthy)';");
->toContain("return 'degraded:unhealthy';");
});
it('handles edge case states in ComplexStatusCheck for non-excluded containers', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
it('handles edge case states in ContainerStatusAggregator for all containers', function () {
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify tracking variables exist
expect($complexStatusCheckFile)
// Verify tracking variables exist in the service
expect($aggregatorFile)
->toContain('$hasStarting = false;')
->toContain('$hasPaused = false;')
->toContain('$hasDead = false;');
// Verify detection for created/starting
expect($complexStatusCheckFile)
->toContain("} elseif (\$containerStatus === 'created' || \$containerStatus === 'starting') {")
expect($aggregatorFile)
->toContain("} elseif (\$state === 'created' || \$state === 'starting') {")
->toContain('$hasStarting = true;');
// Verify detection for paused
expect($complexStatusCheckFile)
->toContain("} elseif (\$containerStatus === 'paused') {")
expect($aggregatorFile)
->toContain("} elseif (\$state === 'paused') {")
->toContain('$hasPaused = true;');
// Verify detection for dead/removing
expect($complexStatusCheckFile)
->toContain("} elseif (\$containerStatus === 'dead' || \$containerStatus === 'removing') {")
expect($aggregatorFile)
->toContain("} elseif (\$state === 'dead' || \$state === 'removing') {")
->toContain('$hasDead = true;');
});
it('handles edge case states in ComplexStatusCheck aggregation', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
it('handles edge case states in ContainerStatusAggregator aggregation', function () {
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify aggregation logic for edge cases
expect($complexStatusCheckFile)
// Verify aggregation logic for edge cases in the service
expect($aggregatorFile)
->toContain('if ($hasDead) {')
->toContain("return 'degraded:unhealthy';")
->toContain('if ($hasPaused) {')
@@ -290,51 +285,58 @@ it('handles edge case states in ComplexStatusCheck aggregation', function () {
->toContain("return 'starting:unknown';");
});
it('handles edge case states in Service model for all 4 locations', function () {
it('handles edge case states in Service model', function () {
$serviceFile = file_get_contents(__DIR__.'/../../app/Models/Service.php');
// Check for created/starting handling pattern
$createdStartingCount = substr_count($serviceFile, "\$status->startsWith('created') || \$status->startsWith('starting')");
expect($createdStartingCount)->toBe(4, 'created/starting handling should appear in all 4 locations');
expect($createdStartingCount)->toBeGreaterThan(0, 'created/starting handling should exist');
// Check for paused handling pattern
$pausedCount = substr_count($serviceFile, "\$status->startsWith('paused')");
expect($pausedCount)->toBe(4, 'paused handling should appear in all 4 locations');
expect($pausedCount)->toBeGreaterThan(0, 'paused handling should exist');
// Check for dead/removing handling pattern
$deadRemovingCount = substr_count($serviceFile, "\$status->startsWith('dead') || \$status->startsWith('removing')");
expect($deadRemovingCount)->toBe(4, 'dead/removing handling should appear in all 4 locations');
expect($deadRemovingCount)->toBeGreaterThan(0, 'dead/removing handling should exist');
});
it('appends :excluded suffix to excluded container statuses in GetContainersStatus', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Verify that we check for exclude_from_hc flag
// Verify that we use the trait for calculating excluded status
expect($getContainersStatusFile)
->toContain('$excludeFromHc = data_get($serviceConfig, \'exclude_from_hc\', false);');
->toContain('CalculatesExcludedStatus');
// Verify that we append :excluded suffix
// Verify that we use the trait to calculate excluded status
expect($getContainersStatusFile)
->toContain('$containerStatus = str_replace(\')\', \':excluded)\', $containerStatus);');
->toContain('use CalculatesExcludedStatus;');
});
it('skips containers with :excluded suffix in Service model non-excluded sections', function () {
$serviceFile = file_get_contents(__DIR__.'/../../app/Models/Service.php');
// Verify that we skip :excluded containers in non-excluded sections
// This should appear twice (once for applications, once for databases)
$skipExcludedCount = substr_count($serviceFile, "if (\$health->contains(':excluded')) {");
expect($skipExcludedCount)->toBeGreaterThanOrEqual(2, 'Should skip :excluded containers in non-excluded sections');
// Verify that we have exclude_from_status field handling
expect($serviceFile)
->toContain('exclude_from_status');
});
it('processes containers with :excluded suffix in Service model excluded sections', function () {
$serviceFile = file_get_contents(__DIR__.'/../../app/Models/Service.php');
// Verify that we process :excluded containers in excluded sections
$processExcludedCount = substr_count($serviceFile, "if (! \$health->contains(':excluded') && !");
expect($processExcludedCount)->toBeGreaterThanOrEqual(2, 'Should process :excluded containers in excluded sections');
// Verify that we strip :excluded suffix before health comparison
$stripExcludedCount = substr_count($serviceFile, "\$health = str(\$health)->replace(':excluded', '');");
expect($stripExcludedCount)->toBeGreaterThanOrEqual(2, 'Should strip :excluded suffix in excluded sections');
// Verify that we handle excluded status
expect($serviceFile)
->toContain(':excluded')
->toContain('exclude_from_status');
});
it('treats containers with starting health status as unknown in ContainerStatusAggregator', function () {
$aggregatorFile = file_get_contents(__DIR__.'/../../app/Services/ContainerStatusAggregator.php');
// Verify that 'starting' health status is treated the same as null (unknown)
// During Docker health check grace period, the health status is 'starting'
// This should be treated as 'unknown' rather than 'healthy'
expect($aggregatorFile)
->toContain('} elseif (is_null($health) || $health === \'starting\') {')
->toContain('$hasUnknown = true;');
});
@@ -0,0 +1,463 @@
<?php
use App\Services\ContainerStatusAggregator;
beforeEach(function () {
$this->aggregator = new ContainerStatusAggregator;
});
describe('aggregateFromStrings', function () {
test('returns exited:unhealthy for empty collection', function () {
$result = $this->aggregator->aggregateFromStrings(collect());
expect($result)->toBe('exited:unhealthy');
});
test('returns running:healthy for single healthy running container', function () {
$statuses = collect(['running:healthy']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:healthy');
});
test('returns running:unhealthy for single unhealthy running container', function () {
$statuses = collect(['running:unhealthy']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unhealthy');
});
test('returns running:unknown for single running container with unknown health', function () {
$statuses = collect(['running:unknown']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unknown');
});
test('returns degraded:unhealthy for restarting container', function () {
$statuses = collect(['restarting']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('returns degraded:unhealthy for mixed running and exited containers', function () {
$statuses = collect(['running:healthy', 'exited']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('returns running:unhealthy when one of multiple running containers is unhealthy', function () {
$statuses = collect(['running:healthy', 'running:unhealthy', 'running:healthy']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unhealthy');
});
test('returns running:unknown when running containers have unknown health', function () {
$statuses = collect(['running:unknown', 'running:healthy']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unknown');
});
test('returns degraded:unhealthy for crash loop (exited with restart count)', function () {
$statuses = collect(['exited']);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 5);
expect($result)->toBe('degraded:unhealthy');
});
test('returns exited:unhealthy for exited containers without restart count', function () {
$statuses = collect(['exited']);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 0);
expect($result)->toBe('exited:unhealthy');
});
test('returns degraded:unhealthy for dead container', function () {
$statuses = collect(['dead']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('returns degraded:unhealthy for removing container', function () {
$statuses = collect(['removing']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('returns paused:unknown for paused container', function () {
$statuses = collect(['paused']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('paused:unknown');
});
test('returns starting:unknown for starting container', function () {
$statuses = collect(['starting']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('starting:unknown');
});
test('returns starting:unknown for created container', function () {
$statuses = collect(['created']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('starting:unknown');
});
test('handles parentheses format input (backward compatibility)', function () {
$statuses = collect(['running (healthy)', 'running (unhealthy)']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unhealthy');
});
test('handles mixed colon and parentheses formats', function () {
$statuses = collect(['running:healthy', 'running (unhealthy)', 'running:healthy']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unhealthy');
});
test('prioritizes restarting over all other states', function () {
$statuses = collect(['restarting', 'running:healthy', 'paused', 'starting']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('prioritizes crash loop over running containers', function () {
$statuses = collect(['exited', 'exited']);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 3);
expect($result)->toBe('degraded:unhealthy');
});
test('prioritizes mixed state over healthy running', function () {
$statuses = collect(['running:healthy', 'exited']);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 0);
expect($result)->toBe('degraded:unhealthy');
});
test('prioritizes running over paused/starting/exited', function () {
$statuses = collect(['running:healthy', 'starting', 'paused']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:healthy');
});
test('prioritizes dead over paused/starting/exited', function () {
$statuses = collect(['dead', 'paused', 'starting']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('prioritizes paused over starting/exited', function () {
$statuses = collect(['paused', 'starting', 'exited']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('paused:unknown');
});
test('prioritizes starting over exited', function () {
$statuses = collect(['starting', 'exited']);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('starting:unknown');
});
});
describe('aggregateFromContainers', function () {
test('returns exited:unhealthy for empty collection', function () {
$result = $this->aggregator->aggregateFromContainers(collect());
expect($result)->toBe('exited:unhealthy');
});
test('returns running:healthy for single healthy running container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => (object) ['Status' => 'healthy'],
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('running:healthy');
});
test('returns running:unhealthy for single unhealthy running container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => (object) ['Status' => 'unhealthy'],
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('running:unhealthy');
});
test('returns running:unknown for running container without health check', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => null,
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('running:unknown');
});
test('returns degraded:unhealthy for restarting container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'restarting',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('degraded:unhealthy');
});
test('returns degraded:unhealthy for mixed running and exited containers', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => (object) ['Status' => 'healthy'],
],
],
(object) [
'State' => (object) [
'Status' => 'exited',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('degraded:unhealthy');
});
test('returns degraded:unhealthy for crash loop (exited with restart count)', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'exited',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers, maxRestartCount: 5);
expect($result)->toBe('degraded:unhealthy');
});
test('returns exited:unhealthy for exited containers without restart count', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'exited',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers, maxRestartCount: 0);
expect($result)->toBe('exited:unhealthy');
});
test('returns degraded:unhealthy for dead container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'dead',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('degraded:unhealthy');
});
test('returns paused:unknown for paused container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'paused',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('paused:unknown');
});
test('returns starting:unknown for starting container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'starting',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('starting:unknown');
});
test('returns starting:unknown for created container', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'created',
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('starting:unknown');
});
test('handles multiple containers with various states', function () {
$containers = collect([
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => (object) ['Status' => 'healthy'],
],
],
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => (object) ['Status' => 'unhealthy'],
],
],
(object) [
'State' => (object) [
'Status' => 'running',
'Health' => null,
],
],
]);
$result = $this->aggregator->aggregateFromContainers($containers);
expect($result)->toBe('running:unhealthy');
});
});
describe('state priority enforcement', function () {
test('restarting has highest priority', function () {
$statuses = collect([
'restarting',
'running:healthy',
'dead',
'paused',
'starting',
'exited',
]);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('degraded:unhealthy');
});
test('crash loop has second highest priority', function () {
$statuses = collect([
'exited',
'running:healthy',
'paused',
'starting',
]);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 1);
expect($result)->toBe('degraded:unhealthy');
});
test('mixed state (running + exited) has third priority', function () {
$statuses = collect([
'running:healthy',
'exited',
'paused',
'starting',
]);
$result = $this->aggregator->aggregateFromStrings($statuses, maxRestartCount: 0);
expect($result)->toBe('degraded:unhealthy');
});
test('running:unhealthy has priority over running:unknown', function () {
$statuses = collect([
'running:unknown',
'running:unhealthy',
'running:healthy',
]);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unhealthy');
});
test('running:unknown has priority over running:healthy', function () {
$statuses = collect([
'running:unknown',
'running:healthy',
]);
$result = $this->aggregator->aggregateFromStrings($statuses);
expect($result)->toBe('running:unknown');
});
});
+32 -31
View File
@@ -13,17 +13,23 @@
it('ensures ComplexStatusCheck returns excluded status when all containers excluded', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
// Check that when all containers are excluded, the status calculation
// processes excluded containers and returns status with :excluded suffix
// Check that when all containers are excluded, ComplexStatusCheck uses the trait
expect($complexStatusCheckFile)
->toContain('// If all containers are excluded, calculate status from excluded containers')
->toContain('// but mark it with :excluded to indicate monitoring is disabled')
->toContain('if ($relevantContainerCount === 0) {')
->toContain("return 'running:unhealthy:excluded';")
->toContain("return 'running:unknown:excluded';")
->toContain("return 'running:healthy:excluded';")
->toContain('if ($relevantContainers->isEmpty()) {')
->toContain('return $this->calculateExcludedStatus($containers, $excludedContainers);');
// Check that the trait uses ContainerStatusAggregator and appends :excluded suffix
$traitFile = file_get_contents(__DIR__.'/../../app/Traits/CalculatesExcludedStatus.php');
expect($traitFile)
->toContain('ContainerStatusAggregator')
->toContain('appendExcludedSuffix')
->toContain('$aggregator->aggregateFromContainers($excludedOnly)')
->toContain("return 'degraded:excluded';")
->toContain("return 'exited:excluded';");
->toContain("return 'paused:excluded';")
->toContain("return 'exited:excluded';")
->toContain('return "$status:excluded";'); // For running:healthy:excluded
});
it('ensures Service model returns excluded status when all services excluded', function () {
@@ -32,64 +38,59 @@ it('ensures Service model returns excluded status when all services excluded', f
// Check that when all services are excluded from status checks,
// the Service model calculates real status and returns it with :excluded suffix
expect($serviceModelFile)
->toContain('// If all services are excluded from status checks, calculate status from excluded containers')
->toContain('// but mark it with :excluded to indicate monitoring is disabled')
->toContain('if (! $hasNonExcluded && ($complexStatus === null && $complexHealth === null)) {')
->toContain('// Calculate status from excluded containers')
->toContain('return "{$excludedStatus}:excluded";')
->toContain("return 'exited:excluded';");
->toContain('exclude_from_status')
->toContain(':excluded')
->toContain('CalculatesExcludedStatus');
});
it('ensures Service model returns unknown:excluded when no containers exist', function () {
it('ensures Service model returns unknown:unknown:excluded when no containers exist', function () {
$serviceModelFile = file_get_contents(__DIR__.'/../../app/Models/Service.php');
// Check that when a service has no applications or databases at all,
// the Service model returns 'unknown:excluded' instead of 'exited:excluded'
// the Service model returns 'unknown:unknown:excluded' instead of 'exited:unhealthy:excluded'
// This prevents misleading status display when containers don't exist
expect($serviceModelFile)
->toContain('// If no status was calculated at all (no containers exist), return unknown')
->toContain('if ($excludedStatus === null && $excludedHealth === null) {')
->toContain("return 'unknown:excluded';");
->toContain("return 'unknown:unknown:excluded';");
});
it('ensures GetContainersStatus returns null when all containers excluded', function () {
it('ensures GetContainersStatus calculates excluded status when all containers excluded', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Check that when all containers are excluded, the aggregateApplicationStatus
// method returns null to avoid updating status
// method calculates and returns status with :excluded suffix
expect($getContainersStatusFile)
->toContain('// If all containers are excluded, don\'t update status')
->toContain("if (\$relevantStatuses->isEmpty()) {\n return null;\n }");
->toContain('// If all containers are excluded, calculate status from excluded containers')
->toContain('if ($relevantStatuses->isEmpty()) {')
->toContain('return $this->calculateExcludedStatusFromStrings($containerStatuses);');
});
it('ensures exclude_from_hc flag is properly checked in ComplexStatusCheck', function () {
$complexStatusCheckFile = file_get_contents(__DIR__.'/../../app/Actions/Shared/ComplexStatusCheck.php');
// Verify that exclude_from_hc is properly parsed from docker-compose
// Verify that exclude_from_hc is parsed using trait helper
expect($complexStatusCheckFile)
->toContain('$excludeFromHc = data_get($serviceConfig, \'exclude_from_hc\', false);')
->toContain('if ($excludeFromHc || $restartPolicy === \'no\') {')
->toContain('$excludedContainers->push($serviceName);');
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
});
it('ensures exclude_from_hc flag is properly checked in GetContainersStatus', function () {
$getContainersStatusFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Verify that exclude_from_hc is properly parsed from docker-compose
// Verify that exclude_from_hc is parsed using trait helper
expect($getContainersStatusFile)
->toContain('$excludeFromHc = data_get($serviceConfig, \'exclude_from_hc\', false);')
->toContain('if ($excludeFromHc || $restartPolicy === \'no\') {')
->toContain('$excludedContainers->push($serviceName);');
->toContain('$excludedContainers = $this->getExcludedContainersFromDockerCompose($dockerComposeRaw);');
});
it('ensures UI displays excluded status correctly in status component', function () {
$servicesStatusFile = file_get_contents(__DIR__.'/../../resources/views/components/status/services.blade.php');
// Verify that the status component detects :excluded suffix and shows monitoring disabled message
// Verify that the status component transforms :excluded suffix to (excluded) for better display
expect($servicesStatusFile)
->toContain('$isExcluded = str($complexStatus)->endsWith(\':excluded\');')
->toContain('$displayStatus = $isExcluded ? str($complexStatus)->beforeLast(\':excluded\') : $complexStatus;')
->toContain('(Monitoring Disabled)');
->toContain('$parts = explode(\':\', $complexStatus);')
->toContain('// Has health status: running:unhealthy:excluded → Running (unhealthy, excluded)')
->toContain('// No health status: exited:excluded → Exited (excluded)');
});
it('ensures UI handles excluded status in service heading buttons', function () {
@@ -1,184 +0,0 @@
<?php
/**
* Unit tests for PushServerUpdateJob status aggregation logic.
*
* These tests verify that the job correctly aggregates container statuses
* when processing Sentinel updates, with proper handling of:
* - running (healthy) - all containers running and healthy
* - running (unhealthy) - some containers unhealthy
* - running (unknown) - some containers with unknown health status
*
* The aggregation follows a priority system: unhealthy > unknown > healthy
*
* This ensures consistency with GetContainersStatus::aggregateApplicationStatus()
* and prevents the bug where "unknown" status was incorrectly converted to "healthy".
*/
it('aggregates status with unknown health state correctly', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify that hasUnknown tracking variable exists
expect($jobFile)
->toContain('$hasUnknown = false;')
->toContain('if (str($status)->contains(\'unknown\')) {')
->toContain('$hasUnknown = true;');
// Verify 3-way status priority logic (unhealthy > unknown > healthy)
expect($jobFile)
->toContain('if ($hasUnhealthy) {')
->toContain('$aggregatedStatus = \'running (unhealthy)\';')
->toContain('} elseif ($hasUnknown) {')
->toContain('$aggregatedStatus = \'running (unknown)\';')
->toContain('} else {')
->toContain('$aggregatedStatus = \'running (healthy)\';');
});
it('checks for unknown status alongside unhealthy status', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify unknown check is placed alongside unhealthy check
expect($jobFile)
->toContain('if (str($status)->contains(\'unhealthy\')) {')
->toContain('if (str($status)->contains(\'unknown\')) {');
});
it('follows same priority as GetContainersStatus', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
$getContainersFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Both should track hasUnknown
expect($jobFile)->toContain('$hasUnknown = false;');
expect($getContainersFile)->toContain('$hasUnknown = false;');
// Both should check for 'unknown' in status strings
expect($jobFile)->toContain('if (str($status)->contains(\'unknown\')) {');
expect($getContainersFile)->toContain('if (str($status)->contains(\'unknown\')) {');
// Both should prioritize unhealthy over unknown over healthy
expect($jobFile)->toContain('} elseif ($hasUnknown) {');
expect($getContainersFile)->toContain('} elseif ($hasUnknown) {');
});
it('does not default unknown to healthy status', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// The old buggy code was:
// $aggregatedStatus = $hasUnhealthy ? 'running (unhealthy)' : 'running (healthy)';
// This would make unknown -> healthy
// Verify we're NOT using ternary operator for status assignment
expect($jobFile)
->not->toContain('$aggregatedStatus = $hasUnhealthy ? \'running (unhealthy)\' : \'running (healthy)\';');
// Verify we ARE using if-elseif-else with proper unknown handling
expect($jobFile)
->toContain('if ($hasUnhealthy) {')
->toContain('} elseif ($hasUnknown) {')
->toContain('$aggregatedStatus = \'running (unknown)\';');
});
it('initializes all required status tracking variables', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify all three tracking variables are initialized together
$pattern = '/\$hasRunning\s*=\s*false;\s*\$hasUnhealthy\s*=\s*false;\s*\$hasUnknown\s*=\s*false;/s';
expect(preg_match($pattern, $jobFile))->toBe(1,
'All status tracking variables ($hasRunning, $hasUnhealthy, $hasUnknown) should be initialized together');
});
it('preserves unknown status through sentinel updates', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// The critical path: when a status contains 'running' AND 'unknown',
// both flags should be set
expect($jobFile)
->toContain('if (str($status)->contains(\'running\')) {')
->toContain('$hasRunning = true;')
->toContain('if (str($status)->contains(\'unhealthy\')) {')
->toContain('$hasUnhealthy = true;')
->toContain('if (str($status)->contains(\'unknown\')) {')
->toContain('$hasUnknown = true;');
// And then unknown should have priority over healthy in aggregation
expect($jobFile)
->toContain('} elseif ($hasUnknown) {')
->toContain('$aggregatedStatus = \'running (unknown)\';');
});
it('implements service multi-container aggregation', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify service container collection exists
expect($jobFile)
->toContain('public Collection $serviceContainerStatuses;')
->toContain('$this->serviceContainerStatuses = collect();');
// Verify aggregateServiceContainerStatuses method exists
expect($jobFile)
->toContain('private function aggregateServiceContainerStatuses()')
->toContain('$this->aggregateServiceContainerStatuses();');
// Verify service aggregation uses same logic as applications
expect($jobFile)
->toContain('$hasUnknown = false;');
});
it('services use same priority as applications', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Both aggregation methods should use the same priority logic
$applicationAggregation = <<<'PHP'
if ($hasUnhealthy) {
$aggregatedStatus = 'running (unhealthy)';
} elseif ($hasUnknown) {
$aggregatedStatus = 'running (unknown)';
} else {
$aggregatedStatus = 'running (healthy)';
}
PHP;
// Count occurrences - should appear twice (once for apps, once for services)
$occurrences = substr_count($jobFile, $applicationAggregation);
expect($occurrences)->toBeGreaterThanOrEqual(2, 'Priority logic should appear for both applications and services');
});
it('collects service containers before aggregating', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify service containers are collected, not immediately updated
expect($jobFile)
->toContain('$key = $serviceId.\':\'.$subType.\':\'.$subId;')
->toContain('$this->serviceContainerStatuses->get($key)->put($containerName, $containerStatus);');
// Verify aggregation happens after collection
expect($jobFile)
->toContain('$this->aggregateMultiContainerStatuses();')
->toContain('$this->aggregateServiceContainerStatuses();');
});
it('defaults to unknown when health_status is missing from Sentinel data', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
// Verify we use null coalescing to default to 'unknown', not 'unhealthy'
// This is critical for containers without healthcheck defined
expect($jobFile)
->toContain('$rawHealthStatus = data_get($container, \'health_status\');')
->toContain('$containerHealth = $rawHealthStatus ?? \'unknown\';')
->not->toContain('data_get($container, \'health_status\', \'unhealthy\')');
});
it('matches SSH path default health status behavior', function () {
$jobFile = file_get_contents(__DIR__.'/../../app/Jobs/PushServerUpdateJob.php');
$getContainersFile = file_get_contents(__DIR__.'/../../app/Actions/Docker/GetContainersStatus.php');
// Both paths should default to 'unknown' when health status is missing
// Sentinel path: health_status field missing -> 'unknown'
expect($jobFile)->toContain('?? \'unknown\'');
// SSH path: State.Health.Status missing -> 'unknown'
expect($getContainersFile)->toContain('?? \'unknown\'');
// Neither should use 'unhealthy' as default for missing health status
expect($jobFile)->not->toContain('data_get($container, \'health_status\', \'unhealthy\')');
});
+53
View File
@@ -0,0 +1,53 @@
<?php
use App\Models\Application;
use App\Models\Server;
/**
* Test the Application::serverStatus() accessor
*
* This accessor determines if the underlying server infrastructure is functional.
* It should check Server::isFunctional() for the main server and all additional servers.
* It should NOT be affected by container/application health status (e.g., degraded:unhealthy).
*
* The bug that was fixed: Previously, it checked pivot.status and returned false
* when any additional server had status != 'running', including 'degraded:unhealthy'.
* This caused false "server has problems" warnings when the server was fine but
* containers were unhealthy.
*/
it('checks server infrastructure health not container status', function () {
// This is a documentation test to explain the fix
// The serverStatus accessor should:
// 1. Check if main server is functional (Server::isFunctional())
// 2. Check if each additional server is functional (Server::isFunctional())
// 3. NOT check pivot.status (that's application/container status, not server status)
//
// Before fix: Checked pivot.status !== 'running', causing false positives
// After fix: Only checks Server::isFunctional() for infrastructure health
expect(true)->toBeTrue();
})->note('The serverStatus accessor now correctly checks only server infrastructure health, not container status');
it('has correct logic in serverStatus accessor', function () {
// Read the actual code to verify the fix
$reflection = new ReflectionClass(Application::class);
$source = file_get_contents($reflection->getFileName());
// Extract just the serverStatus accessor method
preg_match('/protected function serverStatus\(\): Attribute\s*\{.*?^\s{4}\}/ms', $source, $matches);
$serverStatusCode = $matches[0] ?? '';
expect($serverStatusCode)->not->toBeEmpty('serverStatus accessor should exist');
// Check that the new logic exists (checks isFunctional on each server)
expect($serverStatusCode)
->toContain('$main_server_functional = $this->destination?->server?->isFunctional()')
->toContain('foreach ($this->additional_servers as $server)')
->toContain('if (! $server->isFunctional())');
// Check that the old buggy logic is removed from serverStatus accessor
expect($serverStatusCode)
->not->toContain('pluck(\'pivot.status\')')
->not->toContain('str($status)->before(\':\')')
->not->toContain('if ($server_status !== \'running\')');
})->note('Verifies that the serverStatus accessor uses the correct logic');