function MonitoringCoreTest::testSensors in Monitoring 7
Tests individual sensors.
File
- test/
tests/ monitoring.core.test, line 30 - Contains \MonitoringCoreTest.
Class
- MonitoringCoreTest
- Tests for cron sensor.
Code
function testSensors() {
// ======= SensorCronLastRunAge tests ======= //
$time_shift = 60 * 60 * 24 + 1;
variable_set('cron_last', REQUEST_TIME - $time_shift);
$result = $this
->runSensor('core_cron_last_run_age');
$this
->assertTrue($result
->isWarning());
$this
->assertEqual($result
->getValue(), $time_shift);
$time_shift = 60 * 60 * 24 * 3 + 1;
variable_set('cron_last', REQUEST_TIME - $time_shift);
$result = $this
->runSensor('core_cron_last_run_age');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getValue(), $time_shift);
drupal_cron_run();
$result = $this
->runSensor('core_cron_last_run_age');
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getValue(), 0);
// ======= Cron safe threshold (poormanscron) tests ======= //
$result = $this
->runSensor('core_cron_safe_threshold');
$this
->assertTrue($result
->isCritical());
variable_set('cron_safe_threshold', '0');
$result = $this
->runSensor('core_cron_safe_threshold');
$this
->assertTrue($result
->isOk());
// ======= SensorQueue tests ======= //
/** @var DrupalQueueInterface $queue */
$queue = DrupalQueue::get('monitoring_test');
$queue
->createItem(array());
$queue
->createItem(array());
$result = $this
->runSensor('core_queue_monitoring_test');
$this
->assertEqual($result
->getValue(), 2);
// ======= SensorCoreRequirements tests ======= //
$result = $this
->runSensor('core_requirements_monitoring_test');
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'Requirements check OK');
// Set basic requirements saying that all is ok.
$requirements = array(
'requirement1' => array(
'title' => 'requirement1',
'description' => 'requirement1 description',
'severity' => REQUIREMENT_OK,
),
'requirement_excluded' => array(
'title' => 'excluded requirement',
'description' => 'requirement that should be excluded from monitoring by the sensor',
// Set the severity to ERROR to test if the sensor result is not
// affected by this requirement.
'severity' => REQUIREMENT_ERROR,
),
);
variable_set('monitoring_test_requirements', $requirements);
// Set requirements exclude keys into the sensor settings.
$settings = monitoring_sensor_settings_get('core_requirements_monitoring_test');
$settings['exclude keys'] = array(
'requirement_excluded',
);
monitoring_sensor_settings_save('core_requirements_monitoring_test', $settings);
// We still should have OK status but with different message
$result = $this
->runSensor('core_requirements_monitoring_test');
// We expect OK status as REQUIREMENT_ERROR is set by excluded requirement.
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'requirement1, requirement1 description');
// Add warning state.
$requirements['requirement2'] = array(
'title' => 'requirement2',
'description' => 'requirement2 description',
'severity' => REQUIREMENT_WARNING,
);
variable_set('monitoring_test_requirements', $requirements);
// Now the sensor should have escalated to the requirement in warning state.
$result = $this
->runSensor('core_requirements_monitoring_test');
$this
->assertTrue($result
->isWarning());
$this
->assertEqual($result
->getMessage(), 'requirement2, requirement2 description');
// Add error state.
$requirements['requirement3'] = array(
'title' => 'requirement3',
'description' => 'requirement3 description',
'severity' => REQUIREMENT_ERROR,
);
variable_set('monitoring_test_requirements', $requirements);
// Now the sensor should have escalated to the requirement in critical state.
$result = $this
->runSensor('core_requirements_monitoring_test');
$this
->assertTrue($result
->isCritical());
$this
->assertEqual($result
->getMessage(), 'requirement3, requirement3 description');
// ======= Watchdog 404 in last 24 hours tests ======= //
watchdog('page not found', 'not/found');
$result = $this
->runSensor('dblog_404');
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), '1 watchdog events in 1 day, not/found');
$this
->assertEqual($result
->getValue(), 1);
for ($i = 1; $i <= 20; $i++) {
watchdog('page not found', 'not/found');
}
$result = $this
->runSensor('dblog_404');
$this
->assertEqual($result
->getValue(), 21);
$this
->assertTrue($result
->isWarning());
for ($i = 0; $i <= 100; $i++) {
watchdog('page not found', 'not/found/another');
}
$result = $this
->runSensor('dblog_404');
$this
->assertEqual($result
->getValue(), 101);
$this
->assertTrue($result
->isCritical());
// ======= SensorImageMissingStyle tests ======= //
for ($i = 0; $i <= 5; $i++) {
watchdog('image', 'Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array(
'%source_image_path' => 'public://portrait-pictures/redmouse.jpeg',
'%derivative_path' => 'hash://styles/preview/1234.jpeg',
));
}
watchdog('image', 'Source image at %source_image_path not found while trying to generate derivative image at %derivative_path.', array(
'%source_image_path' => 'public://portrait-pictures/bluemouse.jpeg',
'%derivative_path' => 'hash://styles/preview/5678.jpeg',
));
$result = $this
->runSensor('dblog_image_missing_style');
$this
->assertEqual(6, $result
->getValue());
$this
->assertTrue(strpos($result
->getMessage(), 'public://portrait-pictures/redmouse.jpeg') !== FALSE);
$this
->assertTrue($result
->isWarning());
// ======= Watchdog sensor tests ======= //
// Create watchdog entry with severity alert.
watchdog('test', 'test message', array(), WATCHDOG_ALERT);
// Run sensor and test the output.
$severities = monitoring_event_severities();
$result = $this
->runSensor('dblog_event_severity_' . $severities[WATCHDOG_ALERT]);
$this
->assertEqual($result
->getValue(), 1);
// ======= SensorUserFailedLogins tests ======= //
watchdog('user', 'Login attempt failed for %user.', array(
'%user' => 'user1',
), WATCHDOG_NOTICE);
watchdog('user', 'Login attempt failed for %user.', array(
'%user' => 'user1',
), WATCHDOG_NOTICE);
watchdog('user', 'Login attempt failed for %user.', array(
'%user' => 'user2',
), WATCHDOG_NOTICE);
$result = $this
->runSensor('user_failed_logins');
$this
->assertEqual($result
->getValue(), 3);
$this
->assertTrue(strpos($result
->getMessage(), 'user1: 2') !== FALSE);
$this
->assertTrue(strpos($result
->getMessage(), 'user2: 1') !== FALSE);
// ======= Sensor user_session_logouts tests ======= //
watchdog('user', 'Session closed for %name.', array(
'%user' => 'user1',
), WATCHDOG_NOTICE);
watchdog('user', 'Session closed for %name.', array(
'%user' => 'user1',
), WATCHDOG_NOTICE);
watchdog('user', 'Session closed for %name.', array(
'%user' => 'user2',
), WATCHDOG_NOTICE);
$result = $this
->runSensor('user_session_logouts');
$this
->assertEqual($result
->getValue(), 3);
$this
->assertEqual($result
->getMessage(), '3 logouts in 1 day');
// ======= SensorGitDirtyTree tests ======= //
// Enable the sensor and set cmd to output something
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'printf "A addedfile.txt\\nM sites/all/modules/monitoring/test/tests/monitoring.core.test\\nD deleted file.txt"',
'ahead_cmd' => 'true',
'submodules_cmd' => 'true',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isCritical());
// The verbose output should contain the cmd output.
$verboseOutput = $result
->getVerboseOutput();
$this
->assertTrue(strpos($verboseOutput['status']['output']['#markup'], "<pre>A addedfile.txt\nM sites/all/modules/monitoring/test/tests/monitoring.core.test\nD deleted file.txt</pre>") !== FALSE);
// Check if the sensor message has the expected value.
$this
->assertEqual($result
->getMessage(), "3 files in unexpected state: A addedfile.txt, M …modules/monitoring/test/tests/monitoring.core.test");
// Now echo empty string
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'submodules_cmd' => 'true',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isOk());
// The message should say that it is ok.
$this
->assertTrue(strpos($result
->getMessage(), 'Git repository clean') !== FALSE);
// Test 2 commits ahead of origin.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'printf "a4ea5e3ac5b7cca0c96aee4d00447c36121bd128\\n299d85344fab9befbf6a275a4b64bda7b464b493"',
'submodules_cmd' => 'true',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isWarning());
$verboseOutput = $result
->getVerboseOutput();
$this
->assertTrue(strpos($verboseOutput['ahead']['output']['#markup'], "<pre>a4ea5e3ac5b7cca0c96aee4d00447c36121bd128\n299d85344fab9befbf6a275a4b64bda7b464b493</pre>") !== FALSE);
// Test not in main branch.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'check_branch' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'actual_branch_cmd' => 'printf "7.0.x"',
'expected_branch' => '8.0.x',
'submodules_cmd' => 'true',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isWarning());
$verboseOutput = $result
->getVerboseOutput();
$this
->assertTrue(strpos($verboseOutput['check_branch']['output']['#markup'], '7.0.x') !== FALSE);
$this
->assertEqual($result
->getMessage(), 'Active branch 7.0.x, expected 8.0.x');
// Test same as main branch.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'check_branch' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'actual_branch_cmd' => 'printf "8.0.x"',
'expected_branch' => '8.0.x',
'submodules_cmd' => 'true',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'Git repository clean');
// Test submodule not initialized.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'submodules_cmd' => 'printf -- "-a5066d1778b9ec7c86631234ff2795e777bdff12 test"',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isCritical());
debug($result
->getVerboseOutput());
$verboseOutput = $result
->getVerboseOutput();
$this
->assertTrue(strpos($verboseOutput['submodules']['output']['#markup'], "<pre>-a5066d1778b9ec7c86631234ff2795e777bdff12 test</pre>") !== FALSE);
$this
->assertEqual($result
->getMessage(), '1 submodules in unexpected state: -a5066d1778b9ec7c86631234ff2795e777bdff12 test');
// Test submodule with uncommitted changes.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'submodules_cmd' => 'printf "+156fff6ee58497fa22b57c32e22e3f13377b4120 test (8.x-1.0-240-g156fff6)"',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isCritical());
debug($result
->getVerboseOutput());
$verboseOutput = $result
->getVerboseOutput();
$this
->assertTrue(strpos($verboseOutput['submodules']['output']['#markup'], "<pre>+156fff6ee58497fa22b57c32e22e3f13377b4120 test (8.x-1.0-240-g156fff6)</pre>") !== FALSE);
$this
->assertEqual($result
->getMessage(), '1 submodules in unexpected state: +156fff6ee58497fa22b57c32e22e3f13377b4120 test (8.x-1.0-240-g156fff6)');
// Test submodules in expected state.
monitoring_sensor_settings_save('monitoring_git_dirty_tree', array(
'enabled' => TRUE,
'status_cmd' => 'true',
'ahead_cmd' => 'true',
'submodules_cmd' => 'printf "a5066d1778b9ec7c86631234ff2795e777bdff12 test\\n156fff6ee58497fa22b57c32e22e3f13377b4120 test (8.x-1.0-240-g156fff6)"',
));
$result = monitoring_sensor_run('monitoring_git_dirty_tree', TRUE, TRUE);
$this
->assertTrue($result
->isOk());
$this
->assertEqual($result
->getMessage(), 'Git repository clean');
// ======= Active sessions count tests ======= //
// Create and login a user to have data in the sessions table.
$test_user = $this
->drupalCreateUser();
$this
->drupalLogin($test_user);
$result = $this
->runSensor('user_sessions_authenticated');
$this
->assertEqual($result
->getValue(), 1);
$result = $this
->runSensor('user_sessions_all');
$this
->assertEqual($result
->getValue(), 1);
// Logout the user to see if sensors responded to the change.
$this
->drupalLogout();
$result = $this
->runSensor('user_sessions_authenticated');
$this
->assertEqual($result
->getValue(), 0);
$result = $this
->runSensor('user_sessions_all');
$this
->assertEqual($result
->getValue(), 0);
// ======= node sensors tests ======= //
$type1 = $this
->drupalCreateContentType();
$type2 = $this
->drupalCreateContentType();
$this
->drupalCreateNode(array(
'type' => $type1->type,
));
$this
->drupalCreateNode(array(
'type' => $type1->type,
));
$this
->drupalCreateNode(array(
'type' => $type2->type,
));
// Make sure that sensors for the new node types are available.
$this->sensorManager
->resetCache();
$result = $this
->runSensor('node_new_' . $type1->type);
$this
->assertEqual($result
->getValue(), 2);
// Test for the SensorDatabaseAggregator custom message.
$this
->assertEqual($result
->getMessage(), format_string('@count @unit in @time_interval', array(
'@count' => $result
->getValue(),
'@unit' => strtolower($result
->getSensorInfo()
->getValueLabel()),
'@time_interval' => format_interval($result
->getSensorInfo()
->getTimeIntervalValue()),
)));
$result = $this
->runSensor('node_new_all');
$this
->assertEqual($result
->getValue(), 3);
variable_set('theme_default', 'bartik');
$result = $this
->runSensor('core_theme_default');
$this
->assertTrue($result
->isOk());
variable_set('theme_default', 'garland');
$result = $this
->runSensor('core_theme_default');
$this
->assertTrue($result
->isCritical());
}