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