You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2014/05/22 02:55:13 UTC

[2/4] git commit: Added TemporaryDirectoryTest fixture to stout tests.

Added TemporaryDirectoryTest fixture to stout tests.

Review: https://reviews.apache.org/r/21590


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/072bcdd7
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/072bcdd7
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/072bcdd7

Branch: refs/heads/master
Commit: 072bcdd731956abdfa16b23630f4d274c59654b5
Parents: b327bc5
Author: Vinod Kone <vi...@twitter.com>
Authored: Fri May 16 15:16:28 2014 -0700
Committer: Vinod Kone <vi...@twitter.com>
Committed: Wed May 21 17:55:03 2014 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/3rdparty/stout/Makefile.am  |  1 +
 .../3rdparty/stout/tests/os/sendfile_tests.cpp  | 20 +++-------
 .../3rdparty/stout/tests/os_tests.cpp           | 27 ++++---------
 .../libprocess/3rdparty/stout/tests/utils.hpp   | 42 ++++++++++++++++++++
 src/tests/utils.hpp                             |  1 +
 5 files changed, 57 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/072bcdd7/3rdparty/libprocess/3rdparty/stout/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/Makefile.am b/3rdparty/libprocess/3rdparty/stout/Makefile.am
index 81eaaa9..8f32a66 100644
--- a/3rdparty/libprocess/3rdparty/stout/Makefile.am
+++ b/3rdparty/libprocess/3rdparty/stout/Makefile.am
@@ -103,4 +103,5 @@ EXTRA_DIST =					\
   tests/some_tests.cpp				\
   tests/strings_tests.cpp			\
   tests/thread_tests.cpp			\
+  tests/utils.hpp				\
   tests/uuid_tests.cpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/072bcdd7/3rdparty/libprocess/3rdparty/stout/tests/os/sendfile_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os/sendfile_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os/sendfile_tests.cpp
index 194906e..4fe3f22 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os/sendfile_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os/sendfile_tests.cpp
@@ -6,10 +6,11 @@
 #include <stout/os.hpp>
 #include <stout/path.hpp>
 
+#include <stout/tests/utils.hpp>
+
 using std::string;
 
-// TODO(bmahler): Extend from OsTest.
-class OsSendfileTest : public ::testing::Test
+class OsSendfileTest : public TemporaryDirectoryTest
 {
 public:
   OsSendfileTest()
@@ -25,24 +26,15 @@ public:
 protected:
   virtual void SetUp()
   {
-    const Try<string>& mkdtemp = os::mkdtemp();
-    ASSERT_SOME(mkdtemp);
-    tmpdir = mkdtemp.get();
-    filename = path::join(mkdtemp.get(), "lorem.txt");
+    TemporaryDirectoryTest::SetUp();
 
-    ASSERT_SOME(os::write(filename, LOREM_IPSUM));
-  }
+    filename = "lorem.txt";
 
-  virtual void TearDown()
-  {
-    ASSERT_SOME(os::rmdir(tmpdir));
+    ASSERT_SOME(os::write(filename, LOREM_IPSUM));
   }
 
   const string LOREM_IPSUM;
   string filename;
-
-private:
-  string tmpdir;
 };
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/072bcdd7/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
index 76563c2..1a6ce0b 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
@@ -30,6 +30,8 @@
 #include <stout/os/sysctl.hpp>
 #endif
 
+#include <stout/tests/utils.hpp>
+
 using os::Exec;
 using os::Fork;
 using os::Process;
@@ -51,23 +53,7 @@ static hashset<string> listfiles(const string& directory)
 }
 
 
-class OsTest : public ::testing::Test
-{
-protected:
-  virtual void SetUp()
-  {
-    const Try<string>& mkdtemp = os::mkdtemp();
-    ASSERT_SOME(mkdtemp);
-    tmpdir = mkdtemp.get();
-  }
-
-  virtual void TearDown()
-  {
-    ASSERT_SOME(os::rmdir(tmpdir));
-  }
-
-  string tmpdir;
-};
+class OsTest : public TemporaryDirectoryTest {};
 
 
 TEST_F(OsTest, environment)
