Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion tests/common/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
//! ´´´
//!

use std::sync::Once;
use std::io;
use std::sync::{Arc, Mutex, Once};

use tracing::level_filters::LevelFilter;
use tracing_subscriber::fmt::MakeWriter;

#[allow(dead_code)]
pub static INIT: Once = Once::new();
Expand All @@ -28,3 +30,94 @@ pub fn tracing_stderr_init(filter: LevelFilter) {

tracing::info!("Logging initialized");
}

#[allow(dead_code)]
pub fn tracing_init_with_capturer(filter: LevelFilter, log_capturer: Arc<Mutex<LogCapturer>>) {
let writer = LogCapturerWrapper::new(log_capturer);

let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(writer);

builder.pretty().with_file(true).init();

tracing::info!("Logging initialized");
}

pub struct LogCapturerWrapper {
inner: Arc<Mutex<LogCapturer>>,
}

impl LogCapturerWrapper {
pub fn new(inner: Arc<Mutex<LogCapturer>>) -> Self {
Self { inner }
}
}

impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for LogCapturerWrapper {
type Writer = LogCapturerGuard;

fn make_writer(&'a self) -> Self::Writer {
LogCapturerGuard {
inner: self.inner.clone(),
}
}
}

pub struct LogCapturerGuard {
inner: Arc<Mutex<LogCapturer>>,
}

impl io::Write for LogCapturerGuard {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut capturer = self.inner.lock().unwrap();
capturer.write(buf)
}

fn flush(&mut self) -> io::Result<()> {
let mut capturer = self.inner.lock().unwrap();
capturer.flush()
}
}

#[derive(Debug, Default)]
pub struct LogCapturer {
output: String,
}

impl LogCapturer {
pub fn new() -> Self {
Self::default()
}

pub fn contains(&self, message: &str) -> bool {
self.output.contains(message)
}
}

impl io::Write for LogCapturer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let out_str = String::from_utf8_lossy(buf);

// We print to stdout so that the output is visible in the terminal
// when you run the tests with `cargo test -- --nocapture`.
println!("{out_str}");

self.output.push_str(&out_str);

Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

impl<'a> MakeWriter<'a> for LogCapturer {
type Writer = Self;

fn make_writer(&'a self) -> Self::Writer {
Self::default()
}
}
12 changes: 10 additions & 2 deletions tests/servers/api/v1/contract/context/auth_key.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::sync::{Arc, Mutex};
use std::time::Duration;

use serde::Serialize;
use torrust_tracker::core::auth::Key;
use torrust_tracker_test_helpers::configuration;
use tracing::level_filters::LevelFilter;

use crate::common::logging::{tracing_stderr_init, INIT};
use crate::common::logging::{tracing_init_with_capturer, tracing_stderr_init, LogCapturer, INIT};
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::servers::api::v1::asserts::{
assert_auth_key_utf8, assert_failed_to_delete_key, assert_failed_to_generate_key, assert_failed_to_reload_keys,
Expand Down Expand Up @@ -250,8 +251,10 @@ async fn should_fail_deleting_an_auth_key_when_the_key_id_is_invalid() {

#[tokio::test]
async fn should_fail_when_the_auth_key_cannot_be_deleted() {
let log_capturer = Arc::new(Mutex::new(LogCapturer::new()));

INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
tracing_init_with_capturer(LevelFilter::ERROR, log_capturer.clone());
});

let env = Started::new(&configuration::ephemeral().into()).await;
Expand All @@ -271,6 +274,11 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {

assert_failed_to_delete_key(response).await;

// We expect to see a 500 error, it's the current API behavior
assert!(log_capturer.lock().unwrap().contains("ERROR"));
assert!(log_capturer.lock().unwrap().contains("tower_http"));
assert!(log_capturer.lock().unwrap().contains("500 Internal Server Error"));

env.stop().await;
}

Expand Down