]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/librados: extract functions using libcommon in test.cc into test_common.cc
authorKefu Chai <kchai@redhat.com>
Sat, 10 Jun 2017 15:58:47 +0000 (23:58 +0800)
committerJason Dillaman <dillaman@redhat.com>
Fri, 7 Jul 2017 00:16:21 +0000 (20:16 -0400)
Fixes: http://tracker.ceph.com/issues/20175
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 82a848c2053ea69ebc6d3ec1003e18921e2d08d2)

Conflicts:
src/test/librados/CMakeLists.txt: trivial resolution
src/test/librados/test.cc: trivial resolution

src/test/librados/CMakeLists.txt
src/test/librados/list.cc
src/test/librados/test.cc
src/test/librados/test.h
src/test/librados/test_common.cc [new file with mode: 0644]
src/test/librados/test_common.h [new file with mode: 0644]

index aef20ba34b213c43e93b92634fb28cbcc0faad4c..096dee23c7e2e6e807acb1d979a6d513951e2869 100644 (file)
@@ -1,8 +1,12 @@
-# radostest 
-set(libradostest_srcs 
-  test.cc 
+# radostest
+add_library(libradostest_obj OBJECT test.cc)
+set_target_properties(libradostest_obj PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
+set(libradostest_srcs
+  test_common.cc
   TestCase.cc)
-add_library(radostest STATIC ${libradostest_srcs})
+add_library(radostest STATIC
+  ${libradostest_srcs}
+  $<TARGET_OBJECTS:libradostest_obj>)
 target_link_libraries(radostest common json_spirit ${EXTRALIBS})
 set_target_properties(radostest PROPERTIES COMPILE_FLAGS ${UNITTEST_CXX_FLAGS})
 
index e250be34aea8abaf7f072004e18e7d236aeb4aaf..b7d5f886af511054cc5a984057cf5c8800998ac0 100644 (file)
@@ -4,6 +4,7 @@
 #include "include/rados/librados.hpp"
 #include "include/stringify.h"
 #include "test/librados/test.h"
+#include "test/librados/test_common.h"
 #include "test/librados/TestCase.h"
 #include "global/global_context.h"
 
index afb855f3eed6b17194c3d1769d4c37727dacd799..ff991f8c70d39ab5a2e4fb8ef1166f8d72464e47 100644 (file)
@@ -6,9 +6,6 @@
 #include "test/librados/test.h"
 
 #include "include/stringify.h"
-#include "common/Formatter.h"
-#include "json_spirit/json_spirit.h"
-#include "common/errno.h"
 
 #include <errno.h>
 #include <sstream>
@@ -34,97 +31,6 @@ std::string get_temp_pool_name(const std::string &prefix)
   return prefix + out;
 }
 
-int wait_for_healthy(rados_t *cluster)
-{
-  bool healthy = false;
-  // This timeout is very long because the tests are sometimes
-  // run on a thrashing cluster
-  int timeout = 3600;
-  int slept = 0;
-
-  while(!healthy) {
-    JSONFormatter cmd_f;
-    cmd_f.open_object_section("command");
-    cmd_f.dump_string("prefix", "status");
-    cmd_f.dump_string("format", "json");
-    cmd_f.close_section();
-    std::ostringstream cmd_stream;
-    cmd_f.flush(cmd_stream);
-    const std::string serialized_cmd = cmd_stream.str();
-
-    const char *cmd[2];
-    cmd[1] = NULL;
-    cmd[0] = serialized_cmd.c_str();
-
-    char *outbuf = NULL;
-    size_t outlen = 0;
-    int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0,
-        &outbuf, &outlen, NULL, NULL);
-    if (ret) {
-      return ret;
-    }
-
-    std::string out(outbuf, outlen);
-    rados_buffer_free(outbuf);
-
-    json_spirit::mValue root;
-    assert(json_spirit::read(out, root));
-    json_spirit::mObject root_obj = root.get_obj();
-    json_spirit::mObject pgmap = root_obj["pgmap"].get_obj();
-    json_spirit::mArray pgs_by_state = pgmap["pgs_by_state"].get_array();
-
-    if (pgs_by_state.size() == 1) {
-      json_spirit::mObject state = pgs_by_state[0].get_obj();
-      std::string state_name = state["state_name"].get_str();
-      if (state_name != std::string("active+clean")) {
-        healthy = false;
-      } else {
-        healthy = true;
-      }
-
-    } else {
-      healthy = false;
-    }
-
-    if (slept >= timeout) {
-      return -ETIMEDOUT;
-    };
-
-    if (!healthy) {
-      sleep(1);
-      slept += 1;
-    }
-  }
-
-  return 0;
-}
-
-int rados_pool_set(
-    rados_t *cluster,
-    const std::string &pool_name,
-    const std::string &var,
-    const std::string &val)
-{
-  JSONFormatter cmd_f;
-  cmd_f.open_object_section("command");
-  cmd_f.dump_string("prefix", "osd pool set"); 
-  cmd_f.dump_string("pool", pool_name);
-  cmd_f.dump_string("var", var);
-  cmd_f.dump_string("val", val);
-  cmd_f.close_section();
-
-  std::ostringstream cmd_stream;
-  cmd_f.flush(cmd_stream);
-
-  const std::string serialized_cmd = cmd_stream.str();
-
-  const char *cmd[2];
-  cmd[1] = NULL;
-  cmd[0] = serialized_cmd.c_str();
-  int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL,
-      NULL, NULL, NULL);
-  return ret;
-}
 
 std::string create_one_pool(
     const std::string &pool_name, rados_t *cluster, uint32_t pg_num)
