Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: move shared test functionality to a dev-dependency #170

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Cargo.lock

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

61 changes: 32 additions & 29 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
[package]
edition = "2021"
name = "torrust-tracker"
version = "2.3.0"
license = "AGPL-3.0"
authors = ["Mick van Dijke <mick@dutchbits.nl>"]
description = "A feature rich BitTorrent tracker."
repository = "https://github.com/torrust/torrust-tracker"

[profile.dev]
debug = 1
opt-level = 1
lto = "thin"
license = "AGPL-3.0"
authors.workspace = true
edition.workspace = true
version.workspace = true

[profile.release]
debug = 1
opt-level = 3
lto = "fat"
[workspace.package]
authors = ["Nautilus Cyberneering <info@nautilus-cyberneering.de>, Mick van Dijke <mick@dutchbits.nl>"]
edition = "2021"
repository = "https://github.com/torrust/torrust-tracker"
version = "2.3.0"

[dependencies]
tokio = { version = "1", features = [
"rt-multi-thread",
"net",
"sync",
"macros",
"signal",
] }

tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "macros", "signal"] }
serde = { version = "1.0", features = ["derive"] }
serde_bencode = "^0.2.3"
serde_json = "1.0"
Expand All @@ -34,34 +22,30 @@ hex = "0.4.3"
percent-encoding = "2"
binascii = "0.1"
lazy_static = "1.4"

openssl = { version = "0.10", features = ["vendored"] }

warp = { version = "0.3", features = ["tls"] }

config = "0.13"
toml = "0.5"

log = { version = "0.4", features = ["release_max_level_info"] }
fern = "0.6"
chrono = "0.4"

r2d2 = "0.8"
r2d2_mysql = "21"
r2d2_sqlite = { version = "0.21", features = ["bundled"] }

rand = "0.8"
derive_more = "0.99"
thiserror = "1.0"
futures = "0.3"
async-trait = "0.1"

aquatic_udp_protocol = "0.2"
uuid = { version = "1", features = ["v4"] }
axum = "0.6.1"
axum-server = { version = "0.4.4", features = ["tls-rustls"] }
axum-client-ip = "0.4.0"
bip_bencode = "0.4.4"
torrust-tracker-primitives = { path = "packages/primitives" }
torrust-tracker-configuration = { path = "packages/configuration" }
torrust-tracker-located-error = { path = "packages/located-error" }
multimap = "0.8.3"


Expand All @@ -72,3 +56,22 @@ serde_urlencoded = "0.7.1"
serde_repr = "0.1.10"
serde_bytes = "0.11.8"
local-ip-address = "0.5.1"
torrust-tracker-test-helpers = { path = "packages/test-helpers" }

[workspace]
members = [
"packages/configuration",
"packages/primitives",
"packages/test-helpers",
"packages/located-error",
]

[profile.dev]
debug = 1
opt-level = 1
lto = "thin"

[profile.release]
debug = 1
opt-level = 3
lto = "fat"
16 changes: 16 additions & 0 deletions packages/configuration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "torrust-tracker-configuration"
version.workspace = true
authors.workspace = true
edition.workspace = true

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_with = "2.0"
config = "0.13"
toml = "0.5"
log = { version = "0.4", features = ["release_max_level_info"] }
thiserror = "1.0"
torrust-tracker-primitives = { path = "../primitives" }
torrust-tracker-located-error = { path = "../located-error" }
uuid = { version = "1", features = ["v4"] }
70 changes: 10 additions & 60 deletions src/config.rs → packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ use std::{env, fs};

use config::{Config, ConfigError, File, FileFormat};
use log::warn;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, NoneAsEmptyString};
use thiserror::Error;
use {std, toml};
use torrust_tracker_located_error::{Located, LocatedError};
use torrust_tracker_primitives::{DatabaseDriver, TrackerMode};

use crate::databases::driver::Driver;
use crate::located_error::{Located, LocatedError};
use crate::tracker::mode;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct UdpTracker {
pub enabled: bool,
pub bind_address: String,
}

