|
| 1 | +use std::env; |
| 2 | +use std::str::FromStr as _; |
| 3 | + |
| 4 | +use bittorrent_primitives::info_hash::InfoHash; |
| 5 | +use bittorrent_tracker_client::http::client::requests::announce::QueryBuilder; |
| 6 | +use bittorrent_tracker_client::http::client::Client as HttpTrackerClient; |
| 7 | +use reqwest::Url; |
| 8 | +use serde::Deserialize; |
| 9 | +use tokio::time::Duration; |
| 10 | +use torrust_rest_tracker_api_client::connection_info::{ConnectionInfo, Origin}; |
| 11 | +use torrust_rest_tracker_api_client::v1::client::Client as TrackerApiClient; |
| 12 | +use torrust_tracker_lib::app; |
| 13 | + |
| 14 | +#[tokio::test] |
| 15 | +async fn the_stats_api_endpoint_should_return_the_global_stats() { |
| 16 | + // Logging must be OFF otherwise your will get the following error: |
| 17 | + // `Unable to install global subscriber: SetGlobalDefaultError("a global default trace dispatcher has already been set")` |
| 18 | + // That's because we can't initialize the logger twice. |
| 19 | + // You can enable it if you run only this test. |
| 20 | + let config_with_two_http_trackers = r#" |
| 21 | + [metadata] |
| 22 | + app = "torrust-tracker" |
| 23 | + purpose = "configuration" |
| 24 | + schema_version = "2.0.0" |
| 25 | +
|
| 26 | + [logging] |
| 27 | + threshold = "off" |
| 28 | +
|
| 29 | + [core] |
| 30 | + listed = false |
| 31 | + private = false |
| 32 | +
|
| 33 | + [core.database] |
| 34 | + driver = "sqlite3" |
| 35 | + path = "./integration_tests_sqlite3.db" |
| 36 | +
|
| 37 | + [[http_trackers]] |
| 38 | + bind_address = "0.0.0.0:7272" |
| 39 | + tracker_usage_statistics = true |
| 40 | +
|
| 41 | + [[http_trackers]] |
| 42 | + bind_address = "0.0.0.0:7373" |
| 43 | + tracker_usage_statistics = true |
| 44 | +
|
| 45 | + [http_api] |
| 46 | + bind_address = "0.0.0.0:1414" |
| 47 | +
|
| 48 | + [http_api.access_tokens] |
| 49 | + admin = "MyAccessToken" |
| 50 | + "#; |
| 51 | + |
| 52 | + env::set_var("TORRUST_TRACKER_CONFIG_TOML", config_with_two_http_trackers); |
| 53 | + |
| 54 | + let (_app_container, _jobs, _registar) = app::run().await; |
| 55 | + |
| 56 | + announce_to_tracker("http://127.0.0.1:7272").await; |
| 57 | + announce_to_tracker("http://127.0.0.1:7373").await; |
| 58 | + |
| 59 | + let global_stats = get_tracker_statistics("http://127.0.0.1:1414", "MyAccessToken").await; |
| 60 | + |
| 61 | + assert_eq!(global_stats.tcp4_announces_handled, 2); |
| 62 | +} |
| 63 | + |
| 64 | +/// Make a sample announce request to the tracker. |
| 65 | +async fn announce_to_tracker(tracker_url: &str) { |
| 66 | + let response = HttpTrackerClient::new(Url::parse(tracker_url).unwrap(), Duration::from_secs(1)) |
| 67 | + .unwrap() |
| 68 | + .announce( |
| 69 | + &QueryBuilder::with_default_values() |
| 70 | + .with_info_hash(&InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap()) // DevSkim: ignore DS173237 |
| 71 | + .query(), |
| 72 | + ) |
| 73 | + .await; |
| 74 | + |
| 75 | + assert!(response.is_ok()); |
| 76 | +} |
| 77 | + |
| 78 | +/// Global statistics with only metrics relevant to the test. |
| 79 | +#[derive(Deserialize)] |
| 80 | +struct PartialGlobalStatistics { |
| 81 | + tcp4_announces_handled: u64, |
| 82 | +} |
| 83 | + |
| 84 | +async fn get_tracker_statistics(aip_url: &str, token: &str) -> PartialGlobalStatistics { |
| 85 | + let response = TrackerApiClient::new(ConnectionInfo::authenticated(Origin::new(aip_url).unwrap(), token)) |
| 86 | + .unwrap() |
| 87 | + .get_tracker_statistics(None) |
| 88 | + .await; |
| 89 | + |
| 90 | + response |
| 91 | + .json::<PartialGlobalStatistics>() |
| 92 | + .await |
| 93 | + .expect("Failed to parse JSON response") |
| 94 | +} |
0 commit comments