From 3c3089ff7da030d2ea9d954d8135b8584dd8cd4b Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 17 Aug 2011 13:18:05 -0700 Subject: [PATCH] test/rados-api/misc.cc: test tmap_update Signed-off-by: Colin McCabe --- src/test/rados-api/misc.cc | 99 +++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/src/test/rados-api/misc.cc b/src/test/rados-api/misc.cc index a9547207e46b5..a6d77933863b0 100644 --- a/src/test/rados-api/misc.cc +++ b/src/test/rados-api/misc.cc @@ -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 #include "gtest/gtest.h" +#include +#include +#include +#include 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 m; + ::decode(header, p); + ::decode(m, p); + map::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)]; -- 2.39.5