You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bb...@apache.org on 2018/09/26 20:58:09 UTC

[mesos] 02/03: Prevented leaking files in some libprocess tests.

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

bbannier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit 556880af6481332cc252514db9438082f85b643b
Author: Benjamin Bannier <bb...@apache.org>
AuthorDate: Thu Sep 20 10:13:49 2018 +0200

    Prevented leaking files in some libprocess tests.
    
    Review: https://reviews.apache.org/r/68817
---
 3rdparty/libprocess/src/tests/io_tests.cpp         |  7 +-
 3rdparty/libprocess/src/tests/process_tests.cpp    | 76 +++++++++++-----------
 3rdparty/libprocess/src/tests/subprocess_tests.cpp | 19 ++----
 3 files changed, 47 insertions(+), 55 deletions(-)

diff --git a/3rdparty/libprocess/src/tests/io_tests.cpp b/3rdparty/libprocess/src/tests/io_tests.cpp
index d1a7463..7e1d514 100644
--- a/3rdparty/libprocess/src/tests/io_tests.cpp
+++ b/3rdparty/libprocess/src/tests/io_tests.cpp
@@ -349,11 +349,10 @@ TEST_F(IOTest, Redirect)
   AWAIT_EXPECT_FAILED(io::redirect(0, -1));
 
   // Create a temporary file for redirecting into.
