Skip to content

Commit

Permalink
tests: Introduce SlaTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Feb 22, 2023
1 parent 6e0a30d commit a53504f
Show file tree
Hide file tree
Showing 4 changed files with 495 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/php/Lib/FakeReportData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Icinga\Module\Icingadb\Lib;

use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimelines;

class FakeReportData
{
use SlaTimelines;

public function setDimensions(array $_)
{
}

public function getDimensions()
{
return [];
}

public function setRows(array $rows)
{
}
}
78 changes: 78 additions & 0 deletions test/php/Lib/SlaReportWithCustomDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Tests\Icinga\Module\Icingadb\Lib;

use DateTime;
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaReportUtils;
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimeline;
use ipl\Sql\Connection;

class SlaReportWithCustomDb
{
use SlaReportUtils;

/** @var Connection */
protected static $conn;

protected $reportType;

protected static $dbConfiguration = [
'mysql' => [
'db' => 'mysql',
'host' => '127.0.0.1',
'port' => 3306,
'dbname' => 'icingadb_web_unittest',
'username' => 'icingadb_web_unittest',
'password' => 'icingadb_web_unittest'
]
];

public function resetConn()
{
static::$conn = null;
}

public function getDb(): Connection
{
if (! static::$conn) {
$config = static::$dbConfiguration['mysql'];
$host = getenv('ICINGADBWEB_TEST_MYSQL_HOST');
if ($host) {
$config['host'] = $host;
}

$port = getenv('ICINGADBWEB_TEST_MYSQL_PORT');
if ($port) {
$config['port'] = $port;
}

static::$conn = new Connection($config);
$fixtures = file_get_contents(__DIR__ . '/fixtures.sql');
static::$conn->exec($fixtures);
}

return static::$conn;
}

public function getReportType(): string
{
return $this->reportType;
}

public function getSlaTimeline(DateTime $start, DateTime $end, string $type, string $id): SlaTimeline
{
$this->reportType = $type;

return $this->fetchReportData($start, $end, ['filter' => null])->getTimelines(bin2hex($id))[0];
}

protected function createReportData()
{
return new FakeReportData();
}

protected function createReportRow($_)
{
return 'NOPE!';
}
}
55 changes: 55 additions & 0 deletions test/php/Lib/fixtures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
CREATE TABLE host (
id binary(20) NOT NULL PRIMARY KEY,
display_name varchar(254) NOT NULL
);

CREATE TABLE host_state (
id binary(20) NOT NULL,
host_id binary(20) NOT NULL,
hard_state TINYINT UNSIGNED NOT NULL,
previous_hard_state TINYINT UNSIGNED DEFAULT NULL,

PRIMARY KEY(id, host_id)
);

CREATE TABLE service (
id binary(20) NOT NULL,
host_id binary(20) NOT NULL,
display_name varchar(254) NOT NULL,

PRIMARY KEY(id, host_id)
);

CREATE TABLE service_state (
id binary(20) NOT NULL PRIMARY KEY,
host_id binary(20) NOT NULL,
service_id binary(20) NOT NULL,
hard_state TINYINT UNSIGNED NOT NULL,
previous_hard_state TINYINT UNSIGNED DEFAULT NULL
);

CREATE TABLE sla_history_state (
id binary(20) NOT NULL PRIMARY KEY,
environment_id binary(20) DEFAULT NULL,
endpoint_id binary(20) DEFAULT NULL,
object_type enum('host', 'service') NOT NULL,
host_id binary(20) NOT NULL,
service_id binary(20) DEFAULT NULL,

event_time bigint unsigned NOT NULL,
hard_state TINYINT UNSIGNED NOT NULL,
previous_hard_state TINYINT UNSIGNED NOT NULL
);

CREATE TABLE sla_history_downtime (
id binary(20) NOT NULL PRIMARY KEY,
environment_id binary(20) DEFAULT NULL,
endpoint_id binary(20) DEFAULT NULL,
object_type enum('host', 'service') NOT NULL,
host_id binary(20) NOT NULL,
service_id binary(20) DEFAULT NULL,

downtime_id binary(20) NOT NULL,
downtime_start BIGINT UNSIGNED NOT NULL,
downtime_end BIGINT UNSIGNED NOT NULL
);
Loading

0 comments on commit a53504f

Please sign in to comment.