Testfixture insertion fixes

refs #4417
This commit is contained in:
Jannis Moßhammer 2013-07-18 14:53:47 +02:00
parent c36cbd3cb8
commit 8cc3139f17
8 changed files with 196 additions and 46 deletions

View File

@ -0,0 +1,102 @@
<?php
namespace Test\Monitoring\Application\Controllers\ListController;
require_once(dirname(__FILE__).'/../../testlib/MonitoringControllerTest.php');
use Test\Monitoring\Testlib\MonitoringControllerTest;
use Test\Monitoring\Testlib\Datasource\TestFixture;
use Test\Monitoring\Testlib\Datasource\ObjectFlags;
class ListControllerHostMySQLTest extends MonitoringControllerTest
{
public function testHostListMySQL()
{
$this->executeHostListTestFor("mysql");
}
public function testHostListPgSQL()
{
$this->executeHostListTestFor("pgsql");
}
public function testHostListStatus()
{
$this->executeHostListTestFor("statusdat");
}
public function executeHostListTestFor($backend)
{
date_default_timezone_set('UTC');
$checkTime = time()-2000;
$fixture = new TestFixture();
$firstHostFlags = ObjectFlags::PASSIVE_ONLY();
$firstHostFlags->acknowledged = 1;
$firstHostFlags->in_downtime = 1;
$firstHostFlags->notifications = 0;
$firstHostFlags->flapping = 1;
$firstHostFlags->time = $checkTime;
$fixture->addHost('host1', 1, $firstHostFlags, array(
"address" => "10.92.1.5",
"icon_image" => "myIcon.png",
"notes_url" => "note1.html",
"action_url" => "action.html"))->
addToHostgroup('router')->
addComment('author', 'host comment text')->
addService('svc1', 2)->
addService('svc2', 2)->
addService('svc3', 2, ObjectFlags::ACKNOWLEDGED())->
addService('svc4', 0);
$fixture->addHost('host2', 1)->
addService('svc1', 2);
$fixture->addHost('host3', 0)->
addService('svc1', 0);
$fixture->addHost('host4', 0)->
addService('svc1', 0);
$fixture->addHost('host5', 2, ObjectFlags::ACKNOWLEDGED())->
addService('svc1', 3)->addComment('author','svc comment');
try {
$this->setupFixture($fixture, $backend);
} catch (\PDOException $e) {
echo $e->getMessage();
$this->markTestSkipped('Could not setup fixture for backends '.implode(',',$backends).' :'.$e->getMessage());
return null;
}
$controller = $this->requireController('ListController', $backend);
$controller->hostsAction();
$result = $controller->view->hosts->fetchAll();
$this->assertCount(5, $result, 'Testing correct result count for '.$backend);
for($i=1;$i<=5;$i++) {
$this->assertEquals('host'.$i, $result[$i-1]->host_name, "Asserting correct host names for backend ".$backend);
}
$hostToTest = $result[0];
$persistedLastCheck = explode("+", $hostToTest->host_last_check);
$persistedLastCheck = $persistedLastCheck[0];
$this->assertEquals("10.92.1.5", $hostToTest->host_address, "Testing for correct host address field (backend ".$backend.")");
$this->assertEquals(1, $hostToTest->host_state, "Testing for status being DOWN (backend ".$backend.")");
$this->assertEquals(date("Y-m-d H:i:s", intval($checkTime)), $persistedLastCheck, "Testing for correct last check time format (backend ".$backend.")");
$this->assertEquals($checkTime, $hostToTest->host_last_state_change, "Testing for correct last state change (backend ".$backend.")");
$this->assertEquals("Plugin output for host host1", $hostToTest->host_output, "Testing correct output for host (backend ".$backend.")");
$this->assertEquals("Long plugin output for host host1", $hostToTest->host_long_output, "Testing correct long output for host (backend ".$backend.")");
$this->assertEquals(0, $hostToTest->host_notifications_enabled, "Testing for disabled notifications (backend ".$backend.')');
$this->assertEquals(1, $hostToTest->host_acknowledged, "Testing for host being acknowledged (backend ".$backend.')');
$this->assertEquals(1, $hostToTest->host_in_downtime, "Testing for host being in downtime (backend ".$backend.')');
$this->assertEquals(1, $hostToTest->host_is_flapping, "Testing for host being flapping (backend ".$backend.')');
$this->assertEquals(1, $hostToTest->host_last_comment, 'Testing correct comment count for first host (backend '.$backend.')');
$this->assertEquals(0, $hostToTest->host_state_type, 'Testing for soft state');
$this->assertEquals(1, $hostToTest->host_handled, 'Testing for handled host (backend '.$backend.')');
$this->assertEquals("myIcon.png", $hostToTest->host_icon_image, 'Testing for icon image (backend '.$backend.')');
$this->assertEquals("note1.html", $hostToTest->host_notes_url, 'Testing for notes url (backend '.$backend.')');
$this->assertEquals("action.html", $hostToTest->host_action_url, 'Testing for action url (backend '.$backend.')');
$this->assertEquals(2, $hostToTest->host_unhandled_service_count, 'Testing correct open problems count (backend '.$backend.')');
}
}

