diff --git a/modules/monitoring_mail/monitoring_mail.module b/modules/monitoring_mail/monitoring_mail.module index c53f6c8..c022e71 100644 --- a/modules/monitoring_mail/monitoring_mail.module +++ b/modules/monitoring_mail/monitoring_mail.module @@ -87,12 +87,9 @@ function monitoring_mail_monitoring_run_sensors(array $results) { continue; } - $status_old = SensorResultInterface::STATUS_UNKNOWN; - - // @todo Implement interface for SensorResultEntity. - // Try to load the previous log result for this sensor. - if ($previous_result = monitoring_sensor_result_second_last($result->getSensorId())) { + $status_old = SensorResultInterface::STATUS_UNKNOWN; + if ($previous_result = $result->getPreviousResult()) { $status_old = $previous_result->getStatus(); } @@ -104,7 +101,6 @@ function monitoring_mail_monitoring_run_sensors(array $results) { 'sensor_config' => $result->getSensorConfig(), 'status_old' => $status_old, 'status_new' => $status_new, - 'last_result' => $previous_result, ]; // Trigger result transition mail. diff --git a/src/Result/SensorResult.php b/src/Result/SensorResult.php index a245459..0fe3a32 100644 --- a/src/Result/SensorResult.php +++ b/src/Result/SensorResult.php @@ -1,13 +1,10 @@ verboseOutput; } + /** + * {@inheritdoc} + */ + public function setPreviousResult(SensorResultEntity $previous_result = NULL) { + $this->previousResult = $previous_result; + } + + /** + * {@inheritdoc} + */ + public function getPreviousResult() { + return $this->previousResult; + } + } diff --git a/src/Result/SensorResultInterface.php b/src/Result/SensorResultInterface.php index b9b1da2..d740161 100644 --- a/src/Result/SensorResultInterface.php +++ b/src/Result/SensorResultInterface.php @@ -1,12 +1,9 @@ $sensor_config) { if ($result = $this->runSensor($sensor_config)) { + $result->setPreviousResult(monitoring_sensor_result_last($result->getSensorId())); $results[$name] = $result; } } @@ -224,8 +221,8 @@ class SensorRunner { $old_status = NULL; // Try to load the previous log result for this sensor. - if ($last_result = monitoring_sensor_result_last($result->getSensorId())) { - $old_status = $last_result->getStatus(); + if ($result->getPreviousResult()) { + $old_status = $result->getPreviousResult()->getStatus(); } // Check if we need to log the result. diff --git a/tests/src/Kernel/MonitoringCoreKernelTest.php b/tests/src/Kernel/MonitoringCoreKernelTest.php index e0f2c96..af9b8e8 100644 --- a/tests/src/Kernel/MonitoringCoreKernelTest.php +++ b/tests/src/Kernel/MonitoringCoreKernelTest.php @@ -9,6 +9,7 @@ namespace Drupal\Tests\monitoring\Kernel; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Logger\RfcLogLevel; use Drupal\monitoring\Entity\SensorConfig; +use Drupal\monitoring\Entity\SensorResultEntity; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; @@ -770,4 +771,114 @@ class MonitoringCoreKernelTest extends MonitoringUnitTestBase { $this->assertTrue($result->isCritical()); } + /** + * Tests if the previous sensor result retrieved is the expected one. + */ + public function testPreviousSensorResult() { + // Allow running all enabled sensors. + \Drupal::configFactory() + ->getEditable('monitoring.settings') + ->set('cron_run_sensors', TRUE) + ->save(); + + \Drupal::cache('default')->deleteAll(); + $sensor_runner = \Drupal::service('monitoring.sensor_runner'); + // There should be no logged sensor result at the moment. + $sensorConfig = SensorConfig::load('test_sensor_falls'); + $sensor_result_0 = monitoring_sensor_result_last($sensorConfig->id()); + $this->assertNull($sensor_result_0); + + // Run a sensor that is CRITICAL and check there is no previous result. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_1 */ + $sensor_result_1 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_1 = $sensor_result_1->getPreviousResult(); + $this->assertEquals('CRITICAL', $sensor_result_1->getStatus()); + $this->assertEquals($sensor_result_0, $previous_result_1); + //$this->assertSameResult($sensor_result_0, $previous_result_1); + $this->assertNull($previous_result_1); + + // Run the same sensor and check the previous result is $sensor_result_1. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_2 */ + $sensor_result_2 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_2 = $sensor_result_2->getPreviousResult(); + $this->assertEquals('CRITICAL', $sensor_result_2->getStatus()); + $this->assertEquals($sensor_result_1, $previous_result_2); + //$this->assertSameResult($sensor_result_0, $previous_result_2); + //$this->assertSameResult($sensor_result_1, $previous_result_2); + $this->assertNotNull($previous_result_2); + + // Load the same sensor and make sure no additional log is stored, because + // its status has not changed. + $sensorConfig = SensorConfig::load('test_sensor_falls'); + $sensor_result_2_0 = monitoring_sensor_result_last($sensorConfig->id()); + $this->assertNull($sensor_result_2_0); + + // Change sensor threshold settings so that the sensor switches to WARNING. + $sensorConfig->thresholds['critical'] = 0; + + // Run it again, a new log should be stored because the status has changed. + /** @var \Drupal\monitoring\Result\SensorResult $sensor_result_3 */ + $sensor_result_3 = $sensor_runner->runSensors([$sensorConfig])[0]; + $previous_result_3 = $sensor_result_3->getPreviousResult(); + $this->assertEquals('WARNING', $sensor_result_3->getStatus()); + $this->assertEquals($sensor_result_2, $previous_result_3); + //$this->assertNotSameResult($sensor_result_0, $previous_result_3); + //$this->assertSameResult($sensor_result_2, $previous_result_3); + $this->assertNotNull($previous_result_3); + } + + /** + * Passes if two sensor results are the same, fails otherwise. + * + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_1 + * The first sensor result to be checked. + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_2 + * The second sensor result to be checked. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * variables in the message text, not t(). If left blank, a default message + * will be displayed. + */ + protected function assertSameResult(SensorResultEntity $result_1, SensorResultEntity $result_2, $message = '') { + $this->assertSameResultHelper($result_1, $result_2, $message, TRUE); + } + + /** + * Passes if two sensor results are NOT the same, fails otherwise. + * + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_1 + * The first sensor result to be checked. + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_2 + * The second sensor result to be checked. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * variables in the message text, not t(). If left blank, a default message + * will be displayed. + */ + protected function assertNotSameResult(SensorResultEntity $result_1, SensorResultEntity $result_2, $message = '') { + $this->assertSameResultHelper($result_1, $result_2, $message, FALSE); + } + + /** + * Helper for assertSameResult and assertNotSameResult. + * + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_1 + * The first sensor result to be checked. + * @param \Drupal\monitoring\Entity\SensorResultEntity $result_2 + * The second sensor result to be checked. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * variables in the message text, not t(). If left blank, a default message + * will be displayed. + * @param bool $same_result + * (optional) TRUE if the results should be the same, FALSE otherwise. + * Defaults to TRUE. + */ + protected function assertSameResultHelper(SensorResultEntity $result_1, SensorResultEntity $result_2, $message = '', $same_result = TRUE) { + // @todo find a way to assert that two results are the same (probably some + // properties need to be ignored (for example $previousResult, $isCached). + } }