-  Try<string> path = os::mktemp();
-  ASSERT_SOME(path);
+  string path = path::join(sandbox.get(), "output");
 
   Try<int_fd> fd = os::open(
-      path.get(),
+      path,
       O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
@@ -413,7 +412,7 @@ TEST_F(IOTest, Redirect)
   AWAIT_READY(redirect);
 
   // Now make sure all the data is in the file!
-  Try<string> read = os::read(path.get());
+  Try<string> read = os::read(path);
   ASSERT_SOME(read);
   EXPECT_EQ(data, read.get());
 
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index 2742701..60f3dd6 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -63,6 +63,8 @@
 #include <stout/os/killtree.hpp>
 #include <stout/os/write.hpp>
 
+#include <stout/tests/utils.hpp>
+
 #include "encoder.hpp"
 
 namespace http = process::http;
@@ -110,7 +112,10 @@ using testing::ReturnArg;
 
 // TODO(bmahler): Move tests into their own files as appropriate.
 
-TEST(ProcessTest, Event)
+class ProcessTest : public TemporaryDirectoryTest {};
+
+
+TEST_F(ProcessTest, Event)
 {
   Owned<Event> event(new TerminateEvent(UPID(), false));
   EXPECT_FALSE(event->is<MessageEvent>());
@@ -127,7 +132,7 @@ public:
 };
 
 
-TEST(ProcessTest, Spawn)
+TEST_F(ProcessTest, Spawn)
 {
   SpawnProcess process;
 
@@ -175,7 +180,7 @@ public:
 };
 
 
-TEST(ProcessTest, Dispatch)
+TEST_F(ProcessTest, Dispatch)
 {
   DispatchProcess process;
 
@@ -211,7 +216,7 @@ TEST(ProcessTest, Dispatch)
 }
 
 
-TEST(ProcessTest, Defer1)
+TEST_F(ProcessTest, Defer1)
 {
   DispatchProcess process;
 
@@ -331,7 +336,7 @@ private:
 };
 
 
-TEST(ProcessTest, Defer2)
+TEST_F(ProcessTest, Defer2)
 {
   DeferProcess process;
 
@@ -363,7 +368,7 @@ void set(T* t1, const T& t2)
 }
 
 
-TEST(ProcessTest, Defer3)
+TEST_F(ProcessTest, Defer3)
 {
   std::atomic_bool bool1(false);
   std::atomic_bool bool2(false);
@@ -395,7 +400,7 @@ public:
 };
 
 
-TEST(ProcessTest, Handlers)
+TEST_F(ProcessTest, Handlers)
 {
   HandlersProcess process;
 
@@ -418,7 +423,7 @@ TEST(ProcessTest, Handlers)
 
 // Tests DROP_MESSAGE and DROP_DISPATCH and in particular that an
 // event can get dropped before being processed.
-TEST(ProcessTest, Expect)
+TEST_F(ProcessTest, Expect)
 {
   HandlersProcess process;
 
@@ -447,7 +452,7 @@ TEST(ProcessTest, Expect)
 
 
 // Tests the FutureArg<N> action.
-TEST(ProcessTest, Action)
+TEST_F(ProcessTest, Action)
 {
   HandlersProcess process;
 
@@ -492,7 +497,7 @@ public:
 };
 
 
-TEST(ProcessTest, Inheritance)
+TEST_F(ProcessTest, Inheritance)
 {
   DerivedProcess process;
 
@@ -520,7 +525,7 @@ TEST(ProcessTest, Inheritance)
 }
 
 
-TEST(ProcessTest, Thunk)
+TEST_F(ProcessTest, Thunk)
 {
   struct Thunk
   {
@@ -563,7 +568,7 @@ public:
 };
 
 
-TEST(ProcessTest, Delegate)
+TEST_F(ProcessTest, Delegate)
 {
   DelegateeProcess delegatee;
   DelegatorProcess delegator(delegatee.self());
@@ -594,7 +599,7 @@ public:
 };
 
 
-TEST(ProcessTest, Delay)
+TEST_F(ProcessTest, Delay)
 {
   Clock::pause();
 
@@ -631,7 +636,7 @@ public:
 };
 
 
-TEST(ProcessTest, Order)
+TEST_F(ProcessTest, Order)
 {
   Clock::pause();
 
@@ -684,7 +689,7 @@ public:
 };
 
 
-TEST(ProcessTest, Donate)
+TEST_F(ProcessTest, Donate)
 {
   DonateProcess process;
   spawn(process);
@@ -715,7 +720,7 @@ private:
 };
 
 
-TEST(ProcessTest, Exited)
+TEST_F(ProcessTest, Exited)
 {
   UPID pid = spawn(new ProcessBase(), true);
 
@@ -737,7 +742,7 @@ TEST(ProcessTest, Exited)
 }
 
 
-TEST(ProcessTest, InjectExited)
+TEST_F(ProcessTest, InjectExited)
 {
   UPID pid = spawn(new ProcessBase(), true);
 
@@ -761,7 +766,7 @@ TEST(ProcessTest, InjectExited)
 
 // TODO(bmahler): Move all message interception / dropping tests
 // (of the gmock.hpp functionality) into a separate file.
-TEST(ProcessTest, FutureExited)
+TEST_F(ProcessTest, FutureExited)
 {
   UPID linkee = spawn(new ProcessBase(), true);
   ExitedProcess linker(linkee);
@@ -786,7 +791,7 @@ TEST(ProcessTest, FutureExited)
 
 // TODO(bmahler): Move all message interception / dropping tests
 // (of the gmock.hpp functionality) into a separate file.
-TEST(ProcessTest, DropExited)
+TEST_F(ProcessTest, DropExited)
 {
   UPID linkee = spawn(new ProcessBase(), true);
   ExitedProcess linker(linkee);
@@ -1252,7 +1257,7 @@ public:
 };
 
 
-TEST(ProcessTest, Settle)
+TEST_F(ProcessTest, Settle)
 {
   Clock::pause();
   SettleProcess process;
@@ -1265,7 +1270,7 @@ TEST(ProcessTest, Settle)
 }
 
 
-TEST(ProcessTest, Pid)
+TEST_F(ProcessTest, Pid)
 {
   TimeoutProcess process;
 
@@ -1298,7 +1303,7 @@ public:
 };
 
 
-TEST(ProcessTest, Listener)
+TEST_F(ProcessTest, Listener)
 {
   MultipleListenerProcess process;
 
@@ -1324,7 +1329,7 @@ public:
 };
 
 
-TEST(ProcessTest, Executor_Defer)
+TEST_F(ProcessTest, Executor_Defer)
 {
   EventReceiver receiver;
   Executor executor;
@@ -1363,7 +1368,7 @@ TEST(ProcessTest, Executor_Defer)
 }
 
 
-TEST(ProcessTest, Executor_Execute)
+TEST_F(ProcessTest, Executor_Execute)
 {
   Executor executor;
 
@@ -1431,7 +1436,7 @@ public:
 };
 
 
-TEST(ProcessTest, Remote)
+TEST_F(ProcessTest, Remote)
 {
   RemoteProcess process;
   spawn(process);
@@ -1467,7 +1472,7 @@ TEST(ProcessTest, Remote)
 
 
 // Like the 'remote' test but uses http::connect.
-TEST(ProcessTest, Http1)
+TEST_F(ProcessTest, Http1)
 {
   RemoteProcess process;
   spawn(process);
@@ -1520,7 +1525,7 @@ TEST(ProcessTest, Http1)
 
 // Like 'http1' but uses the 'Libprocess-From' header. We can
 // also use http::post here since we expect a 202 response.
-TEST(ProcessTest, Http2)
+TEST_F(ProcessTest, Http2)
 {
   RemoteProcess process;
   spawn(process);
@@ -1630,7 +1635,7 @@ static string itoa2(int* const& i)
 }
 
 
-TEST(ProcessTest, Async)
+TEST_F(ProcessTest, Async)
 {
   // Non-void functions with different no.of args.
   EXPECT_EQ(1, async(&foo).get());
@@ -1663,11 +1668,8 @@ public:
 };
 
 
-TEST(ProcessTest, Provide)
+TEST_F(ProcessTest, Provide)
 {
-  const Try<string> mkdtemp = os::mkdtemp();
-  ASSERT_SOME(mkdtemp);
-
   const string LOREM_IPSUM =
       "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
       "eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad "
@@ -1677,7 +1679,7 @@ TEST(ProcessTest, Provide)
       "sint occaecat cupidatat non proident, sunt in culpa qui officia "
       "deserunt mollit anim id est laborum.";
 
-  const string path = path::join(mkdtemp.get(), "lorem.txt");
+  const string path = path::join(sandbox.get(), "lorem.txt");
   ASSERT_SOME(os::write(path, LOREM_IPSUM));
 
   FileServer server(path);
@@ -1702,7 +1704,7 @@ static int baz(string s) { return 42; }
 static Future<int> bam(string s) { return 42; }
 
 
-TEST(ProcessTest, Defers)
+TEST_F(ProcessTest, Defers)
 {
   {
     std::function<Future<int>(string)> f =
@@ -1858,7 +1860,7 @@ public:
 };
 
 
-TEST(ProcessTest, PercentEncodedURLs)
+TEST_F(ProcessTest, PercentEncodedURLs)
 {
   PercentEncodedIDProcess process;
   spawn(process);
@@ -1942,7 +1944,7 @@ public:
 
 // Sets firewall rules which disable endpoints on a process and then
 // attempts to connect to those endpoints.
-TEST(ProcessTest, FirewallDisablePaths)
+TEST_F(ProcessTest, FirewallDisablePaths)
 {
   const string id = "testprocess";
 
@@ -2026,7 +2028,7 @@ TEST(ProcessTest, FirewallDisablePaths)
 
 // Test that firewall rules can be changed by changing the vector.
 // An empty vector should allow all paths.
-TEST(ProcessTest, FirewallUninstall)
+TEST_F(ProcessTest, FirewallUninstall)
 {
   const string id = "testprocess";
 
diff --git a/3rdparty/libprocess/src/tests/subprocess_tests.cpp b/3rdparty/libprocess/src/tests/subprocess_tests.cpp
index aa939a6..e6742ec 100644
--- a/3rdparty/libprocess/src/tests/subprocess_tests.cpp
+++ b/3rdparty/libprocess/src/tests/subprocess_tests.cpp
@@ -77,13 +77,10 @@ void run_subprocess(const lambda::function<Try<Subprocess>()>& createSubprocess)
 // a file descriptor for a file, rather than a socket).
 TEST_F(SubprocessTest, PipeOutputToFileDescriptor)
 {
-  Try<string> testdir = os::mkdtemp();
-  ASSERT_SOME(testdir);
-
   // Create temporary files to pipe `stdin` to, and open it. We will pipe
   // output into this file.
   const string outfile_name = "out.txt";
-  const string outfile = path::join(testdir.get(), outfile_name);
+  const string outfile = path::join(sandbox.get(), outfile_name);
   ASSERT_SOME(os::touch(outfile));
 
   Try<int_fd> outfile_fd = os::open(outfile, O_RDWR);
@@ -92,7 +89,7 @@ TEST_F(SubprocessTest, PipeOutputToFileDescriptor)
   // Create temporary files to pipe `stderr` to, and open it. We will pipe
   // error into this file.
   const string errorfile_name = "error.txt";
-  const string errorfile = path::join(testdir.get(), errorfile_name);
+  const string errorfile = path::join(sandbox.get(), errorfile_name);
   ASSERT_SOME(os::touch(errorfile));
 
   Try<int_fd> errorfile_fd = os::open(errorfile, O_RDWR);
@@ -142,15 +139,12 @@ TEST_F(SubprocessTest, PipeOutputToFileDescriptor)
 
 TEST_F(SubprocessTest, PipeOutputToPath)
 {
-  Try<string> testdir = os::mkdtemp();
-  ASSERT_SOME(testdir);
-
   // Name the files to pipe output and error to.
   const string outfile_name = "out.txt";
-  const string outfile = path::join(testdir.get(), outfile_name);
+  const string outfile = path::join(sandbox.get(), outfile_name);
 
   const string errorfile_name = "error.txt";
-  const string errorfile = path::join(testdir.get(), errorfile_name);
+  const string errorfile = path::join(sandbox.get(), errorfile_name);
 
   // Pipe simple string to output file.
   run_subprocess(
@@ -189,12 +183,9 @@ TEST_F(SubprocessTest, PipeOutputToPath)
 
 TEST_F(SubprocessTest, EnvironmentEcho)
 {
-  Try<string> testdir = os::mkdtemp();
-  ASSERT_SOME(testdir);
-
   // Name the file to pipe output to.
   const string outfile_name = "out.txt";
-  const string outfile = path::join(testdir.get(), outfile_name);
+  const string outfile = path::join(sandbox.get(), outfile_name);
 
   // Pipe simple string to output file.
   run_subprocess(