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

View File

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

View File

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

View File

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

View File

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

View File

@ -47,9 +47,10 @@ class PDOInsertionStrategy {
);
$insertHostStatusQuery = $this->connection->prepare(
'INSERT INTO icinga_hoststatus'.
'(host_object_id, current_state, last_check, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth)'.
' VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
'(host_object_id, current_state, last_check, last_state_change, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth,'.
'output, long_output, '.
'problem_has_been_acknowledged, has_been_checked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
$insertCVQuery = $this->connection->prepare(
'INSERT INTO icinga_customvariablestatus'.
@ -60,12 +61,15 @@ class PDOInsertionStrategy {
$insertObjectQuery->execute(array($this->objectId, $host["name"]));
$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"]
));
$insertHostStatusQuery->execute(array(
$this->objectId, $host["state"], date($this->datetimeFormat, $flags->time), $flags->notifications,
$flags->active_checks, $flags->passive_checks, $flags->flapping, $flags->in_downtime));
$this->objectId, $host["state"], date($this->datetimeFormat, $flags->time),
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) {
$insertContactQuery->execute(array($this->objectId, $contact["object_id"]));
@ -93,9 +97,10 @@ class PDOInsertionStrategy {
);
$insertServiceStatusQuery = $this->connection->prepare(
'INSERT INTO icinga_servicestatus'.
'(service_object_id, current_state, last_check, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth)'.
' VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
'(service_object_id, current_state, last_check, last_state_change, notifications_enabled, '.
'active_checks_enabled, passive_checks_enabled, is_flapping, scheduled_downtime_depth,'.
'output, long_output, '.
'problem_has_been_acknowledged, has_been_checked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) '
);
$insertContactQuery = $this->connection->prepare(
'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"]
));
$insertServiceStatusQuery->execute(array(
$this->objectId, $service["state"], date($this->datetimeFormat, $flags->time), $flags->notifications,
$flags->active_checks, $flags->passive_checks, $flags->flapping, $flags->in_downtime));
$this->objectId, $service["state"], date($this->datetimeFormat, $flags->time),
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) {
$insertContactQuery->execute(array($this->objectId, $contact["object_id"]));
@ -148,9 +157,10 @@ class PDOInsertionStrategy {
}
private function insertComments()
{
{ $comment_id=0;
$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();
foreach ($comments as $comment) {
@ -161,7 +171,9 @@ class PDOInsertionStrategy {
$type = 2;
$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();
foreach ($hosts as $host) {
if ($host['flags']->is_pending) {
continue; // Pending states are not written to status.dat yet
}
$hostDefinition = str_replace(
array('\t',
'{{HOST_NAME}}', '{{HOST_ADDRESS}}', '{{ICON_IMAGE}}',
@ -93,6 +96,9 @@ class StatusdatInsertionStrategy implements InsertionStrategy {
{
$services = $this->fixture->getServices();
foreach ($services as $service) {
if ($service['flags']->is_pending) {
continue; // Pending states are not written to status.dat yet
}
$cvs = '';
foreach ($service['customvariables'] as $name=>$var) {
$cvs .= '_'.$name.'='.$var;
@ -179,7 +185,7 @@ class StatusdatInsertionStrategy implements InsertionStrategy {
private function insertComments()
{
$comments = $this->fixture->getComments();
$commentId = 0;
$commentId = 1;
foreach($comments as $comment) {
if (isset($comment["service"])) {
$service = $comment["service"];