forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_environment.rs
More file actions
105 lines (85 loc) · 3.11 KB
/
test_environment.rs
File metadata and controls
105 lines (85 loc) · 3.11 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
95
96
97
98
99
100
101
102
103
104
105
use std::sync::Arc;
use torrust_tracker::servers::apis::server::{ApiServer, RunningApiServer, StoppedApiServer};
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
use torrust_tracker::tracker::peer::Peer;
use torrust_tracker::tracker::Tracker;
use super::connection_info::ConnectionInfo;
use crate::common::app::setup_with_configuration;
#[allow(clippy::module_name_repetitions, dead_code)]
pub type StoppedTestEnvironment = TestEnvironment<Stopped>;
#[allow(clippy::module_name_repetitions)]
pub type RunningTestEnvironment = TestEnvironment<Running>;
pub struct TestEnvironment<S> {
pub cfg: Arc<torrust_tracker_configuration::Configuration>,
pub tracker: Arc<Tracker>,
pub state: S,
}
#[allow(dead_code)]
pub struct Stopped {
api_server: StoppedApiServer,
}
pub struct Running {
api_server: RunningApiServer,
}
impl<S> TestEnvironment<S> {
/// Add a torrent to the tracker
pub async fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &Peer) {
self.tracker.update_torrent_with_peer_and_get_stats(info_hash, peer).await;
}
}
impl TestEnvironment<Stopped> {
pub fn new_stopped(cfg: torrust_tracker_configuration::Configuration) -> Self {
let cfg = Arc::new(cfg);
let tracker = setup_with_configuration(&cfg);
let api_server = api_server(cfg.http_api.clone());
Self {
cfg,
tracker,
state: Stopped { api_server },
}
}
pub async fn start(self) -> TestEnvironment<Running> {
TestEnvironment {
cfg: self.cfg,
tracker: self.tracker.clone(),
state: Running {
api_server: self.state.api_server.start(self.tracker).await.unwrap(),
},
}
}
pub fn config_mut(&mut self) -> &mut torrust_tracker_configuration::HttpApi {
&mut self.state.api_server.cfg
}
}
impl TestEnvironment<Running> {
pub async fn new_running(cfg: torrust_tracker_configuration::Configuration) -> Self {
let test_env = StoppedTestEnvironment::new_stopped(cfg);
test_env.start().await
}
pub async fn stop(self) -> TestEnvironment<Stopped> {
TestEnvironment {
cfg: self.cfg,
tracker: self.tracker,
state: Stopped {
api_server: self.state.api_server.stop().await.unwrap(),
},
}
}
pub fn get_connection_info(&self) -> ConnectionInfo {
ConnectionInfo {
bind_address: self.state.api_server.state.bind_addr.to_string(),
api_token: self.state.api_server.cfg.access_tokens.get("admin").cloned(),
}
}
}
#[allow(clippy::module_name_repetitions)]
pub fn stopped_test_environment(cfg: torrust_tracker_configuration::Configuration) -> StoppedTestEnvironment {
TestEnvironment::new_stopped(cfg)
}
#[allow(clippy::module_name_repetitions)]
pub async fn running_test_environment(cfg: torrust_tracker_configuration::Configuration) -> RunningTestEnvironment {
TestEnvironment::new_running(cfg).await
}
pub fn api_server(cfg: torrust_tracker_configuration::HttpApi) -> StoppedApiServer {
ApiServer::new(cfg)
}