Module slog_extlog::slog_test[][src]

Copyright 2017 Metaswitch Networks

Helper functions for use when testing slog logs.

This module is a grab-bag of tools that have been found useful when testing slog logs. A typical test will create a new iobuffer::IoBuffer, pass it to new_test_logger to construct a logger, and pass that logger to the system under test. It will then exercise the system. Finally, it will use logs_in_range to extract the logs it is interested in in a canonical order, and test that they are as expected using standard Rust tools.

Example

use slog::debug;
use slog_extlog::slog_test;

// Setup code
let mut data = iobuffer::IoBuffer::new();
let logger = slog_test::new_test_logger(data.clone());

// Application code
debug!(logger, "Something happened to it";
       "subject" => "something",
       "verb"    => "happened",
       "object"  => "it");

// Test code - parse all logs and check their contents.
let logs = slog_test::read_json_values(&mut data);
slog_test::assert_json_matches(&logs[0], &serde_json::json!({ "subject": "something", "object": "it" }));
assert!(logs[0]["msg"].as_str().unwrap().contains("to it"));

// More application code
debug!(logger, "Another log"; "log_id" => "ABC123");
debug!(logger, "Imposter"; "log_id" => "XYZ123");

// Alternate test code - parse selected logs and check their contents.
let abc_logs = slog_test::logs_in_range("ABC", "ABD", &mut data);
assert_eq!(abc_logs.len(), 1);
assert_eq!(abc_logs[0]["msg"].as_str().unwrap(), "Another log".to_string());

Statistics testing

For verifying statistics, the create_logger_buffer and check_expected_stats methods are useful for creating a slog_extlog::StatisticsLogger and then verifying that statistics are generated as expected.

Re-exports

pub use super::stats::*;

Structs

ExpectedStat

An expected statistic helper method.

ExpectedStatSnapshot

ExpectedStatSnapshot helper. E.g. ExpectedStatSnapshot { name: “test_group_bucket_counter”, description: “Test cumulative bucket counter with groups”, stat_type: BucketCounter, values: vec![ ExpectedStatSnapshotValue { group_values: vec![“one”.to_string(), “two”.to_string()], bucket_limit: Some(BucketLimit::Num(-8)), value: 0f64, }, ExpectedStatSnapshotValue { group_values: vec![“one”.to_string(), “two”.to_string()], bucket_limit: Some(BucketLimit::Num(0)), value: 0f64, }, ExpectedStatSnapshotValue { group_values: vec![“one”.to_string(), “two”.to_string()], bucket_limit: Some(BucketLimit::Unbounded), value: 3f64, }, ExpectedStatSnapshotValue { group_values: vec![“three”.to_string(), “four”.to_string()], bucket_limit: Some(BucketLimit::Num(-8)), value: 4f64, }, ExpectedStatSnapshotValue { group_values: vec![“three”.to_string(), “four”.to_string()], bucket_limit: Some(BucketLimit::Num(0)), value: 4f64, }, ExpectedStatSnapshotValue { group_values: vec![“three”.to_string(), “four”.to_string()], bucket_limit: Some(BucketLimit::Unbounded), value: 4f64, }, ], buckets: Some(Buckets::new(BucketMethod::CumulFreq, “bucket”, &[-8, 0])), }

ExpectedStatSnapshotValue

ExpectedStatSnapshotValue helper.

Statics

TEST_LOG_INTERVAL

A default logging interval for tests, short so UTs run faster.

Functions

assert_json_matches

Assert that every item contained in expected also appears in actual. Additional values in actual are ignored.

check_expected_stat_snapshots

Check that a set of stat snapshots are as expected.

check_expected_stats

Asserts that a set of logs (retrieved using logs_in_range) is exactly equal to an expected set of stats.

create_logger_buffer

Common setup function.

log_in_range

Test whether the given log lies in the given range: between min_id (inclusive) and max_id (exclusive).

logs_in_range

Collect all logs of the indicated type (see log_in_range) and sort them in ascending order of log_id.

new_test_logger

Create a new test logger suitable for use with read_json_values.

read_json_values

Read all the newline-delimited JSON objects from the given stream, panicking if there is an IO error or a JSON parse error. No attempt is made to avoid reading partial lines from the stream.

Type Definitions

Buffer

Buffer containing log data.