From 6bd0b850a06db981b9b149d82c9387db6faea7fb Mon Sep 17 00:00:00 2001 From: Scott Schurr Date: Tue, 10 Apr 2018 12:19:18 -0700 Subject: [PATCH] Fixes for PerfLog unit test in a Docker container --- src/test/basics/PerfLog_test.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/test/basics/PerfLog_test.cpp b/src/test/basics/PerfLog_test.cpp index d6d2a19d29..79c91fa787 100644 --- a/src/test/basics/PerfLog_test.cpp +++ b/src/test/basics/PerfLog_test.cpp @@ -115,7 +115,8 @@ class PerfLog_test : public beast::unit_test::suite // Interfaces for PerfLog file management static path getPerfLogPath() { - return boost::filesystem::current_path () / "perf_log_test_dir"; + using namespace boost::filesystem; + return temp_directory_path() / "perf_log_test_dir"; } static path getPerfLogFileName() @@ -279,15 +280,28 @@ public: if (! BEAST_EXPECT(! ec)) return; - std::ofstream woFile; - woFile.open (fullPath.c_str(), std::ios::out | std::ios::app); - if (! BEAST_EXPECT(woFile)) + auto fileWriteable = [](boost::filesystem::path const& p) -> bool + { + return std::ofstream { + p.c_str(), std::ios::out | std::ios::app}.is_open(); + }; + + if (! BEAST_EXPECT(fileWriteable (fullPath))) return; - woFile.close(); - permissions (fullPath, + + boost::filesystem::permissions (fullPath, perms::remove_perms | perms::owner_write | perms::others_write | perms::group_write); + // If the test is running as root, then the write protect may have + // no effect. Make sure write protect worked before proceeding. + if (fileWriteable (fullPath)) + { + log << "Unable to write protect file. Test skipped." + << std::endl; + return; + } + // Now construct a PerfLog. The PerfLog should attempt to shut // down the server because it can't open its file. BEAST_EXPECT(parent.stopSignaled == false); @@ -302,7 +316,7 @@ public: parent.doStop(); // Fix file permissions so the file can be cleaned up. - permissions (fullPath, + boost::filesystem::permissions (fullPath, perms::add_perms | perms::owner_write | perms::others_write | perms::group_write); }