mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-19 17:04:48 +00:00
Isolate v1 API test env setup
This commit is contained in:
parent
4483f7c40d
commit
28d4bc41d3
4 changed files with 43 additions and 54 deletions
|
|
@ -56,6 +56,7 @@ pub struct TestApp {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct TestAppOptions {
|
pub struct TestAppOptions {
|
||||||
pub env: BTreeMap<String, String>,
|
pub env: BTreeMap<String, String>,
|
||||||
|
pub extra_paths: Vec<PathBuf>,
|
||||||
pub replace_path: bool,
|
pub replace_path: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -266,6 +267,13 @@ fn build_env(
|
||||||
let mut custom_path_entries =
|
let mut custom_path_entries =
|
||||||
custom_path_entries(layout.install_dir.parent().expect("install base"));
|
custom_path_entries(layout.install_dir.parent().expect("install base"));
|
||||||
custom_path_entries.extend(explicit_path_entries());
|
custom_path_entries.extend(explicit_path_entries());
|
||||||
|
custom_path_entries.extend(
|
||||||
|
options
|
||||||
|
.extra_paths
|
||||||
|
.iter()
|
||||||
|
.filter(|path| path.is_absolute() && path.exists())
|
||||||
|
.cloned(),
|
||||||
|
);
|
||||||
custom_path_entries.sort();
|
custom_path_entries.sort();
|
||||||
custom_path_entries.dedup();
|
custom_path_entries.dedup();
|
||||||
|
|
||||||
|
|
@ -285,7 +293,7 @@ fn build_env(
|
||||||
if key == "PATH" {
|
if key == "PATH" {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
env.insert(key.clone(), value.clone());
|
env.insert(key.clone(), rewrite_localhost_url(key, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
env
|
env
|
||||||
|
|
|
||||||
|
|
@ -15,35 +15,6 @@ use serial_test::serial;
|
||||||
mod docker_support;
|
mod docker_support;
|
||||||
use docker_support::{LiveServer, TestApp};
|
use docker_support::{LiveServer, TestApp};
|
||||||
|
|
||||||
struct EnvVarGuard {
|
|
||||||
key: &'static str,
|
|
||||||
previous: Option<std::ffi::OsString>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EnvVarGuard {
|
|
||||||
fn set(key: &'static str, value: &str) -> Self {
|
|
||||||
let previous = std::env::var_os(key);
|
|
||||||
std::env::set_var(key, value);
|
|
||||||
Self { key, previous }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_os(key: &'static str, value: &std::ffi::OsStr) -> Self {
|
|
||||||
let previous = std::env::var_os(key);
|
|
||||||
std::env::set_var(key, value);
|
|
||||||
Self { key, previous }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for EnvVarGuard {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
if let Some(previous) = self.previous.as_ref() {
|
|
||||||
std::env::set_var(self.key, previous);
|
|
||||||
} else {
|
|
||||||
std::env::remove_var(self.key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write_executable(path: &Path, script: &str) {
|
fn write_executable(path: &Path, script: &str) {
|
||||||
fs::write(path, script).expect("write executable");
|
fs::write(path, script).expect("write executable");
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
|
@ -92,12 +63,10 @@ fn serve_registry_once(document: Value) -> String {
|
||||||
let address = listener.local_addr().expect("registry address");
|
let address = listener.local_addr().expect("registry address");
|
||||||
let body = document.to_string();
|
let body = document.to_string();
|
||||||
|
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || loop {
|
||||||
loop {
|
match listener.accept() {
|
||||||
match listener.accept() {
|
Ok((mut stream, _)) => respond_json(&mut stream, &body),
|
||||||
Ok((mut stream, _)) => respond_json(&mut stream, &body),
|
Err(_) => break,
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn v1_health_removed_legacy_and_opencode_unmounted() {
|
async fn v1_health_removed_legacy_and_opencode_unmounted() {
|
||||||
|
|
@ -137,10 +138,19 @@ async fn v1_filesystem_endpoints_round_trip() {
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
#[serial]
|
#[serial]
|
||||||
async fn require_preinstall_blocks_missing_agent() {
|
async fn require_preinstall_blocks_missing_agent() {
|
||||||
let test_app = {
|
let mut env = BTreeMap::new();
|
||||||
let _preinstall = EnvVarGuard::set("SANDBOX_AGENT_REQUIRE_PREINSTALL", "true");
|
env.insert(
|
||||||
TestApp::new(AuthConfig::disabled())
|
"SANDBOX_AGENT_REQUIRE_PREINSTALL".to_string(),
|
||||||
};
|
"true".to_string(),
|
||||||
|
);
|
||||||
|
let test_app = TestApp::with_options(
|
||||||
|
AuthConfig::disabled(),
|
||||||
|
docker_support::TestAppOptions {
|
||||||
|
env,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
|_| {},
|
||||||
|
);
|
||||||
|
|
||||||
let (status, _, body) = send_request(
|
let (status, _, body) = send_request(
|
||||||
&test_app.app,
|
&test_app.app,
|
||||||
|
|
@ -176,25 +186,26 @@ async fn lazy_install_runs_on_first_bootstrap() {
|
||||||
]
|
]
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let _registry = EnvVarGuard::set("SANDBOX_AGENT_ACP_REGISTRY_URL", ®istry_url);
|
|
||||||
let helper_bin_root = tempfile::tempdir().expect("helper bin tempdir");
|
let helper_bin_root = tempfile::tempdir().expect("helper bin tempdir");
|
||||||
let helper_bin = helper_bin_root.path().join("bin");
|
let helper_bin = helper_bin_root.path().join("bin");
|
||||||
fs::create_dir_all(&helper_bin).expect("create helper bin dir");
|
fs::create_dir_all(&helper_bin).expect("create helper bin dir");
|
||||||
write_fake_npm(&helper_bin.join("npm"));
|
write_fake_npm(&helper_bin.join("npm"));
|
||||||
|
|
||||||
let original_path = std::env::var_os("PATH").unwrap_or_default();
|
let mut env = BTreeMap::new();
|
||||||
let mut paths = vec![helper_bin.clone()];
|
env.insert("SANDBOX_AGENT_ACP_REGISTRY_URL".to_string(), registry_url);
|
||||||
paths.extend(std::env::split_paths(&original_path));
|
let test_app = TestApp::with_options(
|
||||||
let merged_path = std::env::join_paths(paths).expect("join PATH");
|
AuthConfig::disabled(),
|
||||||
let _path_guard = EnvVarGuard::set_os("PATH", merged_path.as_os_str());
|
docker_support::TestAppOptions {
|
||||||
let _extra_paths_guard =
|
env,
|
||||||
EnvVarGuard::set_os("SANDBOX_AGENT_TEST_EXTRA_PATHS", helper_bin.as_os_str());
|
extra_paths: vec![helper_bin.clone()],
|
||||||
|
..Default::default()
|
||||||
let test_app = TestApp::with_setup(AuthConfig::disabled(), |install_path| {
|
},
|
||||||
fs::create_dir_all(install_path.join("agent_processes"))
|
|install_path| {
|
||||||
.expect("create agent processes dir");
|
fs::create_dir_all(install_path.join("agent_processes"))
|
||||||
write_executable(&install_path.join("codex"), "#!/usr/bin/env sh\nexit 0\n");
|
.expect("create agent processes dir");
|
||||||
});
|
write_executable(&install_path.join("codex"), "#!/usr/bin/env sh\nexit 0\n");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let (status, _, _) = send_request(
|
let (status, _, _) = send_request(
|
||||||
&test_app.app,
|
&test_app.app,
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ async fn v1_desktop_status_reports_install_required_when_dependencies_are_missin
|
||||||
docker_support::TestAppOptions {
|
docker_support::TestAppOptions {
|
||||||
env,
|
env,
|
||||||
replace_path: true,
|
replace_path: true,
|
||||||
|
..Default::default()
|
||||||
},
|
},
|
||||||
|_| {},
|
|_| {},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue