2024-02-27 16:49:36 +00:00
|
|
|
use ibis_lib::backend::config::{IbisConfig, IbisConfigDatabase, IbisConfigFederation};
|
2024-01-17 15:40:01 +00:00
|
|
|
use ibis_lib::backend::start;
|
|
|
|
use ibis_lib::common::RegisterUserData;
|
|
|
|
use ibis_lib::frontend::api::ApiClient;
|
|
|
|
use ibis_lib::frontend::error::MyResult;
|
2024-01-18 11:18:17 +00:00
|
|
|
use reqwest::ClientBuilder;
|
2023-12-01 11:11:19 +00:00
|
|
|
use std::env::current_dir;
|
2024-02-09 10:08:57 +00:00
|
|
|
use std::fs::{create_dir_all, remove_dir_all};
|
2024-01-17 15:40:01 +00:00
|
|
|
use std::ops::Deref;
|
2023-12-01 11:11:19 +00:00
|
|
|
use std::process::{Command, Stdio};
|
2023-12-04 01:42:53 +00:00
|
|
|
use std::sync::atomic::{AtomicI32, Ordering};
|
2023-11-16 14:27:35 +00:00
|
|
|
use std::sync::Once;
|
2023-12-04 01:42:53 +00:00
|
|
|
use std::thread::{sleep, spawn};
|
|
|
|
use std::time::Duration;
|
2023-11-17 13:36:56 +00:00
|
|
|
use tokio::task::JoinHandle;
|
2023-11-16 14:27:35 +00:00
|
|
|
use tracing::log::LevelFilter;
|
|
|
|
|
2023-11-17 13:36:56 +00:00
|
|
|
pub struct TestData {
|
2023-12-20 16:08:19 +00:00
|
|
|
pub alpha: IbisInstance,
|
|
|
|
pub beta: IbisInstance,
|
|
|
|
pub gamma: IbisInstance,
|
2023-11-17 13:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestData {
|
2023-12-13 15:40:20 +00:00
|
|
|
pub async fn start() -> Self {
|
2023-11-17 13:36:56 +00:00
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
|
|
|
env_logger::builder()
|
|
|
|
.filter_level(LevelFilter::Warn)
|
|
|
|
.filter_module("activitypub_federation", LevelFilter::Info)
|
2023-12-20 16:08:19 +00:00
|
|
|
.filter_module("ibis", LevelFilter::Info)
|
2023-11-17 13:36:56 +00:00
|
|
|
.init();
|
|
|
|
});
|
|
|
|
|
2023-12-04 01:42:53 +00:00
|
|
|
// Run things on different ports and db paths to allow parallel tests
|
|
|
|
static COUNTER: AtomicI32 = AtomicI32::new(0);
|
|
|
|
let current_run = COUNTER.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
|
|
|
// Give each test a moment to start its postgres databases
|
2024-02-09 14:18:29 +00:00
|
|
|
sleep(Duration::from_millis(current_run as u64 * 2000));
|
2023-12-04 01:42:53 +00:00
|
|
|
|
2024-02-12 15:37:12 +00:00
|
|
|
let first_port = 8100 + (current_run * 3);
|
2023-12-04 01:42:53 +00:00
|
|
|
let port_alpha = first_port;
|
|
|
|
let port_beta = first_port + 1;
|
|
|
|
let port_gamma = first_port + 2;
|
|
|
|
|
|
|
|
let alpha_db_path = generate_db_path("alpha", port_alpha);
|
|
|
|
let beta_db_path = generate_db_path("beta", port_beta);
|
|
|
|
let gamma_db_path = generate_db_path("gamma", port_gamma);
|
2023-12-01 14:16:07 +00:00
|
|
|
|
2023-12-01 23:59:24 +00:00
|
|
|
// initialize postgres databases in parallel because its slow
|
|
|
|
for j in [
|
2023-12-20 16:08:19 +00:00
|
|
|
IbisInstance::prepare_db(alpha_db_path.clone()),
|
|
|
|
IbisInstance::prepare_db(beta_db_path.clone()),
|
|
|
|
IbisInstance::prepare_db(gamma_db_path.clone()),
|
2023-12-01 23:59:24 +00:00
|
|
|
] {
|
|
|
|
j.join().unwrap();
|
|
|
|
}
|
2023-12-01 14:16:07 +00:00
|
|
|
|
2023-11-17 13:36:56 +00:00
|
|
|
Self {
|
2023-12-20 16:08:19 +00:00
|
|
|
alpha: IbisInstance::start(alpha_db_path, port_alpha, "alpha").await,
|
|
|
|
beta: IbisInstance::start(beta_db_path, port_beta, "beta").await,
|
|
|
|
gamma: IbisInstance::start(gamma_db_path, port_gamma, "gamma").await,
|
2023-11-17 13:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:48:29 +00:00
|
|
|
pub fn stop(self) -> MyResult<()> {
|
2023-12-01 23:59:24 +00:00
|
|
|
for j in [self.alpha.stop(), self.beta.stop(), self.gamma.stop()] {
|
|
|
|
j.join().unwrap();
|
|
|
|
}
|
2023-11-17 13:36:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-11-16 14:27:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-04 01:42:53 +00:00
|
|
|
/// Generate a unique db path for each postgres so that tests can run in parallel.
|
|
|
|
fn generate_db_path(name: &'static str, port: i32) -> String {
|
2023-12-12 15:32:57 +00:00
|
|
|
let path = format!(
|
2023-12-04 01:42:53 +00:00
|
|
|
"{}/target/test_db/{name}-{port}",
|
|
|
|
current_dir().unwrap().display()
|
2023-12-12 15:32:57 +00:00
|
|
|
);
|
|
|
|
create_dir_all(&path).unwrap();
|
|
|
|
path
|
2023-12-01 14:16:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 16:08:19 +00:00
|
|
|
pub struct IbisInstance {
|
2024-01-17 15:40:01 +00:00
|
|
|
pub api_client: ApiClient,
|
2023-12-01 23:59:24 +00:00
|
|
|
db_path: String,
|
|
|
|
db_handle: JoinHandle<()>,
|
2023-12-01 11:11:19 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 16:08:19 +00:00
|
|
|
impl IbisInstance {
|
2023-12-01 23:59:24 +00:00
|
|
|
fn prepare_db(db_path: String) -> std::thread::JoinHandle<()> {
|
2024-02-09 10:08:57 +00:00
|
|
|
// stop any db leftover from previous run
|
|
|
|
Self::stop_internal(db_path.clone());
|
|
|
|
// remove old db
|
|
|
|
remove_dir_all(&db_path).unwrap();
|
2023-12-01 23:59:24 +00:00
|
|
|
spawn(move || {
|
2024-02-09 10:30:44 +00:00
|
|
|
Command::new("./scripts/start_dev_db.sh")
|
2023-12-01 23:59:24 +00:00
|
|
|
.arg(&db_path)
|
2024-02-09 10:30:44 +00:00
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
2023-12-01 23:59:24 +00:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-13 15:40:20 +00:00
|
|
|
async fn start(db_path: String, port: i32, username: &str) -> Self {
|
2024-02-27 16:49:36 +00:00
|
|
|
let connection_url = format!("postgresql://ibis:password@/ibis?host={db_path}");
|
2024-02-09 14:18:29 +00:00
|
|
|
let hostname = format!("localhost:{port}");
|
2024-02-07 15:54:43 +00:00
|
|
|
let bind = format!("127.0.0.1:{port}").parse().unwrap();
|
|
|
|
let config = IbisConfig {
|
|
|
|
bind,
|
2024-02-27 16:49:36 +00:00
|
|
|
database: IbisConfigDatabase {
|
|
|
|
connection_url,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2024-02-07 15:54:43 +00:00
|
|
|
registration_open: true,
|
|
|
|
federation: IbisConfigFederation {
|
|
|
|
domain: hostname.clone(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-12-01 11:11:19 +00:00
|
|
|
let handle = tokio::task::spawn(async move {
|
2024-02-07 15:54:43 +00:00
|
|
|
start(config).await.unwrap();
|
2023-12-01 11:11:19 +00:00
|
|
|
});
|
2024-01-03 12:29:25 +00:00
|
|
|
// wait a moment for the backend to start
|
2024-02-09 14:18:29 +00:00
|
|
|
tokio::time::sleep(Duration::from_millis(5000)).await;
|
2024-01-16 15:07:01 +00:00
|
|
|
let form = RegisterUserData {
|
|
|
|
username: username.to_string(),
|
|
|
|
password: "hunter2".to_string(),
|
|
|
|
};
|
2024-01-18 14:31:32 +00:00
|
|
|
let client = ClientBuilder::new().cookie_store(true).build().unwrap();
|
2024-01-17 15:40:01 +00:00
|
|
|
let api_client = ApiClient::new(client, hostname.clone());
|
|
|
|
api_client.register(form).await.unwrap();
|
2023-12-01 11:11:19 +00:00
|
|
|
Self {
|
2024-01-17 15:40:01 +00:00
|
|
|
api_client,
|
2023-12-13 15:40:20 +00:00
|
|
|
db_path,
|
2023-12-01 23:59:24 +00:00
|
|
|
db_handle: handle,
|
2023-12-01 11:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:59:24 +00:00
|
|
|
fn stop(self) -> std::thread::JoinHandle<()> {
|
|
|
|
self.db_handle.abort();
|
2024-02-09 10:08:57 +00:00
|
|
|
Self::stop_internal(self.db_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop_internal(db_path: String) -> std::thread::JoinHandle<()> {
|
2023-12-01 23:59:24 +00:00
|
|
|
spawn(move || {
|
2024-02-09 10:30:44 +00:00
|
|
|
Command::new("./scripts/stop_dev_db.sh")
|
2024-02-09 10:08:57 +00:00
|
|
|
.arg(db_path)
|
2023-12-01 23:59:24 +00:00
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
})
|
2023-12-01 11:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:40:01 +00:00
|
|
|
impl Deref for IbisInstance {
|
|
|
|
type Target = ApiClient;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.api_client
|
|
|
|
}
|
|
|
|
}
|
2023-11-27 10:25:29 +00:00
|
|
|
|
2024-01-18 11:18:17 +00:00
|
|
|
pub const TEST_ARTICLE_DEFAULT_TEXT: &str = "some\nexample\ntext\n";
|