You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/05/26 18:57:33 UTC
[08/28] git commit: Moved 'tests::mkdtemp' to Environment.
Moved 'tests::mkdtemp' to Environment.
Review: https://reviews.apache.org/r/11268
Project: http://git-wip-us.apache.org/repos/asf/incubator-mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mesos/commit/65e2fba7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mesos/tree/65e2fba7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mesos/diff/65e2fba7
Branch: refs/heads/master
Commit: 65e2fba73b10368cf5b5e13f273cad0cb8aef223
Parents: 6f6ca87
Author: Benjamin Hindman <be...@twitter.com>
Authored: Sun Apr 28 18:53:58 2013 -0700
Committer: Benjamin Hindman <be...@twitter.com>
Committed: Fri May 24 22:05:05 2013 -0700
----------------------------------------------------------------------
src/tests/environment.cpp | 57 +++++++++++++++++++++++++++++++++++++++-
src/tests/environment.hpp | 24 ++++++++++++++++-
src/tests/main.cpp | 7 ++++-
src/tests/script.cpp | 3 +-
src/tests/utils.cpp | 30 +-------------------
src/tests/utils.hpp | 11 ++-----
6 files changed, 92 insertions(+), 40 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/environment.cpp
----------------------------------------------------------------------
diff --git a/src/tests/environment.cpp b/src/tests/environment.cpp
index 15ef205..2a22acd 100644
--- a/src/tests/environment.cpp
+++ b/src/tests/environment.cpp
@@ -24,6 +24,7 @@
#include <process/gmock.hpp>
#include <process/gtest.hpp>
+#include <stout/error.hpp>
#include <stout/exit.hpp>
#include <stout/os.hpp>
#include <stout/strings.hpp>
@@ -35,6 +36,7 @@
#endif
#include "tests/environment.hpp"
+#include "tests/flags.hpp"
using std::list;
using std::string;
@@ -43,6 +45,10 @@ namespace mesos {
namespace internal {
namespace tests {
+// Storage for the global environment instance.
+Environment* environment;
+
+
// Returns true if we should enable the provided test. Similar to how
// tests can be disabled using the 'DISABLED_' prefix on a test case
// name or test name, we use:
@@ -169,6 +175,18 @@ Environment::Environment()
}
+Environment::~Environment()
+{
+ foreach (const string& directory, directories) {
+ Try<Nothing> rmdir = os::rmdir(directory);
+ if (rmdir.isError()) {
+ LOG(ERROR) << "Failed to remove '" << directory
+ << "': " << rmdir.error();
+ }
+ }
+}
+
+
void Environment::SetUp()
{
// Clear any MESOS_ environment variables so they don't affect our tests.
@@ -177,10 +195,47 @@ void Environment::SetUp()
if (!GTEST_IS_THREADSAFE) {
EXIT(1) << "Testing environment is not thread safe, bailing!";
}
+
+ // For locating killtree.sh.
+ os::setenv("MESOS_SOURCE_DIR", tests::flags.source_dir);
}
-void Environment::TearDown() {}
+void Environment::TearDown()
+{
+ os::unsetenv("MESOS_SOURCE_DIR");
+}
+
+
+Try<string> Environment::mkdtemp()
+{
+ const ::testing::TestInfo* const testInfo =
+ ::testing::UnitTest::GetInstance()->current_test_info();
+
+ if (testInfo == NULL) {
+ return Error("Failed to determine the current test information");
+ }
+
+ // We replace any slashes present in the test names (e.g. TYPED_TEST),
+ // to make sure the temporary directory resides under '/tmp/'.
+ const string& testCase =
+ strings::replace(testInfo->test_case_name(), "/", "_");
+
+ string testName = strings::replace(testInfo->name(), "/", "_");
+
+ // Adjust the test name to remove any 'DISABLED_' prefix (to make
+ // things easier to read). While this might seem alarming, if we are
+ // "running" a disabled test it must be the case that the test was
+ // explicitly enabled (e.g., via 'gtest_filter').
+ if (strings::startsWith(testName, "DISABLED_")) {
+ testName = strings::remove(testName, "DISABLED_", strings::PREFIX);
+ }
+
+ const string& path =
+ path::join("/tmp", strings::join("_", testCase, testName, "XXXXXX"));
+
+ return os::mkdtemp(path);
+}
} // namespace tests {
} // namespace internal {
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/environment.hpp
----------------------------------------------------------------------
diff --git a/src/tests/environment.hpp b/src/tests/environment.hpp
index 61aa84d..691291f 100644
--- a/src/tests/environment.hpp
+++ b/src/tests/environment.hpp
@@ -21,18 +21,40 @@
#include <gtest/gtest.h>
+#include <list>
+#include <string>
+
+#include <stout/try.hpp>
+
namespace mesos {
namespace internal {
namespace tests {
-// Used to set up our particular test environment.
+// Used to set up and manage the test environment.
class Environment : public ::testing::Environment {
public:
Environment();
+ virtual ~Environment();
+
virtual void SetUp();
virtual void TearDown();
+
+ // Helper to create a temporary directory based on the current test
+ // case name and test name (derived from TestInfo via
+ // ::testing::UnitTest::GetInstance()->current_test_info()). Note
+ // that the directory will automagically get removed when the
+ // environment instance gets destructed.
+ Try<std::string> mkdtemp();
+
+private:
+ // Temporary directories that we created and need to remove.
+ std::list<std::string> directories;
};
+
+// Global environment instance.
+extern Environment* environment;
+
} // namespace tests {
} // namespace internal {
} // namespace mesos {
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/main.cpp
----------------------------------------------------------------------
diff --git a/src/tests/main.cpp b/src/tests/main.cpp
index e32ec0c..b06c0d1 100644
--- a/src/tests/main.cpp
+++ b/src/tests/main.cpp
@@ -30,6 +30,7 @@
#include "logging/logging.hpp"
#include "tests/environment.hpp"
+#include "tests/flags.hpp"
#include "tests/utils.hpp"
using namespace mesos::internal;
@@ -98,7 +99,11 @@ int main(int argc, char** argv)
std::cout << "Source directory: " << flags.source_dir << std::endl;
std::cout << "Build directory: " << flags.build_dir << std::endl;
- testing::AddGlobalTestEnvironment(new Environment());
+ // Instantiate our environment. Note that it will be managed by
+ // gtest after we add it via testing::AddGlobalTestEnvironment.
+ environment = new Environment();
+
+ testing::AddGlobalTestEnvironment(environment);
return RUN_ALL_TESTS();
}
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/script.cpp
----------------------------------------------------------------------
diff --git a/src/tests/script.cpp b/src/tests/script.cpp
index d7f103e..e2b4efa 100644
--- a/src/tests/script.cpp
+++ b/src/tests/script.cpp
@@ -28,6 +28,7 @@
#include <stout/path.hpp>
#include <stout/strings.hpp>
+#include "tests/environment.hpp"
#include "tests/script.hpp"
#include "tests/utils.hpp"
@@ -40,7 +41,7 @@ namespace tests {
void execute(const string& script)
{
// Create a temporary directory for the test.
- Try<string> directory = mkdtemp();
+ Try<string> directory = environment->mkdtemp();
CHECK_SOME(directory) << "Failed to create temporary directory";
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/utils.cpp
----------------------------------------------------------------------
diff --git a/src/tests/utils.cpp b/src/tests/utils.cpp
index 0bb1536..9a83053 100644
--- a/src/tests/utils.cpp
+++ b/src/tests/utils.cpp
@@ -26,6 +26,7 @@
#include <stout/path.hpp>
#include <stout/strings.hpp>
+#include "tests/environment.hpp"
#include "tests/flags.hpp"
#include "tests/utils.hpp"
@@ -35,40 +36,13 @@ namespace mesos {
namespace internal {
namespace tests {
-Try<string> mkdtemp()
-{
- const ::testing::TestInfo* const testInfo =
- ::testing::UnitTest::GetInstance()->current_test_info();
-
- // We replace any slashes present in the test names (e.g. TYPED_TEST),
- // to make sure the temporary directory resides under '/tmp/'.
- const string& testCase =
- strings::replace(testInfo->test_case_name(), "/", "_");
-
- string testName = strings::replace(testInfo->name(), "/", "_");
-
- // Adjust the test name to remove any 'DISABLED_' prefix (to make
- // things easier to read). While this might seem alarming, if we are
- // "running" a disabled test it must be the case that the test was
- // explicitly enabled (e.g., via 'gtest_filter').
- if (strings::startsWith(testName, "DISABLED_")) {
- testName = strings::remove(testName, "DISABLED_", strings::PREFIX);
- }
-
- const string& path =
- path::join("/tmp", strings::join("_", testCase, testName, "XXXXXX"));
-
- return os::mkdtemp(path);
-}
-
-
void TemporaryDirectoryTest::SetUp()
{
// Save the current working directory.
cwd = os::getcwd();
// Create a temporary directory for the test.
- Try<string> directory = mkdtemp();
+ Try<string> directory = environment->mkdtemp();
ASSERT_SOME(directory) << "Failed to mkdtemp";
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/65e2fba7/src/tests/utils.hpp
----------------------------------------------------------------------
diff --git a/src/tests/utils.hpp b/src/tests/utils.hpp
index 2e47734..60e4678 100644
--- a/src/tests/utils.hpp
+++ b/src/tests/utils.hpp
@@ -72,6 +72,7 @@
#include "slave/state.hpp"
#include "tests/cluster.hpp"
+#include "tests/environment.hpp"
#include "tests/flags.hpp"
#include "tests/isolator.hpp"
@@ -88,12 +89,6 @@ const static std::string TEST_CGROUPS_ROOT = "mesos_test";
#endif
-// Helper to create a temporary directory based on the current test
-// case name and test name (derived from TestInfo via
-// ::testing::UnitTest::GetInstance()->current_test_info()).
-Try<std::string> mkdtemp();
-
-
// Test fixture for creating a temporary directory for each test.
class TemporaryDirectoryTest : public ::testing::Test
{
@@ -118,7 +113,7 @@ protected:
virtual void SetUp()
{
// Create a temporary directory for the test.
- Try<std::string> directory = mkdtemp();
+ Try<std::string> directory = environment->mkdtemp();
CHECK(directory.isSome())
<< "Failed to create temporary directory: " << directory.error();
@@ -154,7 +149,7 @@ protected:
virtual void SetUp()
{
// Create a temporary directory for the test.
- Try<std::string> directory = mkdtemp();
+ Try<std::string> directory = environment->mkdtemp();
CHECK(directory.isSome())
<< "Failed to create temporary directory: " << directory.error();