Skip to content

Commit

Permalink
update: Replaced Build_Config Fixture with TestConfigBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
heemankv committed Jul 26, 2024
1 parent 63bc846 commit f36b483
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 50 deletions.
115 changes: 115 additions & 0 deletions crates/orchestrator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,118 @@ pub async fn build_storage_client() -> Box<dyn DataStorage + Send + Sync> {
_ => panic!("Unsupported Storage Client"),
}
}

#[cfg(test)]
use httpmock::MockServer;

// Inspiration : https://rust-unofficial.github.io/patterns/patterns/creational/builder.html
// TestConfigBuilder allows to heavily customise the global configs based on the test's requirement.
// Eg: We want to mock only the da client and leave rest to be as it is, use mock_da_client.

// TestBuilder for Config
#[cfg(test)]
pub struct TestConfigBuilder {
/// The starknet client to get data from the node
starknet_client: Option<Arc<JsonRpcClient<HttpTransport>>>,
/// The DA client to interact with the DA layer
da_client: Option<Box<dyn DaClient>>,
/// The service that produces proof and registers it onchain
prover_client: Option<Box<dyn ProverClient>>,
/// Settlement client
settlement_client: Option<Box<dyn SettlementClient>>,
/// The database client
database: Option<Box<dyn Database>>,
/// Queue client
queue: Option<Box<dyn QueueProvider>>,
/// Storage client
storage: Option<Box<dyn DataStorage>>,
}

#[cfg(test)]
impl Default for TestConfigBuilder {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
impl TestConfigBuilder {
/// Create a new config
pub fn new() -> TestConfigBuilder {
TestConfigBuilder {
starknet_client: None,
da_client: None,
prover_client: None,
settlement_client: None,
database: None,
queue: None,
storage: None,
}
}

pub fn mock_da_client(mut self, da_client: Box<dyn DaClient>) -> TestConfigBuilder {
self.da_client = Some(da_client);
self
}

pub async fn build(mut self) -> MockServer {
dotenv().ok();

// init starknet client
if self.starknet_client.is_none() {
let provider = JsonRpcClient::new(HttpTransport::new(
Url::parse(get_env_var_or_panic("MADARA_RPC_URL").as_str()).expect("Failed to parse URL"),
));
self.starknet_client = Some(Arc::new(provider));
}

// init database
if self.database.is_none() {
self.database = Some(Box::new(MongoDb::new(MongoDbConfig::new_from_env()).await));
}

// init queue
if self.queue.is_none() {
self.queue = Some(Box::new(SqsQueue {}));
}

// init the DA client
if self.da_client.is_none() {
self.da_client = Some(build_da_client().await);
}

let settings_provider = DefaultSettingsProvider {};

// init the Settings client
if self.settlement_client.is_none() {
self.settlement_client = Some(build_settlement_client(&settings_provider).await);
}

// init the Prover client
if self.prover_client.is_none() {
self.prover_client = Some(build_prover_service(&settings_provider));
}

// init the storage client
if self.storage.is_none() {
self.storage = Some(build_storage_client().await);
}

// return config and server as tuple
let config = Config::new(
self.starknet_client.unwrap(),
self.da_client.unwrap(),
self.prover_client.unwrap(),
self.settlement_client.unwrap(),
self.database.unwrap(),
self.queue.unwrap(),
self.storage.unwrap(),
);

config_force_init(config).await;

let server = MockServer::connect(get_env_var_or_panic("MADARA_RPC_URL").as_str());

server
}
}
37 changes: 1 addition & 36 deletions crates/orchestrator/src/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ use settlement_client_interface::MockSettlementClient;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::JsonRpcClient;
use url::Url;
use utils::env_utils::get_env_var_or_panic;
use utils::settings::default::DefaultSettingsProvider;

use crate::config::{build_storage_client, config_force_init, Config};
use crate::config::Config;
use crate::data_storage::MockDataStorage;
use crate::database::mongodb::config::MongoDbConfig;
use crate::database::mongodb::MongoDb;
Expand Down Expand Up @@ -80,39 +78,6 @@ pub fn custom_job_item(default_job_item: JobItem, #[default(String::from("0"))]
job_item
}

/// For implementation of integration tests
#[fixture]
pub async fn build_config() -> color_eyre::Result<()> {
// Getting .env.test variables
dotenvy::from_filename("../.env.test")?;

// init starknet client
let provider = JsonRpcClient::new(HttpTransport::new(
Url::parse(get_env_var_or_panic("MADARA_RPC_URL").as_str()).expect("Failed to parse URL"),
));

// init database
let database = Box::new(MongoDb::new(MongoDbConfig::new_from_env()).await);

// init the queue
let queue = Box::new(crate::queue::sqs::SqsQueue {});

let da_client = crate::config::build_da_client().await;
let settings_provider = DefaultSettingsProvider {};
let settlement_client = crate::config::build_settlement_client(&settings_provider).await;
let prover_client = crate::config::build_prover_service(&settings_provider);
let storage_client = build_storage_client().await;

// building a test bucket :
storage_client.build_test_bucket(&get_env_var_or_panic("AWS_S3_BUCKET_NAME")).await?;

let config =
Config::new(Arc::new(provider), da_client, prover_client, settlement_client, database, queue, storage_client);
config_force_init(config).await;

Ok(())
}

pub async fn drop_database() -> color_eyre::Result<()> {
let db_client: Client = MongoDb::new(MongoDbConfig::new_from_env()).await.client();
// dropping `jobs` collection.
Expand Down
5 changes: 3 additions & 2 deletions crates/orchestrator/src/tests/data_storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::config::TestConfigBuilder;
use crate::data_storage::aws_s3::config::AWSS3Config;
use crate::data_storage::aws_s3::AWSS3;
use crate::data_storage::{DataStorage, DataStorageConfig};
use crate::tests::common::build_config;
use bytes::Bytes;
use rstest::rstest;
use serde_json::json;

#[rstest]
#[tokio::test]
async fn test_put_and_get_data_s3() -> color_eyre::Result<()> {
build_config().await?;
TestConfigBuilder::new().build().await;

dotenvy::from_filename("../.env.test")?;

let config = AWSS3Config::new_from_env();
Expand Down
16 changes: 4 additions & 12 deletions crates/orchestrator/src/tests/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
use crate::config::config;
use crate::config::{config, TestConfigBuilder};
use crate::jobs::types::{ExternalId, JobItem, JobStatus, JobType};
use crate::tests::common::{build_config, drop_database};
use color_eyre::eyre::eyre;
use crate::tests::common::drop_database;
use rstest::*;
use uuid::Uuid;

#[rstest]
#[tokio::test]
async fn test_database_connection() -> color_eyre::Result<()> {
let init_config_error = build_config().await.is_err();
if init_config_error {
return Err(eyre!("Not able to init config."));
}

TestConfigBuilder::new().build().await;
Ok(())
}

Expand All @@ -21,10 +16,7 @@ async fn test_database_connection() -> color_eyre::Result<()> {
#[rstest]
#[tokio::test]
async fn test_database_create_job() -> color_eyre::Result<()> {
let init_config = build_config().await.is_ok();
if !init_config {
return Err(eyre!("Not able to init config."));
}
TestConfigBuilder::new().build().await;

drop_database().await.unwrap();

Expand Down

0 comments on commit f36b483

Please sign in to comment.