View File

@ -28,6 +28,7 @@ namespace Test\Monitoring\Testlib
use Test\Monitoring\Testlib\DataSource\TestFixture; use Test\Monitoring\Testlib\DataSource\TestFixture;
use Test\Monitoring\Testlib\DataSource\DataSourceTestSetup; use Test\Monitoring\Testlib\DataSource\DataSourceTestSetup;
use Monitoring\Backend\Ido; use Monitoring\Backend\Ido;
use Monitoring\Backend\Statusdat;
class MonitoringControllerTest extends \PHPUnit_Framework_TestCase class MonitoringControllerTest extends \PHPUnit_Framework_TestCase
{ {
@ -48,7 +49,6 @@ namespace Test\Monitoring\Testlib
$this->requireBase(); $this->requireBase();
$this->requireViews(); $this->requireViews();
$this->requireQueries();
} }
private function requireBase() private function requireBase()
@ -61,33 +61,43 @@ namespace Test\Monitoring\Testlib
require_once('Exception/ProgrammingError.php'); require_once('Exception/ProgrammingError.php');
require_once('library/Monitoring/Backend/AbstractBackend.php'); require_once('library/Monitoring/Backend/AbstractBackend.php');
require_once('library/Monitoring/Backend/Ido.php');
} }
private function requireQueries() private function requireIDOQueries()
{
require_once('library/Monitoring/Backend/Ido.php');
$this->requireFolder('library/Monitoring/Backend/Ido/Query');
}
private function requireFolder($folder)
{ {
$module = $this->moduleDir; $module = $this->moduleDir;
$views = scandir($module.'library/Monitoring/Backend/Ido/Query'); $views = scandir($module.$folder);
foreach ($views as $view) { foreach ($views as $view) {
if (!preg_match('/php$/', $view)) { if (!preg_match('/php$/', $view)) {
continue; continue;
} }
require_once($module.'library/Monitoring/Backend/Ido/Query/'.$view); require_once($module.$folder."/".$view);
} }
} }
private function requireStatusDatQueries()
{
require_once('library/Monitoring/Backend/Statusdat.php');
require_once($this->moduleDir.'/library/Monitoring/Backend/Statusdat/Query/Query.php');
$this->requireFolder('library/Monitoring/Backend/Statusdat');
$this->requireFolder('library/Monitoring/Backend/Statusdat/Criteria');
$this->requireFolder('library/Monitoring/Backend/Statusdat/Query');
$this->requireFolder('library/Monitoring/Backend/Statusdat/DataView');
$this->requireFolder('library/Monitoring/Backend/Statusdat/DataView');
}
private function requireViews() private function requireViews()
{ {
$module = $this->moduleDir; $module = $this->moduleDir;
require_once($module.'library/Monitoring/View/MonitoringView.php'); require_once($module.'library/Monitoring/View/MonitoringView.php');
$this->requireFolder('library/Monitoring/View/');
$views = scandir($module.'library/Monitoring/View');
foreach ($views as $view) {
if (!preg_match('/php$/', $view)) {
continue;
}
require_once($module.'library/Monitoring/View/'.$view);
}
} }
public function requireController($controller, $backend) public function requireController($controller, $backend)
@ -109,6 +119,7 @@ namespace Test\Monitoring\Testlib
public function getBackendFor($type) { public function getBackendFor($type) {
if ($type == "mysql" || $type == "pgsql") { if ($type == "mysql" || $type == "pgsql") {
$this->requireIDOQueries();
return new Ido(new \Zend_Config(array( return new Ido(new \Zend_Config(array(
"dbtype"=> $type, "dbtype"=> $type,
'host' => "localhost", 'host' => "localhost",
@ -117,10 +128,13 @@ namespace Test\Monitoring\Testlib
'db' => "icinga_unittest" 'db' => "icinga_unittest"
))); )));
} else if ($type == "statusdat") { } else if ($type == "statusdat") {
return new Reader(new \Zend_Config(array( $this->requireStatusDatQueries();
return new Statusdat(new \Zend_Config(array(
'status_file' => '/tmp/teststatus.dat', 'status_file' => '/tmp/teststatus.dat',
'objects_file' => '/tmp/testobjects.cache' 'objects_file' => '/tmp/testobjects.cache',
)), null, true); 'no_cache' => true
)));
} }
} }
} }

