Skip to content

Commit

Permalink
tracing: init tracing-subscriber in intragration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Aug 24, 2024
1 parent 38ffa85 commit 28f87d1
Show file tree
Hide file tree
Showing 15 changed files with 551 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn ephemeral() -> Configuration {

let mut config = Configuration::default();

config.logging.threshold = Threshold::Off; // Change to `debug` for tests debugging
config.logging.threshold = Threshold::Off; // It should always be off here, the tests manage their own logging.

// Ephemeral socket address for API
let api_port = 0u16;
Expand Down
6 changes: 6 additions & 0 deletions tests/common/clock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use std::time::Duration;

use torrust_tracker_clock::clock::Time;
use tracing::level_filters::LevelFilter;

use crate::common::logging::{tracing_stderr_init, INIT};
use crate::CurrentClock;

#[test]
fn it_should_use_stopped_time_for_testing() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

assert_eq!(CurrentClock::dbg_clock_type(), "Stopped".to_owned());

let time = CurrentClock::now();
Expand Down
30 changes: 30 additions & 0 deletions tests/common/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(clippy::doc_markdown)]
//! Logging for the Integration Tests
//!
//! Tests should start their own logging.
//!
//! To find tests that do not start their own logging:
//!
//! ´´´ sh
//! awk 'BEGIN{RS=""; FS="\n"} /#\[tokio::test\]\s*async\s+fn\s+\w+\s*\(\s*\)\s*\{[^}]*\}/ && !/#\[tokio::test\]\s*async\s+fn\s+\w+\s*\(\s*\)\s*\{[^}]*INIT\.call_once/' $(find . -name "*.rs")
//! ´´´
//!

use std::sync::Once;

use tracing::level_filters::LevelFilter;

#[allow(dead_code)]
pub static INIT: Once = Once::new();

#[allow(dead_code)]
pub fn tracing_stderr_init(filter: LevelFilter) {
let builder = tracing_subscriber::fmt()
.with_max_level(filter)
.with_ansi(true)
.with_writer(std::io::stderr);

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

tracing::info!("Logging initialized");
}
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod clock;
pub mod fixtures;
pub mod http;
pub mod logging;
pub mod udp;
22 changes: 22 additions & 0 deletions tests/servers/api/v1/contract/authentication.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use torrust_tracker_test_helpers::configuration;
use tracing::level_filters::LevelFilter;

use crate::common::http::{Query, QueryParam};
use crate::common::logging::{tracing_stderr_init, INIT};
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::servers::api::v1::client::Client;
use crate::servers::api::Started;

#[tokio::test]
async fn should_authenticate_requests_by_using_a_token_query_param() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let token = env.get_connection_info().api_token.unwrap();
Expand All @@ -22,6 +28,10 @@ async fn should_authenticate_requests_by_using_a_token_query_param() {

#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_missing() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(env.get_connection_info())
Expand All @@ -35,6 +45,10 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {

#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_empty() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(env.get_connection_info())
Expand All @@ -48,6 +62,10 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {

#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_invalid() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(env.get_connection_info())
Expand All @@ -61,6 +79,10 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {

#[tokio::test]
async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let token = env.get_connection_info().api_token.unwrap();
Expand Down
8 changes: 8 additions & 0 deletions tests/servers/api/v1/contract/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@
// use crate::common::app::setup_with_configuration;
// use crate::servers::api::environment::stopped_environment;

use tracing::level_filters::LevelFilter;

use crate::common::logging::{tracing_stderr_init, INIT};

#[tokio::test]
#[ignore]
#[should_panic = "Could not receive bind_address."]
async fn should_fail_with_ssl_enabled_and_bad_ssl_config() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

// let tracker = setup_with_configuration(&Arc::new(configuration::ephemeral()));

// let config = tracker.config.http_api.clone();
Expand Down
72 changes: 72 additions & 0 deletions tests/servers/api/v1/contract/context/auth_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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::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 All @@ -15,6 +17,10 @@ use crate::servers::api::{force_database_error, Started};

#[tokio::test]
async fn should_allow_generating_a_new_random_auth_key() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(env.get_connection_info())
Expand All @@ -37,6 +43,10 @@ async fn should_allow_generating_a_new_random_auth_key() {

#[tokio::test]
async fn should_allow_uploading_a_preexisting_auth_key() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(env.get_connection_info())
Expand All @@ -59,6 +69,10 @@ async fn should_allow_uploading_a_preexisting_auth_key() {

#[tokio::test]
async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let response = Client::new(connection_with_invalid_token(env.get_connection_info().bind_address.as_str()))
Expand All @@ -84,6 +98,10 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()

#[tokio::test]
async fn should_fail_when_the_auth_key_cannot_be_generated() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

force_database_error(&env.tracker);
Expand All @@ -102,6 +120,10 @@ async fn should_fail_when_the_auth_key_cannot_be_generated() {

#[tokio::test]
async fn should_allow_deleting_an_auth_key() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand Down Expand Up @@ -129,6 +151,10 @@ async fn should_fail_generating_a_new_auth_key_when_the_provided_key_is_invalid(
pub seconds_valid: u64,
}

INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let invalid_keys = [
Expand Down Expand Up @@ -166,6 +192,10 @@ async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid(
pub seconds_valid: String,
}

INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let invalid_key_durations = [
Expand Down Expand Up @@ -193,6 +223,10 @@ async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid(

#[tokio::test]
async fn should_fail_deleting_an_auth_key_when_the_key_id_is_invalid() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let invalid_auth_keys = [
Expand All @@ -216,6 +250,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() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand All @@ -238,6 +276,10 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {

#[tokio::test]
async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand Down Expand Up @@ -273,6 +315,10 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {

#[tokio::test]
async fn should_allow_reloading_keys() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand All @@ -290,6 +336,10 @@ async fn should_allow_reloading_keys() {

#[tokio::test]
async fn should_fail_when_keys_cannot_be_reloaded() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand All @@ -309,6 +359,10 @@ async fn should_fail_when_keys_cannot_be_reloaded() {

#[tokio::test]
async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand Down Expand Up @@ -336,7 +390,9 @@ mod deprecated_generate_key_endpoint {

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::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_generate_key, assert_invalid_key_duration_param, assert_token_not_valid,
Expand All @@ -347,6 +403,10 @@ mod deprecated_generate_key_endpoint {

#[tokio::test]
async fn should_allow_generating_a_new_auth_key() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand All @@ -366,6 +426,10 @@ mod deprecated_generate_key_endpoint {

#[tokio::test]
async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let seconds_valid = 60;
Expand All @@ -387,6 +451,10 @@ mod deprecated_generate_key_endpoint {

#[tokio::test]
async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let invalid_key_durations = [
Expand All @@ -408,6 +476,10 @@ mod deprecated_generate_key_endpoint {

#[tokio::test]
async fn should_fail_when_the_auth_key_cannot_be_generated() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

force_database_error(&env.tracker);
Expand Down
6 changes: 6 additions & 0 deletions tests/servers/api/v1/contract/context/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use torrust_tracker::servers::apis::v1::context::health_check::resources::{Report, Status};
use torrust_tracker_test_helpers::configuration;
use tracing::level_filters::LevelFilter;

use crate::common::logging::{tracing_stderr_init, INIT};
use crate::servers::api::v1::client::get;
use crate::servers::api::Started;

#[tokio::test]
async fn health_check_endpoint_should_return_status_ok_if_api_is_running() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let env = Started::new(&configuration::ephemeral().into()).await;

let url = format!("http://{}/api/health_check", env.get_connection_info().bind_address);
Expand Down
Loading

0 comments on commit 28f87d1

Please sign in to comment.