Skip to content

Commit

Permalink
Merge pull request #22 from ojooss/master
Browse files Browse the repository at this point in the history
add elasticsearch 8 support
  • Loading branch information
bretrzaun authored Jun 28, 2023
2 parents 44e0d64 + b61e789 commit 4ae575f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"twig/twig": "^2.0||^3.0"
},
"require-dev": {
"dg/bypass-finals": "^1.4",
"doctrine/dbal": "~3.2",
"doctrine/migrations": "^3.3",
"elasticsearch/elasticsearch": "^6.0||^7.0",
"elasticsearch/elasticsearch": "^6.0||^7.0||^8.0",
"mongodb/mongodb": "^1.4",
"monolog/monolog": "^2.3",
"nyholm/psr7": "^1.5",
Expand Down
11 changes: 8 additions & 3 deletions examples/standalone.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$checker = new StatusChecker();

$group01 = new StatusCheckerGroup('General');
$group01->addCheck(new PhpVersionCheck('PHP Version', '7.0.0', '7.2.0'));
$group01->addCheck(new PhpVersionCheck('PHP Version', '8.0.0', '8.2.0'));
$group01->addCheck(new PhpMemoryLimitCheck('memory_limit', 128));
$checker->addGroup($group01);

Expand Down Expand Up @@ -39,6 +39,11 @@
$checker->addCheck(new PhpExtensionCheck('PHP Extension / libxml', 'libxml'));
$checker->addCheck(new PhpExtensionCheck('PHP Extension / SimpleXML', 'SimpleXML'));

$client = \Elastic\Elasticsearch\ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
$checker->addCheck(new \BretRZaun\StatusPage\Check\ElasticsearchCheck('Elasticsearch', $client, ['foo', 'bar']));

// run the checks
$checker->check();

Expand All @@ -47,8 +52,8 @@
$showDetails = (bool)preg_match('|(?mi-Us)' . $whitelistPattern . '|', $_SERVER['REMOTE_ADDR']);

// use the built-in Twig template
$loader = new Twig_Loader_Filesystem(__DIR__ . '/../resources/views/');
$twig = new Twig_Environment($loader, ['autoescape' => false]);
$loader = new Twig\Loader\FilesystemLoader(__DIR__ . '/../resources/views/');
$twig = new Twig\Environment($loader, ['autoescape' => false]);

$content = $twig->render(
'status.twig',
Expand Down
36 changes: 27 additions & 9 deletions src/Check/ElasticsearchCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
namespace BretRZaun\StatusPage\Check;

use BretRZaun\StatusPage\Result;
use Elasticsearch\Client;
use Exception;
use function PHPUnit\Framework\isEmpty;

class ElasticsearchCheck extends AbstractCheck
{

/**
* @var Client
* @var \Elasticsearch\Client|\Elastic\Elasticsearch\Client
*/
protected $client;

Expand All @@ -22,12 +22,13 @@ class ElasticsearchCheck extends AbstractCheck
* Constructor
*
* @param string $label
* @param Client $client
* @param \Elasticsearch\Client|\Elastic\Elasticsearch\Client $client
* @param array $indices Indices to check for
*/
public function __construct(string $label, Client $client, array $indices = [])
public function __construct(string $label, \Elasticsearch\Client|\Elastic\Elasticsearch\Client $client, array $indices = [])
{
parent::__construct($label);

$this->client = $client;
$this->indices = $indices;
}
Expand All @@ -41,13 +42,30 @@ public function checkStatus(): Result
{
$result = new Result($this->label);
try {
if ($this->client->ping() !== true) {
$result->setSuccess(false);
return $result;
$info = $this->client->info();
$versionParts = explode('.', $info['version']['number']);
$esMajorVersion = (int)array_shift($versionParts);
if ($esMajorVersion >= 8) {
if ($this->client->ping()->asBool() !== true) {
$result->setError("Elasticsearch is not reachable (ping failed)");
return $result;
}
} else {
if ($this->client->ping() !== true) {
$result->setError("Elasticsearch is not reachable (ping failed)");
return $result;
}
}

foreach ($this->indices as $index) {
if (!$this->client->indices()->exists(['index' => $index])) {
$result->setError("Index '$index' does not exist");
if ($esMajorVersion >= 8) {
if (!$this->client->indices()->exists(['index' => $index])->asBool()) {
$result->setError("Index '$index' does not exist");
}
} else {
if (!$this->client->indices()->exists(['index' => $index])) {
$result->setError("Index '$index' does not exist");
}
}
}
} catch (Exception $e) {
Expand Down
55 changes: 48 additions & 7 deletions tests/Check/ElasticsearchCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@
namespace BretRZaun\StatusPage\Tests\Check;

use BretRZaun\StatusPage\Check\ElasticsearchCheck;
use Elasticsearch\Client;
use Elasticsearch\Namespaces\IndicesNamespace;
use DG\BypassFinals;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Endpoints\Indices;
use Elastic\Elasticsearch\Response\Elasticsearch;
use PHPUnit\Framework\TestCase;

// enable mock final classes

/**
* @runTestsInSeparateProcesses
*/
class ElasticsearchCheckTest extends TestCase
{

public function testSuccess(): void
{
BypassFinals::enable();

/** @noinspection PhpUnitInvalidMockingEntityInspection */
$client = $this->createMock(Client::class);
$client->expects($this->once())
->method('ping')
->method('info')
->willReturn(['version' => ['number' => '8.1.2']]);
$ping = $this->createMock(Elasticsearch::class);
$ping->expects($this->once())
->method('asBool')
->willReturn(true);
$client->expects($this->any())
->method('ping')
->willReturn($ping);

$check = new ElasticsearchCheck('elasticsearch test', $client);
$result = $check->checkStatus();
Expand All @@ -25,10 +42,20 @@ public function testSuccess(): void

public function testFailure(): void
{
BypassFinals::enable();

/** @noinspection PhpUnitInvalidMockingEntityInspection */
$client = $this->createMock(Client::class);
$client->expects($this->once())
->method('ping')
->method('info')
->willReturn(['version' => ['number' => '8.1.2']]);
$ping = $this->createMock(Elasticsearch::class);
$ping->expects($this->once())
->method('asBool')
->willReturn(false);
$client->expects($this->any())
->method('ping')
->willReturn($ping);

$check = new ElasticsearchCheck('elasticsearch test', $client);
$result = $check->checkStatus();
Expand All @@ -38,16 +65,30 @@ public function testFailure(): void

public function testMissingIndex(): void
{
BypassFinals::enable();

/** @noinspection PhpUnitInvalidMockingEntityInspection */
$client = $this->createMock(Client::class);
$client->expects($this->once())
->method('ping')
->method('info')
->willReturn(['version' => ['number' => '8.1.2']]);
$ping = $this->createMock(Elasticsearch::class);
$ping->expects($this->once())
->method('asBool')
->willReturn(true);
$client->expects($this->any())
->method('ping')
->willReturn($ping);

$incidesMock = $this->createMock(IndicesNamespace::class);
$exists = $this->createMock(Elasticsearch::class);
$exists->expects($this->once())
->method('asBool')
->willReturn(false);
$incidesMock = $this->createMock(Indices::class);
$incidesMock->expects($this->once())
->method('exists')
->with(['index' => 'notexisting-test-index'])
->willReturn(false);
->willReturn($exists);

$client->expects($this->once())
->method('indices')
Expand Down

0 comments on commit 4ae575f

Please sign in to comment.