From a728af68eafb025a890e753ca71745235f2aa335 Mon Sep 17 00:00:00 2001 From: WoodenMaiden Date: Tue, 21 Nov 2023 18:20:05 +0100 Subject: [PATCH] feat: add workspace_path opt so tests runs w/ tmpfile Signed-off-by: WoodenMaiden --- agent/lib/src/api/server.rs | 5 ++++- agent/lib/src/config.rs | 7 +++++++ agent/lib/src/runner_engine/service.rs | 26 ++++++-------------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/agent/lib/src/api/server.rs b/agent/lib/src/api/server.rs index 7b2c6f8..d7380e5 100644 --- a/agent/lib/src/api/server.rs +++ b/agent/lib/src/api/server.rs @@ -84,7 +84,10 @@ impl LambdoAgentService for LambdoAgentServer { let request = request.into_inner(); debug!("Received request: {:?}", request); - let mut runner_engine = runner_engine::service::RunnerEngine::new(request); + let mut runner_engine = runner_engine::service::RunnerEngine::new( + request, + &self.config.workspace_path + ); let mut self_client = self.client.lock().await; if let Err(e) = runner_engine.create_workspace() { diff --git a/agent/lib/src/config.rs b/agent/lib/src/config.rs index f3054dc..c92a4a1 100644 --- a/agent/lib/src/config.rs +++ b/agent/lib/src/config.rs @@ -15,6 +15,10 @@ const fn default_local_port() -> u16 { 0 } +fn default_workspace_path() -> String { + std::env::temp_dir().to_str().unwrap().to_string() +} + #[derive(Error, Debug)] pub enum AgentConfigError { #[error("cannot load config file")] @@ -37,6 +41,9 @@ pub struct AgentConfig { /// The gRPC configuration #[serde(default = "default_grpc")] pub grpc: GRPCConfig, + /// The workspace where the agent will store the files of Requests and their resulting files + #[serde(default = "default_workspace_path")] + pub workspace_path: String, } #[derive(Serialize, Deserialize, PartialEq, Debug)] diff --git a/agent/lib/src/runner_engine/service.rs b/agent/lib/src/runner_engine/service.rs index 6592fe7..065ef3e 100644 --- a/agent/lib/src/runner_engine/service.rs +++ b/agent/lib/src/runner_engine/service.rs @@ -25,28 +25,12 @@ impl RunnerEngine { /// # Arguments /// /// * `request_message` - The request message - /// - /// # Returns - /// - /// * `Self` - The new instance of RunnerEngine - pub fn new(request_message: ExecuteRequest) -> Self { - Self { - request_message, - root_path: std::env::temp_dir(), - } - } - - /// Create a new instance of RunnerEngine that uses another root path than /tmp - /// - /// # Arguments - /// - /// * `request_message` - The request message /// * `root_path` - The root path of the workspace /// /// # Returns /// /// * `Self` - The new instance of RunnerEngine - pub fn new_with_path(request_message: ExecuteRequest, root_path: &str) -> Self { + pub fn new(request_message: ExecuteRequest, root_path: &str) -> Self { Self { request_message, root_path: PathBuf::from(root_path), @@ -217,13 +201,15 @@ mod tests { use std::fs::File; use std::io::Read; + const DEFAULT_WORKSPACE_PATH: &str = "/tmp"; + #[test] fn run_one_works_with_ouputs_and_code() { let res = RunnerEngine::new(ExecuteRequest { id: "".to_string(), files: vec![], steps: vec![], - }) + }, DEFAULT_WORKSPACE_PATH) .run_one("echo -n 'This is stdout' && echo -n 'This is stderr' >&2 && exit 1"); assert!(res.is_ok()); @@ -256,7 +242,7 @@ mod tests { steps, }; - let mut api = RunnerEngine::new(request_data); + let mut api = RunnerEngine::new(request_data, DEFAULT_WORKSPACE_PATH); let res = api.run().unwrap(); @@ -291,7 +277,7 @@ mod tests { steps, }; - RunnerEngine::new_with_path(request_data, path.as_os_str().to_str().unwrap()) + RunnerEngine::new(request_data, path.as_os_str().to_str().unwrap()) .create_workspace() .unwrap();