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 2013/03/09 00:13:56 UTC

svn commit: r1454620 - in /incubator/mesos/trunk/src: slave/cgroups_isolation_module.cpp slave/flags.hpp tests/cgroups_tests.cpp tests/utils.hpp

Author: vinodkone
Date: Fri Mar  8 23:13:56 2013
New Revision: 1454620

URL: http://svn.apache.org/r1454620
Log:
Refactored cgroups tests and made the root cgroup
used by cgroups isolation module configurable.

From: Vinod Kone <vi...@gmail.com>
Review: https://reviews.apache.org/r/9814

Modified:
    incubator/mesos/trunk/src/slave/cgroups_isolation_module.cpp
    incubator/mesos/trunk/src/slave/flags.hpp
    incubator/mesos/trunk/src/tests/cgroups_tests.cpp
    incubator/mesos/trunk/src/tests/utils.hpp

Modified: incubator/mesos/trunk/src/slave/cgroups_isolation_module.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/slave/cgroups_isolation_module.cpp?rev=1454620&r1=1454619&r2=1454620&view=diff
==============================================================================
--- incubator/mesos/trunk/src/slave/cgroups_isolation_module.cpp (original)
+++ incubator/mesos/trunk/src/slave/cgroups_isolation_module.cpp Fri Mar  8 23:13:56 2013
@@ -221,7 +221,7 @@ void CgroupsIsolationModule::initialize(
   }
 
   // Configure cgroups hierarchy root path.
-  hierarchy = flags.cgroups_hierarchy_root;
+  hierarchy = flags.cgroups_hierarchy;
 
   LOG(INFO) << "Using " << hierarchy << " as cgroups hierarchy root";
 
@@ -269,43 +269,56 @@ void CgroupsIsolationModule::initialize(
       // directory. This will helps us better deal with slave restarts
       // since we won't need to manually remove the directory.
       Try<Nothing> rmdir = os::rmdir(hierarchy, false);
-      CHECK_SOME(rmdir)
-        << "Failed to mount cgroups hierarchy at '" << hierarchy
-        << "' because we could not remove existing directory";
+      if (rmdir.isError()) {
+        EXIT(1) << "Failed to mount cgroups hierarchy at '" << hierarchy
+                << "' because we could not remove existing directory"
+                << ": " << rmdir.error();
+      }
     }
 
     // Mount the cgroups hierarchy.
     Try<Nothing> mount = cgroups::mount(
         hierarchy, strings::join(",", subsystems));
-    CHECK_SOME(mount)
-      << "Failed to mount cgroups hierarchy at '" << hierarchy << "'";
+
+    if (mount.isError()) {
+      EXIT(1) << "Failed to mount cgroups hierarchy at '" << hierarchy
+              << "': " << mount.error();
+    }
   }
 
-  // Create the root "mesos" cgroup if it doesn't exist.
-  Try<bool> exists = cgroups::exists(hierarchy, "mesos");
+  // Create the root cgroup if it doesn't exist.
+  Try<bool> exists = cgroups::exists(hierarchy, flags.cgroups_root);
   CHECK_SOME(exists)
-    << "Failed to determine if the 'mesos' cgroup already exists "
-    << "in the hierarchy at '" << hierarchy << "'";
+    << "Failed to determine if '"<< flags.cgroups_root << "' cgroup "
+    << "already exists in the hierarchy at '" << hierarchy << "'";
+
   if (!exists.get()) {
     // No root cgroup exists, create it.
-    Try<Nothing> create = cgroups::create(hierarchy, "mesos");
-    CHECK_SOME(create) << "Failed to create the 'mesos' cgroup";
+    Try<Nothing> create = cgroups::create(hierarchy, flags.cgroups_root);
+    CHECK_SOME(create)
+      << "Failed to create the '" << flags.cgroups_root << "' cgroup";
   }
 
   // Make sure this kernel supports creating nested cgroups.
-  Try<Nothing> create = cgroups::create(hierarchy, "mesos/test");
+  Try<Nothing> create =
+    cgroups::create(hierarchy, path::join(flags.cgroups_root, "test"));
+
   if (create.isError()) {
     EXIT(1) << "Failed to create a nested 'test' cgroup. Your kernel "
             << "might be too old to use the cgroups isolation module: "
             << create.error();
   }
 
-  Try<Nothing> remove = cgroups::remove(hierarchy, "mesos/test");
+  Try<Nothing> remove =
+    cgroups::remove(hierarchy, path::join(flags.cgroups_root, "test"));
+
   CHECK_SOME(remove) << "Failed to remove the nested 'test' cgroup";
 
   // Try and put an _advisory_ file lock on the tasks' file of our
   // root cgroup to check and see if another slave is already running.
