Files
ViewDesignEngine/tests/plugin/test_plugin_external_container.cpp
T
hm 0d9108cd2a
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
feat: bridge TU 迁移 + cam/cae/ui/pdm 模块包 + 容器沙箱 + largeMesh 修复 + scaffold/远程索引(1.0.74)
### 宿主复测闭环
- VDE-1249/VDE-1250 标记 Fixed(宿主 1.0.68 复测记录:0 error、13,030/0 ctest、n42 82s、n49/n50/n60、save-load match=1);
- VDE-1251(dense pattern,1.0.72 修复)待宿主复测;

### P3-15c ui/pdm bridge TU 迁移
- geometric_snap/snap_ext→vde_ui、structural_profiles→vde_pdm;规范头 + 旧 brep 兼容转发头(不进 kernel SDK);
- check_kernel_boundary allowlist 清零;默认 build 0 error、定向 170/170、kernel-only 971 步 0 undefined;

### P3-15d/e 模块包
- 新增 vde-cam/vde-cae/vde-ui/vde-pdm 包(共 8 个模块包);独立 consumer:
  CAM_CONSUMER_PASS segments=1006 gcode=1011、CAE_CONSUMER_PASS tet=384 patches=6、
  UI_CONSUMER_PASS layouts=4 picked=1、PDM_CONSUMER_PASS materials=21 profiles=32 bom=1;
- 默认构建/聚合不变;docs module-packaging-split §7.1/§7.2;剩余 assembly/PMI/钣金;

### P3-14c 外部服务容器运行时抽象
- sandbox.container{runtime auto|docker|podman,image,network,read_only};argv 构造 docker/podman run(仅挂载 workdir、
  --network none、--read-only/tmpfs、env 白名单、超时/失败清理);无 runtime 默认 unsupported_sandbox,allow_unsandboxed 降级;
- fake runtime 测试 5/5;docs §7.3/§12.9;真实 docker e2e 需环境;

### P3-16c largeMesh O(F²) 修复
- display_conforming_patch 每体一次统计 sphere 面数;8192 面球 >600s→~2s(261k tris、deg/open/zeroArea=0);
- degenerate_triangles 语义澄清 + 回退场景用 B-Rep wireframe 的宿主指引;test_vde1250_large_mesh 3/3;

### P3-17 插件 SDK scaffold + 远程索引
- tool scaffold 四类模板(独立 build/--check/--list PASS)+ --fetch-index(本地/http WinHTTP,签名/路径/ABI/哈希校验);

### 工程
- 版本 1.0.74;全量构建 0 error(375 targets);全量 ctest 7540 注册、7445/7445 PASS(0 failed,25 skipped,224.07s)。
2026-10-29 08:20:26 +08:00

