feat(clients): sandbox: Agent Sandbox lifecycle, code/shell/computer-use execution, templates, snapshots

Typed results with hard caps, one call table checked against Discovery
(tools/check_discovery.py), live read-only probe, /agent/sandbox demo.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 04:36:39 +02:00
co-authored by Claude Sonnet 5.5
parent 6d744a433f
commit af6ee63211
35 changed files with 3659 additions and 3 deletions
@@ -0,0 +1,560 @@
// Sandbox module: deterministic local checks only. No scripted Google
// replies: validation, caps, ids, request bodies against the declared call
// table (which tools/check_discovery.py proves against Discovery), codec
// round trips, and one request-shape probe over a transport that always fails.
#include <cstdlib>
#include <functional>
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include "framework.h"
#include "clients/transports/mock_transport.h"
#include "google/client.h"
#include "google/credentials.h"
#include "google/resource.h"
#include "google/sandbox/calls.h"
#include "google/sandbox/caps.h"
#include "google/sandbox/chunk.h"
#include "google/sandbox/code_executor.h"
#include "google/sandbox/computer_use.h"
#include "google/sandbox/names.h"
#include "google/sandbox/sandboxes.h"
#include "google/sandbox/shell_runner.h"
#include "google/sandbox/snapshots.h"
#include "google/sandbox/stream_executor.h"
#include "google/sandbox/templates.h"
#include "google/sandbox/workspace_files.h"
using namespace molo::google;
using namespace molo::google::sandbox;
using molo::clients::Method;
using molo::clients::body::Json;
using molo::clients::transports::MockTransport;
namespace {
const std::string kEngine = "projects/p-1/locations/us-central1/reasoningEngines/123";
const std::string kSandbox = kEngine + "/sandboxEnvironments/456";
const std::string kTemplate = kEngine + "/sandboxEnvironmentTemplates/7";
const std::string kSnapshot = kEngine + "/sandboxEnvironmentSnapshots/8";
// A configured client over a transport with no expectations: any request that
// leaves the module would throw, and send_count() proves none did.
struct Rig {
MockTransport* mock;
std::shared_ptr<GoogleClient> client;
Rig() {
auto t = std::make_unique<MockTransport>();
mock = t.get();
client = std::make_shared<GoogleClient>(std::move(t), Credentials::bearer("tok"));
}
};
#define ASSERT_INVALID(stmt) \
do { \
bool caught = false; \
try { \
stmt; \
} catch (const GoogleError& e) { \
caught = e.http_status() == 400 && e.status() == "INVALID_ARGUMENT"; \
} \
ASSERT_TRUE(caught); \
} while (0)
const DeclaredCall& call(const std::string& name) {
for (const DeclaredCall& c : kDeclaredCalls)
if (name == c.name) return c;
throw std::runtime_error("no declared call " + name);
}
// Every top-level key of `body` must be a declared request field of `name`.
void expect_keys_declared(const Json& body, const std::string& name) {
std::set<std::string> allowed;
std::string fields = call(name).request;
std::size_t start = 0;
while (start <= fields.size()) {
std::size_t end = fields.find(',', start);
if (end == std::string::npos) end = fields.size();
std::string f = fields.substr(start, end - start);
allowed.insert(f.substr(0, f.find('.')));
start = end + 1;
}
for (const auto& [k, v] : body.as_object()) {
(void)v;
if (!allowed.count(k)) FAIL("body key " + k + " is not a declared field of " + name);
}
}
} // namespace
TEST(sandbox_names, resource_names) {
ASSERT_TRUE(SandboxNames::valid_engine(kEngine));
ASSERT_TRUE(SandboxNames::valid_sandbox(kSandbox));
ASSERT_TRUE(SandboxNames::valid_template(kTemplate));
ASSERT_TRUE(SandboxNames::valid_snapshot(kSnapshot));
ASSERT_FALSE(SandboxNames::valid_sandbox(kEngine));
ASSERT_FALSE(SandboxNames::valid_sandbox(kTemplate));
ASSERT_FALSE(SandboxNames::valid_engine(kEngine + "/"));
ASSERT_FALSE(SandboxNames::valid_engine("projects/p/locations/us/reasoningEngines/../x"));
ASSERT_FALSE(SandboxNames::valid_engine("projects/P/locations/us/reasoningEngines/1"));
ASSERT_FALSE(SandboxNames::valid_engine("projects/p/locations/us/reasoningEngines/1?x=1"));
ASSERT_FALSE(SandboxNames::valid_sandbox(kSandbox + "/operations/1"));
ASSERT_FALSE(SandboxNames::valid_engine(std::string(600, 'a')));
ASSERT_EQ(SandboxNames::engine_of(kSandbox), kEngine);
ASSERT_EQ(SandboxNames::parse_sandbox(kSandbox).sandbox, std::string("456"));
ASSERT_INVALID(SandboxNames::parse_engine("nope"));
}
TEST(sandbox_names, file_names_and_paths) {
ASSERT_TRUE(valid_file_name("a.txt"));
ASSERT_TRUE(valid_file_name("dir/a.txt"));
ASSERT_FALSE(valid_file_name(""));
ASSERT_FALSE(valid_file_name("/etc/passwd"));
ASSERT_FALSE(valid_file_name("../x"));
ASSERT_FALSE(valid_file_name("a/../x"));
ASSERT_FALSE(valid_file_name("a\\b"));
ASSERT_FALSE(valid_file_name(std::string("a\nb")));
ASSERT_FALSE(valid_file_name(std::string(256, 'a')));
ASSERT_TRUE(valid_workspace_path("/workspace/app"));
ASSERT_TRUE(valid_workspace_path("app/x.py"));
ASSERT_FALSE(valid_workspace_path("a b"));
ASSERT_FALSE(valid_workspace_path("a;rm"));
ASSERT_FALSE(valid_workspace_path("a/../b"));
ASSERT_FALSE(valid_workspace_path("a'b"));
}
TEST(sandbox_caps, code_and_files) {
std::vector<InputFile> ok = {{"a.txt", "x"}};
CodeExecutor::validate("print(1)", ok);
ASSERT_INVALID(CodeExecutor::validate("", {}));
ASSERT_INVALID(CodeExecutor::validate(std::string(SandboxCaps::kMaxCodeBytes + 1, 'a'), {}));
ASSERT_INVALID(CodeExecutor::validate("x", {{"../a", "x"}}));
ASSERT_INVALID(CodeExecutor::validate("x", {{"a", "1"}, {"a", "2"}}));
ASSERT_INVALID(CodeExecutor::validate(
"x", {{"a", std::string(SandboxCaps::kMaxFileBytes + 1, 'a')}}));
std::vector<InputFile> many;
for (std::size_t i = 0; i <= SandboxCaps::kMaxInputFiles; ++i)
many.push_back({"f" + std::to_string(i), "x"});
ASSERT_INVALID(CodeExecutor::validate("x", many));
std::vector<InputFile> big; // Each under the file cap, total over the cap.
for (int i = 0; i < 3; ++i)
big.push_back({"b" + std::to_string(i), std::string(SandboxCaps::kMaxFileBytes, 'a')});
ASSERT_INVALID(CodeExecutor::validate("x", big));
}
TEST(sandbox_caps, shell) {
ShellRequest r;
r.command = "echo hi";
ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes);
r.command = "";
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
r.command = std::string(SandboxCaps::kMaxCommandBytes + 1, 'a');
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
ShellRunner::validate(r, SandboxCaps::kMaxInternalCommandBytes);
r.command = std::string("a\0b", 3);
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
r.command = "ls";
r.timeout_seconds = 0;
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
r.timeout_seconds = SandboxCaps::kMaxShellTimeoutSeconds + 1;
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
r.timeout_seconds = 5;
r.cwd = "/workspace/a b";
ASSERT_INVALID(ShellRunner::validate(r, SandboxCaps::kMaxCommandBytes));
}
TEST(sandbox_caps, lifecycle_requests) {
CreateSandboxRequest r;
r.display_name = "demo";
Sandboxes::validate(r);
r.display_name = "";
ASSERT_INVALID(Sandboxes::validate(r));
r.display_name = std::string(129, 'a');
ASSERT_INVALID(Sandboxes::validate(r));
r.display_name = "demo";
r.ttl_seconds = SandboxCaps::kMinTtlSeconds - 1;
ASSERT_INVALID(Sandboxes::validate(r));
r.ttl_seconds = SandboxCaps::kMaxTtlSeconds + 1;
ASSERT_INVALID(Sandboxes::validate(r));
r.ttl_seconds = 3600;
r.spec.kind = SandboxKind::kShell;
r.spec.language = CodeLanguage::kPython;
ASSERT_INVALID(Sandboxes::validate(r));
r.spec.language = CodeLanguage::kUnspecified;
r.spec.kind = SandboxKind::kComputerUse;
ASSERT_INVALID(Sandboxes::validate(r)); // Needs a template.
r.template_name = kTemplate;
Sandboxes::validate(r);
r.template_name = kSandbox; // Wrong resource type.
ASSERT_INVALID(Sandboxes::validate(r));
SnapshotRequest s;
s.display_name = "snap";
Sandboxes::validate(s);
s.post_action = "STOP";
ASSERT_INVALID(Sandboxes::validate(s));
s.post_action = "PAUSE";
s.ttl_seconds = 5;
ASSERT_INVALID(Sandboxes::validate(s));
}
TEST(sandbox_caps, computer_use_inputs) {
ComputerUse::validate_url("https://example.com/a?b=1");
ASSERT_INVALID(ComputerUse::validate_url("javascript:alert(1)"));
ASSERT_INVALID(ComputerUse::validate_url("file:///etc/passwd"));
ASSERT_INVALID(ComputerUse::validate_url("https://"));
ASSERT_INVALID(ComputerUse::validate_url("https://a b"));
ASSERT_INVALID(ComputerUse::validate_url("https://" + std::string(SandboxCaps::kMaxUrlChars, 'a')));
ComputerUse::validate_point(0, SandboxCaps::kMaxCoordinate);
ASSERT_INVALID(ComputerUse::validate_point(-1, 0));
ASSERT_INVALID(ComputerUse::validate_point(0, SandboxCaps::kMaxCoordinate + 1));
ComputerUse::validate_text("hello");
ASSERT_INVALID(ComputerUse::validate_text(""));
ASSERT_INVALID(ComputerUse::validate_text(std::string(SandboxCaps::kMaxTypedChars + 1, 'a')));
}
TEST(sandbox_caps, template_requests) {
CreateTemplateRequest t;
t.display_name = "shell";
t.category = ContainerCategory::kShellSandbox;
Templates::validate(t);
t.image_uri = "gcr.io/p/img:1"; // Both category and image.
ASSERT_INVALID(Templates::validate(t));
t.category = ContainerCategory::kNone;
Templates::validate(t);
t.image_uri = "gcr.io/p/img;rm";
ASSERT_INVALID(Templates::validate(t));
t.image_uri = "gcr.io/p/img:1";
t.ports = {{"TCP", 0}};
ASSERT_INVALID(Templates::validate(t));
t.ports = {{"ICMP", 80}};
ASSERT_INVALID(Templates::validate(t));
t.ports.assign(17, TemplatePort{"TCP", 80});
ASSERT_INVALID(Templates::validate(t));
t.ports.clear();
t.persistent_disk = true;
t.disk_mount_path = "/data";
t.disk_size_gb = 0;
ASSERT_INVALID(Templates::validate(t));
t.disk_size_gb = 10;
Templates::validate(t);
t.disk_mount_path = "data";
ASSERT_INVALID(Templates::validate(t));
}
TEST(sandbox_caps, workspace_files) {
ASSERT_EQ(WorkspaceFiles::resolve("a/b.txt"), std::string("/workspace/a/b.txt"));
ASSERT_EQ(WorkspaceFiles::resolve("/workspace/x"), std::string("/workspace/x"));
ASSERT_INVALID(WorkspaceFiles::resolve("/etc/passwd"));
ASSERT_INVALID(WorkspaceFiles::resolve("/workspacex/y"));
ASSERT_INVALID(WorkspaceFiles::resolve("../x"));
ASSERT_INVALID(WorkspaceFiles::resolve("a;b"));
// Quoting: only plain path characters ever reach a command line.
std::string cmd = WorkspaceFiles::read_command("/workspace/a.txt");
ASSERT_TRUE(cmd.find("'/workspace/a.txt'") != std::string::npos);
ASSERT_TRUE(WorkspaceFiles::remove_command("/workspace/a").find("rm -f --") != std::string::npos);
ASSERT_TRUE(WorkspaceFiles::remove_command("/workspace/a").find("-r") == std::string::npos);
// The write command carries content as base64 and fits the internal cap.
std::string w = WorkspaceFiles::write_command(
"/workspace/a", std::string(SandboxCaps::kMaxShellWriteBytes, '\''));
ASSERT_TRUE(w.size() <= SandboxCaps::kMaxInternalCommandBytes);
ASSERT_TRUE(w.find("''") == std::string::npos || w.find("base64 -d") != std::string::npos);
}
TEST(sandbox_validation, nothing_leaves_the_module_on_bad_input) {
Rig r;
Sandboxes sandboxes(r.client);
CodeExecutor code(r.client);
ShellRunner shell(r.client);
ComputerUse computer(r.client);
Templates templates(r.client);
Snapshots snapshots(r.client);
StreamExecutor stream(r.client);
WorkspaceFiles files(shell);
CreateSandboxRequest bad;
ASSERT_INVALID(sandboxes.create(kEngine, bad));
ASSERT_INVALID(sandboxes.create("bad", CreateSandboxRequest{"d", {}, 3600, "", "", ""}));
ASSERT_INVALID(sandboxes.get(kEngine));
ASSERT_INVALID(sandboxes.list("x"));
ASSERT_INVALID(sandboxes.list(kEngine, 101));
ASSERT_INVALID(sandboxes.remove("x"));
ASSERT_INVALID(sandboxes.pause("x"));
ASSERT_INVALID(sandboxes.resume("x"));
ASSERT_INVALID(sandboxes.authorize_access("x"));
ASSERT_INVALID(sandboxes.snapshot("x", SnapshotRequest{"s", 0, "", ""}));
ASSERT_INVALID(code.run(kSandbox, ""));
ASSERT_INVALID(code.run("x", "print(1)"));
ASSERT_INVALID(shell.run(kSandbox, ShellRequest{"", "", 5}));
ASSERT_INVALID(computer.navigate(kSandbox, "ftp://x"));
ASSERT_INVALID(computer.click(kSandbox, -5, 0));
ASSERT_INVALID(computer.type_text(kSandbox, ""));
ASSERT_INVALID(templates.get(kSandbox));
ASSERT_INVALID(templates.remove("x"));
ASSERT_INVALID(snapshots.get(kTemplate));
ASSERT_INVALID(snapshots.remove("x"));
ASSERT_INVALID(stream.run(kSandbox, {}));
ASSERT_INVALID(files.write(kSandbox, "/etc/x", "y"));
ASSERT_INVALID(files.write(kSandbox, "a", std::string(SandboxCaps::kMaxShellWriteBytes + 1, 'a')));
ASSERT_INVALID(files.remove_file(kSandbox, "/workspace"));
ASSERT_EQ(r.mock->send_count(), 0u);
}
TEST(sandbox_validation, not_configured_is_loud_before_traffic) {
auto t = std::make_unique<MockTransport>();
MockTransport* mock = t.get();
auto client = std::make_shared<GoogleClient>(std::move(t), nullptr);
CodeExecutor code(client);
bool caught = false;
try {
code.run(kSandbox, "print(1)");
} catch (const NotConfiguredError& e) {
caught = e.http_status() == 503;
}
ASSERT_TRUE(caught);
ASSERT_EQ(mock->send_count(), 0u);
}
TEST(sandbox_bodies, create_matches_declared_fields) {
CreateSandboxRequest r;
r.display_name = "demo";
r.spec.kind = SandboxKind::kCodeExecution;
r.spec.language = CodeLanguage::kPython;
r.spec.machine = MachineConfig::kVcpu4Ram4Gib;
r.ttl_seconds = 3600;
r.template_name = kTemplate;
r.snapshot_name = kSnapshot;
r.owner = "me";
Json b = Sandboxes::create_body(r);
expect_keys_declared(b, "sandbox.create");
ASSERT_EQ(b.find("ttl")->as_string(), std::string("3600s"));
const Json* env = b.find("spec")->find("codeExecutionEnvironment");
ASSERT_TRUE(env != nullptr);
ASSERT_EQ(env->find("codeLanguage")->as_string(), std::string("LANGUAGE_PYTHON"));
ASSERT_EQ(env->find("machineConfig")->as_string(), std::string("MACHINE_CONFIG_VCPU4_RAM4GIB"));
r.spec = SandboxSpec{SandboxKind::kShell};
ASSERT_TRUE(Sandboxes::create_body(r).find("spec")->has("shellEnvironment"));
r.spec = SandboxSpec{SandboxKind::kComputerUse};
Json c = Sandboxes::create_body(r);
ASSERT_TRUE(c.find("spec")->has("computerUseEnvironment"));
expect_keys_declared(c, "sandbox.create.computer");
}
TEST(sandbox_bodies, snapshot_and_template) {
SnapshotRequest s{"snap", 120, "PAUSE", "me"};
Json b = Sandboxes::snapshot_body(kSandbox, s);
expect_keys_declared(b, "sandbox.snapshot");
ASSERT_EQ(b.find("sourceSandboxEnvironment")->as_string(), kSandbox);
ASSERT_EQ(b.find("ttl")->as_string(), std::string("120s"));
CreateTemplateRequest t;
t.display_name = "browser";
t.category = ContainerCategory::kComputerUse;
t.internet_access = true;
Json tb = Templates::create_body(t);
expect_keys_declared(tb, "template.create");
ASSERT_EQ(tb.find("defaultContainerEnvironment")->find("defaultContainerCategory")->as_string(),
std::string("DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE"));
ASSERT_TRUE(tb.find("egressControlConfig")->find("internetAccess")->as_bool());
CreateTemplateRequest c;
c.display_name = "custom";
c.image_uri = "gcr.io/p/i:1";
c.ports = {{"TCP", 8080}};
c.persistent_disk = true;
c.disk_mount_path = "/data";
c.disk_size_gb = 5;
Json cb = Templates::create_body(c);
expect_keys_declared(cb, "template.create");
ASSERT_EQ(cb.find("persistentDiskConfig")->find("sizeGb")->as_string(), std::string("5"));
ASSERT_EQ(cb.find("customContainerEnvironment")->find("ports")->as_array().size(), 1u);
}
TEST(sandbox_bodies, execute_inputs) {
Json req = ChunkCodec::request({ChunkCodec::code_input("print(1)", {{"a.txt", "hi"}})});
expect_keys_declared(req, "sandbox.execute");
const Json& chunk = req.find("inputs")->as_array().front();
ASSERT_EQ(chunk.find("mimeType")->as_string(), std::string("application/json"));
// data is base64 on the wire: it must not be the plain JSON text.
ASSERT_TRUE(chunk.find("data")->as_string().find('{') == std::string::npos);
Chunk back = Chunk::from_json(chunk);
Json payload = Json::parse(back.data);
ASSERT_EQ(payload.find("code")->as_string(), std::string("print(1)"));
ASSERT_EQ(payload.find("files")->as_array().size(), 1u);
Json sh = Json::parse(ChunkCodec::shell_input("ls", "/workspace", 5).data);
ASSERT_EQ(sh.find("command")->as_string(), std::string("ls"));
ASSERT_EQ(sh.find("timeout")->as_int(), 5);
Json cdp = Json::parse(ChunkCodec::cdp_input("Page.navigate", ComputerUse::navigate_params("https://a.b")).data);
ASSERT_EQ(cdp.find("command")->as_string(), std::string("Page.navigate"));
ASSERT_EQ(cdp.find("params")->find("url")->as_string(), std::string("https://a.b"));
Json m = ComputerUse::mouse_params("mousePressed", 3, 4);
ASSERT_EQ(m.find("type")->as_string(), std::string("mousePressed"));
ASSERT_EQ(m.find("x")->as_int(), 3);
}
TEST(sandbox_codec, chunk_round_trip_with_attributes) {
Chunk c;
c.mime_type = "text/plain";
c.data = std::string("bytes\0here", 10);
c.attributes["file_name"] = "out.txt";
Chunk back = Chunk::from_json(c.to_json());
ASSERT_EQ(back.mime_type, c.mime_type);
ASSERT_EQ(back.data, c.data);
ASSERT_EQ(back.attributes["file_name"], std::string("out.txt"));
bool invalid = false;
Json bad = Json::object();
bad.set("data", Json("!!!not base64!!!"));
Chunk::from_json(bad, &invalid);
ASSERT_TRUE(invalid);
}
TEST(sandbox_codec, output_caps_are_loud) {
// Synthetic outputs built with our own encoder exercise the caps.
Json outs = Json::array();
Chunk big;
big.mime_type = "text/plain";
big.data = std::string(SandboxCaps::kMaxOutputBytes + 10, 'x');
outs.push(big.to_json());
for (std::size_t i = 0; i < SandboxCaps::kMaxProducedFiles + 2; ++i) {
Chunk f;
f.mime_type = "text/plain";
f.data = "d";
f.attributes["file_name"] = "f" + std::to_string(i) + ".txt";
outs.push(f.to_json());
}
Chunk huge;
huge.mime_type = "application/octet-stream";
huge.data = std::string(SandboxCaps::kMaxProducedFileBytes + 1, 'z');
huge.attributes["file_name"] = "huge.bin";
outs.push(huge.to_json());
Chunk unsafe;
unsafe.mime_type = "text/plain";
unsafe.data = "x";
unsafe.attributes["file_name"] = "../escape";
outs.push(unsafe.to_json());
Json reply = Json::object();
reply.set("outputs", outs);
ExecutionResult r = ChunkCodec::decode(reply);
ASSERT_EQ(r.std_out.size(), SandboxCaps::kMaxOutputBytes);
ASSERT_TRUE(r.out_truncated);
ASSERT_EQ(r.files.size(), SandboxCaps::kMaxProducedFiles);
ASSERT_TRUE(r.skipped.size() >= 3); // two surplus files, huge, unsafe name
ASSERT_TRUE(r.ok());
Json j = r.to_json();
ASSERT_TRUE(j.find("stdout_truncated")->as_bool());
}
TEST(sandbox_codec, chunk_count_cap) {
Json outs = Json::array();
for (std::size_t i = 0; i < SandboxCaps::kMaxOutputChunks + 5; ++i) {
Chunk c;
c.mime_type = "text/plain";
c.data = "a";
outs.push(c.to_json());
}
Json reply = Json::object();
reply.set("outputs", outs);
std::vector<std::string> skipped;
ASSERT_EQ(ChunkCodec::outputs(reply, &skipped).size(), SandboxCaps::kMaxOutputChunks);
ASSERT_EQ(skipped.size(), 1u);
}
TEST(sandbox_requests, shapes_over_a_failing_transport) {
// The transport fails every send, so no Google reply is scripted. The
// requests it saw prove method, version and path of each call.
auto probe = [](const std::function<void(const std::shared_ptr<GoogleClient>&)>& fn) {
auto t = std::make_unique<MockTransport>();
MockTransport* mock = t.get();
auto client = std::make_shared<GoogleClient>(std::move(t), Credentials::bearer("tok"));
try {
fn(client);
} catch (const std::exception&) {
}
return mock->seen();
};
auto seen = probe([](const auto& c) { Sandboxes(c).get(kSandbox); });
ASSERT_TRUE(!seen.empty());
ASSERT_EQ(seen[0].method, std::string("GET"));
ASSERT_EQ(seen[0].target, "/v1/" + kSandbox);
seen = probe([](const auto& c) { CodeExecutor(c).run(kSandbox, "1"); });
ASSERT_EQ(seen[0].method, std::string("POST"));
ASSERT_EQ(seen[0].target, "/v1/" + kSandbox + ":execute");
CreateSandboxRequest comp;
comp.display_name = "c";
comp.spec.kind = SandboxKind::kComputerUse;
comp.template_name = kTemplate;
seen = probe([&](const auto& c) { Sandboxes(c).create(kEngine, comp); });
ASSERT_EQ(seen[0].target, "/v1beta1/" + kEngine + "/sandboxEnvironments");
CreateSandboxRequest code;
code.display_name = "c";
seen = probe([&](const auto& c) { Sandboxes(c).create(kEngine, code); });
ASSERT_EQ(seen[0].target, "/v1/" + kEngine + "/sandboxEnvironments");
seen = probe([](const auto& c) { Sandboxes(c).remove(kSandbox); });
ASSERT_EQ(seen[0].method, std::string("DELETE"));
seen = probe([](const auto& c) { Templates(c).get(kTemplate); });
ASSERT_EQ(seen[0].target, "/v1beta1/" + kTemplate);
seen = probe([](const auto& c) { Snapshots(c).list(kEngine, 10); });
ASSERT_EQ(seen[0].target, "/v1/" + kEngine + "/sandboxEnvironmentSnapshots?pageSize=10");
seen = probe([](const auto& c) { Sandboxes(c).pause(kSandbox); });
ASSERT_EQ(seen[0].target, "/v1/" + kSandbox + ":pause");
seen = probe([](const auto& c) { StreamExecutor(c).run(kSandbox, {ChunkCodec::shell_input("ls", "", 5)}); });
ASSERT_TRUE(!seen.empty());
ASSERT_EQ(seen[0].target, "/v1beta1/" + kSandbox + ":bidiExecute");
}
TEST(sandbox_calls, table_is_well_formed) {
std::set<std::string> names;
for (const DeclaredCall& c : kDeclaredCalls) {
ASSERT_TRUE(names.insert(c.name).second);
std::string http = c.http;
ASSERT_TRUE(http == "GET" || http == "POST" || http == "DELETE");
ASSERT_TRUE(std::string(c.path).rfind("{+", 0) == 0);
std::string v = c.version;
ASSERT_TRUE(v == "v1" || v == "v1beta1");
ASSERT_TRUE(std::string(c.rpc).rfind("aiplatform.projects.locations.reasoningEngines.", 0) == 0);
}
ASSERT_EQ(sizeof(kDeclaredCalls) / sizeof(kDeclaredCalls[0]), 18u);
}
TEST(google_live_sandbox, read_only_probe) {
// Needs MOLO_LIVE_NET=1, GOOGLE_API_KEY (or GOOGLE_ACCESS_TOKEN) and
// GOOGLE_CLOUD_PROJECT. Creates nothing: it lists sandboxes of a reasoning
// engine id that cannot exist and expects Google's own 404 NOT_FOUND. That
// proves TLS, auth, path and the error mapping against the real service.
if (!std::getenv("MOLO_LIVE_NET") ||
(Credentials::env().empty() && Credentials::env("GOOGLE_ACCESS_TOKEN").empty()) ||
CloudContext::from_env().project.empty()) {
std::cout << "SKIP google_live_sandbox.read_only_probe (set MOLO_LIVE_NET=1, "
"a Google credential and GOOGLE_CLOUD_PROJECT)\n";
return;
}
CloudContext ctx = CloudContext::from_env();
Sandboxes sandboxes(GoogleClient::from_env());
try {
sandboxes.list(ctx.child("reasoningEngines", "1234567890123456789"));
FAIL("an engine that cannot exist must not list");
} catch (const GoogleError& e) {
std::cout << " live: " << e.http_status() << " " << e.status() << "\n";
ASSERT_EQ(e.http_status(), 404);
ASSERT_EQ(e.status(), std::string("NOT_FOUND"));
}
// A sandbox that cannot exist: Google names the sandbox, not the route.
try {
sandboxes.get(ctx.child("reasoningEngines", "1234567890123456789") +
"/sandboxEnvironments/1234567890123456789");
FAIL("a sandbox that cannot exist must not be found");
} catch (const GoogleError& e) {
ASSERT_EQ(e.http_status(), 404);
ASSERT_TRUE(e.message().find("Sandbox Environment") != std::string::npos);
}
}