Skip to content

Commit

Permalink
refactor: replaced test http servers with the new test environments
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke authored and josecelano committed Mar 9, 2023
1 parent 4f2b035 commit d914e5c
Show file tree
Hide file tree
Showing 13 changed files with 532 additions and 526 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "macros",
lazy_static = "1.4"
rand = "0.8.5"
torrust-tracker-configuration = { path = "../configuration"}
torrust-tracker-primitives = { path = "../primitives"}
69 changes: 69 additions & 0 deletions packages/test-helpers/src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;
use std::net::IpAddr;

use torrust_tracker_configuration::Configuration;
use torrust_tracker_primitives::TrackerMode;

use crate::random;

Expand Down Expand Up @@ -43,3 +45,70 @@ pub fn ephemeral() -> Configuration {

config
}

#[must_use]
pub fn ephemeral_with_reverse_proxy() -> Configuration {
let mut cfg = ephemeral();

cfg.on_reverse_proxy = true;

cfg
}

#[must_use]
pub fn ephemeral_mode_public() -> Configuration {
let mut cfg = ephemeral();

cfg.mode = TrackerMode::Public;

cfg
}

#[must_use]
pub fn ephemeral_mode_private() -> Configuration {
let mut cfg = ephemeral();

cfg.mode = TrackerMode::Private;

cfg
}

#[must_use]
pub fn ephemeral_mode_whitelisted() -> Configuration {
let mut cfg = ephemeral();

cfg.mode = TrackerMode::Listed;

cfg
}

#[must_use]
pub fn ephemeral_mode_private_whitelisted() -> Configuration {
let mut cfg = ephemeral();

cfg.mode = TrackerMode::PrivateListed;

cfg
}

#[must_use]
pub fn ephemeral_with_external_ip(ip: IpAddr) -> Configuration {
let mut cfg = ephemeral();

cfg.external_ip = Some(ip.to_string());

cfg
}

#[must_use]
pub fn ephemeral_ipv6() -> Configuration {
let mut cfg = ephemeral();

let ipv6 = format!("[::]:{}", 0);

cfg.http_api.bind_address = ipv6.clone();
cfg.http_trackers[0].bind_address = ipv6.clone();
cfg.udp_trackers[0].bind_address = ipv6;

cfg
}
6 changes: 3 additions & 3 deletions src/http/tracker_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ pub type StoppedHttpServer<I> = HttpServer<Stopped<I>>;
pub type RunningHttpServer<I> = HttpServer<Running<I>>;

pub struct HttpServer<S> {
cfg: torrust_tracker_configuration::HttpTracker,
state: S,
pub cfg: torrust_tracker_configuration::HttpTracker,
pub state: S,
}

pub struct Stopped<I: HttpServerLauncher> {
launcher: I,
}

pub struct Running<I: HttpServerLauncher> {
bind_addr: SocketAddr,
pub bind_addr: SocketAddr,
task_killer: tokio::sync::oneshot::Sender<u8>,
task: tokio::task::JoinHandle<I>,
}
Expand Down
6 changes: 3 additions & 3 deletions tests/api/test_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use torrust_tracker::tracker::peer::Peer;
use torrust_tracker::tracker::Tracker;

use super::connection_info::ConnectionInfo;
use crate::common::tracker::{tracker_configuration, tracker_instance};
use crate::common::tracker::new_tracker;

#[allow(clippy::module_name_repetitions, dead_code)]
pub type StoppedTestEnvironment = TestEnvironment<Stopped>;
Expand Down Expand Up @@ -89,9 +89,9 @@ pub fn running_test_environment() -> RunningTestEnvironment {
}

pub fn api_server() -> StoppedApiServer {
let config = tracker_configuration();
let config = Arc::new(torrust_tracker_test_helpers::configuration::ephemeral());

let tracker = tracker_instance(&config);
let tracker = new_tracker(config.clone());

ApiServer::new(config.http_api.clone(), tracker)
}
Expand Down
5 changes: 0 additions & 5 deletions tests/common/http.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
pub type ReqwestQuery = Vec<ReqwestQueryParam>;
pub type ReqwestQueryParam = (String, String);