#[serde_as]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct HttpTracker {
pub enabled: bool,
pub bind_address: String,
Expand Down Expand Up @@ -62,8 +58,8 @@ impl HttpApi {
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct Configuration {
pub log_level: Option<String>,
pub mode: mode::Mode,
pub db_driver: Driver,
pub mode: TrackerMode,
pub db_driver: DatabaseDriver,
pub db_path: String,
pub announce_interval: u32,
pub min_announce_interval: u32,
Expand Down Expand Up @@ -105,58 +101,12 @@ impl From<ConfigError> for Error {
}
}

/// This configuration is used for testing. It generates random config values so they do not collide
/// if you run more than one tracker at the same time.
///
/// # Panics
///
/// Will panic if it can't convert the temp file path to string
#[must_use]
pub fn ephemeral_configuration() -> Configuration {
// todo: disable services that are not needed.
// For example: a test for the UDP tracker should disable the API and HTTP tracker.

let mut config = Configuration {
log_level: Some("off".to_owned()), // Change to `debug` for tests debugging
..Default::default()
};

// Ephemeral socket address for API
let api_port = random_port();
config.http_api.enabled = true;
config.http_api.bind_address = format!("127.0.0.1:{}", &api_port);

// Ephemeral socket address for UDP tracker
let upd_port = random_port();
config.udp_trackers[0].enabled = true;
config.udp_trackers[0].bind_address = format!("127.0.0.1:{}", &upd_port);

// Ephemeral socket address for HTTP tracker
let http_port = random_port();
config.http_trackers[0].enabled = true;
config.http_trackers[0].bind_address = format!("127.0.0.1:{}", &http_port);

// Ephemeral sqlite database
let temp_directory = env::temp_dir();
let temp_file = temp_directory.join(format!("data_{}_{}_{}.db", &api_port, &upd_port, &http_port));
config.db_path = temp_file.to_str().unwrap().to_owned();

config
}

fn random_port() -> u16 {
// todo: this may produce random test failures because two tests can try to bind the same port.
// We could create a pool of available ports (with read/write lock)
let mut rng = thread_rng();
rng.gen_range(49152..65535)
}

impl Default for Configuration {
fn default() -> Self {
let mut configuration = Configuration {
log_level: Option::from(String::from("info")),
mode: mode::Mode::Public,
db_driver: Driver::Sqlite3,
mode: TrackerMode::Public,
db_driver: DatabaseDriver::Sqlite3,
db_path: String::from("./storage/database/data.db"),
announce_interval: 120,
min_announce_interval: 120,
Expand Down Expand Up @@ -266,7 +216,7 @@ impl Configuration {

#[cfg(test)]
mod tests {
use crate::config::Configuration;
use crate::Configuration;

#[cfg(test)]
fn default_config_toml() -> String {
Expand Down Expand Up @@ -325,7 +275,7 @@ mod tests {
fn configuration_should_contain_the_external_ip() {
let configuration = Configuration::default();

assert_eq!(configuration.external_ip, Option::Some(String::from("0.0.0.0")));
assert_eq!(configuration.external_ip, Some(String::from("0.0.0.0")));
}

#[test]
Expand Down
9 changes: 9 additions & 0 deletions packages/located-error/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "torrust-tracker-located-error"
version.workspace = true
authors.workspace = true
edition.workspace = true

[dependencies]
log = { version = "0.4", features = ["release_max_level_info"] }
thiserror = "1.0"
File renamed without changes.
9 changes: 9 additions & 0 deletions packages/primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "torrust-tracker-primitives"
version.workspace = true
authors.workspace = true
edition.workspace = true

[dependencies]
serde = { version = "1.0", features = ["derive"] }
derive_more = "0.99"
10 changes: 8 additions & 2 deletions src/tracker/mode.rs → packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use serde;
use serde::{Deserialize, Serialize};

// TODO: Move to the database crate once that gets its own crate.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
pub enum DatabaseDriver {
Sqlite3,
MySQL,
}

#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)]
pub enum Mode {
pub enum TrackerMode {
// Will track every new info hash and serve every peer.
#[serde(rename = "public")]
Public,
Expand Down
12 changes: 12 additions & 0 deletions packages/test-helpers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "torrust-tracker-test-helpers"
version.workspace = true
authors.workspace = true
edition.workspace = true

[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "net", "sync", "macros", "signal"] }
lazy_static = "1.4"
rand = "0.8.5"
torrust-tracker-configuration = { path = "../configuration"}
torrust-tracker-primitives = { path = "../primitives"}
Loading