288 lines
12 KiB
C++
288 lines
12 KiB
C++
// WP-P4-13: external-service templates (C++/Python) end-to-end via PluginManager.
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "vde/plugin/plugin_manager.h"
|
|
|
|
#include <chrono>
|
|
#include "vde/fs_compat.h"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
|
|
namespace fs = vde::fs;
|
|
using vde::plugin::ExternalServiceInvokeOptions;
|
|
using vde::plugin::ExternalServiceResult;
|
|
using vde::plugin::PluginManager;
|
|
|
|
#ifndef VDE_TEMPLATE_CPP_DIR
|
|
#define VDE_TEMPLATE_CPP_DIR ""
|
|
#endif
|
|
#ifndef VDE_TEMPLATE_CPP_EXE
|
|
#define VDE_TEMPLATE_CPP_EXE ""
|
|
#endif
|
|
#ifndef VDE_TEMPLATE_PYTHON_DIR
|
|
#define VDE_TEMPLATE_PYTHON_DIR ""
|
|
#endif
|
|
#ifndef VDE_TEMPLATE_PYTHON_LAUNCHER
|
|
#define VDE_TEMPLATE_PYTHON_LAUNCHER ""
|
|
#endif
|
|
#ifndef VDE_TEMPLATE_PYTHON_EXE
|
|
#define VDE_TEMPLATE_PYTHON_EXE ""
|
|
#endif
|
|
|
|
namespace {
|
|
|
|
constexpr const char* kCppId = "com.vde.template.echo.cpp";
|
|
constexpr const char* kPythonId = "com.vde.template.echo.python";
|
|
|
|
std::string read_text(const fs::path& path) {
|
|
std::ifstream in(path.string(), std::ios::binary);
|
|
std::ostringstream ss;
|
|
ss << in.rdbuf();
|
|
return ss.str();
|
|
}
|
|
|
|
void write_text(const fs::path& path, const std::string& text) {
|
|
std::ofstream out(path.string(), std::ios::binary | std::ios::trunc);
|
|
out << text;
|
|
}
|
|
|
|
void set_env(const char* name, const char* value) {
|
|
#if defined(_WIN32)
|
|
_putenv_s(name, value != nullptr ? value : "");
|
|
#else
|
|
if (value != nullptr) {
|
|
setenv(name, value, 1);
|
|
} else {
|
|
unsetenv(name);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
std::string json_escape(const std::string& text) {
|
|
std::string out;
|
|
out.reserve(text.size());
|
|
for (const char c : text) {
|
|
if (c == '\\' || c == '"') out.push_back('\\');
|
|
out.push_back(c);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string manifest_text(const std::string& id, const fs::path& executable) {
|
|
const std::string exe = json_escape(executable.generic_string());
|
|
return std::string("{\n") +
|
|
" \"id\": \"" + id + "\",\n" +
|
|
" \"name\": \"VDE external service template e2e\",\n" +
|
|
" \"version\": \"0.1.0\",\n" +
|
|
" \"protocol\": { \"name\": \"vde-service\", \"version\": \"0.1\" },\n" +
|
|
" \"executable\": { \"windows\": \"" + exe + "\", \"linux\": \"" + exe +
|
|
"\", \"macos\": \"" + exe + "\" },\n" +
|
|
" \"capabilities\": [\"service:template.echo\"],\n" +
|
|
" \"timeouts\": { \"request_ms\": 5000 },\n" +
|
|
" \"limits\": { \"max_memory_mb\": 512, \"max_cpu_seconds\": 30, "
|
|
"\"max_processes\": 8, \"kill_on_close\": true },\n" +
|
|
" \"sandbox\": { \"network\": false, "
|
|
"\"env_allow\": [\"VDE_SERVICE_PYTHON\"] },\n" +
|
|
" \"license\": \"Apache-2.0\"\n}\n";
|
|
}
|
|
|
|
std::string request_text(const std::string& request_id, const fs::path& output,
|
|
const std::string& operation = "echo",
|
|
const std::string& message = "hello-template",
|
|
int sleep_ms = 0) {
|
|
return std::string("{\"protocol\":\"vde-service/0.1\",\"request_id\":\"") +
|
|
request_id + "\",\"operation\":\"" + operation + "\",\"output\":\"" +
|
|
json_escape(output.generic_string()) + "\",\"args\":{\"message\":\"" +
|
|
message + "\",\"sleep_ms\":" + std::to_string(sleep_ms) + "}}";
|
|
}
|
|
|
|
class ExternalServiceTemplatesTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
root_ = fs::temp_directory_path() /
|
|
("vde_ext_tpl_test_" +
|
|
std::to_string(static_cast<long long>(
|
|
std::chrono::steady_clock::now().time_since_epoch().count())));
|
|
fs::create_directories(root_ / "sandbox");
|
|
auto& manager = PluginManager::instance();
|
|
manager.clear();
|
|
manager.clear_external_services();
|
|
manager.clear_service_search_paths();
|
|
manager.set_service_sandbox_root((root_ / "sandbox").string());
|
|
if (std::string(VDE_TEMPLATE_PYTHON_EXE).size() > 0) {
|
|
set_env("VDE_SERVICE_PYTHON", VDE_TEMPLATE_PYTHON_EXE);
|
|
}
|
|
}
|
|
|
|
void TearDown() override {
|
|
auto& manager = PluginManager::instance();
|
|
manager.clear();
|
|
manager.clear_external_services();
|
|
manager.clear_service_search_paths();
|
|
manager.set_service_sandbox_root("");
|
|
set_env("VDE_SERVICE_PYTHON", nullptr);
|
|
std::error_code ec;
|
|
fs::remove_all(root_, ec);
|
|
}
|
|
|
|
fs::path make_service_dir(const std::string& id, const fs::path& executable) {
|
|
const fs::path dir = root_ / ("svc_" + id);
|
|
fs::create_directories(dir);
|
|
write_text(dir / "vde-service.json", manifest_text(id, executable));
|
|
return dir;
|
|
}
|
|
|
|
fs::path make_work_dir(const std::string& name) {
|
|
const fs::path dir = root_ / "sandbox" / name;
|
|
fs::create_directories(dir);
|
|
return dir;
|
|
}
|
|
|
|
ExternalServiceResult invoke(const fs::path& executable, const std::string& id,
|
|
const fs::path& work_dir, const std::string& request,
|
|
int timeout_ms = 0) {
|
|
auto& manager = PluginManager::instance();
|
|
std::string error;
|
|
manager.clear_external_services();
|
|
const fs::path dir = make_service_dir(id, executable);
|
|
const std::size_t added = manager.scan_service_directory(dir.string(), &error);
|
|
EXPECT_GE(added, 1u) << error;
|
|
ExternalServiceInvokeOptions options;
|
|
options.work_dir = work_dir.string();
|
|
options.timeout_ms = timeout_ms;
|
|
ExternalServiceResult result;
|
|
manager.invoke_external_service(id, request, options, &result, &error);
|
|
return result;
|
|
}
|
|
|
|
ExternalServiceResult invoke_ok(const fs::path& executable, const std::string& id,
|
|
const std::string& tag) {
|
|
const fs::path work = make_work_dir(tag);
|
|
const std::string request = request_text(tag, work / "output.txt");
|
|
return invoke(executable, id, work, request);
|
|
}
|
|
|
|
bool python_available(const ExternalServiceResult& result) const {
|
|
return !(result.exit_code == 127 &&
|
|
(result.stderr_excerpt.find("cannot start python") != std::string::npos ||
|
|
result.stderr_excerpt.find("cannot start python3") != std::string::npos));
|
|
}
|
|
|
|
fs::path root_;
|
|
};
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, DiscoverTemplateManifests) {
|
|
auto& manager = PluginManager::instance();
|
|
std::string error;
|
|
ASSERT_TRUE(fs::is_directory(VDE_TEMPLATE_CPP_DIR)) << VDE_TEMPLATE_CPP_DIR;
|
|
const std::size_t cpp_added = manager.scan_service_directory(VDE_TEMPLATE_CPP_DIR, &error);
|
|
EXPECT_GE(cpp_added, 1u) << error;
|
|
ASSERT_TRUE(fs::is_directory(VDE_TEMPLATE_PYTHON_DIR)) << VDE_TEMPLATE_PYTHON_DIR;
|
|
const std::size_t python_added =
|
|
manager.scan_service_directory(VDE_TEMPLATE_PYTHON_DIR, &error);
|
|
EXPECT_GE(python_added, 1u) << error;
|
|
|
|
bool cpp_found = false;
|
|
bool python_found = false;
|
|
for (const auto& service : manager.external_services()) {
|
|
if (service.id == kCppId) {
|
|
cpp_found = true;
|
|
EXPECT_EQ(service.protocol_name, "vde-service");
|
|
EXPECT_EQ(service.protocol_version, "0.1");
|
|
EXPECT_GT(service.request_timeout_ms, 0);
|
|
EXPECT_TRUE(fs::is_regular_file(service.executable)) << service.executable;
|
|
EXPECT_EQ(service.capabilities.front(), "service:template.echo");
|
|
} else if (service.id == kPythonId) {
|
|
python_found = true;
|
|
EXPECT_GT(service.request_timeout_ms, 0);
|
|
EXPECT_TRUE(fs::is_regular_file(service.executable)) << service.executable;
|
|
}
|
|
}
|
|
EXPECT_TRUE(cpp_found);
|
|
EXPECT_TRUE(python_found);
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, CppTemplateSuccessAndLogs) {
|
|
const auto result = invoke_ok(VDE_TEMPLATE_CPP_EXE, kCppId, "cpp_ok");
|
|
ASSERT_TRUE(result.ok) << result.error << "\n" << result.response_json;
|
|
EXPECT_EQ(result.status, "ok");
|
|
EXPECT_EQ(result.exit_code, 0);
|
|
const fs::path output = fs::path(result.work_dir) / "output.txt";
|
|
ASSERT_TRUE(fs::is_regular_file(output)) << output;
|
|
EXPECT_EQ(read_text(output), "hello-template");
|
|
EXPECT_NE(result.response_json.find("diagnostics"), std::string::npos);
|
|
EXPECT_FALSE(result.stdout_excerpt.empty() && result.stderr_excerpt.empty())
|
|
<< "template should emit protocol/log output";
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, CppTemplateErrorPath) {
|
|
const fs::path work = make_work_dir("cpp_error");
|
|
const std::string request =
|
|
request_text("cpp-error", work / "output.txt", "unsupported", "", 0);
|
|
const auto result = invoke(VDE_TEMPLATE_CPP_EXE, kCppId, work, request);
|
|
EXPECT_FALSE(result.ok) << result.response_json;
|
|
EXPECT_TRUE(result.protocol_error) << result.error;
|
|
EXPECT_EQ(result.status, "error");
|
|
EXPECT_EQ(result.error_code, "unsupported_operation");
|
|
EXPECT_FALSE(result.stderr_excerpt.empty());
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, CppTemplateTimeoutPath) {
|
|
const fs::path work = make_work_dir("cpp_timeout");
|
|
const std::string request = request_text("cpp-timeout", work / "output.txt", "echo",
|
|
"", 5000);
|
|
const auto start = std::chrono::steady_clock::now();
|
|
const auto result = invoke(VDE_TEMPLATE_CPP_EXE, kCppId, work, request, 300);
|
|
const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
std::chrono::steady_clock::now() - start)
|
|
.count();
|
|
EXPECT_FALSE(result.ok);
|
|
EXPECT_TRUE(result.timed_out) << result.error;
|
|
EXPECT_LT(elapsed, 4000);
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, PythonTemplateSuccess) {
|
|
if (std::string(VDE_TEMPLATE_PYTHON_EXE).empty()) GTEST_SKIP()
|
|
<< "python interpreter unavailable at configure time";
|
|
const auto result = invoke_ok(VDE_TEMPLATE_PYTHON_LAUNCHER, kPythonId, "py_ok");
|
|
if (!python_available(result)) GTEST_SKIP() << "python interpreter unavailable";
|
|
ASSERT_TRUE(result.ok) << result.error << "\n" << result.response_json;
|
|
EXPECT_EQ(result.status, "ok");
|
|
const fs::path output = fs::path(result.work_dir) / "output.txt";
|
|
ASSERT_TRUE(fs::is_regular_file(output)) << output;
|
|
EXPECT_EQ(read_text(output), "hello-template");
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, PythonTemplateErrorPath) {
|
|
if (std::string(VDE_TEMPLATE_PYTHON_EXE).empty()) GTEST_SKIP()
|
|
<< "python interpreter unavailable at configure time";
|
|
const fs::path work = make_work_dir("py_error");
|
|
const std::string request =
|
|
request_text("py-error", work / "output.txt", "unsupported", "", 0);
|
|
const auto result = invoke(VDE_TEMPLATE_PYTHON_LAUNCHER, kPythonId, work, request);
|
|
if (!python_available(result)) GTEST_SKIP() << "python interpreter unavailable";
|
|
EXPECT_FALSE(result.ok) << result.response_json;
|
|
EXPECT_TRUE(result.protocol_error) << result.error;
|
|
EXPECT_EQ(result.status, "error");
|
|
EXPECT_EQ(result.error_code, "unsupported_operation");
|
|
EXPECT_FALSE(result.stderr_excerpt.empty());
|
|
}
|
|
|
|
TEST_F(ExternalServiceTemplatesTest, PythonTemplateTimeoutPath) {
|
|
if (std::string(VDE_TEMPLATE_PYTHON_EXE).empty()) GTEST_SKIP()
|
|
<< "python interpreter unavailable at configure time";
|
|
const fs::path work = make_work_dir("py_timeout");
|
|
const std::string request = request_text("py-timeout", work / "output.txt", "echo",
|
|
"", 1500);
|
|
const auto result = invoke(VDE_TEMPLATE_PYTHON_LAUNCHER, kPythonId, work, request, 300);
|
|
if (!python_available(result) && !result.timed_out) {
|
|
GTEST_SKIP() << "python interpreter unavailable";
|
|
}
|
|
EXPECT_FALSE(result.ok);
|
|
EXPECT_TRUE(result.timed_out) << result.error;
|
|
}
|
|
|
|
} // namespace
|