-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from nick322/patch-1
(#22) Add health-check:health command
- Loading branch information
Showing
6 changed files
with
236 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace UKFast\HealthCheck\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use UKFast\HealthCheck\Facade\HealthCheck; | ||
|
||
class StatusCommand extends Command | ||
{ | ||
protected $signature = ' | ||
health-check:status | ||
{--only= : comma separated checks names to run} | ||
{--except= : comma separated checks names to skip} | ||
'; | ||
|
||
protected $description = 'Check health status'; | ||
|
||
public function handle() | ||
{ | ||
$only = (string)$this->option('only'); | ||
$except = (string)$this->option('except'); | ||
|
||
if ($only && $except) { | ||
$this->error('Pass --only OR --except, but not both!'); | ||
|
||
return 1; | ||
} | ||
|
||
$onlyChecks = array_map('trim', explode(',', $only)); | ||
$exceptChecks = array_map('trim', explode(',', $except)); | ||
|
||
$problems = []; | ||
/** @var \UKFast\HealthCheck\HealthCheck $check */ | ||
foreach (HealthCheck::all() as $check) { | ||
if ($only && !in_array($check->name(), $onlyChecks)) { | ||
continue; | ||
} elseif ($except && in_array($check->name(), $exceptChecks)) { | ||
continue; | ||
} | ||
|
||
$status = $check->status(); | ||
|
||
if ($status->isProblem()) { | ||
$problems[] = [$check->name(), $status->name(), $status->message()]; | ||
} | ||
} | ||
|
||
if (!$isOkay = empty($problems)) { | ||
$this->table(['name', 'status', 'message'], $problems); | ||
} | ||
|
||
$this->info('All checks passed successfully'); | ||
|
||
return $isOkay ? 0 : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Tests\Commands; | ||
|
||
use Illuminate\Testing\PendingCommand; | ||
use Mockery; | ||
use Mockery\MockInterface; | ||
use Tests\TestCase; | ||
use UKFast\HealthCheck\Checks\DatabaseHealthCheck; | ||
use UKFast\HealthCheck\Checks\LogHealthCheck; | ||
use UKFast\HealthCheck\HealthCheckServiceProvider; | ||
use UKFast\HealthCheck\Status; | ||
|
||
class StatusCommandTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function running_command_status() | ||
{ | ||
$this->app->register(HealthCheckServiceProvider::class); | ||
config(['healthcheck.checks' => [LogHealthCheck::class]]); | ||
|
||
$status = new Status(); | ||
$status->okay(); | ||
$this->mockLogHealthCheck($status); | ||
|
||
$result = $this->artisan('health-check:status'); | ||
|
||
if ($result instanceof PendingCommand) { | ||
$result->assertExitCode(0); | ||
} else { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function running_command_status_with_only_option() | ||
{ | ||
$this->app->register(HealthCheckServiceProvider::class); | ||
|
||
$status = new Status(); | ||
$status->okay(); | ||
$this->mockLogHealthCheck($status); | ||
|
||
$result = $this->artisan('health-check:status', ['--only' => 'log']); | ||
|
||
if ($result instanceof PendingCommand) { | ||
$result->assertExitCode(0); | ||
} else { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function running_command_status_with_except_option() | ||
{ | ||
$this->app->register(HealthCheckServiceProvider::class); | ||
config(['healthcheck.checks' => [LogHealthCheck::class, DatabaseHealthCheck::class]]); | ||
|
||
$status = new Status(); | ||
$status->okay(); | ||
$this->mockLogHealthCheck($status); | ||
|
||
$result = $this->artisan('health-check:status', ['--except' => 'database']); | ||
|
||
if ($result instanceof PendingCommand) { | ||
$result->assertExitCode(0); | ||
} else { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function running_command_status_with_only_and_except_option() | ||
{ | ||
$this->app->register(HealthCheckServiceProvider::class); | ||
|
||
$result = $this->artisan('health-check:status', ['--only' => 'log', '--except' => 'log']); | ||
|
||
if ($result instanceof PendingCommand) { | ||
$result | ||
->assertExitCode(1) | ||
->expectsOutput('Pass --only OR --except, but not both!'); | ||
} else { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function running_command_status_with_failure_condition() | ||
{ | ||
$this->app->register(HealthCheckServiceProvider::class); | ||
config(['healthcheck.checks' => [LogHealthCheck::class]]); | ||
$status = new Status(); | ||
$status->withName('statusName')->problem('statusMessage'); | ||
$this->mockLogHealthCheck($status); | ||
|
||
$result = $this->artisan('health-check:status'); | ||
|
||
if ($result instanceof PendingCommand) { | ||
$result->assertExitCode(1); | ||
//for laravel 5.* | ||
if (method_exists($result, 'expectsTable')) { | ||
$result->expectsTable(['name', 'status', 'message'], [['log', 'statusName', 'statusMessage']]); | ||
} | ||
} | ||
} | ||
|
||
private function mockLogHealthCheck(Status $status) | ||
{ | ||
$this->instance( | ||
LogHealthCheck::class, | ||
Mockery::mock(LogHealthCheck::class, function (MockInterface $mock) use ($status) { | ||
$mock->shouldReceive('name')->andReturn('log'); | ||
$mock->shouldReceive('status')->andReturn($status); | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters