Skip to content

Commit

Permalink
[CLI] Add test for mount_realm_export.
Browse files Browse the repository at this point in the history
  • Loading branch information
AureliaDolo committed Feb 17, 2025
1 parent b67f04b commit b8d7ed2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
8 changes: 5 additions & 3 deletions cli/src/commands/organization/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::Arc;

use libparsec::{
AvailableDevice, ClientConfig, DeviceLabel, DeviceSaveStrategy, HumanHandle,
ParsecOrganizationBootstrapAddr, Password,
ParsecOrganizationBootstrapAddr, Password, SequesterVerifyKeyDer,
};

use crate::utils::*;
Expand Down Expand Up @@ -35,6 +35,7 @@ pub async fn bootstrap_organization_req(
device_label: DeviceLabel,
human_handle: HumanHandle,
password: Password,
sequester_key: Option<SequesterVerifyKeyDer>,
) -> anyhow::Result<AvailableDevice> {
log::trace!(
"Bootstrapping organization (confdir={}, datadir={})",
Expand All @@ -51,8 +52,7 @@ pub async fn bootstrap_organization_req(
DeviceSaveStrategy::Password { password },
human_handle,
device_label,
// TODO: handle sequestered organization
None,
sequester_key,
)
.await?)
}
Expand Down Expand Up @@ -86,6 +86,8 @@ pub async fn main(args: Args) -> anyhow::Result<()> {
device_label,
human_handle,
password,
// TODO handle sequester
None,
)
.await?;

Expand Down
10 changes: 6 additions & 4 deletions cli/src/testenv_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use std::{
};

use libparsec::{
authenticated_cmds::{latest::device_create, latest::user_create},
authenticated_cmds::latest::{device_create, user_create},
AuthenticatedCmds, Bytes, CertificateSignerOwned, ClientConfig, DateTime, DeviceAccessStrategy,
DeviceCertificate, DeviceID, DeviceLabel, DevicePurpose, HumanHandle, LocalDevice,
MaybeRedacted, OrganizationID, ParsecAddr, PrivateKeyAlgorithm, ProxyConfig, SigningKey,
SigningKeyAlgorithm, UserCertificate, UserProfile, PARSEC_BASE_CONFIG_DIR,
PARSEC_BASE_DATA_DIR, PARSEC_BASE_HOME_DIR, PARSEC_SCHEME,
MaybeRedacted, OrganizationID, ParsecAddr, PrivateKeyAlgorithm, ProxyConfig,
SequesterVerifyKeyDer, SigningKey, SigningKeyAlgorithm, UserCertificate, UserProfile,
PARSEC_BASE_CONFIG_DIR, PARSEC_BASE_DATA_DIR, PARSEC_BASE_HOME_DIR, PARSEC_SCHEME,
};