364 lines
15 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// tests/plugin/test_plugin_external_container.cpp
// P3-14c:外部服务容器运行时抽象(清单解析/无 runtime 拒绝与降级/fake runtime 命令构造)。
#include <gtest/gtest.h>
#include "vde/plugin/plugin_manager.h"
#include <chrono>
#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#if defined(_WIN32)
#include <cstdlib>
#else
#include <cstdlib>
#endif
namespace fs = std::filesystem;
using vde::plugin::ExternalServiceInvokeOptions;
using vde::plugin::ExternalServiceResult;
using vde::plugin::PluginManager;
#ifndef VDE_TEST_SERVICE_EXE
#define VDE_TEST_SERVICE_EXE ""
#endif
#ifndef VDE_TEST_CONTAINER_RUNTIME_EXE
#define VDE_TEST_CONTAINER_RUNTIME_EXE ""
#endif
namespace {
void set_test_env(const char* name, const char* value) {
#if defined(_WIN32)
_putenv_s(name, value ? value : "");
#else
if (value)
setenv(name, value, 1);
else
unsetenv(name);
#endif
}
std::string read_text(const fs::path& path) {
std::ifstream in(path, std::ios::binary);
std::ostringstream stream;
stream << in.rdbuf();
return stream.str();
}
bool write_text(const fs::path& path, const std::string& text) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
if (!out) return false;
out.write(text.data(), static_cast<std::streamsize>(text.size()));
return static_cast<bool>(out);
}
std::string make_container_manifest_text(const std::string& id,
const std::string& sandbox_extra_json,
const std::vector<std::string>& env_allow) {
std::string env_json = "[";
for (std::size_t i = 0; i < env_allow.size(); ++i) {
if (i != 0) env_json += ",";
env_json += "\"" + env_allow[i] + "\"";
}
env_json += "]";
const std::string exe = fs::path(VDE_TEST_SERVICE_EXE).generic_string();
std::string sandbox = " \"sandbox\": { " + sandbox_extra_json + ", \"env_allow\": " + env_json + " },\n";
std::string text = "{\n";
text += " \"id\": \"" + id + "\",\n";
text += " \"name\": \"VDE Container Test Service\",\n";
text += " \"version\": \"0.1.0\",\n";
text += " \"protocol\": { \"name\": \"vde-service\", \"version\": \"0.1\" },\n";
text += " \"executable\": { \"windows\": \"" + exe + "\", \"linux\": \"" + exe + "\" },\n";
text += " \"capabilities\": [\"service:test.container\"],\n";
text += " \"timeouts\": { \"request_ms\": 15000 },\n";
text += sandbox;
text += " \"license\": \"Apache-2.0\"\n";
text += "}\n";
return text;
}
fs::path make_container_service_dir(const fs::path& root, const std::string& id,
const std::string& sandbox_extra_json,
const std::vector<std::string>& env_allow) {
const fs::path dir = root / ("svc_" + id);
fs::create_directories(dir);
write_text(dir / "vde-service.json",
make_container_manifest_text(id, sandbox_extra_json, env_allow));
return dir;
}
class ExternalContainerTest : public ::testing::Test {
protected:
void SetUp() override {
root_ = fs::temp_directory_path() /
("vde_container_test_" +
std::to_string(static_cast<long long>(
std::chrono::steady_clock::now().time_since_epoch().count())));
fs::create_directories(root_);
sandbox_root_ = root_ / "sandbox";
fs::create_directories(sandbox_root_);
runtime_bin_ = root_ / "bin";
fs::create_directories(runtime_bin_);
original_path_ = env_or_empty("PATH");
auto& manager = PluginManager::instance();
manager.clear();
manager.clear_external_services();
manager.clear_service_search_paths();
manager.set_service_sandbox_root(sandbox_root_.string());
set_test_env("VDE_FAKE_CONTAINER_LOG", (root_ / "runtime_args.log").string().c_str());
set_test_env("VDE_FAKE_CONTAINER_MODE", "ok");
set_test_env("VDE_TEST_SERVICE_MODE", "ok");
}
void TearDown() override {
auto& manager = PluginManager::instance();
manager.clear();
manager.clear_external_services();
manager.clear_service_search_paths();
manager.set_service_sandbox_root("");
set_test_env("PATH", original_path_.c_str());
set_test_env("VDE_FAKE_CONTAINER_LOG", nullptr);
set_test_env("VDE_FAKE_CONTAINER_MODE", nullptr);
set_test_env("VDE_TEST_SERVICE_MODE", nullptr);
std::error_code remove_ec;
fs::remove_all(root_, remove_ec);
}
std::string env_or_empty(const char* name) {
const char* value = std::getenv(name);
return value ? std::string(value) : std::string();
}
fs::path root_;
fs::path sandbox_root_;
fs::path runtime_bin_;
std::string original_path_;
};
TEST_F(ExternalContainerTest, ContainerManifestParsingAndValidation) {
auto& manager = PluginManager::instance();
const fs::path good = make_container_service_dir(
root_, "com.vde.test.ctr_good",
"\"container\": { \"runtime\": \"docker\", \"image\": \"vde/test:1\", "
"\"network\": \"none\", \"read_only\": true }",
{});
std::string error;
ASSERT_GE(manager.scan_service_directory(good.string(), &error), 1u) << error;
const auto info = manager.external_service("com.vde.test.ctr_good");
ASSERT_TRUE(info.has_value());
EXPECT_TRUE(info->container.enabled);
EXPECT_EQ(info->container.runtime, "docker");
EXPECT_EQ(info->container.image, "vde/test:1");
EXPECT_EQ(info->container.network, "none");
EXPECT_TRUE(info->container.read_only);
manager.clear_external_services();
const fs::path bad_runtime = make_container_service_dir(
root_, "com.vde.test.ctr_bad_runtime", "\"container\": { \"image\": \"x\", \"runtime\": \"rkt\" }", {});
EXPECT_EQ(manager.scan_service_directory(bad_runtime.string(), &error), 0u);
EXPECT_NE(error.find("container.runtime"), std::string::npos) << error;
manager.clear_external_services();
const fs::path bad_image = make_container_service_dir(
root_, "com.vde.test.ctr_bad_image", "\"container\": { \"runtime\": \"docker\" }", {});
EXPECT_EQ(manager.scan_service_directory(bad_image.string(), &error), 0u);
EXPECT_NE(error.find("container.image"), std::string::npos) << error;
manager.clear_external_services();
const fs::path bad_network = make_container_service_dir(
root_, "com.vde.test.ctr_bad_network",
"\"container\": { \"image\": \"x\", \"network\": \"bridge\" }", {});
EXPECT_EQ(manager.scan_service_directory(bad_network.string(), &error), 0u);
EXPECT_NE(error.find("container.network"), std::string::npos) << error;
}
TEST_F(ExternalContainerTest, ContainerRuntimeMissingRejectedOrDowngraded) {
auto& manager = PluginManager::instance();
const fs::path dir = make_container_service_dir(
root_, "com.vde.test.ctr_missing",
"\"container\": { \"runtime\": \"docker\", \"image\": \"vde/test:1\" }", {});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("PATH", runtime_bin_.string().c_str()); // 无 docker
ExternalServiceInvokeOptions options;
options.work_dir = (sandbox_root_ / "missing").string();
fs::create_directories(options.work_dir);
ExternalServiceResult result;
EXPECT_FALSE(manager.invoke_external_service(
"com.vde.test.ctr_missing", "{\"request_id\":\"ctr-missing\",\"operation\":\"echo\"}", options,
&result, &error));
EXPECT_TRUE(result.sandbox_unsupported);
EXPECT_EQ(result.error_code, "unsupported_sandbox");
EXPECT_FALSE(result.container_used);
EXPECT_NE(result.error.find("container runtime"), std::string::npos) << result.error;
manager.clear_external_services();
const fs::path escape_dir = make_container_service_dir(
root_, "com.vde.test.ctr_escape",
"\"allow_unsandboxed\": true, \"container\": { \"runtime\": \"docker\", \"image\": \"vde/test:1\" }",
{});
ASSERT_GE(manager.scan_service_directory(escape_dir.string(), &error), 1u) << error;
// 直接进程降级需要宿主 exe 的运行时 DLL(PATH 保留原值;仍无 docker/podman)
#if defined(_WIN32)
set_test_env("PATH", (runtime_bin_.string() + ";" + original_path_).c_str());
#else
set_test_env("PATH", (runtime_bin_.string() + ":" + original_path_).c_str());
#endif
options.work_dir = (sandbox_root_ / "escape").string();
fs::create_directories(options.work_dir);
ExternalServiceResult escaped;
EXPECT_TRUE(manager.invoke_external_service(
"com.vde.test.ctr_escape", "{\"request_id\":\"ctr-escape\",\"operation\":\"echo\"}", options,
&escaped, &error)) << escaped.error << " / " << error;
EXPECT_TRUE(escaped.sandbox_downgraded);
EXPECT_FALSE(escaped.container_used);
EXPECT_FALSE(escaped.sandbox_warnings.empty());
EXPECT_FALSE(escaped.sandbox_enforced);
}
TEST_F(ExternalContainerTest, ContainerCommandConstructionAndIsolation) {
auto& manager = PluginManager::instance();
const std::string exe_suffix =
#if defined(_WIN32)
".exe";
#else
"";
#endif
const fs::path docker_exe = runtime_bin_ / ("docker" + exe_suffix);
std::error_code copy_ec;
fs::copy_file(fs::path(VDE_TEST_CONTAINER_RUNTIME_EXE), docker_exe,
fs::copy_options::overwrite_existing, copy_ec);
ASSERT_FALSE(copy_ec) << copy_ec.message();
#if defined(_WIN32)
set_test_env("PATH", (runtime_bin_.string() + ";" + original_path_).c_str());
#else
set_test_env("PATH", (runtime_bin_.string() + ":" + original_path_).c_str());
#endif
const fs::path dir = make_container_service_dir(
root_, "com.vde.test.ctr_cmd",
"\"container\": { \"runtime\": \"docker\", \"image\": \"vde/test:1\", "
"\"network\": \"none\", \"read_only\": true }",
{"VDE_FAKE_CONTAINER_LOG", "VDE_FAKE_CONTAINER_MODE", "VDE_TEST_SERVICE_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work = sandbox_root_ / "cmd";
fs::create_directories(work);
ExternalServiceInvokeOptions options;
options.work_dir = work.string();
ExternalServiceResult result;
ASSERT_TRUE(manager.invoke_external_service(
"com.vde.test.ctr_cmd", "{\"request_id\":\"ctr-cmd\",\"operation\":\"echo\"}", options,
&result, &error)) << result.error << " / " << error;
EXPECT_TRUE(result.ok);
EXPECT_TRUE(result.container_used);
EXPECT_EQ(result.container_runtime, "docker");
EXPECT_TRUE(result.sandbox_enforced);
const std::string log = read_text(root_ / "runtime_args.log");
EXPECT_NE(log.find("run"), std::string::npos) << log;
EXPECT_NE(log.find("--rm"), std::string::npos) << log;
EXPECT_NE(log.find("--name"), std::string::npos) << log;
EXPECT_NE(log.find("--network"), std::string::npos) << log;
EXPECT_NE(log.find("--read-only"), std::string::npos) << log;
EXPECT_NE(log.find("--tmpfs"), std::string::npos) << log;
EXPECT_NE(log.find("/tmp"), std::string::npos) << log;
EXPECT_NE(log.find("--volume"), std::string::npos) << log;
EXPECT_NE(log.find(":/vde/work"), std::string::npos) << log;
EXPECT_NE(log.find("vde/test:1"), std::string::npos) << log;
EXPECT_NE(log.find("/vde/work/request.json"), std::string::npos) << log;
EXPECT_NE(log.find("/vde/work/response.json"), std::string::npos) << log;
EXPECT_NE(log.find("VDE_FAKE_CONTAINER_LOG="), std::string::npos) << log;
std::size_t volume_count = 0;
std::size_t pos = 0;
while ((pos = log.find("--volume", pos)) != std::string::npos) {
++volume_count;
pos += 8;
}
EXPECT_EQ(volume_count, 1u) << log;
}
TEST_F(ExternalContainerTest, ContainerAutoDetectsPodmanAndFlagsConfigurable) {
auto& manager = PluginManager::instance();
const std::string exe_suffix =
#if defined(_WIN32)
".exe";
#else
"";
#endif
const fs::path podman_exe = runtime_bin_ / ("podman" + exe_suffix);
std::error_code copy_ec;
fs::copy_file(fs::path(VDE_TEST_CONTAINER_RUNTIME_EXE), podman_exe,
fs::copy_options::overwrite_existing, copy_ec);
ASSERT_FALSE(copy_ec) << copy_ec.message();
#if defined(_WIN32)
set_test_env("PATH", (runtime_bin_.string() + ";" + original_path_).c_str());
#else
set_test_env("PATH", (runtime_bin_.string() + ":" + original_path_).c_str());
#endif
const fs::path dir = make_container_service_dir(
root_, "com.vde.test.ctr_podman",
"\"container\": { \"runtime\": \"auto\", \"image\": \"vde/test:1\", "
"\"network\": \"inherit\", \"read_only\": false }",
{"VDE_FAKE_CONTAINER_LOG", "VDE_FAKE_CONTAINER_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
const fs::path work = sandbox_root_ / "podman";
fs::create_directories(work);
ExternalServiceInvokeOptions options;
options.work_dir = work.string();
ExternalServiceResult result;
ASSERT_TRUE(manager.invoke_external_service(
"com.vde.test.ctr_podman", "{\"request_id\":\"ctr-podman\",\"operation\":\"echo\"}", options,
&result, &error)) << result.error << " / " << error;
EXPECT_TRUE(result.ok);
EXPECT_TRUE(result.container_used);
EXPECT_EQ(result.container_runtime, "podman");
const std::string log = read_text(root_ / "runtime_args.log");
EXPECT_EQ(log.find("--read-only"), std::string::npos) << log;
EXPECT_EQ(log.find("--tmpfs"), std::string::npos) << log;
EXPECT_EQ(log.find("--network"), std::string::npos) << log;
}
TEST_F(ExternalContainerTest, ContainerRuntimeFailureIsolated) {
auto& manager = PluginManager::instance();
const std::string exe_suffix =
#if defined(_WIN32)
".exe";
#else
"";
#endif
const fs::path docker_exe = runtime_bin_ / ("docker" + exe_suffix);
std::error_code copy_ec;
fs::copy_file(fs::path(VDE_TEST_CONTAINER_RUNTIME_EXE), docker_exe,
fs::copy_options::overwrite_existing, copy_ec);
ASSERT_FALSE(copy_ec) << copy_ec.message();
#if defined(_WIN32)
set_test_env("PATH", (runtime_bin_.string() + ";" + original_path_).c_str());
#else
set_test_env("PATH", (runtime_bin_.string() + ":" + original_path_).c_str());
#endif
const fs::path dir = make_container_service_dir(
root_, "com.vde.test.ctr_fail",
"\"container\": { \"runtime\": \"docker\", \"image\": \"vde/test:1\" }",
{"VDE_FAKE_CONTAINER_LOG", "VDE_FAKE_CONTAINER_MODE"});
std::string error;
ASSERT_GE(manager.scan_service_directory(dir.string(), &error), 1u) << error;
set_test_env("VDE_FAKE_CONTAINER_MODE", "badjson");
ExternalServiceInvokeOptions options;
options.work_dir = (sandbox_root_ / "fail1").string();
fs::create_directories(options.work_dir);
ExternalServiceResult failed;
EXPECT_FALSE(manager.invoke_external_service(
"com.vde.test.ctr_fail", "{\"request_id\":\"ctr-fail\",\"operation\":\"echo\"}", options,
&failed, &error));
EXPECT_TRUE(failed.protocol_error);
set_test_env("VDE_FAKE_CONTAINER_MODE", "ok");
options.work_dir = (sandbox_root_ / "fail2").string();
fs::create_directories(options.work_dir);
ExternalServiceResult recovered;
EXPECT_TRUE(manager.invoke_external_service(
"com.vde.test.ctr_fail", "{\"request_id\":\"ctr-recover\",\"operation\":\"echo\"}", options,
&recovered, &error)) << recovered.error << " / " << error;
}
} // namespace