[block-cache] unit tests + debug io_engine and copier

This commit is contained in:
Joe Thornber
2016-06-07 11:12:27 +01:00
parent 34c039d7dc
commit a94bfea798
12 changed files with 560 additions and 150 deletions

View File

@@ -16,10 +16,10 @@
# with thin-provisioning-tools. If not, see
# <http://www.gnu.org/licenses/>.
GMOCK_DIR=gmock-1.6.0/
GMOCK_DIR=googletest
GMOCK_INCLUDES=\
-Igmock-1.6.0/include \
-Igmock-1.6.0/gtest/include
-I$(GMOCK_DIR)/googlemock/include \
-I$(GMOCK_DIR)/googletest/include
GMOCK_FLAGS=\
-Wno-unused-local-typedefs
@@ -28,16 +28,16 @@ GMOCK_LIBS=\
-Llib -lpdata -lgmock -lpthread -laio
GMOCK_DEPS=\
$(wildcard $(GMOCK_DIR)/include/*.h) \
$(wildcard $(GMOCK_DIR)/src/*.cc) \
$(wildcard $(GMOCK_DIR)/gtest/include/*.h) \
$(wildcard $(GMOCK_DIR)/gtest/src/*.cc)
$(wildcard $(GMOCK_DIR)/googlemock/include/*.h) \
$(wildcard $(GMOCK_DIR)/googlemock/src/*.cc) \
$(wildcard $(GMOCK_DIR)/googletest/include/*.h) \
$(wildcard $(GMOCK_DIR)/googletest/src/*.cc)
lib/libgmock.a: $(GMOCK_DEPS)
@echo " [CXX] gtest"
$(V)g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR)/gtest -c $(GMOCK_DIR)/gtest/src/gtest-all.cc
$(V)g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR)/googletest -c $(GMOCK_DIR)/googletest/src/gtest-all.cc
@echo " [CXX] gmock"
$(V)g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR) -c $(GMOCK_DIR)/src/gmock-all.cc
$(V)g++ $(GMOCK_INCLUDES) -I$(GMOCK_DIR)/googlemock -c $(GMOCK_DIR)/googlemock/src/gmock-all.cc
@echo " [AR] $<"
$(V)ar -rv lib/libgmock.a gtest-all.o gmock-all.o > /dev/null 2>&1
@@ -59,6 +59,7 @@ TEST_SOURCE=\
unit-tests/damage_tracker_t.cc \
unit-tests/endian_t.cc \
unit-tests/error_state_t.cc \
unit-tests/io_engine_t.cc \
unit-tests/mem_pool_t.cc \
unit-tests/rmap_visitor_t.cc \
unit-tests/rolling_hash_t.cc \

View File

@@ -31,72 +31,197 @@ using namespace testing;
//----------------------------------------------------------------
namespace {
class temp_file {
class io_engine_mock : public io_engine {
public:
temp_file(string const &name_base, unsigned meg_size)
: path_(gen_path(name_base)) {
MOCK_METHOD2(open_file, handle(string const &, mode));
MOCK_METHOD1(close_file, void(handle));
MOCK_METHOD6(issue_io, bool(handle, dir, sector_t, sector_t, void *, unsigned));
int fd = ::open(path_.c_str(), O_CREAT | O_RDWR, 0666);
if (fd < 0)
throw runtime_error("couldn't open file");
if (::fallocate(fd, 0, 0, 1024 * 1024 * meg_size))
throw runtime_error("couldn't fallocate");
::close(fd);
}
~temp_file() {
::unlink(path_.c_str());
}
string const &get_path() const {
return path_;
}
private:
static string gen_path(string const &base) {
return string("./") + base + string(".tmp");
}
string path_;
MOCK_METHOD0(wait, wait_result());
};
unsigned const BLOCK_SIZE = 64u;
using wait_result = io_engine::wait_result;
class CopierTests : public Test {
public:
CopierTests()
: src_file_("copy_src", 32),
dest_file_("copy_dest", 32),
copier_(src_file_.get_path(),
dest_file_.get_path(),
64, 1 * 1024 * 1024) {
: src_file_("copy_src"),
dest_file_("copy_dest") {
}
copier &get_copier() {
return copier_;
unique_ptr<copier> make_copier() {
EXPECT_CALL(engine_, open_file(src_file_, io_engine::READ_ONLY)).
WillOnce(Return(SRC_HANDLE));
EXPECT_CALL(engine_, open_file(dest_file_, io_engine::READ_WRITE)).
WillOnce(Return(DEST_HANDLE));
EXPECT_CALL(engine_, close_file(SRC_HANDLE)).Times(1);
EXPECT_CALL(engine_, close_file(DEST_HANDLE)).Times(1);
return unique_ptr<copier>(new copier(engine_, src_file_,
dest_file_,
BLOCK_SIZE, 1 * 1024 * 1024));
}
private:
temp_file src_file_;
temp_file dest_file_;
void issue_successful_op(copier &c, copy_op &op, unsigned context) {
InSequence dummy;
copier copier_;
unsigned nr_pending = c.nr_pending();
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::READ,
op.src_b * BLOCK_SIZE,
op.src_e * BLOCK_SIZE, _, context)).
WillOnce(Return(true));
c.issue(op);
ASSERT_TRUE(c.nr_pending() == nr_pending + 1);
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(true, context)));
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::WRITE,
op.dest_b * BLOCK_SIZE,
(op.dest_b + (op.src_e - op.src_b)) * BLOCK_SIZE, _, context)).
WillOnce(Return(true));
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(true, context)));
auto mop = c.wait();
ASSERT_EQ(c.nr_pending(), nr_pending);
ASSERT_TRUE(mop->success());
}
unsigned const SRC_HANDLE = 10;
unsigned const DEST_HANDLE = 11;
string src_file_;
string dest_file_;
StrictMock<io_engine_mock> engine_;
};
}
//----------------------------------------------------------------
TEST_F(CopierTests, empty_test)
{
auto c = make_copier();
}
TEST_F(CopierTests, successful_copy)
{
// Copy first block
copy_op op1(0, 1, 0);
auto c = make_copier();
issue_successful_op(*c, op1, 0);
}
TEST_F(CopierTests, unsuccessful_issue_read)
{
copy_op op1(0, 1, 0);
auto c = make_copier();
InSequence dummy;
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::READ, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(false));
c->issue(op1);
ASSERT_EQ(c->nr_pending(), 1u);
auto mop = c->wait();
ASSERT_EQ(c->nr_pending(), 0u);
ASSERT_FALSE(mop->success());
}
TEST_F(CopierTests, unsuccessful_read)
{
copy_op op1(0, 1, 0);
auto c = make_copier();
InSequence dummy;
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::READ, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(true));
c->issue(op1);
ASSERT_EQ(c->nr_pending(), 1u);
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(false, 0u)));
ASSERT_EQ(c->nr_pending(), 1u);
auto mop = c->wait();
ASSERT_EQ(c->nr_pending(), 0u);
ASSERT_FALSE(mop->success());
}
TEST_F(CopierTests, unsuccessful_issue_write)
{
copy_op op1(0, 1, 0);
auto c = make_copier();
InSequence dummy;
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::READ, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(true));
c->issue(op1);
ASSERT_EQ(c->nr_pending(), 1u);
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(true, 0u)));
ASSERT_EQ(c->nr_pending(), 1u);
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::WRITE, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(false));
auto mop = c->wait();
ASSERT_EQ(c->nr_pending(), 0u);
ASSERT_FALSE(mop->success());
}
TEST_F(CopierTests, unsuccessful_write)
{
// Copy first block
copy_op op1(0, 1, 0);
get_copier().issue(op1);
auto mop = get_copier().wait();
if (mop) {
cerr << "op completed\n";
} else {
cerr << "no op completed\n";
auto c = make_copier();
InSequence dummy;
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::READ, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(true));
c->issue(op1);
ASSERT_EQ(c->nr_pending(), 1u);
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(true, 0u)));
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::WRITE, 0, BLOCK_SIZE, _, 0)).
WillOnce(Return(true));
EXPECT_CALL(engine_, wait()).
WillOnce(Return(wait_result(false, 0u)));
auto mop = c->wait();
ASSERT_EQ(c->nr_pending(), 0u);
ASSERT_FALSE(mop->success());
}
TEST_F(CopierTests, copy_the_same_block_many_times)
{
auto c = make_copier();
copy_op op1(0, 1, 0);
for (unsigned i = 0; i < 50000; i++)
issue_successful_op(*c, op1, i);
}
TEST_F(CopierTests, copy_different_blocks)
{
auto c = make_copier();
for (unsigned i = 0; i < 5000; i++) {
copy_op op(i, i + 1, i);
issue_successful_op(*c, op, i);
}
}

185
unit-tests/io_engine_t.cc Normal file
View File

@@ -0,0 +1,185 @@
// Copyright (C) 2016 Red Hat, Inc. All rights reserved.
//
// This file is part of the thin-provisioning-tools source.
//
// thin-provisioning-tools is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// thin-provisioning-tools is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>.
#include "gmock/gmock.h"
#include "block-cache/mem_pool.h"
#include "block-cache/io_engine.h"
#include "test_utils.h"
#include <fcntl.h>
using namespace boost;
using namespace std;
using namespace test;
using namespace testing;
//----------------------------------------------------------------
namespace {
unsigned const MAX_IO = 64;
unsigned const PAGE_SIZE = 4096;
class IOEngineTests : public Test {
public:
IOEngineTests()
: pool_(64 * 512, 128 * 512, PAGE_SIZE),
src_file_("copy_src", 32),
dest_file_("copy_dest", 32),
engine_(new aio_engine(MAX_IO)) {
}
// in sectors
static uint64_t meg(unsigned n) {
return 2 * 1024 * n;
}
mempool pool_;
temp_file src_file_;
temp_file dest_file_;
unique_ptr<io_engine> engine_;
};
}
//----------------------------------------------------------------
TEST_F(IOEngineTests, empty_test)
{
}
TEST_F(IOEngineTests, open_and_close)
{
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
auto dest_handle = engine_->open_file(dest_file_.get_path(), io_engine::READ_WRITE);
ASSERT_TRUE(src_handle != dest_handle);
engine_->close_file(src_handle);
engine_->close_file(dest_handle);
}
TEST_F(IOEngineTests, you_can_read_a_read_only_handle)
{
unsigned nr_sectors = 8;
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
void *data = pool_.alloc();
bool r = engine_->issue_io(src_handle,
io_engine::READ,
0, nr_sectors,
data,
123);
ASSERT_TRUE(r);
auto wr = engine_->wait();
ASSERT_TRUE(wr.first);
ASSERT_TRUE(wr.second == 123);
engine_->close_file(src_handle);
pool_.free(data);
}
TEST_F(IOEngineTests, you_cannot_write_to_a_read_only_handle)
{
unsigned nr_sectors = 8;
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
void *data = pool_.alloc();
bool r = engine_->issue_io(src_handle,
io_engine::WRITE,
0, nr_sectors,
data,
0);
ASSERT_FALSE(r);
engine_->close_file(src_handle);
pool_.free(data);
}
TEST_F(IOEngineTests, you_can_write_to_a_read_write_handle)
{
unsigned nr_sectors = 8;
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
void *data = pool_.alloc();
bool r = engine_->issue_io(src_handle,
io_engine::READ,
0, nr_sectors,
data,
123);
ASSERT_TRUE(r);
auto wr = engine_->wait();
ASSERT_TRUE(wr.first);
ASSERT_TRUE(wr.second == 123);
engine_->close_file(src_handle);
pool_.free(data);
}
TEST_F(IOEngineTests, final_block_read_succeeds)
{
unsigned nr_sectors = 8;
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
void *data = pool_.alloc();
bool r = engine_->issue_io(src_handle,
io_engine::READ,
meg(32) - nr_sectors, meg(32),
data,
123);
ASSERT_TRUE(r);
auto wr = engine_->wait();
ASSERT_TRUE(wr.first);
engine_->close_file(src_handle);
pool_.free(data);
}
TEST_F(IOEngineTests, out_of_bounds_read_fails)
{
unsigned nr_sectors = 8;
auto src_handle = engine_->open_file(src_file_.get_path(), io_engine::READ_ONLY);
void *data = pool_.alloc();
bool r = engine_->issue_io(src_handle,
io_engine::READ,
meg(32), meg(32) + nr_sectors,
data,
123);
ASSERT_TRUE(r);
auto wr = engine_->wait();
ASSERT_FALSE(wr.first);
engine_->close_file(src_handle);
pool_.free(data);
}
TEST_F(IOEngineTests, out_of_bounds_write_succeeds)
{
unsigned nr_sectors = 8;
auto handle = engine_->open_file(dest_file_.get_path(), io_engine::READ_WRITE);
void *data = pool_.alloc();
bool r = engine_->issue_io(handle,
io_engine::WRITE,
meg(32), meg(32) + nr_sectors,
data,
123);
ASSERT_TRUE(r);
auto wr = engine_->wait();
ASSERT_TRUE(wr.first);
engine_->close_file(handle);
pool_.free(data);
}
//----------------------------------------------------------------

View File

@@ -93,4 +93,20 @@ TEST_F(MempoolTests, exhaust_pool)
ASSERT_EQ(md, nullptr);
}
// Use valgrind
TEST_F(MempoolTests, data_can_be_written)
{
mempool mp(512, 100 * 512, 512);
for (unsigned i = 0; i < 100; i++) {
auto md = mp.alloc();
ASSERT_NE(md, nullptr);
memset(md, 0, 512);
}
auto md = mp.alloc();
ASSERT_EQ(md, nullptr);
}
//----------------------------------------------------------------

View File

@@ -3,6 +3,8 @@
#include "persistent-data/space-maps/core.h"
using namespace persistent_data;
using namespace std;
using namespace test;
//----------------------------------------------------------------
@@ -21,3 +23,35 @@ test::open_temporary_tm(block_manager<>::ptr bm)
}
//----------------------------------------------------------------
temp_file::temp_file(string const &name_base, unsigned meg_size)
: path_(gen_path(name_base))
{
int fd = ::open(path_.c_str(), O_CREAT | O_RDWR, 0666);
if (fd < 0)
throw runtime_error("couldn't open file");
if (::fallocate(fd, 0, 0, 1024 * 1024 * meg_size))
throw runtime_error("couldn't fallocate");
::close(fd);
}
temp_file::~temp_file()
{
// ::unlink(path_.c_str());
}
string const &
temp_file::get_path() const
{
return path_;
}
string
temp_file::gen_path(string const &base)
{
return string("./") + base + string(".tmp");
}
//----------------------------------------------------------------

View File

@@ -114,6 +114,18 @@ namespace test {
std::unique_ptr<with_directory> dir_;
};
class temp_file {
public:
temp_file(std::string const &name_base, unsigned meg_size);
~temp_file();
std::string const &get_path() const;
private:
static string gen_path(string const &base);
string path_;
};
//--------------------------------
template <typename T>