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 2012/10/27 00:07:11 UTC

svn commit: r1402691 - in /incubator/mesos/trunk/src: Makefile.am tests/environment.cpp tests/environment.hpp tests/main.cpp

Author: benh
Date: Fri Oct 26 22:07:11 2012
New Revision: 1402691

URL: http://svn.apache.org/viewvc?rev=1402691&view=rev
Log:
Added a gtest Environment for doing better setup for our tests.

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

Added:
    incubator/mesos/trunk/src/tests/environment.cpp
    incubator/mesos/trunk/src/tests/environment.hpp
Modified:
    incubator/mesos/trunk/src/Makefile.am
    incubator/mesos/trunk/src/tests/main.cpp

Modified: incubator/mesos/trunk/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/Makefile.am?rev=1402691&r1=1402690&r2=1402691&view=diff
==============================================================================
--- incubator/mesos/trunk/src/Makefile.am (original)
+++ incubator/mesos/trunk/src/Makefile.am Fri Oct 26 22:07:11 2012
@@ -211,7 +211,7 @@ libmesos_no_third_party_la_SOURCES += co
 	slave/paths.hpp slave/state.hpp					\
 	slave/process_based_isolation_module.hpp slave/reaper.hpp	\
 	slave/slave.hpp slave/solaris_project_isolation_module.hpp	\
-	slave/webui.hpp tests/external_test.hpp				\
+	slave/webui.hpp tests/environment.hpp tests/external_test.hpp	\
 	tests/zookeeper_test.hpp tests/utils.hpp			\
 	tests/zookeeper_test_server.hpp zookeeper/authentication.hpp	\
 	zookeeper/group.hpp zookeeper/watcher.hpp			\
@@ -766,6 +766,7 @@ balloon_executor_LDADD = libmesos.la
 check_PROGRAMS += mesos-tests
 
 mesos_tests_SOURCES = tests/main.cpp tests/utils.cpp			\
+	              tests/environment.cpp				\
 	              tests/master_tests.cpp tests/state_tests.cpp	\
 	              tests/slave_state_tests.cpp			\
 	              tests/gc_tests.cpp				\

Added: incubator/mesos/trunk/src/tests/environment.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/environment.cpp?rev=1402691&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/environment.cpp (added)
+++ incubator/mesos/trunk/src/tests/environment.cpp Fri Oct 26 22:07:11 2012
@@ -0,0 +1,123 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include <stout/os.hpp>
+#include <stout/strings.hpp>
+
+#include "configurator/configurator.hpp"
+
+#include "tests/environment.hpp"
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+// Returns true if we should enable a test case or test with the given
+// name. For now, this ONLY disables test cases and tests in two
+// circumstances:
+//   (1) The test case or test contains the string 'ROOT' but the test
+//       is being run via a non-root user.
+//   (2) The test case or test contains the string 'CGROUPS' but
+//       cgroups are not supported on this machine.
+// TODO(benh): Provide a generic way to enable/disable tests by
+// registering "filter" functions (also, make these functions take
+// ::testing::TestCase and ::testing::TestInfo instead of just a
+// "name").
+static bool enable(const std::string& name)
+{
+  if (strings::contains(name, "ROOT") && os::user() != "root") {
+    return false;
+  }
+
+  if (strings::contains(name, "CGROUPS") && !os::exists("/proc/cgroups")) {
+    return false;
+  }
+
+  return true;
+}
+
+
+// Setup special tests by updating the gtest filter (i.e., selectively
+// enable/disable tests based on certain "environmental" criteria,
+// such as whether or not the machine has 'cgroups' support).
+static void setupFilter()
+{
+  // First we split the current filter into positive and negative
+  // components (which are separated by a '-').
+  const std::string& filter = ::testing::GTEST_FLAG(filter);
+  std::string positive;
+  std::string negative;
+
+  size_t dash = filter.find('-');
+  if (dash != std::string::npos) {
+    positive = filter.substr(0, dash);
+    negative = filter.substr(dash + 1);
+  } else {
+    positive = filter;
+  }
+
+  // Use universal filter if not specified.
+  if (positive.empty()) {
+    positive = "*";
+  }
+
+  // Construct the filter string to handle system or platform specific tests.
+  ::testing::UnitTest* unitTest = ::testing::UnitTest::GetInstance();
+  for (int i = 0; i < unitTest->total_test_case_count(); i++) {
+    const ::testing::TestCase* testCase = unitTest->GetTestCase(i);
+    for (int j = 0; j < testCase->total_test_count(); j++) {
+      const ::testing::TestInfo* testInfo = testCase->GetTestInfo(j);
+      const std::string& testCaseName = testInfo->test_case_name();
+      const std::string& testName = testInfo->name();
+      if (!enable(testCaseName)) {
+        negative.append(":" + testCaseName + ".*");
+      } else if (!enable(testName)) {
+        negative.append(":" + testCaseName + "." + testName);
+      }
+    }
+  }
+
+  // Now update the gtest flag.
+  ::testing::GTEST_FLAG(filter) = positive + "-" + negative;
+}
+
+
+void Environment::SetUp()
+{
+  // Clear any MESOS_ environment variables so they don't affect our tests.
+  Configurator::clearMesosEnvironmentVars();
+
+  // Setup specific tests by updating the filter. We do this so that
+  // we can selectively run tests that require root or specific OS
+  // support (e.g., cgroups). Note that this should not effect any
+  // other filters that have been put in place either on the command
+  // line or via an environment variable.
+  setupFilter();
+}
+
+
+void Environment::TearDown() {}
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {
+

Added: incubator/mesos/trunk/src/tests/environment.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/environment.hpp?rev=1402691&view=auto
==============================================================================
--- incubator/mesos/trunk/src/tests/environment.hpp (added)
+++ incubator/mesos/trunk/src/tests/environment.hpp Fri Oct 26 22:07:11 2012
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TESTS_ENVIRONMENT_HPP__
+#define __TESTS_ENVIRONMENT_HPP__
+
+#include <gtest/gtest.h>
+
+namespace mesos {
+namespace internal {
+namespace tests {
+
+// Used to set up our particular test environment.
+class Environment : public ::testing::Environment {
+public:
+  virtual void SetUp();
+  virtual void TearDown();
+};
+
+} // namespace tests {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __TESTS_ENVIRONMENT_HPP__

Modified: incubator/mesos/trunk/src/tests/main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/main.cpp?rev=1402691&r1=1402690&r2=1402691&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/main.cpp (original)
+++ incubator/mesos/trunk/src/tests/main.cpp Fri Oct 26 22:07:11 2012
@@ -30,6 +30,7 @@
 #include "logging/flags.hpp"
 #include "logging/logging.hpp"
 
+#include "tests/environment.hpp"
 #include "tests/utils.hpp"
 
 using namespace mesos::internal;
@@ -40,80 +41,6 @@ using std::endl;
 using std::string;
 
 
-// Return true if the given test or test case can be run. This is an additional
-// check for selectively running tests depending on the runtime system
-// information. For example, if the name of a test contains ROOT (a special
-// name), but the test is run under a non-root user, this function will return
-// false indicating that this test (contains special names) should not be run.
-// Other tests without special names are not affected.
-static bool shouldRun(const std::string& name)
-{
-  if (strings::contains(name, "ROOT") &&
-      os::user() != "root") {
-    return false;
-  }
-
-  // TODO(jieyu): Replace the cgroups check with cgroups::enabled() once the
-  // cgroups module is checked in.
-  if (strings::contains(name, "CGROUPS") &&
-      !os::exists("/proc/cgroups")) {
-    return false;
-  }
-
-  return true;
-}
-
-
-// Setup special tests by updating the gtest filter. This function should be
-// called right before RUN_ALL_TESTS().
-static void setupFilter()
-{
-  // Split the current filter into positive filter and negative filter. The
-  // current filter can be set by the environment variable. In gtest, the filter
-  // is divided into two parts: the positive part and the negative part. The
-  // gtest will choose to run those tests that match the positive filter but do
-  // not match the negative filter. Two parts of the filter are separated by a
-  // dash ('-').
-  const std::string filter = ::testing::GTEST_FLAG(filter);
-  std::string positive;
-  std::string negative;
-
-  size_t dash = filter.find('-');
-  if (dash != std::string::npos) {
-    positive = filter.substr(0, dash);
-    negative = filter.substr(dash + 1);
-  } else {
-    positive = filter;
-  }
-
-  // Use universal filter if not specified.
-  if (positive.empty()) {
-    positive = "*";
-  }
-
-  // Construct the filter string to handle system or platform specific tests.
-  ::testing::UnitTest* unitTest = ::testing::UnitTest::GetInstance();
-  int totalTestCaseCount = unitTest->total_test_case_count();
-  for (int i = 0; i < totalTestCaseCount; i++) {
-    const ::testing::TestCase* testCase = unitTest->GetTestCase(i);
-    int totalTestCount = testCase->total_test_count();
-    for (int j = 0; j < totalTestCount; j++) {
-      const ::testing::TestInfo* testInfo = testCase->GetTestInfo(j);
-      std::string testCaseName = testInfo->test_case_name();
-      std::string testName = testInfo->name();
-      if (!shouldRun(testCaseName)) {
-        negative.append(":" + testCaseName + ".*");
-      } else if (!shouldRun(testName)) {
-        negative.append(":" + testCaseName + "." + testName);
-      }
-    }
-  }
-
-  // Set the gtest flags.
-  ::testing::GTEST_FLAG(filter) = positive + "-" + negative;
-}
-
-
 void usage(const char* argv0, const Configurator& configurator)
 {
   cerr << "Usage: " << os::basename(argv0).get() << " [...]" << endl
@@ -192,15 +119,7 @@ int main(int argc, char** argv)
 
   std::cout << "Build directory: " << mesosBuildDirectory << std::endl;
 
-  // Clear any MESOS_ environment variables so they don't affect our tests.
-  Configurator::clearMesosEnvironmentVars();
-
-  // Setup specific tests by updating the filter. We do this so that
-  // we can selectively run tests that require root or specific OS
-  // support (e.g., cgroups). Note that this should not effect any
-  // other filters that have been put in place either on the command
-  // line or via an environment variable.
-  setupFilter();
+  ::testing::AddGlobalTestEnvironment(new Environment());
 
   return RUN_ALL_TESTS();
 }