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

feat: read test keys from file #79

Merged
merged 4 commits into from
Jan 24, 2024
Merged
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
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 integrity_verification/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rand = "0.8.5"
tokio-util = "0.7.10"
assert-json-diff = "2.0.2"
regex = "1.10.2"
async-trait = "0.1.77"

[features]
rpc_tests = []
15 changes: 14 additions & 1 deletion integrity_verification/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@ use figment::providers::Env;
use figment::Figment;
use serde_derive::Deserialize;

#[derive(Deserialize, Default, PartialEq, Debug, Clone)]
pub enum TestSourceMode {
File,
#[default]
Database,
}

#[derive(Deserialize, Debug)]
pub struct IntegrityVerificationConfig {
pub metrics_port: u16,
pub reference_host: String,
pub testing_host: String,
pub database_url: String,
pub database_url: Option<String>,
pub run_secondary_indexes_tests: Option<bool>,
pub run_proofs_tests: Option<bool>,
pub run_assets_tests: Option<bool>,
pub test_source_mode: TestSourceMode,
pub test_file_path: Option<String>,
}

impl IntegrityVerificationConfig {
Expand All @@ -19,6 +29,9 @@ impl IntegrityVerificationConfig {
pub fn get_run_proofs_tests(&self) -> bool {
self.run_proofs_tests.unwrap_or_default()
}
pub fn get_run_assets_tests(&self) -> bool {
self.run_assets_tests.unwrap_or_default()
}
}

// Use unwraps because it just config-setup stage
Expand Down
66 changes: 66 additions & 0 deletions integrity_verification/src/file_keys_fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::diff_checker::{
GET_ASSET_BY_AUTHORITY_METHOD, GET_ASSET_BY_CREATOR_METHOD, GET_ASSET_BY_GROUP_METHOD,
GET_ASSET_BY_OWNER_METHOD, GET_ASSET_METHOD, GET_ASSET_PROOF_METHOD,
};
use async_trait::async_trait;
use postgre_client::storage_traits::IntegrityVerificationKeysFetcher;
use std::collections::HashMap;
use tokio::fs::File;
use tokio::io::{AsyncBufReadExt, BufReader};

pub struct FileKeysFetcher {
keys_map: HashMap<String, Vec<String>>,
}

impl FileKeysFetcher {
pub async fn new(file_path: &str) -> Result<Self, String> {
let file = File::open(file_path).await.map_err(|e| e.to_string())?;
let reader = BufReader::new(file);
let mut lines = reader.lines();

let mut keys_map = HashMap::new();
let mut current_key = None;

while let Some(line) = lines.next_line().await.map_err(|e| e.to_string())? {
if line.ends_with(':') {
current_key = Some(line.trim_end_matches(':').to_string());
} else if let Some(key) = &current_key {
if !line.is_empty() {
let keys = line.split(',').map(String::from).collect();
keys_map.insert(key.clone(), keys);
}
}
}

Ok(FileKeysFetcher { keys_map })
}
fn read_keys(&self, method_name: &str) -> Result<Vec<String>, String> {
Ok(self.keys_map.get(method_name).cloned().unwrap_or_default())
}
}
#[async_trait]
impl IntegrityVerificationKeysFetcher for FileKeysFetcher {
async fn get_verification_required_owners_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_BY_OWNER_METHOD)
}

async fn get_verification_required_creators_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_BY_CREATOR_METHOD)
}

async fn get_verification_required_authorities_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_BY_AUTHORITY_METHOD)
}

async fn get_verification_required_groups_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_BY_GROUP_METHOD)
}

async fn get_verification_required_assets_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_METHOD)
}

