]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test/rados-api/misc.cc: test tmap_update
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 17 Aug 2011 20:18:05 +0000 (13:18 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 17 Aug 2011 20:18:05 +0000 (13:18 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/test/rados-api/misc.cc

index a9547207e46b57a2d2f7c84bdf79b7f97451ce64..a6d77933863b053ec1c1b7763dde837f7f8c1b67 100644 (file)
@@ -1,11 +1,20 @@
+#include "include/buffer.h"
 #include "include/rados/librados.h"
 #include "include/rados/librados.hpp"
+#include "mds/mdstypes.h"
 #include "test/rados-api/test.h"
 
-#include <errno.h>
 #include "gtest/gtest.h"
+#include <errno.h>
+#include <map>
+#include <sstream>
+#include <string>
 
 using namespace librados;
+using ceph::buffer;
+using std::map;
+using std::ostringstream;
+using std::string;
 
 TEST(LibRadosMisc, Version) {
   int major, minor, extra;
@@ -17,6 +26,94 @@ TEST(LibRadosMisc, VersionPP) {
   Rados::version(&major, &minor, &extra);
 }
 
+static std::string read_key_from_tmap(IoCtx& ioctx, const std::string &obj,
+                                     const std::string &key)
+{
+  bufferlist bl;
+  int r = ioctx.read(obj, bl, 0, 0);
+  if (r <= 0) {
+    ostringstream oss;
+    oss << "ioctx.read(" << obj << ", bl, 0, 0) returned " << r;
+    return oss.str();
+  }
+  bufferlist::iterator p = bl.begin();
+  bufferlist header;
+  map<string, bufferlist> m;
+  ::decode(header, p);
+  ::decode(m, p);
+  map<string, bufferlist>::iterator i = m.find(key);
+  if (i == m.end())
+    return "";
+  std::string retstring;
+  ::decode(retstring, i->second);
+  return retstring;
+}
+
+static std::string add_key_to_tmap(IoCtx &ioctx, const std::string &obj,
+         const std::string &key, const std::string &val)
+{
+  __u8 c = CEPH_OSD_TMAP_SET;
+
+  bufferlist tmbl;
+  ::encode(c, tmbl);
+  ::encode(key, tmbl);
+  bufferlist blbl;
+  ::encode(val, blbl);
+  ::encode(blbl, tmbl);
+  int ret = ioctx.tmap_update(obj, tmbl);
+  if (ret) {
+    ostringstream oss;
+    oss << "ioctx.tmap_update(obj=" << obj << ", key="
+       << key << ", val=" << val << ") failed with error " << ret;
+    return oss.str();
+  }
+  return "";
+}
+
+TEST(LibRadosMisc, TmapUpdatePP) {
+  Rados cluster;
+  std::string pool_name = get_temp_pool_name();
+  ASSERT_EQ("", create_one_pool_pp(pool_name, cluster));
+  IoCtx ioctx;
+  cluster.ioctx_create(pool_name.c_str(), ioctx);
+
+  // create tmap
+  {
+    __u8 c = CEPH_OSD_TMAP_CREATE;
+    std::string my_tmap("my_tmap");
+    bufferlist emptybl;
+
+    bufferlist tmbl;
+    ::encode(c, tmbl);
+    ::encode(my_tmap, tmbl);
+    ::encode(emptybl, tmbl);
+    ASSERT_EQ(0, ioctx.tmap_update("foo", tmbl));
+  }
+
+  ASSERT_EQ(string(""), add_key_to_tmap(ioctx, "foo", "key1", "val1"));
+
+  ASSERT_EQ(string(""), add_key_to_tmap(ioctx, "foo", "key2", "val2"));
+
+  // read key1 from the tmap
+  ASSERT_EQ(string("val1"), read_key_from_tmap(ioctx, "foo", "key1"));
+
+  // remove key1 from tmap
+  {
+    __u8 c = CEPH_OSD_TMAP_RM;
+    std::string key1("key1");
+    bufferlist tmbl;
+    ::encode(c, tmbl);
+    ::encode(key1, tmbl);
+    ASSERT_EQ(0, ioctx.tmap_update("foo", tmbl));
+  }
+
+  // key should be removed
+  ASSERT_EQ(string(""), read_key_from_tmap(ioctx, "foo", "key1"));
+
+  ioctx.close();
+  ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}
+
 //TEST(LibRadosMisc, Exec) {
 //  char buf[128];
 //  char buf2[sizeof(buf)];