-  Try<int> open = os::open(path::join(hierarchy, "mesos", "tasks"), O_RDONLY);
+  Try<int> open =
+    os::open(path::join(hierarchy, flags.cgroups_root, "tasks"), O_RDONLY);
+
   CHECK_SOME(open);
 
   lockFile = open.get();
@@ -319,8 +332,12 @@ void CgroupsIsolationModule::initialize(
   // should be safe because we've been able to acquire the file lock).
   // TODO(vinod): Wait for all the orphaned cgroups to be destroyed
   // before moving on with the rest of the initialization.
-  Try<vector<string> > cgroups = cgroups::get(hierarchy, "mesos");
-  CHECK_SOME(cgroups) << "Failed to get nested cgroups of 'mesos'";
+  Try<vector<string> > cgroups =
+    cgroups::get(hierarchy, flags.cgroups_root);
+
+  CHECK_SOME(cgroups)
+    << "Failed to get nested cgroups of '" << flags.cgroups_root << "'";
+
   foreach (const string& cgroup, cgroups.get()) {
     LOG(INFO) << "Removing orphaned cgroup '" << cgroup << "'";
     cgroups::destroy(hierarchy, cgroup)
@@ -331,9 +348,12 @@ void CgroupsIsolationModule::initialize(
   }
 
   // Make sure the kernel supports OOM controls.
-  exists = cgroups::exists(hierarchy, "mesos", "memory.oom_control");
+  exists =
+    cgroups::exists(hierarchy, flags.cgroups_root, "memory.oom_control");
+
   CHECK_SOME(exists)
     << "Failed to determine if 'memory.oom_control' control exists";
+
   if (!exists.get()) {
     EXIT(1) << "Failed to find 'memory.oom_control', your kernel "
             << "might be too old to use the cgroups isolation module";
@@ -341,7 +361,8 @@ void CgroupsIsolationModule::initialize(
 
   // Disable the OOM killer so that we can capture 'memory.stat'.
   Try<Nothing> write = cgroups::write(
-      hierarchy, "mesos", "memory.oom_control", "1");
+      hierarchy, flags.cgroups_root, "memory.oom_control", "1");
+
   CHECK_SOME(write) << "Failed to disable OOM killer";
 
   if (subsystems.contains("cpu") && subsystems.contains("cpuset")) {
@@ -361,7 +382,9 @@ void CgroupsIsolationModule::initialize(
     // TODO(bmahler): Consider making a cgroups primitive helper to perform
     // cgroups list format -> list of ints / strings conversion.
     hashset<unsigned int> cgroupCpus;
-    Try<string> cpuset = cgroups::read(hierarchy, "mesos", "cpuset.cpus");
+    Try<string> cpuset =
+      cgroups::read(hierarchy, flags.cgroups_root, "cpuset.cpus");
+
     CHECK_SOME(cpuset) << "Failed to read cpuset.cpus";
     cpuset = strings::trim(cpuset.get());
 
@@ -436,12 +459,15 @@ void CgroupsIsolationModule::finalize()
   CHECK_SOME(lockFile) << "Uninitialized file descriptor!";
   if (flock(lockFile.get(), LOCK_UN) != 0) {
     PLOG(FATAL)
-      << "Failed to unlock '" << path::join(hierarchy, "mesos", "tasks");
+      << "Failed to unlock advisory lock file '"
+      << path::join(hierarchy, flags.cgroups_root, "tasks") << "'";
   }
 
   Try<Nothing> close = os::close(lockFile.get());
   if (close.isError()) {
-    LOG(ERROR) << "Failed to close advisory lock file: " << close.error();
+    LOG(ERROR) << "Failed to close advisory lock file '"
+               << path::join(hierarchy, flags.cgroups_root, "tasks")
+               << "': " << close.error();
   }
 }
 

Modified: incubator/mesos/trunk/src/slave/flags.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/slave/flags.hpp?rev=1454620&r1=1454619&r2=1454620&view=diff
==============================================================================
--- incubator/mesos/trunk/src/slave/flags.hpp (original)
+++ incubator/mesos/trunk/src/slave/flags.hpp Fri Mar  8 23:13:56 2013
@@ -109,11 +109,16 @@ public:
         RESOURCE_MONITORING_INTERVAL);
 
 #ifdef __linux__
-    add(&Flags::cgroups_hierarchy_root,
-        "cgroups_hierarchy_root",
+    add(&Flags::cgroups_hierarchy,
+        "cgroups_hierarchy",
         "The path to the cgroups hierarchy root\n",
         "/cgroup");
 
+    add(&Flags::cgroups_root,
+        "cgroups_root",
+        "Name of the root cgroup\n",
+        "mesos");
+
     add(&Flags::cgroups_subsystems,
         "cgroups_subsystems",
         "List of subsystems to enable (e.g., 'cpu,freezer')\n",
@@ -134,7 +139,8 @@ public:
   Duration disk_watch_interval;
   Duration resource_monitoring_interval;
 #ifdef __linux__
-  std::string cgroups_hierarchy_root;
+  std::string cgroups_hierarchy;
+  std::string cgroups_root;
   std::string cgroups_subsystems;
 #endif
 };

Modified: incubator/mesos/trunk/src/tests/cgroups_tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/cgroups_tests.cpp?rev=1454620&r1=1454619&r2=1454620&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/cgroups_tests.cpp (original)
+++ incubator/mesos/trunk/src/tests/cgroups_tests.cpp Fri Mar  8 23:13:56 2013
@@ -51,150 +51,6 @@ using namespace mesos::internal::tests;
 using namespace process;
 
 
-class CgroupsTest : public ::testing::Test
-{
-protected:
-  static void SetUpTestCase()
-  {
-    TearDownTestCase();
-  }
-
-  static void TearDownTestCase()
-  {
-    // Remove the testing hierarchy.
-    Try<bool> mounted = cgroups::mounted(TEST_HIERARCHY);
-    ASSERT_SOME(mounted);
-    if (mounted.get()) {
-      // Remove all cgroups.
-      Try<std::vector<std::string> > cgroups = cgroups::get(TEST_HIERARCHY);
-      ASSERT_SOME(cgroups);
-      foreach (const std::string& cgroup, cgroups.get()) {
-        ASSERT_SOME(cgroups::remove(TEST_HIERARCHY, cgroup));
-      }
-
-      // Remove the hierarchy.
-      ASSERT_SOME(cgroups::unmount(TEST_HIERARCHY));
-
-      // Remove the directory if still exists.
-      if (os::exists(TEST_HIERARCHY)) {
-        os::rmdir(TEST_HIERARCHY);
-      }
-    }
-  }
-};
-
-
-// A fixture which is used to name tests that expect NO hierarchy to
-// exist in order to test the ability to create a hierarchy (since
-// most likely existing hierarchies will have all or most subsystems
-// attached rendering our ability to create a hierarchy fruitless).
-class CgroupsNoHierarchyTest : public CgroupsTest
-{
-protected:
-  static void SetUpTestCase()
-  {
-    CgroupsTest::SetUpTestCase();
-
-    Try<std::set<std::string> > hierarchies = cgroups::hierarchies();
-    ASSERT_SOME(hierarchies);
-    if (!hierarchies.get().empty()) {
-      std::cerr
-        << "-------------------------------------------------------------\n"
-        << "We cannot run any cgroups tests that require mounting\n"
-        << "hierarchies because you have the following hierarchies mounted:\n"
-        << strings::trim(stringify(hierarchies.get()), " {},") << "\n"
-        << "You can either unmount those hierarchies, or disable\n"
-        << "this test case (i.e., --gtest_filter=-CgroupsNoHierarchyTest.*).\n"
-        << "-------------------------------------------------------------"
-        << std::endl;
-    }
-  }
-};
-
-
-// A fixture that assumes ANY hierarchy is acceptable for use provided
-// it has the subsystems attached that were specified in the
-// constructor. If no hierarchy could be found that has all the
-// required subsystems then we attempt to create a new hierarchy.
-class CgroupsAnyHierarchyTest : public CgroupsTest
-{
-public:
-  CgroupsAnyHierarchyTest(const std::string& _subsystems = "cpu")
-    : subsystems(_subsystems) {}
-
-protected:
-  virtual void SetUp()
-  {
-    Try<std::set<std::string> > hierarchies = cgroups::hierarchies();
-    ASSERT_SOME(hierarchies);
-    foreach (const std::string& candidate, hierarchies.get()) {
-      if (subsystems.empty()) {
-        hierarchy = candidate;
-        break;
-      }
-
-      // Check and see if this candidate meets our subsystem requirements.
-      Try<bool> mounted = cgroups::mounted(candidate, subsystems);
-      ASSERT_SOME(mounted);
-      if (mounted.get()) {
-        hierarchy = candidate;
-        break;
-      }
-    }
-
-    if (hierarchy.empty()) {
-      // Try to mount a hierarchy for testing.
-      ASSERT_SOME(cgroups::mount(TEST_HIERARCHY, subsystems))
-        << "-------------------------------------------------------------\n"
-        << "We cannot run any cgroups tests that require\n"
-        << "a hierarchy with subsystems '" << subsystems << "'\n"
-        << "because we failed to find an existing hierarchy\n"
-        << "or create a new one. You can either remove all existing\n"
-        << "hierarchies, or disable this test case\n"
-        << "(i.e., --gtest_filter=-"
-        << ::testing::UnitTest::GetInstance()
-             ->current_test_info()
-             ->test_case_name() << ".*).\n"
-        << "-------------------------------------------------------------";
-
-      hierarchy = TEST_HIERARCHY;
-    }
-
-    // Create a cgroup (removing first if necessary) for the tests to use.
-    Try<bool> exists = cgroups::exists(hierarchy, "mesos_test");
-    ASSERT_SOME(exists);
-    if (exists.get()) {
-      ASSERT_FUTURE_WILL_SUCCEED(cgroups::destroy(hierarchy, "mesos_test"))
-        << "-------------------------------------------------------------\n"
-        << "We failed to destroy our \"testing\" cgroup (most likely left\n"
-        << "around from a previously failing test). This is a pretty\n"
-        << "serious error, please report a bug!\n"
-        << "-------------------------------------------------------------";
-    }
-
-    ASSERT_SOME(cgroups::create(hierarchy, "mesos_test"));
-  }
-
-  virtual void TearDown()
-  {
-    // Remove all *our* cgroups.
-    Try<std::vector<std::string> > cgroups = cgroups::get(hierarchy);
-    ASSERT_SOME(cgroups);
-    foreach (const std::string& cgroup, cgroups.get()) {
-      if (strings::startsWith(cgroup, "mesos_test")) {
-        ASSERT_SOME(cgroups::remove(hierarchy, cgroup));
-      }
-    }
-
-    // And destroy TEST_HIERARCHY in the event it is needed to be created.
-    CgroupsTest::TearDownTestCase();
-  }
-
-  const std::string subsystems; // Subsystems required to run tests.
-  std::string hierarchy; // Path to the hierarchy being used.
-};
-
-
 class CgroupsAnyHierarchyWithCpuMemoryTest
   : public CgroupsAnyHierarchyTest
 {
@@ -282,11 +138,11 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryT
 TEST_F(CgroupsNoHierarchyTest, ROOT_CGROUPS_MountUnmountHierarchy)
 {
   EXPECT_ERROR(cgroups::mount("/tmp", "cpu"));
-  EXPECT_ERROR(cgroups::mount(TEST_HIERARCHY, "invalid"));
-  ASSERT_SOME(cgroups::mount(TEST_HIERARCHY, "cpu,memory"));
-  EXPECT_ERROR(cgroups::mount(TEST_HIERARCHY, "cpuset"));
+  EXPECT_ERROR(cgroups::mount(TEST_CGROUPS_HIERARCHY, "invalid"));
+  ASSERT_SOME(cgroups::mount(TEST_CGROUPS_HIERARCHY, "cpu,memory"));
+  EXPECT_ERROR(cgroups::mount(TEST_CGROUPS_HIERARCHY, "cpuset"));
   EXPECT_ERROR(cgroups::unmount("/tmp"));
-  ASSERT_SOME(cgroups::unmount(TEST_HIERARCHY));
+  ASSERT_SOME(cgroups::unmount(TEST_CGROUPS_HIERARCHY));
 }
 
 
@@ -333,7 +189,7 @@ TEST_F(CgroupsAnyHierarchyTest, ROOT_CGR
 
   EXPECT_EQ(cgroups.get()[0], "mesos_test2");
   EXPECT_EQ(cgroups.get()[1], "mesos_test1");
-  EXPECT_EQ(cgroups.get()[2], "mesos_test");
+  EXPECT_EQ(cgroups.get()[2], TEST_CGROUPS_ROOT);
 
   ASSERT_SOME(cgroups::remove(hierarchy, "mesos_test1"));
   ASSERT_SOME(cgroups::remove(hierarchy, "mesos_test2"));
@@ -342,7 +198,7 @@ TEST_F(CgroupsAnyHierarchyTest, ROOT_CGR
 
 TEST_F(CgroupsAnyHierarchyTest, ROOT_CGROUPS_NestedCgroups)
 {
-  ASSERT_SOME(cgroups::create(hierarchy, "mesos_test/1"))
+  ASSERT_SOME(cgroups::create(hierarchy, path::join(TEST_CGROUPS_ROOT, "1")))
     << "-------------------------------------------------------------\n"
     << "We cannot run this test because it appears you do not have\n"
     << "a modern enough version of the Linux kernel. You won't be\n"
@@ -350,17 +206,19 @@ TEST_F(CgroupsAnyHierarchyTest, ROOT_CGR
     << "disable this test.\n"
     << "-------------------------------------------------------------";
 
-  ASSERT_SOME(cgroups::create(hierarchy, "mesos_test/2"));
+  ASSERT_SOME(cgroups::create(hierarchy, path::join(TEST_CGROUPS_ROOT, "2")));
+
+  Try<std::vector<std::string> > cgroups =
+    cgroups::get(hierarchy, TEST_CGROUPS_ROOT);
 
-  Try<std::vector<std::string> > cgroups = cgroups::get(hierarchy, "mesos_test");
   ASSERT_SOME(cgroups);
   ASSERT_EQ(2u, cgroups.get().size());
 
-  EXPECT_EQ(cgroups.get()[0], "mesos_test/2");
-  EXPECT_EQ(cgroups.get()[1], "mesos_test/1");
+  EXPECT_EQ(cgroups.get()[0], path::join(TEST_CGROUPS_ROOT, "2"));
+  EXPECT_EQ(cgroups.get()[1], path::join(TEST_CGROUPS_ROOT, "1"));
 
-  ASSERT_SOME(cgroups::remove(hierarchy, "mesos_test/1"));
-  ASSERT_SOME(cgroups::remove(hierarchy, "mesos_test/2"));
+  ASSERT_SOME(cgroups::remove(hierarchy, path::join(TEST_CGROUPS_ROOT, "1")));
+  ASSERT_SOME(cgroups::remove(hierarchy, path::join(TEST_CGROUPS_ROOT, "2")));
 }
 
 
@@ -375,7 +233,7 @@ TEST_F(CgroupsAnyHierarchyTest, ROOT_CGR
 
 TEST_F(CgroupsAnyHierarchyTest, ROOT_CGROUPS_Read)
 {
-  EXPECT_ERROR(cgroups::read(hierarchy, "mesos_test", "invalid"));
+  EXPECT_ERROR(cgroups::read(hierarchy, TEST_CGROUPS_ROOT, "invalid"));
 
   std::string pid = stringify(::getpid());
 
@@ -387,17 +245,18 @@ TEST_F(CgroupsAnyHierarchyTest, ROOT_CGR
 
 TEST_F(CgroupsAnyHierarchyTest, ROOT_CGROUPS_Write)
 {
-  EXPECT_ERROR(cgroups::write(hierarchy, "mesos_test", "invalid", "invalid"));
+  EXPECT_ERROR(
+      cgroups::write(hierarchy, TEST_CGROUPS_ROOT, "invalid", "invalid"));
 
   pid_t pid = ::fork();
   ASSERT_NE(-1, pid);
 
   if (pid) {
     // In parent process.
-    ASSERT_SOME(cgroups::write(
-                    hierarchy, "mesos_test", "tasks", stringify(pid)));
+    ASSERT_SOME(
+        cgroups::write(hierarchy, TEST_CGROUPS_ROOT, "tasks", stringify(pid)));
 
-    Try<std::set<pid_t> > pids = cgroups::tasks(hierarchy, "mesos_test");
+    Try<std::set<pid_t> > pids = cgroups::tasks(hierarchy, TEST_CGROUPS_ROOT);
     ASSERT_SOME(pids);
 
     EXPECT_NE(0u, pids.get().count(pid));
@@ -432,7 +291,7 @@ public:
 
 TEST_F(CgroupsAnyHierarchyWithCpuAcctMemoryTest, ROOT_CGROUPS_Stat)
 {
-  EXPECT_ERROR(cgroups::stat(hierarchy, "mesos_test", "invalid"));
+  EXPECT_ERROR(cgroups::stat(hierarchy, TEST_CGROUPS_ROOT, "invalid"));
 
   Try<hashmap<std::string, uint64_t> > result =
     cgroups::stat(hierarchy, "/", "cpuacct.stat");
@@ -451,7 +310,8 @@ TEST_F(CgroupsAnyHierarchyWithCpuAcctMem
 
 TEST_F(CgroupsAnyHierarchyWithCpuMemoryTest, ROOT_CGROUPS_Listen)
 {
-  ASSERT_SOME(cgroups::exists(hierarchy, "mesos_test", "memory.oom_control"))
+  ASSERT_SOME(
+      cgroups::exists(hierarchy, TEST_CGROUPS_ROOT, "memory.oom_control"))
     << "-------------------------------------------------------------\n"
     << "We cannot run this test because it appears you do not have\n"
     << "a modern enough version of the Linux kernel. You won't be\n"
@@ -460,30 +320,27 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryT
     << "-------------------------------------------------------------";
 
   // Disable oom killer.
-  ASSERT_SOME(cgroups::write(
-                  hierarchy,
-                  "mesos_test",
-                  "memory.oom_control",
-                  "1"));
+  ASSERT_SOME(
+      cgroups::write(hierarchy, TEST_CGROUPS_ROOT, "memory.oom_control", "1"));
 
-  // Limit the memory usage of "mesos_test" to 64MB.
+  // Limit the memory usage of the test cgroup to 64MB.
   size_t limit = 1024 * 1024 * 64;
   ASSERT_SOME(cgroups::write(
                   hierarchy,
-                  "mesos_test",
+                  TEST_CGROUPS_ROOT,
                   "memory.limit_in_bytes",
                   stringify(limit)));
 
-  // Listen on oom events for "mesos_test" cgroup.
+  // Listen on oom events for test cgroup.
   Future<uint64_t> future = cgroups::listen(
-      hierarchy, "mesos_test", "memory.oom_control");
+      hierarchy, TEST_CGROUPS_ROOT, "memory.oom_control");
   ASSERT_FALSE(future.isFailed());
 
   // Test the cancellation.
   future.discard();
 
   // Test the normal operation below.
-  future = cgroups::listen(hierarchy, "mesos_test", "memory.oom_control");
+  future = cgroups::listen(hierarchy, TEST_CGROUPS_ROOT, "memory.oom_control");
   ASSERT_FALSE(future.isFailed());
 
   pid_t pid = ::fork();
@@ -505,8 +362,10 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryT
     EXPECT_EQ(SIGKILL, WTERMSIG(status));
   } else {
     // In child process. We try to trigger an oom here.
-    // Put self into the "mesos_test" cgroup.
-    Try<Nothing> assign = cgroups::assign(hierarchy, "mesos_test", ::getpid());
+    // Put self into the test cgroup.
+    Try<Nothing> assign =
+      cgroups::assign(hierarchy, TEST_CGROUPS_ROOT, ::getpid());
+
     if (assign.isError()) {
       std::cerr << "Failed to assign cgroup: " << assign.error() << std::endl;
       abort();
@@ -557,14 +416,14 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     ASSERT_LT(0, ::read(pipes[0], &dummy, sizeof(dummy)));
     ::close(pipes[0]);
 
-    // Freeze the "mesos_test" cgroup.
-    Future<bool> freeze = cgroups::freeze(hierarchy, "mesos_test");
+    // Freeze the test cgroup.
+    Future<bool> freeze = cgroups::freeze(hierarchy, TEST_CGROUPS_ROOT);
     freeze.await(Seconds(5.0));
     ASSERT_TRUE(freeze.isReady());
     EXPECT_EQ(true, freeze.get());
 
-    // Thaw the "mesos_test" cgroup.
-    Future<bool> thaw = cgroups::thaw(hierarchy, "mesos_test");
+    // Thaw the test cgroup.
+    Future<bool> thaw = cgroups::thaw(hierarchy, TEST_CGROUPS_ROOT);
     thaw.await(Seconds(5.0));
     ASSERT_TRUE(thaw.isReady());
     EXPECT_EQ(true, thaw.get());
@@ -581,8 +440,10 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     // In child process.
     ::close(pipes[0]);
 
-    // Put self into the "mesos_test" cgroup.
-    Try<Nothing> assign = cgroups::assign(hierarchy, "mesos_test", ::getpid());
+    // Put self into the test cgroup.
+    Try<Nothing> assign =
+      cgroups::assign(hierarchy, TEST_CGROUPS_ROOT, ::getpid());
+
     if (assign.isError()) {
       std::cerr << "Failed to assign cgroup: " << assign.error() << std::endl;
       abort();
@@ -625,7 +486,7 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     ASSERT_LT(0, ::read(pipes[0], &dummy, sizeof(dummy)));
     ::close(pipes[0]);
 
-    Try<Nothing> kill = cgroups::kill(hierarchy, "mesos_test", SIGKILL);
+    Try<Nothing> kill = cgroups::kill(hierarchy, TEST_CGROUPS_ROOT, SIGKILL);
     EXPECT_SOME(kill);
 
     int status;
@@ -640,8 +501,10 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     ::fork();
     ::fork();
 
-    // Put self into "mesos_test" cgroup.
-    Try<Nothing> assign = cgroups::assign(hierarchy, "mesos_test", ::getpid());
+    // Put self into the test cgroup.
+    Try<Nothing> assign =
+      cgroups::assign(hierarchy, TEST_CGROUPS_ROOT, ::getpid());
+
     if (assign.isError()) {
       std::cerr << "Failed to assign cgroup: " << assign.error() << std::endl;
       abort();
@@ -686,7 +549,7 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     ASSERT_LT(0, ::read(pipes[0], &dummy, sizeof(dummy)));
     ::close(pipes[0]);
 
-    Future<bool> future = cgroups::destroy(hierarchy, "mesos_test");
+    Future<bool> future = cgroups::destroy(hierarchy, TEST_CGROUPS_ROOT);
     future.await(Seconds(5.0));
     ASSERT_TRUE(future.isReady());
     EXPECT_TRUE(future.get());
@@ -703,8 +566,10 @@ TEST_F(CgroupsAnyHierarchyWithCpuMemoryF
     ::fork();
     ::fork();
 
-    // Put self into "mesos_test" cgroup.
-    Try<Nothing> assign = cgroups::assign(hierarchy, "mesos_test", ::getpid());
+    // Put self into the test cgroup.
+    Try<Nothing> assign =
+      cgroups::assign(hierarchy, TEST_CGROUPS_ROOT, ::getpid());
+
     if (assign.isError()) {
       std::cerr << "Failed to assign cgroup: " << assign.error() << std::endl;
       abort();

Modified: incubator/mesos/trunk/src/tests/utils.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/utils.hpp?rev=1454620&r1=1454619&r2=1454620&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/utils.hpp (original)
+++ incubator/mesos/trunk/src/tests/utils.hpp Fri Mar  8 23:13:56 2013
@@ -46,6 +46,10 @@
 #include "common/resources.hpp"
 #include "common/type_utils.hpp"
 
+#ifdef __linux__
+#include "linux/cgroups.hpp"
+#endif
+
 #include "logging/logging.hpp"
 
 #include "master/allocator.hpp"
@@ -75,7 +79,10 @@ extern flags::Flags<logging::Flags, Flag
 
 #ifdef __linux__
 // Cgroups hierarchy used by the cgroups related tests.
-const static std::string TEST_HIERARCHY = "/tmp/mesos_test_cgroup";
+const static std::string TEST_CGROUPS_HIERARCHY = "/tmp/mesos_test_cgroup";
+
+// Name of the root cgroup used by the cgroups related tests.
+const static std::string TEST_CGROUPS_ROOT = "mesos_test";
 #endif
 
 
@@ -114,8 +121,11 @@ protected:
       << "Failed to create temporary directory: " << directory.error();
 
     slaveFlags.work_dir = directory.get();
-
     slaveFlags.launcher_dir = path::join(tests::flags.build_dir, "src");
+#ifdef __linux__
+    slaveFlags.cgroups_hierarchy = TEST_CGROUPS_HIERARCHY;
+    slaveFlags.cgroups_root = TEST_CGROUPS_ROOT;
+#endif
 
     // For locating killtree.sh.
     os::setenv("MESOS_SOURCE_DIR",tests::flags.source_dir);
@@ -139,6 +149,158 @@ protected:
 };
 
 
+#ifdef __linux__
+class CgroupsTest : public MesosTest
+{
+protected:
+  static void SetUpTestCase()
+  {
+    // Clean up the testing hierarchy, in case it wasn't cleaned up
+    // properly from previous tests.
+    TearDownTestCase();
+  }
+
+  static void TearDownTestCase()
+  {
+    // Remove the testing hierarchy.
+    Try<bool> mounted = cgroups::mounted(TEST_CGROUPS_HIERARCHY);
+    ASSERT_SOME(mounted);
+    if (mounted.get()) {
+      // Remove all cgroups.
+      Try<std::vector<std::string> > cgroups =
+        cgroups::get(TEST_CGROUPS_HIERARCHY);
+
+      ASSERT_SOME(cgroups);
+      foreach (const std::string& cgroup, cgroups.get()) {
+        ASSERT_FUTURE_WILL_SUCCEED(
+            cgroups::destroy(TEST_CGROUPS_HIERARCHY, cgroup));
+      }
+
+      // Remove the hierarchy.
+      ASSERT_SOME(cgroups::unmount(TEST_CGROUPS_HIERARCHY));
+
+      // Remove the directory if still exists.
+      if (os::exists(TEST_CGROUPS_HIERARCHY)) {
+        os::rmdir(TEST_CGROUPS_HIERARCHY);
+      }
+    }
+  }
+};
+
+
+// A fixture which is used to name tests that expect NO hierarchy to
+// exist in order to test the ability to create a hierarchy (since
+// most likely existing hierarchies will have all or most subsystems
+// attached rendering our ability to create a hierarchy fruitless).
+class CgroupsNoHierarchyTest : public CgroupsTest
+{
+protected:
+  static void SetUpTestCase()
+  {
+    CgroupsTest::SetUpTestCase();
+
+    Try<std::set<std::string> > hierarchies = cgroups::hierarchies();
+    ASSERT_SOME(hierarchies);
+    if (!hierarchies.get().empty()) {
+      std::cerr
+        << "-------------------------------------------------------------\n"
+        << "We cannot run any cgroups tests that require mounting\n"
+        << "hierarchies because you have the following hierarchies mounted:\n"
+        << strings::trim(stringify(hierarchies.get()), " {},") << "\n"
+        << "You can either unmount those hierarchies, or disable\n"
+        << "this test case (i.e., --gtest_filter=-CgroupsNoHierarchyTest.*).\n"
+        << "-------------------------------------------------------------"
+        << std::endl;
+    }
+  }
+};
+
+
+// A fixture that assumes ANY hierarchy is acceptable for use provided
+// it has the subsystems attached that were specified in the
+// constructor. If no hierarchy could be found that has all the
+// required subsystems then we attempt to create a new hierarchy.
+class CgroupsAnyHierarchyTest : public CgroupsTest
+{
+public:
+  CgroupsAnyHierarchyTest(const std::string& _subsystems = "cpu")
+    : subsystems(_subsystems) {}
+
+protected:
+  virtual void SetUp()
+  {
+    Try<std::set<std::string> > hierarchies = cgroups::hierarchies();
+    ASSERT_SOME(hierarchies);
+    foreach (const std::string& candidate, hierarchies.get()) {
+      if (subsystems.empty()) {
+        hierarchy = candidate;
+        break;
+      }
+
+      // Check and see if this candidate meets our subsystem requirements.
+      Try<bool> mounted = cgroups::mounted(candidate, subsystems);
+      ASSERT_SOME(mounted);
+      if (mounted.get()) {
+        hierarchy = candidate;
+        break;
+      }
+    }
+
+    if (hierarchy.empty()) {
+      // Try to mount a hierarchy for testing.
+      ASSERT_SOME(cgroups::mount(TEST_CGROUPS_HIERARCHY, subsystems))
+        << "-------------------------------------------------------------\n"
+        << "We cannot run any cgroups tests that require\n"
+        << "a hierarchy with subsystems '" << subsystems << "'\n"
+        << "because we failed to find an existing hierarchy\n"
+        << "or create a new one. You can either remove all existing\n"
+        << "hierarchies, or disable this test case\n"
+        << "(i.e., --gtest_filter=-"
+        << ::testing::UnitTest::GetInstance()
+             ->current_test_info()
+             ->test_case_name() << ".*).\n"
+        << "-------------------------------------------------------------";
+
+      hierarchy = TEST_CGROUPS_HIERARCHY;
+    }
+
+    // Create a cgroup (removing first if necessary) for the tests to use.
+    Try<bool> exists = cgroups::exists(hierarchy, TEST_CGROUPS_ROOT);
+    ASSERT_SOME(exists);
+    if (exists.get()) {
+      ASSERT_FUTURE_WILL_SUCCEED(cgroups::destroy(hierarchy, TEST_CGROUPS_ROOT))
+        << "-------------------------------------------------------------\n"
+        << "We failed to destroy our \"testing\" cgroup (most likely left\n"
+        << "around from a previously failing test). This is a pretty\n"
+        << "serious error, please report a bug!\n"
+        << "-------------------------------------------------------------";
+    }
+
+    ASSERT_SOME(cgroups::create(hierarchy, TEST_CGROUPS_ROOT));
+  }
+
+  virtual void TearDown()
+  {
+    // Remove all *our* cgroups.
+    Try<std::vector<std::string> > cgroups = cgroups::get(hierarchy);
+    ASSERT_SOME(cgroups);
+    foreach (const std::string& cgroup, cgroups.get()) {
+      if (strings::startsWith(cgroup, TEST_CGROUPS_ROOT)) {
+        ASSERT_FUTURE_WILL_SUCCEED(cgroups::destroy(hierarchy, cgroup));
+      }
+    }
+
+    // And destroy TEST_CGROUPS_HIERARCHY in the event it is needed
+    // to be created.
+    CgroupsTest::TearDownTestCase();
+  }
+
+  const std::string subsystems; // Subsystems required to run tests.
+  std::string hierarchy; // Path to the hierarchy being used.
+};
+#endif
+
+
 // Macros to get/create (default) ExecutorInfos and FrameworkInfos.
 #define DEFAULT_EXECUTOR_INFO                                           \
       ({ ExecutorInfo executor;                                         \