You are here

public function MonitoringCoreKernelTest::testDatabaseAggregator in Monitoring 8

Tests the database aggregator sensor.

See also

\Drupal\monitoring\Plugin\monitoring\SensorPlugin\DatabaseAggregatorSensorPlugin

File

tests/src/Kernel/MonitoringCoreKernelTest.php, line 727

Class

MonitoringCoreKernelTest
Kernel tests for the core pieces of monitoring.

Namespace

Drupal\Tests\monitoring\Kernel

Code

public function testDatabaseAggregator() {

  // Aggregate by watchdog type.
  $sensor_config = SensorConfig::load('watchdog_aggregate_test');
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'type',
      'value' => 'test_type',
    ),
  );
  $sensor_config
    ->save();
  \Drupal::logger('test_type')
    ->notice($this
    ->randomMachineName());
  \Drupal::logger('test_type')
    ->notice($this
    ->randomMachineName());
  \Drupal::logger('other_test_type')
    ->notice($this
    ->randomMachineName());
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Aggregate by watchdog message.
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'message',
      'value' => 'test_message',
    ),
  );
  $sensor_config
    ->save();
  \Drupal::logger($this
    ->randomMachineName())
    ->notice('test_message');
  \Drupal::logger($this
    ->randomMachineName())
    ->notice('another_test_message');
  \Drupal::logger($this
    ->randomMachineName())
    ->notice('another_test_message');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 1);

  // Aggregate by watchdog severity.
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'severity',
      'value' => RfcLogLevel::CRITICAL,
    ),
  );
  $sensor_config
    ->save();
  \Drupal::logger($this
    ->randomMachineName())
    ->critical($this
    ->randomMachineName());
  \Drupal::logger($this
    ->randomMachineName())
    ->critical($this
    ->randomMachineName());
  \Drupal::logger($this
    ->randomMachineName())
    ->critical($this
    ->randomMachineName());
  \Drupal::logger($this
    ->randomMachineName())
    ->critical($this
    ->randomMachineName());
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 4);

  // Aggregate by watchdog location.
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'location',
      'value' => 'http://some.url.dev',
    ),
  );
  $sensor_config
    ->save();

  // Update the two test_type watchdog entries with a custom location.
  \Drupal::database()
    ->update('watchdog')
    ->fields(array(
    'location' => 'http://some.url.dev',
  ))
    ->condition('type', 'test_type')
    ->execute();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Filter for time period.
  $sensor_config->settings['conditions'] = array();
  $sensor_config->settings['time_interval_value'] = 10;
  $sensor_config->settings['time_interval_field'] = 'timestamp';
  $sensor_config
    ->save();

  // Make all system watchdog messages older than the configured time period.
  \Drupal::database()
    ->update('watchdog')
    ->fields(array(
    'timestamp' => \Drupal::time()
      ->getRequestTime() - 20,
  ))
    ->condition('type', 'system')
    ->execute();
  $count_latest = \Drupal::database()
    ->query('SELECT COUNT(*) FROM {watchdog} WHERE timestamp > :timestamp', array(
    ':timestamp' => \Drupal::time()
      ->getRequestTime() - 10,
  ))
    ->fetchField();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), $count_latest);

  // Test for thresholds and statuses.
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'type',
      'value' => 'test_watchdog_aggregate_sensor',
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEqual($result
    ->getValue(), 0);
  \Drupal::logger('test_watchdog_aggregate_sensor')
    ->notice('testing');
  \Drupal::logger('test_watchdog_aggregate_sensor')
    ->notice('testing');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isWarning());
  $this
    ->assertEqual($result
    ->getValue(), 2);
  \Drupal::logger('test_watchdog_aggregate_sensor')
    ->notice('testing');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isCritical());
  $this
    ->assertEqual($result
    ->getValue(), 3);

  // Test database aggregator with invalid conditions.
  $sensor = SensorConfig::create(array(
    'id' => 'db_test',
    'label' => 'Database sensor invalid',
    'plugin_id' => 'database_aggregator',
    'settings' => array(
      'table' => 'watchdog',
    ),
  ));
  $sensor->settings['conditions'] = array(
    array(
      'field' => 'invalid',
      'value' => '',
    ),
  );
  $sensor->settings['verbose_fields']['0'] = 'wid';
  $sensor
    ->save();
  $result = $this
    ->runSensor('db_test');
  $this
    ->assertTrue($result
    ->isCritical());

  // Test STARTS_WITH and CONTAINS operators.
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'type',
      'value' => 'other',
      'operator' => 'STARTS_WITH',
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 1);
  $sensor_config->settings['conditions'] = array(
    array(
      'field' => 'type',
      'value' => 'test_type',
      'operator' => 'CONTAINS',
    ),
  );
  $sensor_config
    ->save();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 3);
}