traeak commented on code in PR #13853:
URL: https://github.com/apache/trafficserver/pull/13853#discussion_r4569016479


##########
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()` reports `file_type::not_found` for both `ENOENT` 
and `ENOTDIR` in libstdc++ and libc++. The `lib/hosts.yaml` case in the unit 
test covers the not-a-directory lookup and passes on the Linux and macOS CI 
builds.



##########
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:
   This is intended. The warning is per deprecated `#include` line and names 
the file holding it, so a second file that includes the same target still gets 
reported and can be fixed.



##########
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:
   The project builds as C++20, so `starts_with` is fine. For consistency the 
remaining `rfind` prefix check in this file is switched to `starts_with` in the 
next push.



##########
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:
   Leaving this as is. The throw only happens when copying the fixture fails, 
which already fails the test run, and the directory is an empty one under the 
system temp directory.



-- 
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]

Reply via email to