[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mesos-commits
Subject:    svn commit: r1351873 - /incubator/mesos/trunk/src/tests/main.cpp
From:       benh () apache ! org
Date:       2012-06-19 21:57:10
Message-ID: 20120619215710.9B0E72388962 () eris ! apache ! org
[Download RAW message or body]

Author: benh
Date: Tue Jun 19 21:57:10 2012
New Revision: 1351873

URL: http://svn.apache.org/viewvc?rev=1351873&view=rev
Log:
Added support for selectively running tests based on runtime system information \
(contributed by Jie Yu, https://reviews.apache.org/r/5393).

Modified:
    incubator/mesos/trunk/src/tests/main.cpp

Modified: incubator/mesos/trunk/src/tests/main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/main.cpp?rev=1351873&r1=1351872&r2=1351873&view=diff
 ==============================================================================
--- incubator/mesos/trunk/src/tests/main.cpp (original)
+++ incubator/mesos/trunk/src/tests/main.cpp Tue Jun 19 21:57:10 2012
@@ -38,6 +38,80 @@ 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") &&
+      utils::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") &&
+      !utils::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: " << utils::os::basename(argv0) << " [...]" << endl
@@ -113,5 +187,12 @@ int main(int argc, char** argv)
   // 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();
+
   return RUN_ALL_TESTS();
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic