Skip to content

Commit 8510990

Browse files
committed
test: [torrust#1407] add test for global metrics with 2 http trackers
A new integration test that checks that the global metrics are udapted when you run 2 HTTP trackers. Ony one metric is checked in this test. It uses fixed port that migth conflict with other running instances in the future. We should use a random free port if we run more integration tests like this in the future.
1 parent 1f1da7b commit 8510990

File tree

7 files changed

+104
-0
lines changed

7 files changed

+104
-0
lines changed

Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ tracing = "0"
6060
tracing-subscriber = { version = "0", features = ["json"] }
6161

6262
[dev-dependencies]
63+
bittorrent-primitives = "0.1.0"
64+
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
6365
local-ip-address = "0"
6466
mockall = "0"
6567
torrust-rest-tracker-api-client = { version = "3.0.0-develop", path = "packages/rest-tracker-api-client" }

src/app.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) ->
138138
));
139139
}
140140

141+
println!("Registar entries: {:?}", registar.entries());
142+
141143
// Start Health Check API
142144
jobs.push(health_check_api::start_job(&config.health_check_api, registar.entries()).await);
143145

tests/servers/api/contract/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod stats;
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

tests/servers/api/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod contract;

tests/servers/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pub mod api;
12
pub mod health_check_api;

0 commit comments

Comments
 (0)