Files
ViewDesignEngine/tests/plugin/test_plugin_external_container.cpp
T

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 "vde/fs_compat.h"
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#if defined(_WIN32)
#include <cstdlib>
#else
#include <cstdlib>
#endif
namespace fs = vde::fs;
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.string(), 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.string(), 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