traeak commented on code in PR #13853:
URL: https://github.com/apache/trafficserver/pull/13853#discussion_r4567972587
##########
src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc:
##########
@@ -27,16 +27,61 @@
*/
#include <catch2/catch_test_macros.hpp> /* catch unit-test framework */
-#include <fstream> /* ofstream */
+#include <catch2/reporters/catch_reporter_event_listener.hpp>
+#include <catch2/reporters/catch_reporter_registrars.hpp>
+#include <chrono>
+#include <cstdlib>
+#include <filesystem>
+#include <fstream> /* ofstream */
#include <memory>
#include <utime.h>
#include <yaml-cpp/yaml.h>
-#include "nexthop_test_stubs.h"
+#include "tscore/Layout.h"
#include "proxy/http/remap/NextHopSelectionStrategy.h"
#include "proxy/http/remap/NextHopStrategyFactory.h"
#include "proxy/http/remap/NextHopConsistentHash.h"
-#include "proxy/http/remap/NextHopRoundRobin.h"
+
+// '#include' directives in strategy files resolve against the configuration
+// directory, so point it at a temporary configuration directory containing the
+// test fixtures.
+static std::filesystem::path config_root;
+
+struct ConfigDirListener : Catch::EventListenerBase {
+ using EventListenerBase::EventListenerBase;
+
+ void
+ testRunStarting(Catch::TestRunInfo const & /* testRunInfo ATS_UNUSED */)
override
+ {
+ const auto temp_dir = std::filesystem::temp_directory_path();
+ const auto prefix = "nh_strategy_config_test-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+
+ for (unsigned int attempt = 0; attempt < 100; ++attempt) {
+ config_root = temp_dir / (prefix + "-" + std::to_string(attempt));
+ if (std::filesystem::create_directory(config_root)) {
+ break;
+ }
+ config_root.clear();
+ }
+ if (config_root.empty()) {
+ std::abort();
+ }
+
+ setenv("PROXY_CONFIG_CONFIG_DIR", "etc/trafficserver", 1);
+ Layout::create(config_root.string());
+ const auto config_dir = std::filesystem::path(Layout::get()->sysconfdir);
Review Comment:
The listener now points `PROXY_CONFIG_CONFIG_DIR` at a temporary directory,
copies the fixtures there, and checks that `RecConfigReadConfigDir()` agrees,
so the test no longer depends on the install layout.
##########
src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc:
##########
@@ -64,9 +109,81 @@ SCENARIO("factory tests loading yaml configs",
"[loadConfig]")
}
}
+ GIVEN("Loading a strategy.yaml outside the configuration directory with an
absolute include.")
Review Comment:
Added a scenario that writes the strategy into its own temporary directory
with `#include hosts.yaml`, so the file exists only in the configuration
directory and resolving against the including file's directory fails.
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -271,6 +332,10 @@ NextHopStrategyFactory::loadConfigFile(const std::string
&fileName, std::strings
throw std::invalid_argument("Unable to load included file '" +
f + "' from '" + fileName + "': " + ex.what());
}
}
+ if (used_legacy_path) {
+ const auto recommended_include =
std::filesystem::path(f).lexically_relative(RecConfigReadConfigDir()).string();
+ NH_Warn("Deprecated '#include %s'; use '#include %s' instead.",
include_target.c_str(), recommended_include.c_str());
Review Comment:
Done, the warning now names the including file (`in '<fileName>'`).
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -23,15 +23,60 @@
#include <yaml-cpp/yaml.h>
+#include <filesystem>
#include <fstream>
#include <cstring>
#include <string_view>
+#include <utility>
+#include "records/RecCore.h"
+#include "tscore/Layout.h"
#include "proxy/http/remap/NextHopStrategyFactory.h"
#include "proxy/http/remap/NextHopConsistentHash.h"
#include "proxy/http/remap/NextHopRoundRobin.h"
#include <tsutil/YamlCfg.h>
+namespace
+{
+std::string
+legacy_config_dir_include_path(std::string_view include_path)
+{
+ std::filesystem::path const target{include_path};
+ if (target.is_absolute()) {
+ return {};
+ }
+
+ std::filesystem::path const config_dir{RecConfigReadConfigDir()};
+ std::filesystem::path const install_prefix{Layout::get()->prefix};
+ std::filesystem::path const config_dir_prefix =
config_dir.lexically_relative(install_prefix);
+ if (config_dir_prefix.empty() || config_dir_prefix == "." ||
config_dir_prefix.is_absolute()) {
+ return {};
+ }
+ for (const auto &part : config_dir_prefix) {
+ if (part == "..") {
+ return {};
+ }
+ }
+
+ auto target_part = target.begin();
+ for (const auto &prefix_part : config_dir_prefix) {
+ if (target_part == target.end() || *target_part != prefix_part) {
+ return {};
+ }
+ ++target_part;
+ }
+ if (target_part == target.end()) {
+ return {};
+ }
+
+ std::filesystem::path stripped_path;
+ for (; target_part != target.end(); ++target_part) {
+ stripped_path /= *target_part;
+ }
+ return RecConfigReadConfigPath(nullptr, stripped_path.string().c_str());
Review Comment:
The prefix-stripping match is gone. An include missing from the
configuration directory is retried relative to the install prefix, joined
without normalization. `./etc/trafficserver/hosts.yaml` is a test case, and the
recommended spelling in the warning ignores empty and `.` path elements, which
covers a configuration directory ending in `/`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]