@@ -189,35 +95,6 @@ int destroy_ec_profile_and_ruleset(rados_t *cluster,
   return destroy_ruleset(cluster, ruleset, oss);
 }
 
-std::string set_pg_num(
-    rados_t *cluster, const std::string &pool_name, uint32_t pg_num)
-{
-  // Wait for 'creating' to clear
-  int r = wait_for_healthy(cluster);
-  if (r != 0) {
-    goto err;
-  }
-
-  // Adjust pg_num
-  r = rados_pool_set(cluster, pool_name, "pg_num", stringify(pg_num));
-  if (r != 0) {
-    goto err;
-  }
-
-  // Wait for 'creating' to clear
-  r = wait_for_healthy(cluster);
-  if (r != 0) {
-    goto err;
-  }
-
-  return "";
-
-err:
-  rados_shutdown(*cluster);
-  std::ostringstream oss;
-  oss << __func__ << "(" << pool_name << ") failed with error " << r;
-  return oss.str();
-}
 
 std::string create_one_ec_pool(const std::string &pool_name, rados_t *cluster)
 {
@@ -413,7 +290,7 @@ std::string connect_cluster_pp(librados::Rados &cluster,
     if (ret) {
       std::ostringstream oss;
       oss << "failed to set config value " << setting.first << " to '"
-          << setting.second << "': " << cpp_strerror(ret);
+          << setting.second << "': " << strerror(-ret);
       return oss.str();
     }
   }
index 3d249787bf8deb254aa748189e633d3fad37b461..8b505957fd08cfff5821069c3057038a58a4912f 100644 (file)
@@ -34,9 +34,6 @@ std::string create_one_pool_pp(const std::string &pool_name,
                               const std::map<std::string, std::string> &config);
 std::string create_one_ec_pool_pp(const std::string &pool_name,
                            librados::Rados &cluster);
-std::string set_pg_num(
-    rados_t *cluster, const std::string &pool_name, uint32_t pg_num);
-
 std::string connect_cluster(rados_t *cluster);
 std::string connect_cluster_pp(librados::Rados &cluster);
 std::string connect_cluster_pp(librados::Rados &cluster,
diff --git a/src/test/librados/test_common.cc b/src/test/librados/test_common.cc
new file mode 100644 (file)
index 0000000..c276d33
--- /dev/null
@@ -0,0 +1,134 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "common/Formatter.h"
+#include "include/stringify.h"
+#include "json_spirit/json_spirit.h"
+#include "test_common.h"
+
+namespace {
+
+using namespace ceph;
+
+int wait_for_healthy(rados_t *cluster)
+{
+  bool healthy = false;
+  // This timeout is very long because the tests are sometimes
+  // run on a thrashing cluster
+  int timeout = 3600;
+  int slept = 0;
+
+  while(!healthy) {
+    JSONFormatter cmd_f;
+    cmd_f.open_object_section("command");
+    cmd_f.dump_string("prefix", "status");
+    cmd_f.dump_string("format", "json");
+    cmd_f.close_section();
+    std::ostringstream cmd_stream;
+    cmd_f.flush(cmd_stream);
+    const std::string serialized_cmd = cmd_stream.str();
+
+    const char *cmd[2];
+    cmd[1] = NULL;
+    cmd[0] = serialized_cmd.c_str();
+
+    char *outbuf = NULL;
+    size_t outlen = 0;
+    int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0,
+        &outbuf, &outlen, NULL, NULL);
+    if (ret) {
+      return ret;
+    }
+
+    std::string out(outbuf, outlen);
+    rados_buffer_free(outbuf);
+
+    json_spirit::mValue root;
+    assert(json_spirit::read(out, root));
+    json_spirit::mObject root_obj = root.get_obj();
+    json_spirit::mObject pgmap = root_obj["pgmap"].get_obj();
+    json_spirit::mArray pgs_by_state = pgmap["pgs_by_state"].get_array();
+
+    if (pgs_by_state.size() == 1) {
+      json_spirit::mObject state = pgs_by_state[0].get_obj();
+      std::string state_name = state["state_name"].get_str();
+      if (state_name != std::string("active+clean")) {
+        healthy = false;
+      } else {
+        healthy = true;
+      }
+    } else {
+      healthy = false;
+    }
+
+    if (slept >= timeout) {
+      return -ETIMEDOUT;
+    };
+
+    if (!healthy) {
+      sleep(1);
+      slept += 1;
+    }
+  }
+
+  return 0;
+}
+
+int rados_pool_set(
+    rados_t *cluster,
+    const std::string &pool_name,
+    const std::string &var,
+    const std::string &val)
+{
+  JSONFormatter cmd_f;
+  cmd_f.open_object_section("command");
+  cmd_f.dump_string("prefix", "osd pool set");
+  cmd_f.dump_string("pool", pool_name);
+  cmd_f.dump_string("var", var);
+  cmd_f.dump_string("val", val);
+  cmd_f.close_section();
+
+  std::ostringstream cmd_stream;
+  cmd_f.flush(cmd_stream);
+
+  const std::string serialized_cmd = cmd_stream.str();
+
+  const char *cmd[2];
+  cmd[1] = NULL;
+  cmd[0] = serialized_cmd.c_str();
+  int ret = rados_mon_command(*cluster, (const char **)cmd, 1, "", 0, NULL,
+      NULL, NULL, NULL);
+  return ret;
+}
+
+}
+
+std::string set_pg_num(
+    rados_t *cluster, const std::string &pool_name, uint32_t pg_num)
+{
+  // Wait for 'creating' to clear
+  int r = wait_for_healthy(cluster);
+  if (r != 0) {
+    goto err;
+  }
+
+  // Adjust pg_num
+  r = rados_pool_set(cluster, pool_name, "pg_num", stringify(pg_num));
+  if (r != 0) {
+    goto err;
+  }
+
+  // Wait for 'creating' to clear
+  r = wait_for_healthy(cluster);
+  if (r != 0) {
+    goto err;
+  }
+
+  return "";
+
+err:
+  rados_shutdown(*cluster);
+  std::ostringstream oss;
+  oss << __func__ << "(" << pool_name << ") failed with error " << r;
+  return oss.str();
+}
diff --git a/src/test/librados/test_common.h b/src/test/librados/test_common.h
new file mode 100644 (file)
index 0000000..f3f1bdc
--- /dev/null
@@ -0,0 +1,7 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*
+// vim: ts=8 sw=2 smarttab
+
+#include "include/rados/librados.h"
+
+std::string set_pg_num(
+    rados_t *cluster, const std::string &pool_name, uint32_t pg_num);