]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test: add TestSessionFilter 6180/head
authorJohn Spray <john.spray@redhat.com>
Mon, 5 Oct 2015 17:56:56 +0000 (18:56 +0100)
committerJohn Spray <john.spray@redhat.com>
Tue, 6 Oct 2015 13:27:40 +0000 (14:27 +0100)
Tests for SessionFilter, the class filters
allows session listing and eviction in the
new MDS tell commands.

Signed-off-by: John Spray <john.spray@redhat.com>
src/test/CMakeLists.txt
src/test/mds/TestSessionFilter.cc [new file with mode: 0644]

index 2edffcf9075472f7135b68537b88827a0af1a0e5..f8b62922082043d551faaea4bec9d7976674bd20 100644 (file)
@@ -1283,6 +1283,19 @@ target_link_libraries(unittest_mds_authcap mds global ${CMAKE_DL_LIBS}
 set_target_properties(unittest_mds_authcap PROPERTIES COMPILE_FLAGS
   ${UNITTEST_CXX_FLAGS})
 
+# unittest_mds_sessionfilter
+add_executable(unittest_mds_sessionfilter EXCLUDE_FROM_ALL
+    mds/TestSessionFilter.cc
+  $<TARGET_OBJECTS:heap_profiler_objs>
+  $<TARGET_OBJECTS:common_util_obj>
+  )
+add_test(unittest_mds_sessionfilter unittest_mds_sessionfilter)
+add_dependencies(check unittest_mds_sessionfilter)
+target_link_libraries(unittest_mds_sessionfilter mds osdc common global
+  ${CMAKE_DL_LIBS} ${BLKID_LIBRARIES} ${TCMALLOC_LIBS} ${UNITTEST_LIBS})
+set_target_properties(unittest_mds_sessionfilter PROPERTIES COMPILE_FLAGS
+  ${UNITTEST_CXX_FLAGS})
+
 # unittest_ipaddr
 add_executable(unittest_ipaddr EXCLUDE_FROM_ALL
   test_ipaddr.cc)  
diff --git a/src/test/mds/TestSessionFilter.cc b/src/test/mds/TestSessionFilter.cc
new file mode 100644 (file)
index 0000000..2d058da
--- /dev/null
@@ -0,0 +1,162 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2012 Inktank
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include <iostream>
+
+#include "include/stringify.h"
+#include "mds/SessionMap.h"
+#include "common/ceph_argparse.h"
+#include "common/common_init.h"
+#include "global/global_init.h"
+
+#include "gtest/gtest.h"
+
+typedef std::vector<std::string> args_eg;
+typedef std::vector<args_eg> args_eg_set;
+
+TEST(MDSSessionFilter, ParseGood)
+{
+  args_eg_set examples = {
+    {"id=34"},
+    {"auth_name=foxtrot"},
+    {"state=reconnecting"},
+    {"reconnecting=true"},
+    {"client_metadata.root=/foo/bar"},
+    {},
+    {"id=123"},
+    {"id=34", "client_metadata.root=/foo/bar", "auth_name=foxtrot",
+      "state=reconnecting", "reconnecting=true"}
+  };
+
+  for (auto ex : examples) {
+    SessionFilter f;
+    std::stringstream ss;
+
+    std::cout << "Testing '" << ex << "'" << std::endl;
+    int r = f.parse(ex, &ss);
+    ASSERT_EQ(r, 0);
+    ASSERT_TRUE(ss.str().empty());
+  }
+}
+
+TEST(MDSSessionFilter, ParseBad)
+{
+  args_eg_set examples = {
+    {"rhubarb"},
+    {"id="},
+    {"id=custard"},
+    {"=custard"},
+    {"reconnecting=MAYBE"},
+    {"reconnecting=2"}
+  };
+
+  for (auto ex : examples) {
+    SessionFilter f;
+    std::stringstream ss;
+
+    std::cout << "Testing '" << ex << "'" << std::endl;
+    int r = f.parse(ex, &ss);
+    ASSERT_EQ(r, -EINVAL);
+    ASSERT_FALSE(ss.str().empty());
+  }
+}
+
+TEST(MDSSessionFilter, IdEquality)
+{
+  SessionFilter filter;
+  std::stringstream ss;
+  filter.parse({"id=123"}, &ss);
+  Session *a = new Session();;
+  Session *b = new Session();;
+  a->info.inst.name.parse("client.123");
+  b->info.inst.name.parse("client.456");
+
+  ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
+  ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
+  a->put();
+  b->put();
+}
+
+TEST(MDSSessionFilter, StateEquality)
+{
+  SessionFilter filter;
+  std::stringstream ss;
+  filter.parse({"state=closing"}, &ss);
+  Session *a = new Session();
+  a->set_state(Session::STATE_CLOSING);
+  Session *b = new Session();
+  b->set_state(Session::STATE_OPENING);
+
+  ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
+  ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
+  a->put();
+  b->put();
+}
+
+TEST(MDSSessionFilter, AuthEquality)
+{
+  SessionFilter filter;
+  std::stringstream ss;
+  filter.parse({"auth_name=rhubarb"}, &ss);
+  Session *a = new Session();
+  a->info.auth_name.set_id("rhubarb");
+  Session *b = new Session();
+  b->info.auth_name.set_id("custard");
+
+  ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
+  ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
+  a->put();
+  b->put();
+}
+
+TEST(MDSSessionFilter, MetadataEquality)
+{
+  SessionFilter filter;
+  std::stringstream ss;
+  int r = filter.parse({"client_metadata.root=/rhubarb"}, &ss);
+  ASSERT_EQ(r, 0);
+  Session *a = new Session();
+  a->set_client_metadata({{"root", "/rhubarb"}});
+  Session *b = new Session();
+  b->set_client_metadata({{"root", "/custard"}});
+
+  ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return false;}));
+  ASSERT_FALSE(filter.match(*b, [](client_t c) -> bool {return false;}));
+  a->put();
+  b->put();
+}
+
+TEST(MDSSessionFilter, ReconnectingEquality)
+{
+  SessionFilter filter;
+  std::stringstream ss;
+  int r = filter.parse({"reconnecting=true"}, &ss);
+  ASSERT_EQ(r, 0);
+  Session *a = new Session();
+
+  ASSERT_TRUE(filter.match(*a, [](client_t c) -> bool {return true;}));
+  ASSERT_FALSE(filter.match(*a, [](client_t c) -> bool {return false;}));
+  a->put();
+}
+
+int main(int argc, char **argv) {
+  vector<const char*> args;
+  argv_to_vec(argc, (const char **)argv, args);
+
+  global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY, 0); 
+  common_init_finish(g_ceph_context);
+
+  ::testing::InitGoogleTest(&argc, argv);
+  return RUN_ALL_TESTS();
+}