summaryrefslogtreecommitdiffstats
path: root/src/test/cls_numops
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/test/cls_numops
parentInitial commit. (diff)
downloadceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz
ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/cls_numops')
-rw-r--r--src/test/cls_numops/CMakeLists.txt16
-rw-r--r--src/test/cls_numops/test_cls_numops.cc414
2 files changed, 430 insertions, 0 deletions
diff --git a/src/test/cls_numops/CMakeLists.txt b/src/test/cls_numops/CMakeLists.txt
new file mode 100644
index 000000000..827ff606f
--- /dev/null
+++ b/src/test/cls_numops/CMakeLists.txt
@@ -0,0 +1,16 @@
+# ceph_test_cls_numops
+add_executable(ceph_test_cls_numops
+ test_cls_numops.cc)
+target_link_libraries(ceph_test_cls_numops
+ librados
+ global
+ cls_numops_client
+ ${EXTRALIBS}
+ ${BLKID_LIBRARIES}
+ ${CMAKE_DL_LIBS}
+ radostest-cxx
+ ${UNITTEST_LIBS}
+ )
+install(TARGETS
+ ceph_test_cls_numops
+ DESTINATION ${CMAKE_INSTALL_BINDIR})
diff --git a/src/test/cls_numops/test_cls_numops.cc b/src/test/cls_numops/test_cls_numops.cc
new file mode 100644
index 000000000..ebb225620
--- /dev/null
+++ b/src/test/cls_numops/test_cls_numops.cc
@@ -0,0 +1,414 @@
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2015 CERN
+ *
+ * Author: Joaquim Rocha <joaquim.rocha@cern.ch>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <iostream>
+#include <errno.h>
+#include <set>
+#include <sstream>
+#include <string>
+
+#include "cls/numops/cls_numops_client.h"
+#include "gtest/gtest.h"
+#include "include/rados/librados.hpp"
+#include "test/librados/test_cxx.h"
+
+using namespace librados;
+
+TEST(ClsNumOps, Add) {
+ 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);
+
+ // exec numops add method with an empty bufferlist
+
+ bufferlist in, out;
+
+ ASSERT_EQ(-EINVAL, ioctx.exec("myobject", "numops", "add", in, out));
+
+ // add a number to a non-existing key
+
+ std::string key = "my-key";
+ double value_in = 0.5;
+
+ std::stringstream stream;
+ stream << value_in;
+
+ ASSERT_EQ(0, rados::cls::numops::add(&ioctx, "myobject", key, value_in));
+
+ // check that the omap entry was set and the value matches
+
+ std::set<std::string> keys;
+ std::map<std::string, bufferlist> omap;
+ keys.insert(key);
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ std::map<std::string, bufferlist>::iterator it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bufferlist bl = (*it).second;
+ std::string value_out(bl.c_str(), bl.length());
+
+ EXPECT_EQ(stream.str(), value_out);
+
+ // add another value to the existing one
+
+ double new_value_in = 3.001;
+
+ ASSERT_EQ(0, rados::cls::numops::add(&ioctx, "myobject", key, new_value_in));
+
+ // check that the omap entry's value matches
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ stream.str("");
+ stream << (value_in + new_value_in);
+
+ EXPECT_EQ(stream.str(), value_out);
+
+ // set the omap entry with some non-numeric value
+
+ omap.clear();
+
+ std::string non_numeric_value("some-non-numeric-text");
+ omap[key].append(non_numeric_value);
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // check that adding a number does not succeed
+
+ omap.clear();
+
+ ASSERT_EQ(-EBADMSG, rados::cls::numops::add(&ioctx, "myobject", key, 2.0));
+
+ // check that the omap entry was not changed
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ EXPECT_EQ(non_numeric_value, value_out);
+
+ ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}
+
+TEST(ClsNumOps, Sub) {
+ 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);
+
+ // subtract a number from a non-existing key
+
+ std::string key = "my-key";
+ double value_in = 0.5;
+
+ std::stringstream stream;
+ stream << value_in;
+
+ ASSERT_EQ(0, rados::cls::numops::sub(&ioctx, "myobject", key, value_in));
+
+ // check that the omap entry was set and the value matches
+
+ std::set<std::string> keys;
+ std::map<std::string, bufferlist> omap;
+ keys.insert(key);
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ std::map<std::string, bufferlist>::iterator it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bufferlist bl = (*it).second;
+ std::string value_out(bl.c_str(), bl.length());
+
+ EXPECT_EQ("-" + stream.str(), value_out);
+
+ // subtract another value to the existing one
+
+ double new_value_in = 3.001;
+
+ ASSERT_EQ(0, rados::cls::numops::sub(&ioctx, "myobject", key, new_value_in));
+
+ // check that the omap entry's value matches
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ stream.str("");
+ stream << -(value_in + new_value_in);
+
+ EXPECT_EQ(stream.str(), value_out);
+
+ // set the omap entry with some non-numeric value
+
+ omap.clear();
+
+ std::string non_numeric_value("some-non-numeric-text");
+ omap[key].append(non_numeric_value);
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // check that subtracting a number does not succeed
+
+ omap.clear();
+
+ ASSERT_EQ(-EBADMSG, rados::cls::numops::sub(&ioctx, "myobject", key, 2.0));
+
+ // check that the omap entry was not changed
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ EXPECT_EQ(non_numeric_value, value_out);
+
+ ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}
+
+TEST(ClsNumOps, Mul) {
+ 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);
+
+ // exec numops mul method with an empty bufferlist
+
+ bufferlist in, out;
+
+ ASSERT_EQ(-EINVAL, ioctx.exec("myobject", "numops", "mul", in, out));
+
+ // multiply a number to a non-existing key
+
+ std::string key = "my-key";
+ double value_in = 0.5;
+
+ std::stringstream stream;
+ stream << value_in;
+
+ ASSERT_EQ(0, rados::cls::numops::mul(&ioctx, "myobject", key, value_in));
+
+ // check that the omap entry was set and the value is zero
+
+ std::set<std::string> keys;
+ std::map<std::string, bufferlist> omap;
+ keys.insert(key);
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ std::map<std::string, bufferlist>::iterator it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bufferlist bl = (*it).second;
+ std::string value_out(bl.c_str(), bl.length());
+
+ EXPECT_EQ("0", value_out);
+
+ // set a non-zero value so we can effectively test multiplications
+
+ omap.clear();
+
+ omap[key].append(stream.str());
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // multiply another value to the existing one
+
+ double new_value_in = 3.001;
+
+ ASSERT_EQ(0, rados::cls::numops::mul(&ioctx, "myobject", key, new_value_in));
+
+ // check that the omap entry's value matches
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ stream.str("");
+ stream << (value_in * new_value_in);
+
+ EXPECT_EQ(stream.str(), value_out);
+
+ // set the omap entry with some non-numeric value
+
+ omap.clear();
+
+ std::string non_numeric_value("some-non-numeric-text");
+ omap[key].append(non_numeric_value);
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // check that adding a number does not succeed
+
+ ASSERT_EQ(-EBADMSG, rados::cls::numops::mul(&ioctx, "myobject", key, 2.0));
+
+ // check that the omap entry was not changed
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ EXPECT_EQ(non_numeric_value, value_out);
+
+ ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}
+
+TEST(ClsNumOps, Div) {
+ 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);
+
+ // divide a non-existing key by a number
+
+ std::string key = "my-key";
+ double value_in = 0.5;
+
+ std::stringstream stream;
+ stream << value_in;
+
+ ASSERT_EQ(0, rados::cls::numops::div(&ioctx, "myobject", key, value_in));
+
+ // check that the omap entry was set and the value is zero
+
+ std::set<std::string> keys;
+ std::map<std::string, bufferlist> omap;
+ keys.insert(key);
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ std::map<std::string, bufferlist>::iterator it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bufferlist bl = (*it).second;
+ std::string value_out(bl.c_str(), bl.length());
+
+ EXPECT_EQ("0", value_out);
+
+ // check that division by zero is not allowed
+
+ ASSERT_EQ(-EINVAL, rados::cls::numops::div(&ioctx, "myobject", key, 0));
+
+ // set a non-zero value so we can effectively test divisions
+
+ omap.clear();
+
+ omap[key].append(stream.str());
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // divide another value to the existing one
+
+ double new_value_in = 3.001;
+
+ ASSERT_EQ(0, rados::cls::numops::div(&ioctx, "myobject", key, new_value_in));
+
+ // check that the omap entry's value matches
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ stream.str("");
+ stream << (value_in / new_value_in);
+
+ EXPECT_EQ(stream.str(), value_out);
+
+ omap.clear();
+
+ // set the omap entry with some non-numeric value
+
+ std::string non_numeric_value("some-non-numeric-text");
+ omap[key].append(non_numeric_value);
+
+ ASSERT_EQ(0, ioctx.omap_set("myobject", omap));
+
+ // check that adding a number does not succeed
+
+ ASSERT_EQ(-EBADMSG, rados::cls::numops::div(&ioctx, "myobject", key, 2.0));
+
+ // check that the omap entry was not changed
+
+ omap.clear();
+
+ ASSERT_EQ(0, ioctx.omap_get_vals_by_keys("myobject", keys, &omap));
+
+ it = omap.find(key);
+
+ ASSERT_NE(omap.end(), it);
+
+ bl = (*it).second;
+ value_out.assign(bl.c_str(), bl.length());
+
+ EXPECT_EQ(non_numeric_value, value_out);
+
+ ASSERT_EQ(0, destroy_one_pool_pp(pool_name, cluster));
+}