Copilot commented on code in PR #13853:
URL: https://github.com/apache/trafficserver/pull/13853#discussion_r4568133304
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -272,16 +346,40 @@ NextHopStrategyFactory::loadConfigFile(const std::string
&fileName, std::strings
}
const auto include_line = line_view.substr(include_start);
const auto include_end =
include_line.find_first_of(separators);
- const std::string f{include_line.substr(0, include_end)};
+ const std::string include_target{include_line.substr(0,
include_end)};
+ std::string f =
config_dir_include_path(include_target).string();
+ bool used_legacy_path = false;
+ std::string legacy_path;
+
+ // Retry an include that is absent from the configuration
directory relative to the install prefix, the way earlier
+ // releases resolved it. not_found covers a missing file and a
path component that is not a directory. Other
+ // failures, such as a permission error, are left to fail the load
so they are reported rather than silently
+ // replaced by a file from the install prefix.
+ std::error_code ec;
+ if (std::filesystem::status(f, ec).type() ==
std::filesystem::file_type::not_found) {
+ legacy_path = legacy_include_path(include_target).string();
+ if (!legacy_path.empty() && std::filesystem::exists(legacy_path,
ec)) {
+ f = legacy_path;
+ used_legacy_path = true;
+ }
+ }
+
if (include_once.find(f) == include_once.end()) {
include_once.insert(f);
// try to load included file.
try {
loadConfigFile(f, doc, include_once);
} catch (const std::exception &ex) {
- throw std::invalid_argument("Unable to load included file '" +
f + "' from '" + fileName + "': " + ex.what());
+ std::string const also_tried =
+ !used_legacy_path && !legacy_path.empty() ? " (also tried '"
+ legacy_path + "')" : "";
+ throw std::invalid_argument("Unable to load '#include " +
include_target + "' from '" + fileName + "' as '" + f +
+ "'" + also_tried + ": " +
ex.what());
}
}
+ if (used_legacy_path) {
+ NH_Warn("Deprecated '#include %s' in '%s'; use '#include %s'
instead. This fallback will be removed in 12.0.0.",
+ include_target.c_str(), fileName.c_str(),
recommended_include(f).c_str());
+ }
Review Comment:
The deprecation warning is emitted even when the include is skipped due to
`include_once` (i.e., repeated `#include` lines for the same resolved file).
This can create noisy logs for configs that repeat includes. Consider emitting
the warning only when the include is actually processed (e.g., inside the
`include_once` insertion block), or otherwise deduplicate warnings per resolved
include path.
##########
src/proxy/http/remap/NextHopStrategyFactory.cc:
##########
@@ -272,16 +346,40 @@ NextHopStrategyFactory::loadConfigFile(const std::string
&fileName, std::strings
}
const auto include_line = line_view.substr(include_start);
const auto include_end =
include_line.find_first_of(separators);
- const std::string f{include_line.substr(0, include_end)};
+ const std::string include_target{include_line.substr(0,
include_end)};
+ std::string f =
config_dir_include_path(include_target).string();
+ bool used_legacy_path = false;
+ std::string legacy_path;
+
+ // Retry an include that is absent from the configuration
directory relative to the install prefix, the way earlier
+ // releases resolved it. not_found covers a missing file and a
path component that is not a directory. Other
+ // failures, such as a permission error, are left to fail the load
so they are reported rather than silently
+ // replaced by a file from the install prefix.
+ std::error_code ec;
+ if (std::filesystem::status(f, ec).type() ==
std::filesystem::file_type::not_found) {
+ legacy_path = legacy_include_path(include_target).string();
+ if (!legacy_path.empty() && std::filesystem::exists(legacy_path,
ec)) {
+ f = legacy_path;
+ used_legacy_path = true;
+ }
+ }
Review Comment:
`std::filesystem::status(p, ec)` does not reliably report
`file_type::not_found` for the 'a path component is not a directory' case
(`ENOTDIR` / `std::errc::not_a_directory`); it may return `file_type::none`
with `ec` set. That means the legacy fallback may not trigger in the exact
scenario described in the comment (and covered by the unit test for
`<config>/lib` being a file). Update the condition to also treat `ec ==
std::errc::not_a_directory` (and potentially other equivalent not-found-ish
errors you intend to cover) as eligible for legacy fallback.
##########
src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc:
##########
@@ -27,16 +27,165 @@
*/
#include <catch2/catch_test_macros.hpp> /* catch unit-test framework */
-#include <fstream> /* ofstream */
+#include <catch2/generators/catch_generators.hpp>
+#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 <stdexcept>
+#include <unistd.h>
#include <utime.h>
#include <yaml-cpp/yaml.h>
-#include "nexthop_test_stubs.h"
+#include "records/RecCore.h"
+#include "tscore/ink_platform.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"
+
+namespace
+{
+std::filesystem::path
+make_temp_dir(std::string const &name)
+{
+ const auto temp_dir = std::filesystem::temp_directory_path();
+ const auto prefix = name + "-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+
+ for (unsigned int attempt = 0; attempt < 100; ++attempt) {
+ auto dir = temp_dir / (prefix + "-" + std::to_string(attempt));
+ if (std::filesystem::create_directory(dir)) {
+ return dir;
+ }
+ }
+ throw std::runtime_error("unable to create a temporary directory in " +
temp_dir.string());
+}
+
+// Copy the strategy.yaml fixture into a new temporary directory, with its
'#include' replaced by include_target.
+std::filesystem::path
+write_strategy_with_include(std::string const &include_target)
+{
+ const auto dir = make_temp_dir("nh_strategy_include_test");
+ std::ofstream out(dir / "strategy.yaml");
+ std::ifstream in(TS_SRC_DIR "/strategy.yaml");
+
+ if (!in || !out) {
+ throw std::runtime_error("unable to copy " TS_SRC_DIR "/strategy.yaml into
" + dir.string());
+ }
+ out << "#include " << include_target << "\n";
+ for (std::string line; std::getline(in, line);) {
+ if (!line.starts_with("#include")) {
Review Comment:
`std::string::starts_with` requires C++20. Elsewhere in this same file you
use `line.rfind(\"#include\", 0) != 0`, which is compatible with older
standards and is already the established pattern here (see the later
long-include test block). To avoid tightening the language-standard requirement
unexpectedly (or creating inconsistent style), replace `starts_with` usage with
the existing prefix-check approach.
##########
src/proxy/http/remap/unit-tests/test_NextHopStrategyFactory.cc:
##########
@@ -27,16 +27,165 @@
*/
#include <catch2/catch_test_macros.hpp> /* catch unit-test framework */
-#include <fstream> /* ofstream */
+#include <catch2/generators/catch_generators.hpp>
+#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 <stdexcept>
+#include <unistd.h>
#include <utime.h>
#include <yaml-cpp/yaml.h>
-#include "nexthop_test_stubs.h"
+#include "records/RecCore.h"
+#include "tscore/ink_platform.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"
+
+namespace
+{
+std::filesystem::path
+make_temp_dir(std::string const &name)
+{
+ const auto temp_dir = std::filesystem::temp_directory_path();
+ const auto prefix = name + "-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
+
+ for (unsigned int attempt = 0; attempt < 100; ++attempt) {
+ auto dir = temp_dir / (prefix + "-" + std::to_string(attempt));
+ if (std::filesystem::create_directory(dir)) {
+ return dir;
+ }
+ }
+ throw std::runtime_error("unable to create a temporary directory in " +
temp_dir.string());
+}
+
+// Copy the strategy.yaml fixture into a new temporary directory, with its
'#include' replaced by include_target.
+std::filesystem::path
+write_strategy_with_include(std::string const &include_target)
+{
+ const auto dir = make_temp_dir("nh_strategy_include_test");
+ std::ofstream out(dir / "strategy.yaml");
+ std::ifstream in(TS_SRC_DIR "/strategy.yaml");
+
+ if (!in || !out) {
+ throw std::runtime_error("unable to copy " TS_SRC_DIR "/strategy.yaml into
" + dir.string());
+ }
+ out << "#include " << include_target << "\n";
+ for (std::string line; std::getline(in, line);) {
+ if (!line.starts_with("#include")) {
+ out << line << "\n";
+ }
+ }
+ return dir;
+}
Review Comment:
If an exception is thrown after `make_temp_dir()` succeeds (e.g., fixture
read fails, or an unexpected I/O error occurs during copying), the newly
created temp directory is leaked. Consider adding a small RAII helper (or a
`try`/`catch` that calls `std::filesystem::remove_all(dir)` before rethrowing)
so the test cleans up temp artifacts even on failures.
--
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]