A: share tracing capture and peer config fixtures in tests (gate round 1)
This commit is contained in:
1 parent
6bff60c210
commit
d3bef6bd94
5 files changed
+106
-136
No files matched your search
+1
-33
@@ -184,11 +184,10 @@ fn is_loopback(url: &Url) -> bool {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::*;
|
||||
use crate::peers::testing::capture::logged;
|
||||
use crate::peers::types::PeerName;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
fn peer(name: &str, url: &str) -> PeerConfig {
|
||||
PeerConfig {
|
||||
@@ -216,37 +215,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
struct Capture(Arc<Mutex<Vec<String>>>);
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Capture {
|
||||
fn on_event(
|
||||
&self,
|
||||
event: &tracing::Event<'_>,
|
||||
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let mut msg = String::new();
|
||||
event.record(&mut FieldVisit(&mut msg));
|
||||
self.0.lock().expect("log lock").push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
struct FieldVisit<'a>(&'a mut String);
|
||||
|
||||
impl tracing::field::Visit for FieldVisit<'_> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
use std::fmt::Write;
|
||||
let _ = write!(self.0, "{}={value:?} ", field.name());
|
||||
}
|
||||
}
|
||||
|
||||
fn logged(f: impl FnOnce()) -> String {
|
||||
let buf = Arc::new(Mutex::new(Vec::new()));
|
||||
let sub = tracing_subscriber::registry().with(Capture(buf.clone()));
|
||||
tracing::subscriber::with_default(sub, f);
|
||||
let text = buf.lock().expect("log lock").join("\n");
|
||||
text
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_and_loopback_http_are_valid() {
|
||||
validate(&PeersConfig::default()).unwrap();
|
||||
|
||||
+5
-35
@@ -98,11 +98,10 @@ fn trim_one_newline(buf: &mut Vec<u8>) {
|
||||
mod tests {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::*;
|
||||
use crate::peers::testing::capture::logged;
|
||||
use crate::peers::types::PeerName;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
const SECRET: &str = "slinuxm_PEER_TOKEN_SHOULD_NOT_LEAK";
|
||||
|
||||
@@ -161,29 +160,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
struct Capture(Arc<Mutex<Vec<String>>>);
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Capture {
|
||||
fn on_event(
|
||||
&self,
|
||||
event: &tracing::Event<'_>,
|
||||
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let mut msg = String::new();
|
||||
event.record(&mut FieldVisit(&mut msg));
|
||||
self.0.lock().expect("log lock").push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
struct FieldVisit<'a>(&'a mut String);
|
||||
|
||||
impl tracing::field::Visit for FieldVisit<'_> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
use std::fmt::Write;
|
||||
let _ = write!(self.0, "{}={value:?} ", field.name());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trims_one_newline_and_rereads_a_replaced_file() {
|
||||
let dir = TempDir::new();
|
||||
@@ -257,16 +233,10 @@ mod tests {
|
||||
SECRET
|
||||
);
|
||||
|
||||
let logs = {
|
||||
let buf = Arc::new(Mutex::new(Vec::new()));
|
||||
let sub = tracing_subscriber::registry().with(Capture(buf.clone()));
|
||||
let private = dir.write("logged", format!("{SECRET}\n").as_bytes(), 0o600);
|
||||
tracing::subscriber::with_default(sub, || {
|
||||
load_token_at(&peer(private), None).unwrap();
|
||||
});
|
||||
let text = buf.lock().expect("log lock").join("\n");
|
||||
text
|
||||
};
|
||||
let private = dir.write("logged", format!("{SECRET}\n").as_bytes(), 0o600);
|
||||
let logs = logged(|| {
|
||||
load_token_at(&peer(private), None).unwrap();
|
||||
});
|
||||
assert!(!logs.contains("SHOULD_NOT_LEAK"), "{logs}");
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,47 @@ use crate::state::AppState;
|
||||
|
||||
const DEFAULT_PING_BODY: &str = r#"{"status":"ok","version":"0.0.0-fake"}"#;
|
||||
|
||||
/// Capture of `tracing` events for tests that assert nothing leaks into logs.
|
||||
#[cfg(test)]
|
||||
pub(crate) mod capture {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
struct Layer(Arc<Mutex<Vec<String>>>);
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Layer {
|
||||
fn on_event(
|
||||
&self,
|
||||
event: &tracing::Event<'_>,
|
||||
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
let mut msg = String::new();
|
||||
event.record(&mut FieldVisit(&mut msg));
|
||||
self.0.lock().expect("log lock").push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
struct FieldVisit<'a>(&'a mut String);
|
||||
|
||||
impl tracing::field::Visit for FieldVisit<'_> {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
use std::fmt::Write;
|
||||
let _ = write!(self.0, "{}={value:?} ", field.name());
|
||||
}
|
||||
}
|
||||
|
||||
/// Run `f` under a subscriber that records every event, and return the
|
||||
/// rendered fields joined by newlines.
|
||||
pub(crate) fn logged(f: impl FnOnce()) -> String {
|
||||
let buf = Arc::new(Mutex::new(Vec::new()));
|
||||
let sub = tracing_subscriber::registry().with(Layer(buf.clone()));
|
||||
tracing::subscriber::with_default(sub, f);
|
||||
let text = buf.lock().expect("log lock").join("\n");
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Scripted {
|
||||
Response {
|
||||
|
||||
Reference in new issue
Block a user