View source
<?php
namespace Drupal\Tests\monitoring\Functional;
use Drupal\Core\Url;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\monitoring\Entity\SensorConfig;
class MonitoringServicesTest extends MonitoringTestBase {
public static $modules = array(
'dblog',
'basic_auth',
'monitoring',
'views',
'node',
'rest',
);
protected $servicesAccount;
public function setUp() {
parent::setUp();
$this->servicesAccount = $this
->drupalCreateUser(array(
'restful get monitoring-sensor',
'restful get monitoring-sensor-result',
));
}
public function testSensorConfig() {
$this
->drupalLogin($this->servicesAccount);
$response_data = $this
->doJsonRequest('monitoring-sensor');
$this
->assertResponse(200);
foreach (monitoring_sensor_manager()
->getAllSensorConfig() as $sensor_name => $sensor_config) {
$this
->assertEqual($response_data[$sensor_name]['sensor'], $sensor_config
->id());
$this
->assertEqual($response_data[$sensor_name]['label'], $sensor_config
->getLabel());
$this
->assertEqual($response_data[$sensor_name]['category'], $sensor_config
->getCategory());
$this
->assertEqual($response_data[$sensor_name]['description'], $sensor_config
->getDescription());
$this
->assertEqual($response_data[$sensor_name]['numeric'], $sensor_config
->isNumeric());
$this
->assertEqual($response_data[$sensor_name]['value_label'], $sensor_config
->getValueLabel());
$this
->assertEqual($response_data[$sensor_name]['caching_time'], $sensor_config
->getCachingTime());
$this
->assertEqual($response_data[$sensor_name]['time_interval'], $sensor_config
->getTimeIntervalValue());
$this
->assertEqual($response_data[$sensor_name]['enabled'], $sensor_config
->isEnabled());
$this
->assertEqual($response_data[$sensor_name]['uri'], Url::fromRoute('rest.monitoring-sensor.GET', [
'id' => $sensor_name,
'_format' => 'json',
])
->setAbsolute()
->toString());
if ($sensor_config
->isDefiningThresholds()) {
$this
->assertEqual($response_data[$sensor_name]['thresholds'], $sensor_config
->getThresholds());
}
}
$sensor_name = 'sensor_that_does_not_exist';
$this
->doJsonRequest('monitoring-sensor/' . $sensor_name);
$this
->assertResponse(404);
$sensor_name = 'dblog_event_severity_error';
$response_data = $this
->doJsonRequest('monitoring-sensor/' . $sensor_name);
$this
->assertResponse(200);
$sensor_config = SensorConfig::load($sensor_name);
$this
->assertEqual($response_data['sensor'], $sensor_config
->id());
$this
->assertEqual($response_data['label'], $sensor_config
->getLabel());
$this
->assertEqual($response_data['category'], $sensor_config
->getCategory());
$this
->assertEqual($response_data['description'], $sensor_config
->getDescription());
$this
->assertEqual($response_data['numeric'], $sensor_config
->isNumeric());
$this
->assertEqual($response_data['value_label'], $sensor_config
->getValueLabel());
$this
->assertEqual($response_data['caching_time'], $sensor_config
->getCachingTime());
$this
->assertEqual($response_data['time_interval'], $sensor_config
->getTimeIntervalValue());
$this
->assertEqual($response_data['enabled'], $sensor_config
->isEnabled());
$this
->assertEqual($response_data['uri'], Url::fromRoute('rest.monitoring-sensor.GET', [
'id' => $sensor_name,
'_format' => 'json',
])
->setAbsolute()
->toString());
if ($sensor_config
->isDefiningThresholds()) {
$this
->assertEqual($response_data['thresholds'], $sensor_config
->getThresholds());
}
}
public function testSensorCaching() {
$this
->drupalLogin($this->servicesAccount);
$sensor_name = 'dblog_event_severity_error';
$response_data = $this
->doJsonRequest('monitoring-sensor/' . $sensor_name);
$this
->assertResponse(200);
$sensor_config = SensorConfig::load($sensor_name);
$this
->assertEqual($response_data['label'], $sensor_config
->getLabel());
$sensor_config
->set('label', 'TestLabelForCaching');
$sensor_config
->save();
$response_data = $this
->doJsonRequest('monitoring-sensor/' . $sensor_name);
$this
->assertResponse(200);
$this
->assertEqual($response_data['label'], 'TestLabelForCaching');
}
public function testSensorResult() {
$this
->drupalLogin($this->servicesAccount);
$response_data = $this
->doJsonRequest('monitoring-sensor-result', array(
'expand' => 'sensor',
));
$this
->assertResponse(200);
foreach (monitoring_sensor_manager()
->getEnabledSensorConfig() as $sensor_name => $sensor_config) {
$this
->assertTrue(isset($response_data[$sensor_name]['sensor']));
$this
->assertSensorResult($response_data[$sensor_name], $sensor_config);
}
$response_data = $this
->doJsonRequest('monitoring-sensor-result');
$this
->assertResponse(200);
$this
->assertEqual('UNCACHEABLE', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER), 'Render array returned, rendered as HTML response, but uncacheable: Dynamic Page Cache is running, but not caching.');
$sensor_result = reset($response_data);
$this
->assertTrue(!isset($sensor_result['sensor_info']));
$this
->assertEqual(count($response_data), count(monitoring_sensor_manager()
->getEnabledSensorConfig()));
$sensor_name = 'sensor_that_does_not_exist';
$this
->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
$this
->assertResponse(404);
$sensor_name = 'monitoring_git_dirty_tree';
$this
->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
$this
->assertResponse(404);
$sensor_name = 'dblog_event_severity_error';
$response_data = $this
->doJsonRequest('monitoring-sensor-result/' . $sensor_name, array(
'expand' => 'sensor',
));
$this
->assertResponse(200);
$this
->assertTrue(isset($response_data['sensor']));
$this
->assertSensorResult($response_data, SensorConfig::load($sensor_name));
$response_data = $this
->doJsonRequest('monitoring-sensor-result/' . $sensor_name);
$this
->assertResponse(200);
$this
->assertTrue(!isset($response_data['sensor']));
}
protected function assertSensorResult($response_result, SensorConfig $sensor_config) {
$this
->assertEqual($response_result['sensor_name'], $sensor_config
->id());
$this
->assertEqual($response_result['uri'], Url::fromRoute('rest.monitoring-sensor-result.GET', [
'id' => $sensor_config
->id(),
'_format' => 'json',
])
->setAbsolute()
->toString());
if ($sensor_config
->getCachingTime()) {
$result = monitoring_sensor_run($sensor_config
->id());
$this
->assertEqual($response_result['status'], $result
->getStatus());
$this
->assertEqual($response_result['value'], $result
->getValue());
$this
->assertEqual($response_result['expected_value'], $result
->getExpectedValue());
$this
->assertEqual($response_result['numeric_value'], $result
->toNumber());
$this
->assertEqual($response_result['message'], $result
->getMessage());
$this
->assertEqual($response_result['timestamp'], $result
->getTimestamp());
$this
->assertEqual($response_result['execution_time'], $result
->getExecutionTime());
}
if (isset($response_result['sensor_info'])) {
$this
->assertEqual($response_result['sensor_info'], $sensor_config
->toArray());
}
}
}