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