async fn get_verification_required_assets_proof_keys(&self) -> Result<Vec<String>, String> {
self.read_keys(GET_ASSET_PROOF_METHOD)
}
}
89 changes: 60 additions & 29 deletions integrity_verification/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::config::setup_config;
use crate::config::{setup_config, TestSourceMode};
use crate::diff_checker::{
DiffChecker, GET_ASSET_BY_AUTHORITY_METHOD, GET_ASSET_BY_CREATOR_METHOD,
GET_ASSET_BY_GROUP_METHOD, GET_ASSET_BY_OWNER_METHOD, GET_ASSET_METHOD, GET_ASSET_PROOF_METHOD,
};
use crate::error::IntegrityVerificationError;
use crate::file_keys_fetcher::FileKeysFetcher;
use clap::Parser;
use metrics_utils::utils::start_metrics;
use metrics_utils::{
IntegrityVerificationMetrics, IntegrityVerificationMetricsConfig, MetricsTrait,
};
use postgre_client::storage_traits::IntegrityVerificationKeysFetcher;
use postgre_client::PgClient;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -20,6 +22,7 @@ mod api;
mod config;
mod diff_checker;
mod error;
mod file_keys_fetcher;
mod params;
mod requests;

Expand All @@ -42,25 +45,48 @@ async fn main() -> Result<(), IntegrityVerificationError> {
metrics.register_metrics();
start_metrics(metrics.registry, Some(config.metrics_port)).await;

let keys_fetcher = PgClient::new(&config.database_url, 100, 500).await;
let diff_checker = DiffChecker::new(
config.reference_host.clone(),
config.testing_host.clone(),
keys_fetcher,
metrics.integrity_verification_metrics.clone(),
);

let mut tasks = JoinSet::new();
let cancel_token = CancellationToken::new();
run_tests(
&mut tasks,
config.get_run_secondary_indexes_tests(),
config.get_run_proofs_tests(),
diff_checker,
metrics.integrity_verification_metrics.clone(),
cancel_token.clone(),
)
.await;
match config.test_source_mode {
TestSourceMode::File => {
let diff_checker = DiffChecker::new(
config.reference_host.clone(),
config.testing_host.clone(),
FileKeysFetcher::new(&config.test_file_path.clone().unwrap())
.await
.unwrap(),
metrics.integrity_verification_metrics.clone(),
);
run_tests(
&mut tasks,
config.get_run_secondary_indexes_tests(),
config.get_run_proofs_tests(),
config.get_run_assets_tests(),
diff_checker,
metrics.integrity_verification_metrics.clone(),
cancel_token.clone(),
)
.await;
}
TestSourceMode::Database => {
let diff_checker = DiffChecker::new(
config.reference_host.clone(),
config.testing_host.clone(),
PgClient::new(&config.database_url.clone().unwrap(), 100, 500).await,
metrics.integrity_verification_metrics.clone(),
);
run_tests(
&mut tasks,
config.get_run_secondary_indexes_tests(),
config.get_run_proofs_tests(),
config.get_run_assets_tests(),
diff_checker,
metrics.integrity_verification_metrics.clone(),
cancel_token.clone(),
)
.await;
}
};

usecase::graceful_stop::listen_shutdown().await;
cancel_token.cancel();
Expand Down Expand Up @@ -94,23 +120,28 @@ macro_rules! spawn_test {
}};
}

async fn run_tests(
async fn run_tests<T>(
tasks: &mut JoinSet<Result<(), JoinError>>,
run_secondary_indexes_tests: bool,
run_proofs_tests: bool,
diff_checker: DiffChecker<PgClient>,
run_assets_tests: bool,
diff_checker: DiffChecker<T>,
metrics: Arc<IntegrityVerificationMetricsConfig>,
cancel_token: CancellationToken,
) {
) where
T: IntegrityVerificationKeysFetcher + Send + Sync + 'static,
{
let diff_checker = Arc::new(diff_checker);
spawn_test!(
tasks,
diff_checker,
metrics,
check_get_asset,
GET_ASSET_METHOD,
cancel_token
);
if run_assets_tests {
spawn_test!(
tasks,
diff_checker,
metrics,
check_get_asset,
GET_ASSET_METHOD,
cancel_token
);
}
if run_proofs_tests {
spawn_test!(
tasks,
Expand Down