Skip to content

Commit

Permalink
#142: added unit test support.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrse committed May 22, 2018
1 parent ba53ee4 commit 81cc8fa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/librmb/tools/rmb/rmb-commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/librmb/tools/rmb/rmb-commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<librmb::RadosMailObject *> &mail_objects,
std::string &sort_string);
Expand Down
2 changes: 1 addition & 1 deletion src/librmb/tools/rmb/rmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion src/tests/rmb/test_rmb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<std::string, std::string> opts;
librmb::RmbCommands rmb_cmd(&storage_mock, &cluster_mock, &opts);
std::vector<librmb::RadosMailObject *> 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);
Expand Down

0 comments on commit 81cc8fa

Please sign in to comment.