diff --git a/services/commtest/src/backup/backup_utils.rs b/services/commtest/src/backup/backup_utils.rs new file mode 100644 --- /dev/null +++ b/services/commtest/src/backup/backup_utils.rs @@ -0,0 +1,97 @@ +use crate::identity::device::DeviceInfo; + +use crate::tools::generate_stable_nbytes; +use backup_client::{BackupData, Error as BackupClientError}; +use bytesize::ByteSize; +use comm_lib::auth::UserIdentity; +use comm_lib::backup::UploadLogRequest; +use reqwest::StatusCode; +use uuid::Uuid; + +pub fn generate_backup_data(predefined_byte_value: u8) -> BackupData { + BackupData { + backup_id: Uuid::new_v4().to_string(), + user_keys: generate_stable_nbytes( + ByteSize::kib(4).as_u64() as usize, + Some(predefined_byte_value), + ), + user_data: generate_stable_nbytes( + ByteSize::mib(4).as_u64() as usize, + Some(predefined_byte_value), + ), + attachments: vec![], + siwe_backup_msg: None, + } +} + +fn generate_log_data(backup_id: &str, value: u8) -> Vec { + const IN_DB_SIZE: usize = ByteSize::kib(4).as_u64() as usize; + const IN_BLOB_SIZE: usize = ByteSize::kib(400).as_u64() as usize; + + (1..30) + .map(|log_id| { + let size = if log_id % 2 == 0 { + IN_DB_SIZE + } else { + IN_BLOB_SIZE + }; + let attachments = if log_id % 10 == 0 { + Some(vec![Uuid::new_v4().to_string()]) + } else { + None + }; + let mut content = generate_stable_nbytes(size, Some(value)); + let unique_suffix = log_id.to_string(); + content.extend(unique_suffix.as_bytes()); + + UploadLogRequest { + backup_id: backup_id.to_string(), + log_id, + content, + attachments, + } + }) + .collect() +} + +pub fn generate_backup_data_with_logs( + predefined_byte_values: Vec, +) -> Vec<(BackupData, Vec)> { + predefined_byte_values + .into_iter() + .map(|byte_value| { + let backup_data = generate_backup_data(byte_value); + let log_data = generate_log_data(&backup_data.backup_id, byte_value); + (backup_data, log_data) + }) + .collect() +} + +pub fn assert_reqwest_error( + response: Result, + expected_status: StatusCode, +) { + match response { + Err(BackupClientError::ReqwestError(error)) => { + assert_eq!( + error.status(), + Some(expected_status), + "Expected status {}", + expected_status + ); + } + Err(err) => panic!( + "Backup should return ReqwestError, instead got response: {:?}", + err + ), + Ok(_) => panic!("Backup should return BackupClientError"), + } +} + +pub fn create_user_identity(device_info: DeviceInfo) -> UserIdentity { + UserIdentity { + user_id: device_info.user_id.clone(), + access_token: device_info.access_token.clone(), + device_id: device_info.device_id.clone(), + } +} diff --git a/services/commtest/src/backup/mod.rs b/services/commtest/src/backup/mod.rs new file mode 100644 --- /dev/null +++ b/services/commtest/src/backup/mod.rs @@ -0,0 +1 @@ +pub mod backup_utils; diff --git a/services/commtest/src/identity/device.rs b/services/commtest/src/identity/device.rs --- a/services/commtest/src/identity/device.rs +++ b/services/commtest/src/identity/device.rs @@ -19,6 +19,7 @@ pub const DEVICE_TYPE: &str = "service"; const PASSWORD: &str = "pass"; +#[derive(Clone)] pub struct DeviceInfo { pub username: String, pub user_id: String, diff --git a/services/commtest/src/lib.rs b/services/commtest/src/lib.rs --- a/services/commtest/src/lib.rs +++ b/services/commtest/src/lib.rs @@ -1,3 +1,4 @@ +pub mod backup; pub mod blob; pub mod constants; pub mod identity; diff --git a/services/commtest/tests/backup_integration_test.rs b/services/commtest/tests/backup_integration_test.rs --- a/services/commtest/tests/backup_integration_test.rs +++ b/services/commtest/tests/backup_integration_test.rs @@ -1,36 +1,25 @@ use backup_client::{ - BackupClient, BackupData, BackupDescriptor, DownloadedLog, - Error as BackupClientError, LogUploadConfirmation, RequestedData, SinkExt, - StreamExt, TryStreamExt, + BackupClient, BackupDescriptor, DownloadedLog, LogUploadConfirmation, + RequestedData, SinkExt, StreamExt, TryStreamExt, }; -use bytesize::ByteSize; -use comm_lib::{ - auth::UserIdentity, - backup::{LatestBackupInfoResponse, UploadLogRequest}, +use comm_lib::backup::LatestBackupInfoResponse; +use commtest::backup::backup_utils::{ + assert_reqwest_error, create_user_identity, generate_backup_data_with_logs, }; use commtest::identity::device::register_user_device; -use commtest::{ - service_addr, - tools::{generate_stable_nbytes, Error}, -}; +use commtest::{service_addr, tools::Error}; use grpc_clients::identity::DeviceType; use reqwest::StatusCode; use std::collections::HashSet; -use uuid::Uuid; #[tokio::test] async fn backup_integration_test() -> Result<(), Error> { let backup_client = BackupClient::new(service_addr::BACKUP_SERVICE_HTTP)?; let device_info = register_user_device(None, Some(DeviceType::Ios)).await; + let user_identity = create_user_identity(device_info.clone()); - let user_identity = UserIdentity { - user_id: device_info.user_id.clone(), - access_token: device_info.access_token, - device_id: device_info.device_id, - }; - - let backup_datas = generate_backup_data(); + let backup_datas = generate_backup_data_with_logs(vec![b'a', b'b']); // Upload backups for (backup_data, log_datas) in &backup_datas { @@ -84,17 +73,7 @@ .download_backup_data(&latest_backup_descriptor, RequestedData::BackupInfo) .await; - match nonexistent_user_response { - Ok(_) => panic!("Expected error, but got success response"), - Err(BackupClientError::ReqwestError(error)) => { - assert_eq!( - error.status(), - Some(StatusCode::BAD_REQUEST), - "Expected bad request status" - ); - } - Err(_) => panic!("Unexpected error type"), - } + assert_reqwest_error(nonexistent_user_response, StatusCode::BAD_REQUEST); // Test latest backup lookup let latest_backup_descriptor = BackupDescriptor::Latest { @@ -141,15 +120,7 @@ .download_backup_data(&removed_backup_descriptor, RequestedData::UserKeys) .await; - let Err(BackupClientError::ReqwestError(error)) = response else { - panic!("First backup should have been removed, instead got response: {response:?}"); - }; - - assert_eq!( - error.status(), - Some(StatusCode::NOT_FOUND), - "Expected status 'not found'" - ); + assert_reqwest_error(response, StatusCode::NOT_FOUND); // Test log cleanup let log_stream = backup_client @@ -167,70 +138,3 @@ Ok(()) } - -fn generate_backup_data() -> [(BackupData, Vec); 2] { - [ - ( - BackupData { - backup_id: "b1".to_string(), - user_keys: generate_stable_nbytes( - ByteSize::kib(4).as_u64() as usize, - Some(b'a'), - ), - user_data: generate_stable_nbytes( - ByteSize::mib(4).as_u64() as usize, - Some(b'A'), - ), - attachments: vec![], - siwe_backup_msg: None, - }, - generate_log_data("b1", b'a'), - ), - ( - BackupData { - backup_id: "b2".to_string(), - user_keys: generate_stable_nbytes( - ByteSize::kib(4).as_u64() as usize, - Some(b'b'), - ), - user_data: generate_stable_nbytes( - ByteSize::mib(4).as_u64() as usize, - Some(b'B'), - ), - attachments: vec![], - siwe_backup_msg: None, - }, - generate_log_data("b2", b'b'), - ), - ] -} - -fn generate_log_data(backup_id: &str, value: u8) -> Vec { - const IN_DB_SIZE: usize = ByteSize::kib(4).as_u64() as usize; - const IN_BLOB_SIZE: usize = ByteSize::kib(400).as_u64() as usize; - - (1..30) - .map(|log_id| { - let size = if log_id % 2 == 0 { - IN_DB_SIZE - } else { - IN_BLOB_SIZE - }; - let attachments = if log_id % 10 == 0 { - Some(vec![Uuid::new_v4().to_string()]) - } else { - None - }; - let mut content = generate_stable_nbytes(size, Some(value)); - let unique_suffix = log_id.to_string(); - content.extend(unique_suffix.as_bytes()); - - UploadLogRequest { - backup_id: backup_id.to_string(), - log_id, - content, - attachments, - } - }) - .collect() -} diff --git a/services/commtest/tests/backup_performance_test.rs b/services/commtest/tests/backup_performance_test.rs --- a/services/commtest/tests/backup_performance_test.rs +++ b/services/commtest/tests/backup_performance_test.rs @@ -1,12 +1,13 @@ -use backup_client::{ - BackupClient, BackupData, BackupDescriptor, RequestedData, +use backup_client::{BackupClient, BackupDescriptor, RequestedData}; + +use comm_lib::backup::LatestBackupInfoResponse; +use commtest::backup::backup_utils::{ + create_user_identity, generate_backup_data, }; -use bytesize::ByteSize; -use comm_lib::{auth::UserIdentity, backup::LatestBackupInfoResponse}; use commtest::identity::device::register_user_device; use commtest::{ service_addr, - tools::{generate_stable_nbytes, obtain_number_of_threads, Error}, + tools::{obtain_number_of_threads, Error}, }; use grpc_clients::identity::DeviceType; use tokio::{runtime::Runtime, task::JoinSet}; @@ -24,37 +25,16 @@ number_of_threads ); - let mut backup_data = vec![]; - for i in 0..number_of_threads { - backup_data.push(BackupData { - backup_id: format!("b{i}"), - user_keys: generate_stable_nbytes( - ByteSize::kib(4).as_u64() as usize, - Some(i as u8), - ), - user_data: generate_stable_nbytes( - ByteSize::mib(4).as_u64() as usize, - Some(i as u8), - ), - attachments: vec![], - siwe_backup_msg: None, - }); - } + let backup_data: Vec<_> = (0..number_of_threads) + .map(|i| generate_backup_data(i as u8)) + .collect(); let device_info_1 = register_user_device(None, Some(DeviceType::Ios)).await; let device_info_2 = register_user_device(None, Some(DeviceType::Ios)).await; let user_identities = [ - UserIdentity { - user_id: device_info_1.user_id.clone(), - access_token: device_info_1.access_token, - device_id: device_info_1.device_id, - }, - UserIdentity { - user_id: device_info_2.user_id.clone(), - access_token: device_info_2.access_token, - device_id: device_info_2.device_id, - }, + create_user_identity(device_info_1.clone()), + create_user_identity(device_info_2.clone()), ]; tokio::task::spawn_blocking(move || {