#[derive(Clone, Debug)]
pub struct ConnectionInfo {
pub bind_address: String,
}

/// URL Query component
#[derive(Default, Debug)]
pub struct Query {
Expand Down
10 changes: 3 additions & 7 deletions tests/common/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ use torrust_tracker::tracker::statistics::Keeper;
use torrust_tracker::tracker::Tracker;
use torrust_tracker::{ephemeral_instance_keys, logging, static_time};

pub fn tracker_configuration() -> Arc<torrust_tracker_configuration::Configuration> {
Arc::new(torrust_tracker_test_helpers::configuration::ephemeral())
}

// TODO: Move to test-helpers crate once `Tracker` is isolated.
pub fn tracker_instance(configuration: &Arc<torrust_tracker_configuration::Configuration>) -> Arc<Tracker> {
pub fn new_tracker(configuration: Arc<torrust_tracker_configuration::Configuration>) -> Arc<Tracker> {
// Set the time of Torrust app starting
lazy_static::initialize(&static_time::TIME_AT_APP_START);

Expand All @@ -20,15 +16,15 @@ pub fn tracker_instance(configuration: &Arc<torrust_tracker_configuration::Confi
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();

// Initialize Torrust tracker
let tracker = match Tracker::new(configuration, Some(stats_event_sender), stats_repository) {
let tracker = match Tracker::new(&configuration, Some(stats_event_sender), stats_repository) {
Ok(tracker) => Arc::new(tracker),
Err(error) => {
panic!("{}", error)
}
};

// Initialize logging
logging::setup(configuration);
logging::setup(&configuration);

tracker
}
17 changes: 8 additions & 9 deletions tests/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ use std::net::IpAddr;
use reqwest::{Client as ReqwestClient, Response};
use torrust_tracker::tracker::auth::Key;

use super::connection_info::ConnectionInfo;
use super::requests::announce::{self, Query};
use super::requests::scrape;

/// HTTP Tracker Client
pub struct Client {
connection_info: ConnectionInfo,
server_addr: std::net::SocketAddr,
reqwest_client: ReqwestClient,
key: Option<Key>,
}
Expand All @@ -23,26 +22,26 @@ pub struct Client {
/// base url path query
/// ```
impl Client {
pub fn new(connection_info: ConnectionInfo) -> Self {
pub fn new(server_addr: std::net::SocketAddr) -> Self {
Self {
connection_info,
server_addr,
reqwest_client: reqwest::Client::builder().build().unwrap(),
key: None,
}
}

/// Creates the new client binding it to an specific local address
pub fn bind(connection_info: ConnectionInfo, local_address: IpAddr) -> Self {
pub fn bind(server_addr: std::net::SocketAddr, local_address: IpAddr) -> Self {
Self {
connection_info,
server_addr,
reqwest_client: reqwest::Client::builder().local_address(local_address).build().unwrap(),
key: None,
}
}

pub fn authenticated(connection_info: ConnectionInfo, key: Key) -> Self {
pub fn authenticated(server_addr: std::net::SocketAddr, key: Key) -> Self {
Self {
connection_info,
server_addr,
reqwest_client: reqwest::Client::builder().build().unwrap(),
key: Some(key),
}
Expand Down Expand Up @@ -95,6 +94,6 @@ impl Client {
}

fn base_url(&self) -> String {
format!("http://{}/", &self.connection_info.bind_address)
format!("http://{}/", &self.server_addr)
}
}
2 changes: 0 additions & 2 deletions tests/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
pub mod asserts;
pub mod asserts_warp;
pub mod client;
pub mod connection_info;
pub mod requests;
pub mod responses;
pub mod server;
pub mod test_environment;

use percent_encoding::NON_ALPHANUMERIC;
Expand Down
137 changes: 0 additions & 137 deletions tests/http/server.rs

This file was deleted.

Loading

0 comments on commit d914e5c

Please sign in to comment.