Skip to content

Commit

Permalink
Merge #1163: Log assertions: add assertions for log errors in HTTP tr…
Browse files Browse the repository at this point in the history
…acker

03243cb tests: add assertions for HTTP tracker error logs (Jose Celano)

Pull request description:

  Log assertions: add assertions for log errors in HTTP tracker.

  It's using the info-hash to find the ERROR in logs. It's generated a newly random info-hash for each test.

  It could have been also used a `x-request-id` header in the HTTP request but this solution is simpler and the chances to have an info-hash collision is very low.

ACKs for top commit:
  josecelano:
    ACK 03243cb

Tree-SHA512: 4c7780f2e30a262ec7167e6d9128dd366653ed9d182c6614e116600b68f22c0d2132c7c9ce7dae68b15493bf4a0c2c2b2d6b54fb6c40f113afada1ce5e57b557
  • Loading branch information
josecelano committed Dec 26, 2024
2 parents 2a8a114 + 03243cb commit e58831c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tests/common/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bittorrent_primitives::info_hash::InfoHash;

#[allow(dead_code)]
pub fn invalid_info_hashes() -> Vec<String> {
[
Expand All @@ -10,3 +12,11 @@ pub fn invalid_info_hashes() -> Vec<String> {
]
.to_vec()
}

/// Returns a random info hash.
pub fn random_info_hash() -> InfoHash {
let mut rng = rand::thread_rng();
let random_bytes: [u8; 20] = rand::Rng::gen(&mut rng);

InfoHash::from_bytes(&random_bytes)
}
28 changes: 23 additions & 5 deletions tests/servers/http/v1/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,8 +1217,10 @@ mod configured_as_whitelisted {

use bittorrent_primitives::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;
use uuid::Uuid;

use crate::common::logging::{self};
use crate::common::fixtures::random_info_hash;
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response};
use crate::servers::http::client::Client;
use crate::servers::http::requests::announce::QueryBuilder;
Expand All @@ -1230,14 +1232,24 @@ mod configured_as_whitelisted {

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

let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
let request_id = Uuid::new_v4();
let info_hash = random_info_hash();

let response = Client::new(*env.bind_address())
.announce(&QueryBuilder::default().with_info_hash(&info_hash).query())
.announce_with_header(
&QueryBuilder::default().with_info_hash(&info_hash).query(),
"x-request-id",
&request_id.to_string(),
)
.await;

assert_torrent_not_in_whitelist_error_response(response).await;

assert!(
logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]),
"Expected logs to contain: ERROR ... {info_hash} is not whitelisted"
);

env.stop().await;
}

Expand Down Expand Up @@ -1272,7 +1284,8 @@ mod configured_as_whitelisted {
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_test_helpers::configuration;

use crate::common::logging::{self};
use crate::common::fixtures::random_info_hash;
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::http::asserts::assert_scrape_response;
use crate::servers::http::client::Client;
use crate::servers::http::responses::scrape::{File, ResponseBuilder};
Expand All @@ -1284,7 +1297,7 @@ mod configured_as_whitelisted {

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

let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
let info_hash = random_info_hash();

env.add_torrent_peer(
&info_hash,
Expand All @@ -1306,6 +1319,11 @@ mod configured_as_whitelisted {

assert_scrape_response(response, &expected_scrape_response).await;

assert!(
logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]),
"Expected logs to contain: ERROR ... {info_hash} is not whitelisted"
);

env.stop().await;
}

Expand Down

0 comments on commit e58831c

Please sign in to comment.