Skip to content

Commit e58831c

Browse files
committed
Merge #1163: Log assertions: add assertions for log errors in HTTP tracker
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
2 parents 2a8a114 + 03243cb commit e58831c

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

tests/common/fixtures.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use bittorrent_primitives::info_hash::InfoHash;
2+
13
#[allow(dead_code)]
24
pub fn invalid_info_hashes() -> Vec<String> {
35
[
@@ -10,3 +12,11 @@ pub fn invalid_info_hashes() -> Vec<String> {
1012
]
1113
.to_vec()
1214
}
15+
16+
/// Returns a random info hash.
17+
pub fn random_info_hash() -> InfoHash {
18+
let mut rng = rand::thread_rng();
19+
let random_bytes: [u8; 20] = rand::Rng::gen(&mut rng);
20+
21+
InfoHash::from_bytes(&random_bytes)
22+
}

tests/servers/http/v1/contract.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,10 @@ mod configured_as_whitelisted {
12171217

12181218
use bittorrent_primitives::info_hash::InfoHash;
12191219
use torrust_tracker_test_helpers::configuration;
1220+
use uuid::Uuid;
12201221

1221-
use crate::common::logging::{self};
1222+
use crate::common::fixtures::random_info_hash;
1223+
use crate::common::logging::{self, logs_contains_a_line_with};
12221224
use crate::servers::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response};
12231225
use crate::servers::http::client::Client;
12241226
use crate::servers::http::requests::announce::QueryBuilder;
@@ -1230,14 +1232,24 @@ mod configured_as_whitelisted {
12301232

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

1233-
let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
1235+
let request_id = Uuid::new_v4();
1236+
let info_hash = random_info_hash();
12341237

12351238
let response = Client::new(*env.bind_address())
1236-
.announce(&QueryBuilder::default().with_info_hash(&info_hash).query())
1239+
.announce_with_header(
1240+
&QueryBuilder::default().with_info_hash(&info_hash).query(),
1241+
"x-request-id",
1242+
&request_id.to_string(),
1243+
)
12371244
.await;
12381245

12391246
assert_torrent_not_in_whitelist_error_response(response).await;
12401247

1248+
assert!(
1249+
logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]),
1250+
"Expected logs to contain: ERROR ... {info_hash} is not whitelisted"
1251+
);
1252+
12411253
env.stop().await;
12421254
}
12431255

@@ -1272,7 +1284,8 @@ mod configured_as_whitelisted {
12721284
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
12731285
use torrust_tracker_test_helpers::configuration;
12741286

1275-
use crate::common::logging::{self};
1287+
use crate::common::fixtures::random_info_hash;
1288+
use crate::common::logging::{self, logs_contains_a_line_with};
12761289
use crate::servers::http::asserts::assert_scrape_response;
12771290
use crate::servers::http::client::Client;
12781291
use crate::servers::http::responses::scrape::{File, ResponseBuilder};
@@ -1284,7 +1297,7 @@ mod configured_as_whitelisted {
12841297

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

1287-
let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
1300+
let info_hash = random_info_hash();
12881301

12891302
env.add_torrent_peer(
12901303
&info_hash,
@@ -1306,6 +1319,11 @@ mod configured_as_whitelisted {
13061319

13071320
assert_scrape_response(response, &expected_scrape_response).await;
13081321

1322+
assert!(
1323+
logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]),
1324+
"Expected logs to contain: ERROR ... {info_hash} is not whitelisted"
1325+
);
1326+
13091327
env.stop().await;
13101328
}
13111329

0 commit comments

Comments
 (0)