View File

@ -33,6 +33,8 @@ class DataSourceTestSetup implements SetupStrategy, InsertionStrategy
if ($type == 'mysql') { if ($type == 'mysql') {
$this->setupStrategy = new MySQLSetupStrategy(); $this->setupStrategy = new MySQLSetupStrategy();
$this->insertionStrategy = new PDOInsertionStrategy(); $this->insertionStrategy = new PDOInsertionStrategy();
$this->insertionStrategy->datetimeFormat = "Y-m-d H:i:s";
} elseif ($type == 'pgsql') { } elseif ($type == 'pgsql') {
$this->setupStrategy = new PgSQLSetupStrategy(); $this->setupStrategy = new PgSQLSetupStrategy();
$this->insertionStrategy = new PDOInsertionStrategy(); $this->insertionStrategy = new PDOInsertionStrategy();

View File

@ -8,33 +8,46 @@ class ObjectFlags {
public $passive_checks = 1; public $passive_checks = 1;
public $acknowledged = 0; public $acknowledged = 0;
public $in_downtime = 0; public $in_downtime = 0;
public $is_pending = 0;
public $time = 0; public $time = 0;
public function __construct($ageInSeconds = null) { public function __construct($ageInSeconds = null)
{
if(!is_int($ageInSeconds)) if(!is_int($ageInSeconds))
$ageInSeconds = 0; $ageInSeconds = 0;
$this->time = time()-$ageInSeconds; $this->time = time()-$ageInSeconds;
} }
public static function FLAPPING() { public static function FLAPPING()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->flapping = 0; $flags->flapping = 0;
return $flags; return $flags;
} }
public static function DISABLE_NOTIFICATIONS() { public static function PENDING()
{
$flags = new ObjectFlags();
$flags->is_pending = 1;
return $flags;
}
public static function DISABLE_NOTIFICATIONS()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->notifications = 0; $flags->notifications = 0;
return $flags; return $flags;
} }
public static function PASSIVE_ONLY() { public static function PASSIVE_ONLY()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->active_checks = 0; $flags->active_checks = 0;
return $flags; return $flags;
} }
public static function ACTIVE_ONLY() { public static function ACTIVE_ONLY()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->passive_checks = 0; $flags->passive_checks = 0;
return $flags; return $flags;
@ -48,13 +61,15 @@ class ObjectFlags {
return $flags; return $flags;
} }
public static function ACKNOWLEDGED() { public static function ACKNOWLEDGED()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->acknowledged = 1; $flags->acknowledged = 1;
return $flags; return $flags;
} }
public static function IN_DOWNTIME() { public static function IN_DOWNTIME()
{
$flags = new ObjectFlags(); $flags = new ObjectFlags();
$flags->in_downtime = 1; $flags->in_downtime = 1;
return $flags; return $flags;

View File

@ -33,10 +33,10 @@ class StatusdatTemplates {
current_event_id=14756 current_event_id=14756
current_problem_id=6016 current_problem_id=6016
last_problem_id=6010 last_problem_id=6010
plugin_output=host_00 (checked by localhost.localdomain) FLAP: flap hostcheck down plugin_output=Plugin output for host {{HOST_NAME}}
long_plugin_output= long_plugin_output=Long plugin output for host {{HOST_NAME}}
performance_data= performance_data=
last_check=1373991189 last_check={{TIME}}
next_check=1374002661 next_check=1374002661
check_options=0 check_options=0
current_attempt=2 current_attempt=2
@ -105,8 +105,8 @@ EOF;
last_time_warning=0 last_time_warning=0
last_time_unknown=0 last_time_unknown=0
last_time_critical=1373024663 last_time_critical=1373024663
plugin_output=host_00 (checked by localhost.localdomain) CRITICAL: critical critical_6 plugin_output=Plugin output for service {{SERVICE_NAME}}
long_plugin_output= long_plugin_output=Long plugin output for service {{SERVICE_NAME}}
performance_data=runtime=0.012226 performance_data=runtime=0.012226
last_check=1373087666 last_check=1373087666
next_check=1374002401 next_check=1374002401
@ -123,8 +123,8 @@ EOF;
active_checks_enabled={{ACTIVE_ENABLED}} active_checks_enabled={{ACTIVE_ENABLED}}
passive_checks_enabled={{PASSIVE_ENABLED}} passive_checks_enabled={{PASSIVE_ENABLED}}
event_handler_enabled=1 event_handler_enabled=1
problem_has_been_acknowledged=0 problem_has_been_acknowledged={{ACKNOWLEDGED}}
acknowledgement_type={{ACKNOWLEDGED}} acknowledgement_type=1
acknowledgement_end_time=0 acknowledgement_end_time=0
flap_detection_enabled=1 flap_detection_enabled=1
failure_prediction_enabled=1 failure_prediction_enabled=1

View File

@ -34,7 +34,6 @@ class MySQLSetupStrategy implements SetupStrategy {
if ($connection === null) { if ($connection === null) {
$connection = new \PDO("mysql:dbname=icinga_unittest", "icinga_unittest", "icinga_unittest"); $connection = new \PDO("mysql:dbname=icinga_unittest", "icinga_unittest", "icinga_unittest");
} }
echo "teardown";
$tables = $connection->query("SHOW TABLES")->fetchAll(); $tables = $connection->query("SHOW TABLES")->fetchAll();
foreach($tables as $table) { foreach($tables as $table) {

View File

@ -47,9 +47,10 @@ class PDOInsertionStrategy {
); );
$insertHostStatusQuery = $this->connection->prepare( $insertHostStatusQuery = $this->connection->prepare(
'INSERT INTO icinga_hoststatus'. 'INSERT INTO icinga_hoststatus'.
'(host_object_id, current_state, last_check, notifications_enabled, '. '(host_object_id, current_state, last_check, last_state_change, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth)'. 'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth,'.
' VALUES (?, ?, ?, ?, ?, ?, ?, ?)' 'output, long_output, '.
'problem_has_been_acknowledged, has_been_checked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
); );
$insertCVQuery = $this->connection->prepare( $insertCVQuery = $this->connection->prepare(
'INSERT INTO icinga_customvariablestatus'. 'INSERT INTO icinga_customvariablestatus'.
@ -60,12 +61,15 @@ class PDOInsertionStrategy {
$insertObjectQuery->execute(array($this->objectId, $host["name"])); $insertObjectQuery->execute(array($this->objectId, $host["name"]));
$insertHostQuery->execute(array( $insertHostQuery->execute(array(
$this->objectId, $host["name"], $host["name"]." alias", $host["address"], $this->objectId, $this->objectId, $host["name"]." alias", $host["name"], $host["address"], $this->objectId,
$host["icon_image"], $host["notes_url"], $host["action_url"] $host["icon_image"], $host["notes_url"], $host["action_url"]
)); ));
$insertHostStatusQuery->execute(array( $insertHostStatusQuery->execute(array(
$this->objectId, $host["state"], date($this->datetimeFormat, $flags->time), $flags->notifications, $this->objectId, $host["state"], date($this->datetimeFormat, $flags->time),
$flags->active_checks, $flags->passive_checks, $flags->flapping, $flags->in_downtime)); date($this->datetimeFormat, $flags->time), $flags->notifications, $flags->active_checks,
$flags->passive_checks, $flags->flapping, $flags->in_downtime, "Plugin output for host ".$host["name"],
"Long plugin output for host ".$host["name"], $flags->acknowledged, $flags->is_pending == 0
));
foreach($host["contacts"] as $contact) { foreach($host["contacts"] as $contact) {
$insertContactQuery->execute(array($this->objectId, $contact["object_id"])); $insertContactQuery->execute(array($this->objectId, $contact["object_id"]));
@ -93,9 +97,10 @@ class PDOInsertionStrategy {
); );
$insertServiceStatusQuery = $this->connection->prepare( $insertServiceStatusQuery = $this->connection->prepare(
'INSERT INTO icinga_servicestatus'. 'INSERT INTO icinga_servicestatus'.
'(service_object_id, current_state, last_check, notifications_enabled, '. '(service_object_id, current_state, last_check, last_state_change, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth)'. 'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth,'.
' VALUES (?, ?, ?, ?, ?, ?, ?, ?)' 'output, long_output, '.
'problem_has_been_acknowledged, has_been_checked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) '
); );
$insertContactQuery = $this->connection->prepare( $insertContactQuery = $this->connection->prepare(
'INSERT INTO icinga_service_contacts (host_id, contact_object_id) VALUES (?, ?);' 'INSERT INTO icinga_service_contacts (host_id, contact_object_id) VALUES (?, ?);'
@ -114,8 +119,12 @@ class PDOInsertionStrategy {
$service["notes_url"], $service["action_url"], $service["icon_image"] $service["notes_url"], $service["action_url"], $service["icon_image"]
)); ));
$insertServiceStatusQuery->execute(array( $insertServiceStatusQuery->execute(array(
$this->objectId, $service["state"], date($this->datetimeFormat, $flags->time), $flags->notifications, $this->objectId, $service["state"], date($this->datetimeFormat, $flags->time),
$flags->active_checks, $flags->passive_checks, $flags->flapping, $flags->in_downtime)); date($this->datetimeFormat, $flags->time), $flags->notifications, $flags->active_checks,
$flags->passive_checks, $flags->flapping, $flags->in_downtime, "Plugin output for service ".$service["name"],
"Long plugin output for service ".$service["name"], $flags->acknowledged,
$flags->is_pending == 0
));
foreach($service["contacts"] as $contact) { foreach($service["contacts"] as $contact) {
$insertContactQuery->execute(array($this->objectId, $contact["object_id"])); $insertContactQuery->execute(array($this->objectId, $contact["object_id"]));
@ -148,9 +157,10 @@ class PDOInsertionStrategy {
} }
private function insertComments() private function insertComments()
{ { $comment_id=0;
$insertCommentsQuery = $this->connection->prepare( $insertCommentsQuery = $this->connection->prepare(
'INSERT INTO icinga_comments (object_id, comment_type, author_name, comment_data) VALUES (?, ?, ?, ?);' 'INSERT INTO icinga_comments (object_id, comment_type, internal_comment_id, author_name, comment_data)'.
' VALUES (?, ?, ?, ?, ?);'
); );
$comments = &$this->fixture->getComments(); $comments = &$this->fixture->getComments();
foreach ($comments as $comment) { foreach ($comments as $comment) {
@ -161,7 +171,9 @@ class PDOInsertionStrategy {
$type = 2; $type = 2;
$object_id = $comment["service"]["object_id"]; $object_id = $comment["service"]["object_id"];
} }
$insertCommentsQuery->execute(array($object_id, $type, $comment["author"], $comment["text"])); $insertCommentsQuery->execute(array(
$object_id, $type, $comment_id++, $comment["author"], $comment["text"]
));
} }
} }

View File

@ -74,6 +74,9 @@ class StatusdatInsertionStrategy implements InsertionStrategy {
{ {
$hosts = $this->fixture->getHosts(); $hosts = $this->fixture->getHosts();
foreach ($hosts as $host) { foreach ($hosts as $host) {
if ($host['flags']->is_pending) {
continue; // Pending states are not written to status.dat yet
}
$hostDefinition = str_replace( $hostDefinition = str_replace(
array('\t', array('\t',
'{{HOST_NAME}}', '{{HOST_ADDRESS}}', '{{ICON_IMAGE}}', '{{HOST_NAME}}', '{{HOST_ADDRESS}}', '{{ICON_IMAGE}}',
@ -93,6 +96,9 @@ class StatusdatInsertionStrategy implements InsertionStrategy {
{ {
$services = $this->fixture->getServices(); $services = $this->fixture->getServices();
foreach ($services as $service) { foreach ($services as $service) {
if ($service['flags']->is_pending) {
continue; // Pending states are not written to status.dat yet
}
$cvs = ''; $cvs = '';
foreach ($service['customvariables'] as $name=>$var) { foreach ($service['customvariables'] as $name=>$var) {
$cvs .= '_'.$name.'='.$var; $cvs .= '_'.$name.'='.$var;
@ -179,7 +185,7 @@ class StatusdatInsertionStrategy implements InsertionStrategy {
private function insertComments() private function insertComments()
{ {
$comments = $this->fixture->getComments(); $comments = $this->fixture->getComments();
$commentId = 0; $commentId = 1;
foreach($comments as $comment) { foreach($comments as $comment) {
if (isset($comment["service"])) { if (isset($comment["service"])) {
$service = $comment["service"]; $service = $comment["service"];