Skip to content

Commit

Permalink
#142: new unit tests (read metadata goodcase, read metadata (recv. date)
Browse files Browse the repository at this point in the history
- added new unit tests
  read object with metadata good case
  read object with invalid receive date.
  • Loading branch information
jrse committed May 23, 2018
1 parent e081b77 commit ac76882
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/librmb/rados-storage-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,13 @@ bool RadosStorageImpl::copy(std::string &src_oid, const char *src_ns, std::strin
// if save_async = true, don't forget to call wait_for_rados_operations e.g. wait_for_write_operations_complete
// to wait for completion and free resources.
bool RadosStorageImpl::save_mail(librados::ObjectWriteOperation *write_op_xattr, RadosMailObject *mail,
bool &save_async) {
bool save_async) {
if (!cluster->is_connected() || !io_ctx_created) {
return false;
}

if (write_op_xattr == nullptr || mail == nullptr) {
return false;
}
write_op_xattr->mtime(mail->get_rados_save_date());
int ret = split_buffer_and_exec_op(mail, write_op_xattr, get_max_write_size_bytes());
mail->set_active_op(true);
Expand Down
2 changes: 1 addition & 1 deletion src/librmb/rados-storage-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class RadosStorageImpl : public RadosStorage {

int save_mail(const std::string &oid, librados::bufferlist &buffer);
bool save_mail(RadosMailObject *mail, bool &save_async);
bool save_mail(librados::ObjectWriteOperation *write_op_xattr, RadosMailObject *mail, bool &save_async);
bool save_mail(librados::ObjectWriteOperation *write_op_xattr, RadosMailObject *mail, bool save_async);
librmb::RadosMailObject *alloc_mail_object();

void free_mail_object(librmb::RadosMailObject *mail);
Expand Down
2 changes: 1 addition & 1 deletion src/librmb/rados-storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class RadosStorage {
std::list<RadosMetadata> &to_update) = 0;
/* save the mail */
virtual bool save_mail(RadosMailObject *mail, bool &save_async) = 0;
virtual bool save_mail(librados::ObjectWriteOperation *write_op_xattr, RadosMailObject *mail, bool &save_async) = 0;
virtual bool save_mail(librados::ObjectWriteOperation *write_op_xattr, RadosMailObject *mail, bool save_async) = 0;
/* create a new RadosMailObject */
virtual librmb::RadosMailObject *alloc_mail_object() = 0;
/* free the Rados Mail Object */
Expand Down
280 changes: 280 additions & 0 deletions src/tests/librmb/it_test_librmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,286 @@ TEST(librmb, rmb_load_objects) {
cluster.deinit();
}

TEST(librmb, rmb_load_objects_valid_metadata) {
librados::IoCtx io_ctx;

librmb::RadosClusterImpl cluster;
librmb::RadosStorageImpl storage(&cluster);

std::string pool_name("rmb_tool_tests");
std::string ns("t1");

int open_connection = storage.open_connection(pool_name);

EXPECT_EQ(0, open_connection);
librmb::RadosCephConfig ceph_cfg(&storage.get_io_ctx());
EXPECT_EQ(0, ceph_cfg.save_cfg());

std::map<std::string, std::string> opts;
opts["pool"] = pool_name;
opts["namespace"] = ns;
opts["print_cfg"] = "true";
opts["cfg_obj"] = ceph_cfg.get_cfg_object_name();

librmb::RmbCommands rmb_commands(&storage, &cluster, &opts);

/* update config
rmb_commands.configuration(false, ceph_cfg);
*/
// load metadata info
std::string uid;
librmb::RadosStorageMetadataModule *ms = rmb_commands.init_metadata_storage_module(ceph_cfg, &uid);
EXPECT_NE(nullptr, ms);

storage.set_namespace(ns);

librmb::RadosMailObject obj2;
obj2.set_oid("myobject_valid");
obj2.get_mail_buffer()->append("hallo_welt"); // make sure obj is not empty.
obj2.set_mail_size(obj2.get_mail_buffer()->length());
librados::ObjectWriteOperation *write_op = new librados::ObjectWriteOperation();
{
std::string key = "M";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "I";
std::string val = "v0.1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "G";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "R";
std::string val = "1234567";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "S";
std::string val = "1234561117";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "P";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "O";
std::string val = "0";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "Z";
std::string val = "200";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "V";
std::string val = "250";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "U";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "A";
std::string val = "";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "F";
std::string val = "01";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "B";
std::string val = "DRAFTS";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
// convert metadata to xattr. and add to write_op
ms->save_metadata(write_op, &obj2);
// save complete mail.
EXPECT_EQ(true, storage.save_mail(write_op, &obj2, true));
std::vector<librmb::RadosMailObject *> list;
list.push_back(&obj2);

EXPECT_EQ(true, !storage.wait_for_rados_operations(list));
std::cout << " wait ok " << std::endl;
std::vector<librmb::RadosMailObject *> mail_objects;
std::string sort_string = "uid";

EXPECT_EQ(0, rmb_commands.load_objects(ms, mail_objects, sort_string));
// there needs to be one mail
EXPECT_EQ(1, mail_objects.size());

storage.delete_mail(&obj2);
storage.delete_mail(ceph_cfg.get_cfg_object_name());
delete ms;
delete mail_objects[0];
mail_objects.clear();
// tear down
cluster.deinit();
}

TEST(librmb, rmb_load_objects_invalid_metadata) {
librados::IoCtx io_ctx;

librmb::RadosClusterImpl cluster;
librmb::RadosStorageImpl storage(&cluster);

std::string pool_name("rmb_tool_tests");
std::string ns("t1");

int open_connection = storage.open_connection(pool_name);

EXPECT_EQ(0, open_connection);
librmb::RadosCephConfig ceph_cfg(&storage.get_io_ctx());
EXPECT_EQ(0, ceph_cfg.save_cfg());

std::map<std::string, std::string> opts;
opts["pool"] = pool_name;
opts["namespace"] = ns;
opts["print_cfg"] = "true";
opts["cfg_obj"] = ceph_cfg.get_cfg_object_name();

librmb::RmbCommands rmb_commands(&storage, &cluster, &opts);

/* update config
rmb_commands.configuration(false, ceph_cfg);
*/
// load metadata info
std::string uid;
librmb::RadosStorageMetadataModule *ms = rmb_commands.init_metadata_storage_module(ceph_cfg, &uid);
EXPECT_NE(nullptr, ms);

storage.set_namespace(ns);

librmb::RadosMailObject obj2;
obj2.set_oid("myobject_invalid");
obj2.get_mail_buffer()->append("hallo_welt"); // make sure obj is not empty.
obj2.set_mail_size(obj2.get_mail_buffer()->length());
librados::ObjectWriteOperation *write_op = new librados::ObjectWriteOperation();
{
std::string key = "M";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "I";
std::string val = "v0.1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "G";
std::string val = "8eed840764b05359f12718004d2485ee";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "R";
std::string val = "abnahsijsksisis"; // <-- This should be numeric
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "S";
std::string val = "1234561117";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "P";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "O";
std::string val = "0";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "Z";
std::string val = "200";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "V";
std::string val = "250";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "U";
std::string val = "1";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "A";
std::string val = "";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "F";
std::string val = "01";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
{
std::string key = "B";
std::string val = "DRAFTS";
librmb::RadosMetadata m(key, val);
obj2.add_metadata(m);
}
// convert metadata to xattr. and add to write_op
ms->save_metadata(write_op, &obj2);
// save complete mail.
EXPECT_EQ(true, storage.save_mail(write_op, &obj2, true));
std::vector<librmb::RadosMailObject *> list;
list.push_back(&obj2);

EXPECT_EQ(true, !storage.wait_for_rados_operations(list));
std::cout << " wait ok " << std::endl;
std::vector<librmb::RadosMailObject *> mail_objects;
std::string sort_string = "uid";

EXPECT_EQ(0, rmb_commands.load_objects(ms, mail_objects, sort_string));
// there needs to be one mail
EXPECT_EQ(0, mail_objects.size());

storage.delete_mail(&obj2);
storage.delete_mail(ceph_cfg.get_cfg_object_name());
delete ms;
mail_objects.clear();
// tear down
cluster.deinit();
}
TEST(librmb, mock_obj) {}
int main(int argc, char **argv) {
::testing::InitGoogleMock(&argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/mocks/mock_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class RadosStorageMock : public RadosStorage {
std::list<RadosMetadata> &to_update));
MOCK_METHOD2(save_mail, int(const std::string &oid, librados::bufferlist &bufferlist));
MOCK_METHOD2(save_mail, bool(RadosMailObject *mail, bool &save_async));
MOCK_METHOD3(save_mail, bool(librados::ObjectWriteOperation *write_op, RadosMailObject *mail, bool &save_async));
MOCK_METHOD3(save_mail, bool(librados::ObjectWriteOperation *write_op, RadosMailObject *mail, bool save_async));
MOCK_METHOD0(alloc_mail_object, librmb::RadosMailObject *());

MOCK_METHOD1(free_mail_object, void(librmb::RadosMailObject *mail));
Expand Down
2 changes: 1 addition & 1 deletion src/tests/storage-mock-rbox/test_storage_mock_rbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ TEST_F(StorageTest, exec_write_op_fails) {
EXPECT_CALL(*storage_mock, open_connection("mail_storage", "ceph", "client.admin"))
.Times(AtLeast(1))
.WillRepeatedly(Return(0));
EXPECT_CALL(*storage_mock, save_mail(_, _, Matcher<bool &>(_))).Times(1).WillOnce(Return(false));
EXPECT_CALL(*storage_mock, save_mail(_, _, Matcher<bool>(_))).Times(1).WillOnce(Return(false));
// EXPECT_CALL(*storage_mock, save_mail(Matcher<const std::string &>(_), _)).WillOnce(Return(0));
// EXPECT_CALL(*storage_mock, read_mail(_, _)).WillOnce(Return(-2));

Expand Down

0 comments on commit ac76882

Please sign in to comment.