use crate::{
Expand Down Expand Up @@ -39,6 +39,7 @@ pub async fn initialize_test_organization(
client_config: ClientConfig,
addr: ParsecAddr,
organization_id: OrganizationID,
sequester_key: Option<SequesterVerifyKeyDer>,
) -> anyhow::Result<TestOrganization> {
// Create organization
let organization_addr =
Expand All @@ -51,6 +52,7 @@ pub async fn initialize_test_organization(
"laptop".parse().expect("Unreachable"),
HumanHandle::new("[email protected]", "Alice").expect("Unreachable"),
DEFAULT_DEVICE_PASSWORD.to_string().into(),
sequester_key,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions cli/tests/integration/device_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ async fn multiple_device_found(tmp_path: TmpPath) {
crate::testenv_utils::DEFAULT_DEVICE_PASSWORD
.to_string()
.into(),
None,
)
.await
.unwrap();
Expand Down
33 changes: 28 additions & 5 deletions cli/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod device;
mod device_option;
mod invitations;
mod ls;
mod mount_realm_export;
mod organization;
mod rm;
mod server;
Expand All @@ -17,11 +18,11 @@ use std::{
};

use crate::utils::*;
use libparsec::LocalDevice;
use libparsec::{
ClientConfig, OrganizationID, ParsecAddr, TmpPath, PARSEC_BASE_CONFIG_DIR,
PARSEC_BASE_DATA_DIR, PARSEC_BASE_HOME_DIR,
};
use libparsec::{LocalDevice, SequesterVerifyKeyDer};
use std::sync::Arc;

use crate::testenv_utils::DEFAULT_DEVICE_PASSWORD;
Expand Down Expand Up @@ -71,16 +72,22 @@ async fn run_local_organization(
tmp_dir: &Path,
source_file: Option<PathBuf>,
config: TestenvConfig,
sequester_key: Option<SequesterVerifyKeyDer>,
) -> anyhow::Result<(ParsecAddr, TestOrganization, OrganizationID)> {
let url = new_environment(tmp_dir, source_file, config, false)
.await?
.unwrap();

println!("Initializing test organization to {url}");
let org_id = unique_org_id();
initialize_test_organization(ClientConfig::default(), url.clone(), org_id.clone())
.await
.map(|v| (url, v, org_id))
initialize_test_organization(
ClientConfig::default(),
url.clone(),
org_id.clone(),
sequester_key,
)
.await
.map(|v| (url, v, org_id))
}

fn wait_for(mut reader: impl BufRead, buf: &mut String, text: &str) {
Expand All @@ -99,7 +106,22 @@ async fn bootstrap_cli_test(
let _ = env_logger::builder().is_test(true).try_init();
let tmp_path_str = tmp_path.to_str().unwrap();
let config = get_testenv_config();
let (url, devices, org_id) = run_local_organization(tmp_path, None, config).await?;
let (url, devices, org_id) = run_local_organization(tmp_path, None, config, None).await?;

set_env(tmp_path_str, &url);
Ok((url, devices, org_id))
}

// Same as above, but with sequester
async fn bootstrap_cli_test_with_sequester(
tmp_path: &TmpPath,
sequester_key: SequesterVerifyKeyDer,
) -> anyhow::Result<(ParsecAddr, TestOrganization, OrganizationID)> {
let _ = env_logger::builder().is_test(true).try_init();
let tmp_path_str = tmp_path.to_str().unwrap();
let config = get_testenv_config();
let (url, devices, org_id) =
run_local_organization(tmp_path, None, config, Some(sequester_key)).await?;

set_env(tmp_path_str, &url);
Ok((url, devices, org_id))
Expand Down Expand Up @@ -147,6 +169,7 @@ macro_rules! assert_cmd_failure {
}

/// Creates a std `Command` to be run with the `parsec-cli` binary.
/// No comma after the last argument
#[macro_export]
macro_rules! std_cmd {
($($args:expr),*) => {
Expand Down
69 changes: 69 additions & 0 deletions cli/tests/integration/mount_realm_export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{env::current_dir, io::Read};

use libparsec::{tmp_path, SequesterKeySize, SequesterSigningKeyDer, TmpPath};

use crate::{
integration_tests::{bootstrap_cli_test_with_sequester, spawn_interactive_command},
std_cmd,
testenv_utils::TestOrganization,
utils::start_client,
};

#[rstest::rstest]
#[tokio::test]
async fn mount_realm_export(tmp_path: TmpPath) {
let (seq_sign_key, seq_verification_key) =
SequesterSigningKeyDer::generate_pair(SequesterKeySize::_1024Bits);

let (_, TestOrganization { alice, .. }, org_id) =
bootstrap_cli_test_with_sequester(&tmp_path, seq_verification_key)
.await
.unwrap();
let client = start_client(alice.clone()).await.unwrap();

// Create the workspace used to copy the file to
let wid = client
.create_workspace("new-workspace".parse().unwrap())
.await
.unwrap();
client.ensure_workspaces_bootstrapped().await.unwrap();

// TODO update when realm export available from here

let mut parsec_dir = current_dir().unwrap();
assert!(parsec_dir.pop()); // got to parsec-cloud
let parsec_dir = dbg!(parsec_dir.join("server"));
let output = std::process::Command::new("poetry")
.current_dir(parsec_dir)
.arg("run")
.arg("parsec")
.arg("export_realm")
.arg(format!("--organization={org_id}"))
.arg(format!("--realm={wid}"))
.arg("--db=MOCKED")
.arg("--blockstore=MOCKED")
.spawn()
.unwrap();

let mut buf = String::new();
dbg!(output)
.stdout
.unwrap()
.read_to_string(&mut buf)
.unwrap();
let db_export = buf.lines().next().unwrap();
assert_eq!("Creating ", &db_export[0..9]);
let db_export_path = dbg!(&db_export[9..]);

let cmd = std_cmd!(
"mount-realm-export",
db_export_path,
&format!("{seq_sign_key:?}"),
"mounted_workspace"
);

let mut p = spawn_interactive_command(cmd, Some(1500)).unwrap();

p.exp_string("Mounted workspace at mounted_workspace")
.unwrap();
}

0 comments on commit b8d7ed2

Please sign in to comment.