You are here

function MonitoringApiTest::testLogging in Monitoring 7

Test logging with different settings.

File

test/tests/monitoring.api.test, line 339
Contains \MonitoringApiTest.

Class

MonitoringApiTest
Tests for Monitoring API.

Code

function testLogging() {

  // First perform tests with the logging strategy in default mode - that is
  // "Log only on request or on status change".
  $test_sensor_result_data = array(
    'sensor_value' => 1,
    'sensor_message' => 'test message',
    'sensor_status' => SensorResultInterface::STATUS_OK,
  );
  variable_set('test_sensor_result_data', $test_sensor_result_data);
  $settings = monitoring_sensor_settings_get('test_sensor');
  $settings['result_logging'] = TRUE;
  monitoring_sensor_settings_save('test_sensor', $settings);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorData('test_sensor');
  $this
    ->assertEqual(count($logs), 1);
  $log = array_shift($logs);
  $this
    ->assertEqual($log->sensor_name, 'test_sensor');
  $this
    ->assertEqual($log->sensor_status, SensorResultInterface::STATUS_OK);
  $this
    ->assertEqual($log->sensor_value, 1);
  $this
    ->assertEqual($log->sensor_message, 'Value 1, test message');

  // Set log_calls sensor settings to false - that should prevent logging.
  $settings = monitoring_sensor_settings_get('test_sensor');
  $settings['result_logging'] = FALSE;
  monitoring_sensor_settings_save('test_sensor', $settings);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorData('test_sensor');
  $this
    ->assertEqual(count($logs), 1);

  // Now change the status - that should result in the call being logged.
  $test_sensor_result_data = array(
    'sensor_status' => SensorResultInterface::STATUS_WARNING,
  );
  variable_set('test_sensor_result_data', $test_sensor_result_data);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorData('test_sensor');
  $this
    ->assertEqual(count($logs), 2);
  $log = array_pop($logs);
  $this
    ->assertEqual($log->sensor_status, SensorResultInterface::STATUS_WARNING);

  // Set the logging strategy to "Log all events".
  variable_set('monitoring_sensor_call_logging', 'all');

  // Running the sensor with 'result_logging' settings FALSE must record the call.
  $settings = monitoring_sensor_settings_get('test_sensor');
  $settings['result_logging'] = FALSE;
  monitoring_sensor_settings_save('test_sensor', $settings);
  $this
    ->runSensor('test_sensor');
  $logs = $this
    ->loadSensorData('test_sensor');
  $this
    ->assertEqual(count($logs), 3);

  // Set the logging strategy to "No logging".
  variable_set('monitoring_sensor_call_logging', 'none');

  // Despite log_calls TRUE we should not log any call.
  $settings = monitoring_sensor_settings_get('test_sensor');
  $settings['result_logging'] = TRUE;
  monitoring_sensor_settings_save('test_sensor', $settings);
  $logs = $this
    ->loadSensorData('test_sensor');
  $this
    ->runSensor('test_sensor');
  $this
    ->assertEqual(count($logs), 3);
}