Skip to content

Commit

Permalink
- improve: added test to check deno permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
agallardol committed Nov 15, 2024
1 parent 529531e commit ec1908b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
6 changes: 4 additions & 2 deletions libs/shinkai-tools-runner/src/tools/deno_runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::tools::deno_execution_storage::DenoExecutionStorage;

use super::deno_runner_options::DenoRunnerOptions;
use super::{container_utils::DockerStatus, deno_runner_options::DenoRunnerOptions};
use std::collections::HashMap;

#[derive(Default)]
Expand Down Expand Up @@ -49,7 +49,9 @@ impl DenoRunner {
) -> anyhow::Result<String> {
let force_deno_in_host =
std::env::var("CI_FORCE_DENO_IN_HOST").unwrap_or(String::from("false")) == *"true";
if !force_deno_in_host && super::container_utils::is_docker_available() {
if !force_deno_in_host
&& super::container_utils::is_docker_available() == DockerStatus::Running
{
self.run_in_docker(code, envs, max_execution_time_s).await
} else {
self.run_in_host(code, envs, max_execution_time_s).await
Expand Down
42 changes: 37 additions & 5 deletions libs/shinkai-tools-runner/src/tools/deno_runner.test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ async fn test_run_echo_tool() {
console.log('{"message":"hello world"}');
"#;

let result = deno_runner.run(code, None, None).await.map_err(|e| {
log::error!("Failed to run deno code: {}", e);
e
}).unwrap();
let result = deno_runner
.run(code, None, None)
.await
.map_err(|e| {
log::error!("Failed to run deno code: {}", e);
e
})
.unwrap();

assert_eq!(result, "{\"message\":\"hello world\"}");
}

#[tokio::test]
async fn test_run_with_env() {

let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
Expand All @@ -41,3 +44,32 @@ async fn test_run_with_env() {

assert_eq!(result, "hello world!");
}

#[tokio::test]
async fn test_write_forbidden_folder() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init();

std::env::set_var("CI_FORCE_DENO_IN_HOST", "true");

let mut deno_runner = DenoRunner::default();
let code = r#"
try {
await Deno.writeTextFile("/test.txt", "This should fail");
console.log('write succeeded');
} catch (e) {
// We expect this to fail due to permissions
console.log('error', e);
throw e;
}
"#;

let result = deno_runner.run(code, None, None).await.map_err(|e| {
log::error!("Failed to run deno code: {}", e);
e
});

assert!(result.is_err());
}

0 comments on commit ec1908b

Please sign in to comment.