You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by bn...@apache.org on 2022/04/14 15:26:22 UTC

[trafficserver] branch 10-Dev updated: test_jsonrpcserver: do not remove files from /tmp (#8794)

This is an automated email from the ASF dual-hosted git repository.

bneradt pushed a commit to branch 10-Dev
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/10-Dev by this push:
     new fc7787da4 test_jsonrpcserver: do not remove files from /tmp (#8794)
fc7787da4 is described below

commit fc7787da49de64b59effd0e4aa6816a81b66f7e1
Author: Brian Neradt <br...@gmail.com>
AuthorDate: Thu Apr 14 10:26:13 2022 -0500

    test_jsonrpcserver: do not remove files from /tmp (#8794)
    
    Some tests create a so-called sandbox directory, use it for the files
    the object under test creates, then remove that temporary sandbox
    directory. To do this they use getTemporaryDir(). The test_jsonrpcserver
    test attempted to do the same thing, but since that function wasn't
    readily available, it used a similarly sounding
    fs::temp_directory_path(). That function, however, just references /tmp.
    As a result, test_jsonrpcserver would store files in /tmp then remove
    /tmp instead of a test-specific temporary directory. This is a problem
    since it would remove other processes' temporary files stored in /tmp
    along with its own test files. This patch fixes this by creating
    getTemporaryDir() for test_jsonrpcserver and calling that instead.
    
    Fixes #8792
---
 mgmt2/rpc/server/unit_tests/test_rpcserver.cc | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/mgmt2/rpc/server/unit_tests/test_rpcserver.cc b/mgmt2/rpc/server/unit_tests/test_rpcserver.cc
index 7a5941faf..381970bce 100644
--- a/mgmt2/rpc/server/unit_tests/test_rpcserver.cc
+++ b/mgmt2/rpc/server/unit_tests/test_rpcserver.cc
@@ -46,6 +46,8 @@
 
 #define DEFINE_JSONRPC_PROTO_FUNCTION(fn) ts::Rv<YAML::Node> fn(std::string_view const &id, const YAML::Node &params)
 
+namespace fs = ts::file;
+
 namespace rpc
 {
 bool
@@ -133,6 +135,20 @@ DEFINE_JSONRPC_PROTO_FUNCTION(some_foo) // id, params
 }
 namespace
 {
+/* Create and return a path to a temporary sandbox directory. */
+fs::path
+getTemporaryDir()
+{
+  std::error_code ec;
+  fs::path tmpDir = fs::canonical(fs::temp_directory_path(), ec);
+  tmpDir /= "sandbox_XXXXXX";
+
+  char dirNameTemplate[tmpDir.string().length() + 1];
+  sprintf(dirNameTemplate, "%s", tmpDir.c_str());
+
+  return fs::path(mkdtemp(dirNameTemplate));
+}
+
 // Handy class to avoid manually disconecting the socket.
 // TODO: should it also connect?
 struct ScopedLocalSocket : shared::rpc::IPCSocketClient {
@@ -501,9 +517,7 @@ TEST_CASE("Test configuration parsing. UDS values", "[string]")
 
 TEST_CASE("Test configuration parsing from a file. UDS Server", "[file]")
 {
-  namespace fs = ts::file;
-
-  fs::path sandboxDir = fs::temp_directory_path();
+  fs::path sandboxDir = getTemporaryDir();
   fs::path configPath = sandboxDir / "jsonrpc.yaml";
 
   // define here to later compare.