From c36cbd3cb8a19309f03064723630a512999792b4 Mon Sep 17 00:00:00 2001 From: Jannis Mosshammer Date: Wed, 17 Jul 2013 11:07:39 +0000 Subject: [PATCH] Add StatusDat support for testfixtures refs #4417 --- .../php/testlib/MonitoringControllerTest.php | 6 + .../datasource/DataSourceTestSetup.php | 10 + .../schemes/ObjectsCacheTemplates.php | 97 ++++++++ .../datasource/schemes/statusdatTemplates.php | 171 ++++++++++++++ .../strategies/PDOInsertionStrategy.php | 2 +- .../datasource/strategies/SetupStrategy.php | 4 +- .../strategies/StatusdatInsertionStrategy.php | 210 ++++++++++++++++++ .../strategies/StatusdatSetupStrategy.php | 64 ++++++ 8 files changed, 561 insertions(+), 3 deletions(-) create mode 100644 modules/monitoring/test/php/testlib/datasource/schemes/ObjectsCacheTemplates.php create mode 100644 modules/monitoring/test/php/testlib/datasource/schemes/statusdatTemplates.php create mode 100644 modules/monitoring/test/php/testlib/datasource/strategies/StatusdatInsertionStrategy.php create mode 100644 modules/monitoring/test/php/testlib/datasource/strategies/StatusdatSetupStrategy.php diff --git a/modules/monitoring/test/php/testlib/MonitoringControllerTest.php b/modules/monitoring/test/php/testlib/MonitoringControllerTest.php index d9ff6f1f0..f6420ce09 100644 --- a/modules/monitoring/test/php/testlib/MonitoringControllerTest.php +++ b/modules/monitoring/test/php/testlib/MonitoringControllerTest.php @@ -24,6 +24,7 @@ namespace Icinga\Web namespace Test\Monitoring\Testlib { + use Icinga\Protocol\Statusdat\Reader; use Test\Monitoring\Testlib\DataSource\TestFixture; use Test\Monitoring\Testlib\DataSource\DataSourceTestSetup; use Monitoring\Backend\Ido; @@ -115,6 +116,11 @@ namespace Test\Monitoring\Testlib 'pass' => "icinga_unittest", 'db' => "icinga_unittest" ))); + } else if ($type == "statusdat") { + return new Reader(new \Zend_Config(array( + 'status_file' => '/tmp/teststatus.dat', + 'objects_file' => '/tmp/testobjects.cache' + )), null, true); } } } diff --git a/modules/monitoring/test/php/testlib/datasource/DataSourceTestSetup.php b/modules/monitoring/test/php/testlib/datasource/DataSourceTestSetup.php index 4249af36c..7e1f3a53b 100644 --- a/modules/monitoring/test/php/testlib/datasource/DataSourceTestSetup.php +++ b/modules/monitoring/test/php/testlib/datasource/DataSourceTestSetup.php @@ -8,6 +8,10 @@ require_once(dirname(__FILE__).'/strategies/SetupStrategy.php'); require_once(dirname(__FILE__).'/strategies/MySQLSetupStrategy.php'); require_once(dirname(__FILE__).'/strategies/PgSQLSetupStrategy.php'); require_once(dirname(__FILE__).'/strategies/PDOInsertionStrategy.php'); + +require_once(dirname(__FILE__).'/strategies/StatusdatInsertionStrategy.php'); +require_once(dirname(__FILE__).'/strategies/StatusdatSetupStrategy.php'); + require_once(dirname(__FILE__).'/TestFixture.php'); use \Test\Monitoring\Testlib\Datasource\Strategies\InsertionStrategy; @@ -15,6 +19,8 @@ use \Test\Monitoring\Testlib\Datasource\Strategies\SetupStrategy; use \Test\Monitoring\Testlib\Datasource\Strategies\MySQLSetupStrategy; use \Test\Monitoring\Testlib\Datasource\Strategies\PgSQLSetupStrategy; use \Test\Monitoring\Testlib\Datasource\Strategies\PDOInsertionStrategy; +use \Test\Monitoring\Testlib\Datasource\Strategies\StatusdatInsertionStrategy; +use \Test\Monitoring\Testlib\Datasource\Strategies\StatusdatSetupStrategy; class DataSourceTestSetup implements SetupStrategy, InsertionStrategy { @@ -31,6 +37,9 @@ class DataSourceTestSetup implements SetupStrategy, InsertionStrategy $this->setupStrategy = new PgSQLSetupStrategy(); $this->insertionStrategy = new PDOInsertionStrategy(); $this->insertionStrategy->datetimeFormat = "Y-m-d H:i:s"; + } elseif ($type == 'statusdat') { + $this->setupStrategy = new StatusdatSetupStrategy(); + $this->insertionStrategy = new StatusdatInsertionStrategy(); } else { throw new \Exception('Unsupported backend '.$type); } @@ -44,6 +53,7 @@ class DataSourceTestSetup implements SetupStrategy, InsertionStrategy public function setup($version = null, $connection = null) { $c = $this->setupStrategy->setup($version, $connection); + $this->insertionStrategy->setConnection($c); } diff --git a/modules/monitoring/test/php/testlib/datasource/schemes/ObjectsCacheTemplates.php b/modules/monitoring/test/php/testlib/datasource/schemes/ObjectsCacheTemplates.php new file mode 100644 index 000000000..a70be9996 --- /dev/null +++ b/modules/monitoring/test/php/testlib/datasource/schemes/ObjectsCacheTemplates.php @@ -0,0 +1,97 @@ +execute(array($this->objectId, $host["name"])); $insertHostQuery->execute(array( - $this->objectId, $host["name"], $host["name"], $host["address"], $this->objectId, + $this->objectId, $host["name"], $host["name"]." alias", $host["address"], $this->objectId, $host["icon_image"], $host["notes_url"], $host["action_url"] )); $insertHostStatusQuery->execute(array( diff --git a/modules/monitoring/test/php/testlib/datasource/strategies/SetupStrategy.php b/modules/monitoring/test/php/testlib/datasource/strategies/SetupStrategy.php index 88c217442..a55a0791b 100644 --- a/modules/monitoring/test/php/testlib/datasource/strategies/SetupStrategy.php +++ b/modules/monitoring/test/php/testlib/datasource/strategies/SetupStrategy.php @@ -3,6 +3,6 @@ namespace Test\Monitoring\Testlib\Datasource\Strategies; interface SetupStrategy { - public function setup($version = null, $connection = null); - public function teardown($connection = null); + public function setup($version = null, $resource = null); + public function teardown($resource = null); } \ No newline at end of file diff --git a/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatInsertionStrategy.php b/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatInsertionStrategy.php new file mode 100644 index 000000000..4a5b9a7e1 --- /dev/null +++ b/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatInsertionStrategy.php @@ -0,0 +1,210 @@ +statusDatFile = $ressource['status_file']; + $this->objectsCacheFile = $ressource['objects_file']; + } + + public function insert(TestFixture $fixture) + { + $this->fixture = $fixture; + $this->statusDat = '# Automatically created test statusdat from fixture\n'; + $this->objectsCache = ''; + $this->insertHoststatus(); + $this->insertHosts(); + $this->insertServicestatus(); + $this->insertServices(); + + $this->insertHostgroups(); + $this->insertServicegroups(); + $this->insertComments(); + + file_put_contents($this->statusDatFile, $this->statusDat); + file_put_contents($this->objectsCacheFile, $this->objectsCache); + } + + private function insertHoststatus() + { + $hosts = $this->fixture->getHosts(); + foreach ($hosts as $host) { + $cvs = ''; + foreach ($host['customvariables'] as $name=>$var) { + $cvs .= '_'.$name.'='.$var."\n"; + } + $flags = $host['flags']; + $hostStatus = str_replace( + array( + '{{HOST_NAME}}', '{{TIME}}', '{{NOTIFICATIONS_ENABLED}}', + '{{ACKNOWLEDGED}}', '{{ACTIVE_ENABLED}}', '{{PASSIVE_ENABLED}}', + '{{FLAPPING}}', '{{IN_DOWNTIME}}', '{{HOST_STATUS}}','{{CVS}}') + , array( + $host['name'], $flags->time, $flags->notifications, $flags->acknowledged, + $flags->active_checks, $flags->passive_checks, $flags->flapping, + $flags->in_downtime, $host['state'], $cvs + ), StatusdatTemplates::$HOST); + $this->statusDat .= "\n".$hostStatus; + } + } + + private function insertHosts() + { + $hosts = $this->fixture->getHosts(); + foreach ($hosts as $host) { + $hostDefinition = str_replace( + array('\t', + '{{HOST_NAME}}', '{{HOST_ADDRESS}}', '{{ICON_IMAGE}}', + '{{NOTES_URL}}', '{{ACTION_URL}}' + ), + array("\t", + $host['name'], $host['address'], $host['icon_image'], + $host['notes_url'], $host['action_url'] + ), + ObjectsCacheTemplates::$HOST + ); + $this->objectsCache .= "\n".$hostDefinition; + } + } + + private function insertServicestatus() + { + $services = $this->fixture->getServices(); + foreach ($services as $service) { + $cvs = ''; + foreach ($service['customvariables'] as $name=>$var) { + $cvs .= '_'.$name.'='.$var; + } + + $flags = $service['flags']; + $serviceStatus = str_replace( + array( + '{{HOST_NAME}}','{{SERVICE_NAME}}', '{{TIME}}', '{{NOTIFICATIONS_ENABLED}}', + '{{ACKNOWLEDGED}}', '{{ACTIVE_ENABLED}}', '{{PASSIVE_ENABLED}}', + '{{FLAPPING}}', '{{IN_DOWNTIME}}', '{{SERVICE_STATUS}}','{{CVS}}') + , array( + $service['host']['name'], $service['name'], $flags->time, $flags->notifications, + $flags->acknowledged, $flags->active_checks, $flags->passive_checks, + $flags->flapping, $flags->in_downtime, $service['state'], $cvs + ), StatusdatTemplates::$SERIVCE); + + + + $this->statusDat .= "\n".$serviceStatus; + } + + + } + + private function insertServices() + { + $services = $this->fixture->getServices(); + foreach ($services as $service) { + $serviceDefinition = str_replace( + array('\t', + '{{HOST_NAME}}', '{{SERVICE_NAME}}', '{{ICON_IMAGE}}', + '{{NOTES_URL}}', '{{ACTION_URL}}' + ), + array("\t", + $service['host']['name'], $service['name'], $service['icon_image'], + $service['notes_url'], $service['action_url'] + ), + ObjectsCacheTemplates::$SERVICE + ); + $this->objectsCache .= "\n".$serviceDefinition; + } + } + + private function insertGroup($type, $name, array $members) + { + $groupDefinition = str_replace( + array('\t', + '{{TYPE}}', '{{NAME}}', '{{MEMBERS}}' + ), + array("\t", + 'host', $name, implode(",", $members) + ), + ObjectsCacheTemplates::$GROUP + ); + $this->objectsCache .= "\n".$groupDefinition; + } + + private function insertHostgroups() + { + $hostgroups = $this->fixture->getHostgroups(); + foreach ($hostgroups as $hostgroup) { + $memberNames = array(); + foreach ($hostgroup["members"] as $member) { + $memberNames[] = $member["name"]; + } + $this->insertGroup("host", $hostgroup["name"], $memberNames); + } + } + + private function insertServicegroups() + { + $servicegroups = $this->fixture->getServicegroups(); + foreach ($servicegroups as $servicegroup) { + $memberNames = array(); + foreach ($servicegroup["members"] as $member) { + $memberNames[] = $member["host"]["name"]; + $memberNames[] = $member["name"]; + } + $this->insertGroup("service", $serviegroup["name"], $memberNames); + } + } + + private function insertComments() + { + $comments = $this->fixture->getComments(); + $commentId = 0; + foreach($comments as $comment) { + if (isset($comment["service"])) { + $service = $comment["service"]; + $commentDefinition = str_replace( + array('{{HOST_NAME}}', '{{SERVICE_NAME}}', '{{TIME}}', '{{AUTHOR}}', '{{TEXT}}', '{{ID}}'), + array( + $service["host"]["name"], $service["name"], $service["flags"]->time, + $comment["author"], $comment["text"], $commentId++ + ), + StatusdatTemplates::$SERVICECOMMENT + ); + } elseif (isset($comment["host"])) { + $host = $comment["host"]; + $commentDefinition = str_replace( + array('{{HOST_NAME}}', '{{TIME}}', '{{AUTHOR}}', '{{TEXT}}', '{{ID}}'), + array( + $host["name"], $host["flags"]->time, + $comment["author"], $comment["text"], $commentId++ + ), + StatusdatTemplates::$HOSTCOMMENT + ); + } + $this->statusDat .= "\n".$commentDefinition; + + } + + } +} \ No newline at end of file diff --git a/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatSetupStrategy.php b/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatSetupStrategy.php new file mode 100644 index 000000000..d5051cc68 --- /dev/null +++ b/modules/monitoring/test/php/testlib/datasource/strategies/StatusdatSetupStrategy.php @@ -0,0 +1,64 @@ +requireFolder($folder."/".$file); + } elseif (preg_match("/\.php/", $file)) { + require_once($folder."/".$file); + } + } + } + + private function requireStatusDat() + { + $moduleDir = realpath(dirname(__FILE__)."/../../../../../"); + $appDir = realpath($moduleDir."/../../"); + $base = $appDir."/library/Icinga/Protocol/StatusDat"; + require_once("Zend/Cache.php"); + require_once("Zend/Log.php"); + require_once($appDir."/library/Icinga/Application/Logger.php"); + require_once($appDir."/library/Icinga/Protocol/AbstractQuery.php"); + require_once($base."/Exception/ParsingException.php"); + require_once($base."/Query/IQueryPart.php"); + require_once($base."/IReader.php"); + $this->requireFolder($base); + + } + + public function setup($version = null, $resource = null) + { + if ($resource == null) { + $resource = array( + "status_file" => "/tmp/teststatus.dat", + "objects_file" => "/tmp/testobjects.cache" + ); + } + $this->requireStatusDat(); + $this->teardown($resource); + touch($resource["status_file"]); + touch($resource["objects_file"]); + return $resource; + } + + public function teardown($resource = null) + { + + if (file_exists($resource["status_file"])) { + unlink($resource["status_file"]); + } + if (file_exists($resource["objects_file"])) { + unlink($resource["objects_file"]); + } + } +} \ No newline at end of file