Skip to content

Commit 72bfa88

Browse files
committed
tests: Introduce SlaTest
1 parent b76aa63 commit 72bfa88

File tree

4 files changed

+501
-0
lines changed

4 files changed

+501
-0
lines changed

test/php/Lib/FakeReportData.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Tests\Icinga\Module\Icingadb\Lib;
4+
5+
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimelines;
6+
7+
class FakeReportData
8+
{
9+
use SlaTimelines;
10+
11+
public function setDimensions(array $_)
12+
{
13+
}
14+
15+
public function getDimensions()
16+
{
17+
return [];
18+
}
19+
20+
public function setRows(array $rows)
21+
{
22+
}
23+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tests\Icinga\Module\Icingadb\Lib;
4+
5+
use DateTime;
6+
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaReportUtils;
7+
use Icinga\Module\Icingadb\ProvidedHook\Reporting\Common\SlaTimeline;
8+
use ipl\Sql\Connection;
9+
10+
class SlaReportWithCustomDb
11+
{
12+
use SlaReportUtils;
13+
14+
public const HOST_NAME = 'icinga2';
15+
16+
public const SERVICE_NAME = 'disk';
17+
18+
/** @var Connection */
19+
protected static $conn;
20+
21+
protected $reportType;
22+
23+
protected static $dbConfiguration = [
24+
'mysql' => [
25+
'db' => 'mysql',
26+
'host' => '127.0.0.1',
27+
'port' => 3306,
28+
'dbname' => 'icingadb_web_unittest',
29+
'username' => 'icingadb_web_unittest',
30+
'password' => 'icingadb_web_unittest'
31+
]
32+
];
33+
34+
public function resetConn()
35+
{
36+
static::$conn = null;
37+
}
38+
39+
public function getDb(): Connection
40+
{
41+
if (! static::$conn) {
42+
$config = static::$dbConfiguration['mysql'];
43+
$host = getenv('ICINGADBWEB_TEST_MYSQL_HOST');
44+
if ($host) {
45+
$config['host'] = $host;
46+
}
47+
48+
$port = getenv('ICINGADBWEB_TEST_MYSQL_PORT');
49+
if ($port) {
50+
$config['port'] = $port;
51+
}
52+
53+
static::$conn = new Connection($config);
54+
$fixtures = file_get_contents(__DIR__ . '/fixtures.sql');
55+
static::$conn->exec($fixtures);
56+
}
57+
58+
return static::$conn;
59+
}
60+
61+
public function getReportType(): string
62+
{
63+
return $this->reportType;
64+
}
65+
66+
public function getSlaTimeline(DateTime $start, DateTime $end, string $type): SlaTimeline
67+
{
68+
$this->reportType = $type;
69+
$name = $type === 'host'
70+
? static::HOST_NAME
71+
: static::HOST_NAME . static::$hostServiceSeparator . static::SERVICE_NAME;
72+
73+
return $this->fetchReportData($start, $end, ['filter' => null])->getTimelines($name)[0];
74+
}
75+
76+
protected function createReportData()
77+
{
78+
return new FakeReportData();
79+
}
80+
81+
protected function createReportRow($row)
82+
{
83+
return 'NOPE!';
84+
}
85+
}

test/php/Lib/fixtures.sql

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
CREATE TABLE host (
2+
id binary(20) NOT NULL PRIMARY KEY,
3+
display_name varchar(254) NOT NULL
4+
);
5+
6+
CREATE TABLE host_state (
7+
id binary(20) NOT NULL,
8+
host_id binary(20) NOT NULL,
9+
hard_state TINYINT UNSIGNED NOT NULL,
10+
previous_hard_state TINYINT UNSIGNED DEFAULT NULL,
11+
12+
PRIMARY KEY(id, host_id)
13+
);
14+
15+
CREATE TABLE service (
16+
id binary(20) NOT NULL,
17+
host_id binary(20) NOT NULL,
18+
display_name varchar(254) NOT NULL,
19+
20+
PRIMARY KEY(id, host_id)
21+
);
22+
23+
CREATE TABLE service_state (
24+
id binary(20) NOT NULL PRIMARY KEY,
25+
host_id binary(20) NOT NULL,
26+
service_id binary(20) NOT NULL,
27+
hard_state TINYINT UNSIGNED NOT NULL,
28+
previous_hard_state TINYINT UNSIGNED DEFAULT NULL
29+
);
30+
31+
CREATE TABLE sla_history_state (
32+
id binary(20) NOT NULL PRIMARY KEY,
33+
environment_id binary(20) DEFAULT NULL,
34+
endpoint_id binary(20) DEFAULT NULL,
35+
object_type enum('host', 'service') NOT NULL,
36+
host_id binary(20) NOT NULL,
37+
service_id binary(20) DEFAULT NULL,
38+
39+
event_time bigint unsigned NOT NULL,
40+
hard_state TINYINT UNSIGNED NOT NULL,
41+
previous_hard_state TINYINT UNSIGNED NOT NULL
42+
);
43+
44+
CREATE TABLE sla_history_downtime (
45+
id binary(20) NOT NULL PRIMARY KEY,
46+
environment_id binary(20) DEFAULT NULL,
47+
endpoint_id binary(20) DEFAULT NULL,
48+
object_type enum('host', 'service') NOT NULL,
49+
host_id binary(20) NOT NULL,
50+
service_id binary(20) DEFAULT NULL,
51+
52+
downtime_id binary(20) NOT NULL,
53+
downtime_start BIGINT UNSIGNED NOT NULL,
54+
downtime_end BIGINT UNSIGNED NOT NULL
55+
);

0 commit comments

Comments
 (0)