You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/02/05 21:52:29 UTC

mesos git commit: Added unit-test for NetClsHandleManager.

Repository: mesos
Updated Branches:
  refs/heads/master ef2ac3d29 -> db29d426f


Added unit-test for NetClsHandleManager.

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


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

Branch: refs/heads/master
Commit: db29d426f6d476722147f3ca603c7bbdde0cad67
Parents: ef2ac3d
Author: Avinash sridharan <av...@mesosphere.io>
Authored: Fri Feb 5 11:35:29 2016 -0800
Committer: Jie Yu <yu...@gmail.com>
Committed: Fri Feb 5 12:52:28 2016 -0800

----------------------------------------------------------------------
 .../mesos/isolators/cgroups/net_cls.cpp         | 20 ++++++++
 .../mesos/isolators/cgroups/net_cls.hpp         |  3 ++
 src/tests/containerizer/isolator_tests.cpp      | 54 ++++++++++++++++++++
 3 files changed, 77 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/db29d426/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
index bf6c883..f96c327 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.cpp
@@ -165,6 +165,26 @@ Try<Nothing> NetClsHandleManager::free(const NetClsHandle& handle)
 }
 
 
+Try<bool> NetClsHandleManager::isUsed(const NetClsHandle& handle)
+{
+  if (!primaries.contains(handle.primary)) {
+    return Error(
+        "Primary handle: " + hexify(handle.primary) +
+        " is not within the primary's range");
+  }
+
+  if (handle.secondary == 0) {
+    return Error("Secondary handle is 0");
+  }
+
+  if (!used.contains(handle.primary)) {
+    return false;
+  }
+
+  return used[handle.primary].test(handle.secondary);
+}
+
+
 CgroupsNetClsIsolatorProcess::CgroupsNetClsIsolatorProcess(
     const Flags& _flags,
     const string& _hierarchy)

http://git-wip-us.apache.org/repos/asf/mesos/blob/db29d426/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
----------------------------------------------------------------------
diff --git a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
index 85004ae..d8cebd7 100644
--- a/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
+++ b/src/slave/containerizer/mesos/isolators/cgroups/net_cls.hpp
@@ -83,6 +83,9 @@ public:
   Try<Nothing> reserve(const NetClsHandle& handle);
   Try<Nothing> free(const NetClsHandle& handle);
 
+  // Check if a handle is used.
+  Try<bool> isUsed(const NetClsHandle& handle);
+
 private:
   // The key to this hashmap is the 16-bit primary handle.
   hashmap<uint16_t, std::bitset<0x10000>> used;

http://git-wip-us.apache.org/repos/asf/mesos/blob/db29d426/src/tests/containerizer/isolator_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/containerizer/isolator_tests.cpp b/src/tests/containerizer/isolator_tests.cpp
index 67322ab..84fe4fb 100644
--- a/src/tests/containerizer/isolator_tests.cpp
+++ b/src/tests/containerizer/isolator_tests.cpp
@@ -85,6 +85,8 @@ using mesos::internal::slave::CgroupsPerfEventIsolatorProcess;
 using mesos::internal::slave::CPU_SHARES_PER_CPU_REVOCABLE;
 using mesos::internal::slave::Fetcher;
 using mesos::internal::slave::LinuxLauncher;
+using mesos::internal::slave::NetClsHandle;
+using mesos::internal::slave::NetClsHandleManager;
 using mesos::internal::slave::SharedFilesystemIsolatorProcess;
 #endif // __linux__
 using mesos::internal::slave::Launcher;
@@ -369,6 +371,58 @@ TYPED_TEST(CpuIsolatorTest, SystemCpuUsage)
 
 
 #ifdef __linux__
+class NetClsHandleManagerTest : public testing::Test {};
+
+
+// Tests the ability of the `NetClsHandleManager` class to allocate
+// and free secondary handles from a range of primary handles.
+TEST_F(NetClsHandleManagerTest, AllocateFreeHandles)
+{
+  NetClsHandleManager manager(IntervalSet<uint16_t>(
+      (Bound<uint16_t>::closed(0x0002),
+       Bound<uint16_t>::closed(0x0003))));
+
+  Try<NetClsHandle> handle = manager.alloc(0x0003);
+  ASSERT_SOME(handle);
+
+  EXPECT_SOME_TRUE(manager.isUsed(handle.get()));
+
+  ASSERT_SOME(manager.free(handle.get()));
+
+  EXPECT_SOME_FALSE(manager.isUsed(handle.get()));
+}
+
+
+// Make sure allocation of secondary handles for invalid primary
+// handles results in an error.
+TEST_F(NetClsHandleManagerTest, AllocateInvalidPrimary)
+{
+  NetClsHandleManager manager(IntervalSet<uint16_t>(
+      (Bound<uint16_t>::closed(0x0002),
+       Bound<uint16_t>::closed(0x0003))));
+
+  ASSERT_ERROR(manager.alloc(0x0001));
+}
+
+
+// Tests that we can reserve secondary handles for a given primary
+// handle so that they won't be allocated out later.
+TEST_F(NetClsHandleManagerTest, ReserveHandles)
+{
+  NetClsHandleManager manager(IntervalSet<uint16_t>(
+      (Bound<uint16_t>::closed(0x0002),
+       Bound<uint16_t>::closed(0x0003))));
+
+  NetClsHandle handle(0x0003, 0xffff);
+
+  ASSERT_SOME(manager.reserve(handle));
+
+  EXPECT_SOME_TRUE(manager.isUsed(handle));
+}
+#endif // __linux__
+
+
+#ifdef __linux__
 class RevocableCpuIsolatorTest : public MesosTest {};