@@ -96,6 +82,7 @@ TEST_F(OsTest, environment)
 TEST_F(OsTest, rmdir)
 {
   const hashset<string> EMPTY;
+  const string& tmpdir = os::getcwd();
 
   hashset<string> expectedListing = EMPTY;
   EXPECT_EQ(expectedListing, listfiles(tmpdir));
@@ -170,7 +157,7 @@ TEST_F(OsTest, nonblock)
 
 TEST_F(OsTest, touch)
 {
-  const string& testfile  = tmpdir + "/" + UUID::random().toString();
+  const string& testfile  = path::join(os::getcwd(), UUID::random().toString());
 
   ASSERT_SOME(os::touch(testfile));
   ASSERT_TRUE(os::exists(testfile));
@@ -179,7 +166,7 @@ TEST_F(OsTest, touch)
 
 TEST_F(OsTest, readWriteString)
 {
-  const string& testfile  = tmpdir + "/" + UUID::random().toString();
+  const string& testfile  = path::join(os::getcwd(), UUID::random().toString());
   const string& teststr = "line1\nline2";
 
   ASSERT_SOME(os::write(testfile, teststr));
@@ -193,7 +180,7 @@ TEST_F(OsTest, readWriteString)
 
 TEST_F(OsTest, find)
 {
-  const string& testdir = tmpdir + "/" + UUID::random().toString();
+  const string& testdir = path::join(os::getcwd(), UUID::random().toString());
   const string& subdir = testdir + "/test1";
   ASSERT_SOME(os::mkdir(subdir)); // Create the directories.
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/072bcdd7/3rdparty/libprocess/3rdparty/stout/tests/utils.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/utils.hpp b/3rdparty/libprocess/3rdparty/stout/tests/utils.hpp
new file mode 100644
index 0000000..070bc6e
--- /dev/null
+++ b/3rdparty/libprocess/3rdparty/stout/tests/utils.hpp
@@ -0,0 +1,42 @@
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include <stout/gtest.hpp>
+#include <stout/os.hpp>
+#include <stout/try.hpp>
+
+class TemporaryDirectoryTest : public ::testing::Test
+{
+protected:
+  virtual void SetUp()
+  {
+    // Save the current working directory.
+    cwd = os::getcwd();
+
+    // Create a temporary directory for the test.
+    Try<std::string> directory = os::mkdtemp();
+
+    ASSERT_SOME(directory) << "Failed to mkdtemp";
+
+    sandbox = directory.get();
+
+    // Run the test out of the temporary directory we created.
+    ASSERT_TRUE(os::chdir(sandbox.get()))
+      << "Failed to chdir into '" << sandbox.get() << "'";
+  }
+
+  virtual void TearDown()
+  {
+    // Return to previous working directory and cleanup the sandbox.
+    ASSERT_TRUE(os::chdir(cwd));
+
+    if (sandbox.isSome()) {
+      ASSERT_SOME(os::rmdir(sandbox.get()));
+    }
+  }
+
+private:
+  std::string cwd;
+  Option<std::string> sandbox;
+};

http://git-wip-us.apache.org/repos/asf/mesos/blob/072bcdd7/src/tests/utils.hpp
----------------------------------------------------------------------
diff --git a/src/tests/utils.hpp b/src/tests/utils.hpp
index 3b80933..5c86fd4 100644
--- a/src/tests/utils.hpp
+++ b/src/tests/utils.hpp
@@ -30,6 +30,7 @@ namespace internal {
 namespace tests {
 
 // Test fixture for creating a temporary directory for each test.
+// TODO(vinod): Fold this into stout/tests/utils.hpp.
 class TemporaryDirectoryTest : public ::testing::Test
 {
 protected: