From 81cc8fa90afa796c349a93198856dbd05e1c606a Mon Sep 17 00:00:00 2001 From: Jan Radon Date: Tue, 22 May 2018 18:05:08 +0200 Subject: [PATCH] #142: added unit test support. --- src/librmb/tools/rmb/rmb-commands.cpp | 13 +++++++------ src/librmb/tools/rmb/rmb-commands.h | 2 +- src/librmb/tools/rmb/rmb.cpp | 2 +- src/tests/Makefile.am | 2 +- src/tests/rmb/test_rmb.cpp | 25 ++++++++++++++++++++++++- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/librmb/tools/rmb/rmb-commands.cpp b/src/librmb/tools/rmb/rmb-commands.cpp index 7d2e69fb..f15e2c97 100644 --- a/src/librmb/tools/rmb/rmb-commands.cpp +++ b/src/librmb/tools/rmb/rmb-commands.cpp @@ -30,9 +30,10 @@ RmbCommands::~RmbCommands() { // TODO Auto-generated destructor stub } +// TODO:: currently untestable with mocks. int RmbCommands::lspools() { librmb::RadosClusterImpl cluster; - librmb::RadosStorageImpl storage(&cluster); + cluster.init(); if (cluster.connect() < 0) { std::cout << " error opening rados connection" << std::endl; @@ -116,9 +117,8 @@ int RmbCommands::rename_user(librmb::RadosDovecotCephCfg *cfg, bool confirmed, return ret; } -int RmbCommands::config_option(bool create_config, const std::string &obj_, bool confirmed, +int RmbCommands::configuration(bool create_config, const std::string &obj_, bool confirmed, librmb::RadosCephConfig &ceph_cfg) { - bool has_update = (*opts).find("update") != (*opts).end(); bool has_ls = (*opts).find("print_cfg") != (*opts).end(); if (has_update && has_ls) { @@ -206,13 +206,13 @@ int RmbCommands::load_objects(librmb::RadosStorageMetadataModule *ms, } // get load all objects metadata into memory - librados::NObjectIterator iter(storage->get_io_ctx().nobjects_begin()); - while (iter != storage->get_io_ctx().nobjects_end()) { + librados::NObjectIterator iter(storage->find_mails(nullptr)); + while (iter != librados::NObjectIterator::__EndObjectIterator) { librmb::RadosMailObject *mail = new librmb::RadosMailObject(); std::string oid = iter->get_oid(); uint64_t object_size = 0; time_t save_date_rados; - int ret = storage->get_io_ctx().stat(oid, &object_size, &save_date_rados); + int ret = storage->stat_mail(oid, &object_size, &save_date_rados); if (ret != 0 || object_size <= 0) { std::cout << " object '" << oid << "' is not a valid mail object, size = 0" << std::endl; ++iter; @@ -242,6 +242,7 @@ int RmbCommands::load_objects(librmb::RadosStorageMetadataModule *ms, ++iter; mail_objects.push_back(mail); } + std::cout << "hallo welrt" << std::endl; if (sort_string.compare("uid") == 0) { std::sort(mail_objects.begin(), mail_objects.end(), sort_uid); diff --git a/src/librmb/tools/rmb/rmb-commands.h b/src/librmb/tools/rmb/rmb-commands.h index ee9c0967..103d9880 100644 --- a/src/librmb/tools/rmb/rmb-commands.h +++ b/src/librmb/tools/rmb/rmb-commands.h @@ -39,7 +39,7 @@ class RmbCommands { int rename_user(librmb::RadosDovecotCephCfg *cfg, bool confirmed, const std::string &uid); - int config_option(bool create_config, const std::string &obj_, bool confirmed, librmb::RadosCephConfig &ceph_cfg); + int configuration(bool create_config, const std::string &obj_, bool confirmed, librmb::RadosCephConfig &ceph_cfg); int load_objects(librmb::RadosStorageMetadataModule *ms, std::vector &mail_objects, std::string &sort_string); diff --git a/src/librmb/tools/rmb/rmb.cpp b/src/librmb/tools/rmb/rmb.cpp index 183db612..4579842e 100644 --- a/src/librmb/tools/rmb/rmb.cpp +++ b/src/librmb/tools/rmb/rmb.cpp @@ -352,7 +352,7 @@ int main(int argc, const char **argv) { // connection to rados is established! rmb_commands = new librmb::RmbCommands(&storage, &cluster, &opts); if (is_config_option) { - if (rmb_commands->config_option(create_config, config_obj, confirmed, ceph_cfg) < 0) { + if (rmb_commands->configuration(create_config, config_obj, confirmed, ceph_cfg) < 0) { std::cerr << "error processing config option" << std::endl; } delete rmb_commands; diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index d8b56370..730cb497 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -51,7 +51,7 @@ gtest_shlibs = libgmockgtest.la TESTS = test_rmb test_rmb_SOURCES = rmb/test_rmb.cpp mocks/mock_test.h -test_rmb_LDADD = $(rmb_shlibs) $(top_builddir)/src/librmb/tools/rmb/ls_cmd_parser.o $(top_builddir)/src/librmb/tools/rmb/mailbox_tools.o $(gtest_shlibs) +test_rmb_LDADD = $(rmb_shlibs) $(top_builddir)/src/librmb/tools/rmb/ls_cmd_parser.o $(top_builddir)/src/librmb/tools/rmb/rmb-commands.o $(top_builddir)/src/librmb/tools/rmb/mailbox_tools.o $(gtest_shlibs) TESTS += test_storage_mock_rbox test_storage_mock_rbox_SOURCES = storage-mock-rbox/test_storage_mock_rbox.cpp storage-mock-rbox/TestCase.cpp storage-mock-rbox/TestCase.h mocks/mock_test.h test-utils/it_utils.cpp test-utils/it_utils.h diff --git a/src/tests/rmb/test_rmb.cpp b/src/tests/rmb/test_rmb.cpp index de1d3dd3..f92da653 100644 --- a/src/tests/rmb/test_rmb.cpp +++ b/src/tests/rmb/test_rmb.cpp @@ -19,6 +19,12 @@ #include "../../librmb/rados-storage-impl.h" #include "../../librmb/tools/rmb/ls_cmd_parser.h" #include "../../librmb/tools/rmb/mailbox_tools.h" +#include "../../librmb/tools/rmb/rmb-commands.h" +#include "mock_test.h" + +using ::testing::Return; +using ::testing::_; +using ::testing::ReturnRef; TEST(rmb, test_cmd_parser) { std::string key = "M"; @@ -118,7 +124,24 @@ TEST(rmb1, path_tests) { librmb::MailboxTools tools3(&mbox, test_path2); EXPECT_EQ("abc", tools3.get_mailbox_path()); } - +TEST(rmb1, rmb_commands_no_objects_found) { + librmbtest::RadosStorageMock storage_mock; + librmbtest::RadosClusterMock cluster_mock; + librmbtest::RadosStorageMetadataMock ms_module_mock; + + std::map opts; + librmb::RmbCommands rmb_cmd(&storage_mock, &cluster_mock, &opts); + std::vector mails; + std::string search_string = "uid"; + const librados::NObjectIterator iter = librados::NObjectIterator::__EndObjectIterator; + librados::IoCtx test_ioctx; + + EXPECT_CALL(storage_mock, find_mails(nullptr)).WillRepeatedly(Return(iter)); + EXPECT_CALL(storage_mock, get_io_ctx()).WillRepeatedly(ReturnRef(test_ioctx)); + EXPECT_CALL(storage_mock, stat_mail(_, _, _)).WillRepeatedly(Return(0)); + int ret = rmb_cmd.load_objects(&ms_module_mock, mails, search_string); + EXPECT_EQ(ret, 0); +} int main(int argc, char **argv) { ::testing::InitGoogleMock(&argc, argv);