From a47a1dcfb338ea3592b8072807d011f697494838 Mon Sep 17 00:00:00 2001 From: Renan Date: Wed, 21 Jun 2023 20:29:32 -0300 Subject: [PATCH] fast --- offchain/test-fixtures/src/docker_cli.rs | 7 +++- offchain/test-fixtures/src/local_stack.rs | 47 +++++++++++++++-------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/offchain/test-fixtures/src/docker_cli.rs b/offchain/test-fixtures/src/docker_cli.rs index dbcaeb37..e69ef201 100644 --- a/offchain/test-fixtures/src/docker_cli.rs +++ b/offchain/test-fixtures/src/docker_cli.rs @@ -44,8 +44,11 @@ pub fn rm(id: &str) { docker_run(&["rm", "-v", id]); } -pub fn run(image: &str) -> String { - let id = docker_run(&["run", "-d", "-v", "awsout:/app", image]); +pub fn run(image: &str, args: &[&str]) -> String { + let mut cmd = vec!["run", "-d"]; + cmd.extend(args); + cmd.push(image); + let id = docker_run(&cmd); let id = std::str::from_utf8(&id).unwrap().trim(); id.to_string() } diff --git a/offchain/test-fixtures/src/local_stack.rs b/offchain/test-fixtures/src/local_stack.rs index f43ba71c..a3aa0a37 100644 --- a/offchain/test-fixtures/src/local_stack.rs +++ b/offchain/test-fixtures/src/local_stack.rs @@ -19,16 +19,16 @@ use crate::docker_cli; const TAG: &str = "cartesi/localstack"; const DOCKERFILE: &str = "../test-fixtures/docker/local_stack.Dockerfile"; +const FROM_CONTAINER: &str = "/app/key_id.txt"; pub struct LocalStackFixture { container_id: String, - dir: tempfile::TempDir, } impl Drop for LocalStackFixture { fn drop(&mut self) { - //docker_cli::stop(&self.container_id); - //docker_cli::rm(&self.container_id); + docker_cli::stop(&self.container_id); + docker_cli::rm(&self.container_id); } } @@ -43,34 +43,49 @@ impl LocalStackFixture { docker_cli::build(DOCKERFILE, TAG, &[]); tracing::debug!("finished building the docker image"); - let container_id = docker_cli::run("cartesi/localstack"); + let container_id = docker_cli::run( + "cartesi/localstack", + &[ + "-p", + "4566:4566", + "-p", + "4510-4559:4510-4559", + "-v", + "awsout:/app", + ], + ); tracing::debug!("running container {}", container_id); - let dir = tempfile::tempdir().expect("failed to create temp dir"); - - Self { container_id, dir } + Self { container_id } } pub fn endpoint(&self) -> String { - "todo".to_string() + "http://0.0.0.0:4566".to_string() } pub fn create_key(&self) -> String { tracing::debug!("creating key"); - let aws_cmd = "awslocal kms create-key \ + let aws_cmd = format!( + "awslocal kms create-key \ --key-spec ECC_SECG_P256K1 \ --key-usage SIGN_VERIFY \ - > /app/key_id.txt 2>&1"; - let docker_cmd = ["sh", "-c", aws_cmd]; + > {} 2>&1", + FROM_CONTAINER + ); + let docker_cmd = ["sh", "-c", &aws_cmd]; println!("create-key cmd: {:?}", docker_cmd); docker_cli::exec(&self.container_id, &docker_cmd); tracing::trace!("created key"); - // TODO: user docker_cli::cp + let from_container = + format!("{}:{}", self.container_id, FROM_CONTAINER); + let temp_dir = tempfile::tempdir().expect("failed to create temp dir"); + let to_host = temp_dir.path().join("key_id.txt"); + let to_host = to_host.to_str().unwrap(); + docker_cli::cp(&from_container, to_host); - /* - let json_string = - std::fs::read_to_string(path).expect("failed to read volume"); + let json_string = std::fs::read_to_string(to_host) + .expect("failed to read volume data"); tracing::trace!("JSON STRING: {:?}", json_string); let key_id = json::parse(&json_string).unwrap()["KeyMetadata"]["KeyId"] @@ -78,8 +93,6 @@ impl LocalStackFixture { tracing::trace!("created key {}", key_id); key_id - */ - "todo".into() } /*