Skip to content

Commit aca7fc0

Browse files
committed
test: remove logs assertions
The assertions were not using any specific scope. There is no way to match a logs line qith a test. I tried using: ```rust async fn should_blab_bla() { let span = tracing::info_span!("unique scope"); let _guard = span.enter(); // test assert!(logs_contains_a_line_with( "unique scope", &["ERROR", "tower_http", "response failed"] )); } ``` But span in not clone into the spwaned tasks, so log lines do not contain the scope. In the end, it's what happening in this bug: https://github.com/josecelano/tracing-test-bug
1 parent ebc0971 commit aca7fc0

File tree

7 files changed

+15
-53
lines changed

7 files changed

+15
-53
lines changed

tests/common/logging.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,33 @@ fn tracing_init(level_filter: LevelFilter, style: &TraceStyle) {
4141
tracing::info!("Logging initialized");
4242
}
4343

44-
/// It returns true is there is a log line containing all the words passed.
44+
/// It returns true is there is a log line containing all the texts passed.
4545
///
4646
/// # Panics
4747
///
4848
/// Will panic if it can't get the lock for the global buffer or convert it into
4949
/// a vec.
5050
#[must_use]
51-
pub fn logs_contains_a_line_with(words: &[&str]) -> bool {
51+
#[allow(dead_code)]
52+
pub fn logs_contains_a_line_with(texts: &[&str]) -> bool {
5253
// code-review: we can search directly in the buffer instead of converting
5354
// the buffer into a string but that would slow down the tests because
5455
// cloning should be faster that locking the buffer for searching.
5556
// Because the buffer is not big.
5657
let logs = String::from_utf8(captured_logs_buffer().lock().unwrap().as_vec()).unwrap();
5758

5859
for line in logs.split('\n') {
59-
if contains_words_in_any_order(line, words) {
60+
if contains(line, texts) {
6061
return true;
6162
}
6263
}
6364

6465
false
6566
}
6667

67-
fn contains_words_in_any_order(text: &str, words: &[&str]) -> bool {
68-
words.iter().all(|&word| text.contains(word))
68+
#[allow(dead_code)]
69+
fn contains(text: &str, texts: &[&str]) -> bool {
70+
texts.iter().all(|&word| text.contains(word))
6971
}
7072

7173
/// A tracing writer which captures the latests logs lines into a buffer.

tests/servers/api/v1/contract/authentication.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use torrust_tracker_test_helpers::configuration;
22
use tracing::level_filters::LevelFilter;
33

44
use crate::common::http::{Query, QueryParam};
5-
use crate::common::logging::{self, logs_contains_a_line_with};
5+
use crate::common::logging::{self};
66
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
77
use crate::servers::api::v1::client::Client;
88
use crate::servers::api::Started;
@@ -35,8 +35,6 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {
3535
assert_unauthorized(response).await;
3636

3737
env.stop().await;
38-
39-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
4038
}
4139

4240
#[tokio::test]
@@ -52,8 +50,6 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {
5250
assert_token_not_valid(response).await;
5351

5452
env.stop().await;
55-
56-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
5753
}
5854

5955
#[tokio::test]
@@ -69,8 +65,6 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {
6965
assert_token_not_valid(response).await;
7066

7167
env.stop().await;
72-
73-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
7468
}
7569

7670
#[tokio::test]

tests/servers/api/v1/contract/context/auth_key.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use torrust_tracker::core::auth::Key;
55
use torrust_tracker_test_helpers::configuration;
66
use tracing::level_filters::LevelFilter;
77

8-
use crate::common::logging::{self, logs_contains_a_line_with};
8+
use crate::common::logging::{self};
99
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
1010
use crate::servers::api::v1::asserts::{
1111
assert_auth_key_utf8, assert_failed_to_delete_key, assert_failed_to_generate_key, assert_failed_to_reload_keys,
@@ -84,8 +84,6 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
8484
assert_unauthorized(response).await;
8585

8686
env.stop().await;
87-
88-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
8987
}
9088

9189
#[tokio::test]
@@ -106,8 +104,6 @@ async fn should_fail_when_the_auth_key_cannot_be_generated() {
106104
assert_failed_to_generate_key(response).await;
107105

108106
env.stop().await;
109-
110-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
111107
}
112108

113109
#[tokio::test]
@@ -246,8 +242,6 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {
246242
assert_failed_to_delete_key(response).await;
247243

248244
env.stop().await;
249-
250-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
251245
}
252246

253247
#[tokio::test]
@@ -285,8 +279,6 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
285279
assert_unauthorized(response).await;
286280

287281
env.stop().await;
288-
289-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
290282
}
291283

292284
#[tokio::test]
@@ -325,8 +317,6 @@ async fn should_fail_when_keys_cannot_be_reloaded() {
325317
assert_failed_to_reload_keys(response).await;
326318

327319
env.stop().await;
328-
329-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
330320
}
331321

332322
#[tokio::test]
@@ -354,8 +344,6 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
354344
assert_unauthorized(response).await;
355345

356346
env.stop().await;
357-
358-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
359347
}
360348

361349
mod deprecated_generate_key_endpoint {
@@ -364,7 +352,7 @@ mod deprecated_generate_key_endpoint {
364352
use torrust_tracker_test_helpers::configuration;
365353
use tracing::level_filters::LevelFilter;
366354

367-
use crate::common::logging::{self, logs_contains_a_line_with};
355+
use crate::common::logging::{self};
368356
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
369357
use crate::servers::api::v1::asserts::{
370358
assert_auth_key_utf8, assert_failed_to_generate_key, assert_invalid_key_duration_param, assert_token_not_valid,
@@ -413,8 +401,6 @@ mod deprecated_generate_key_endpoint {
413401
assert_unauthorized(response).await;
414402

415403
env.stop().await;
416-
417-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
418404
}
419405

420406
#[tokio::test]
@@ -452,7 +438,5 @@ mod deprecated_generate_key_endpoint {
452438
assert_failed_to_generate_key(response).await;
453439

454440
env.stop().await;
455-
456-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
457441
}
458442
}

tests/servers/api/v1/contract/context/stats.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use torrust_tracker_primitives::peer::fixture::PeerBuilder;
66
use torrust_tracker_test_helpers::configuration;
77
use tracing::level_filters::LevelFilter;
88

9-
use crate::common::logging::{self, logs_contains_a_line_with};
9+
use crate::common::logging::{self};
1010
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
1111
use crate::servers::api::v1::asserts::{assert_stats, assert_token_not_valid, assert_unauthorized};
1212
use crate::servers::api::v1::client::Client;
@@ -77,6 +77,4 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
7777
assert_unauthorized(response).await;
7878

7979
env.stop().await;
80-
81-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
8280
}

tests/servers/api/v1/contract/context/torrent.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use torrust_tracker_test_helpers::configuration;
88
use tracing::level_filters::LevelFilter;
99

1010
use crate::common::http::{Query, QueryParam};
11-
use crate::common::logging::{self, logs_contains_a_line_with};
11+
use crate::common::logging::{self};
1212
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
1313
use crate::servers::api::v1::asserts::{
1414
assert_bad_request, assert_invalid_infohash_param, assert_not_found, assert_token_not_valid, assert_torrent_info,
@@ -218,8 +218,6 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
218218
assert_unauthorized(response).await;
219219

220220
env.stop().await;
221-
222-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
223221
}
224222

225223
#[tokio::test]
@@ -308,6 +306,4 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
308306
assert_unauthorized(response).await;
309307

310308
env.stop().await;
311-
312-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
313309
}

tests/servers/api/v1/contract/context/whitelist.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bittorrent_primitives::info_hash::InfoHash;
44
use torrust_tracker_test_helpers::configuration;
55
use tracing::level_filters::LevelFilter;
66

7-
use crate::common::logging::{self, logs_contains_a_line_with};
7+
use crate::common::logging::{self};
88
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
99
use crate::servers::api::v1::asserts::{
1010
assert_failed_to_reload_whitelist, assert_failed_to_remove_torrent_from_whitelist, assert_failed_to_whitelist_torrent,
@@ -72,8 +72,6 @@ async fn should_not_allow_whitelisting_a_torrent_for_unauthenticated_users() {
7272
assert_unauthorized(response).await;
7373

7474
env.stop().await;
75-
76-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
7775
}
7876

7977
#[tokio::test]
@@ -189,8 +187,6 @@ async fn should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist() {
189187
assert_failed_to_remove_torrent_from_whitelist(response).await;
190188

191189
env.stop().await;
192-
193-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
194190
}
195191

196192
#[tokio::test]
@@ -217,8 +213,6 @@ async fn should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthentica
217213
assert_unauthorized(response).await;
218214

219215
env.stop().await;
220-
221-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
222216
}
223217

224218
#[tokio::test]
@@ -263,6 +257,4 @@ async fn should_fail_when_the_whitelist_cannot_be_reloaded_from_the_database() {
263257
assert_failed_to_reload_whitelist(response).await;
264258

265259
env.stop().await;
266-
267-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
268260
}

tests/servers/http/v1/contract.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ mod configured_as_whitelisted {
11281128
use torrust_tracker_test_helpers::configuration;
11291129
use tracing::level_filters::LevelFilter;
11301130

1131-
use crate::common::logging::{self, logs_contains_a_line_with};
1131+
use crate::common::logging::{self};
11321132
use crate::servers::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response};
11331133
use crate::servers::http::client::Client;
11341134
use crate::servers::http::requests::announce::QueryBuilder;
@@ -1149,8 +1149,6 @@ mod configured_as_whitelisted {
11491149
assert_torrent_not_in_whitelist_error_response(response).await;
11501150

11511151
env.stop().await;
1152-
1153-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
11541152
}
11551153

11561154
#[tokio::test]
@@ -1183,7 +1181,7 @@ mod configured_as_whitelisted {
11831181
use torrust_tracker_test_helpers::configuration;
11841182
use tracing::level_filters::LevelFilter;
11851183

1186-
use crate::common::logging::{self, logs_contains_a_line_with};
1184+
use crate::common::logging::{self};
11871185
use crate::servers::http::asserts::assert_scrape_response;
11881186
use crate::servers::http::client::Client;
11891187
use crate::servers::http::responses::scrape::{File, ResponseBuilder};
@@ -1218,8 +1216,6 @@ mod configured_as_whitelisted {
12181216
assert_scrape_response(response, &expected_scrape_response).await;
12191217

12201218
env.stop().await;
1221-
1222-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
12231219
}
12241220

12251221
#[tokio::test]

0 commit comments

Comments
 (0)