2016-05-07 16:17:40 +05:30
|
|
|
// 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/copier.h"
|
|
|
|
#include "test_utils.h"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
using namespace boost;
|
|
|
|
using namespace std;
|
|
|
|
using namespace test;
|
|
|
|
using namespace testing;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2016-06-14 20:57:17 +05:30
|
|
|
unsigned const BLOCK_SIZE = 64u;
|
|
|
|
using wait_result = io_engine::wait_result;
|
|
|
|
|
|
|
|
ostream &operator <<(ostream &out, optional<wait_result> const &mwr) {
|
|
|
|
if (mwr) {
|
|
|
|
out << "Just[wait_result[" << mwr->first << ", " << mwr->second << "]]";
|
|
|
|
} else
|
|
|
|
out << "Nothing";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
class io_engine_mock : public io_engine {
|
2016-05-07 16:17:40 +05:30
|
|
|
public:
|
2016-06-07 18:15:27 +05:30
|
|
|
MOCK_METHOD3(open_file, handle(string const &, mode, sharing));
|
2016-06-07 15:42:27 +05:30
|
|
|
MOCK_METHOD1(close_file, void(handle));
|
|
|
|
MOCK_METHOD6(issue_io, bool(handle, dir, sector_t, sector_t, void *, unsigned));
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
MOCK_METHOD0(wait, optional<wait_result>());
|
|
|
|
MOCK_METHOD1(wait, optional<wait_result>(unsigned &));
|
2016-06-07 15:42:27 +05:30
|
|
|
};
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
class CopierTests : public Test {
|
|
|
|
public:
|
|
|
|
CopierTests()
|
|
|
|
: src_file_("copy_src"),
|
|
|
|
dest_file_("copy_dest") {
|
2016-05-07 16:17:40 +05:30
|
|
|
}
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
unique_ptr<copier> make_copier() {
|
2021-01-17 22:41:01 +05:30
|
|
|
return make_copier(BLOCK_SIZE, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<copier> make_copier(sector_t block_size,
|
|
|
|
sector_t src_offset,
|
|
|
|
sector_t dest_offset) {
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, open_file(src_file_, io_engine::M_READ_ONLY, io_engine::EXCLUSIVE)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(SRC_HANDLE));
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, open_file(dest_file_, io_engine::M_READ_WRITE, io_engine::EXCLUSIVE)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(DEST_HANDLE));
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
EXPECT_CALL(engine_, close_file(SRC_HANDLE)).Times(1);
|
|
|
|
EXPECT_CALL(engine_, close_file(DEST_HANDLE)).Times(1);
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
return unique_ptr<copier>(new copier(engine_, src_file_,
|
|
|
|
dest_file_,
|
2021-01-17 22:41:01 +05:30
|
|
|
block_size, 1 * 1024 * 1024,
|
|
|
|
src_offset, dest_offset));
|
2016-05-07 16:17:40 +05:30
|
|
|
}
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
static optional<wait_result> make_wr(bool success, unsigned context) {
|
|
|
|
return optional<wait_result>(wait_result(success, context));
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
// issue a copy_op, and wait for its completion synchronously
|
2016-06-07 15:42:27 +05:30
|
|
|
void issue_successful_op(copier &c, copy_op &op, unsigned context) {
|
|
|
|
InSequence dummy;
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
unsigned nr_pending = c.nr_pending();
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ,
|
2021-01-17 22:41:01 +05:30
|
|
|
expected_src_sector(c, op.src_b),
|
|
|
|
expected_src_sector(c, op.src_e), _, context)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
c.issue(op);
|
|
|
|
|
|
|
|
ASSERT_TRUE(c.nr_pending() == nr_pending + 1);
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(true, context)));
|
2016-06-07 15:42:27 +05:30
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::D_WRITE,
|
2021-01-17 22:41:01 +05:30
|
|
|
expected_dest_sector(c, op.dest_b),
|
|
|
|
expected_dest_sector(c, op.dest_b + (op.src_e - op.src_b)),
|
|
|
|
_, context)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(true, context)));
|
2016-06-07 15:42:27 +05:30
|
|
|
|
|
|
|
auto mop = c.wait();
|
|
|
|
ASSERT_EQ(c.nr_pending(), nr_pending);
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
ASSERT_TRUE(mop->success());
|
2016-05-07 16:17:40 +05:30
|
|
|
}
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
unsigned const SRC_HANDLE = 10;
|
|
|
|
unsigned const DEST_HANDLE = 11;
|
2016-05-07 16:17:40 +05:30
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
StrictMock<io_engine_mock> engine_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
sector_t expected_src_sector(const copier &c, block_address b) {
|
|
|
|
return c.get_src_offset() + b * c.get_block_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
sector_t expected_dest_sector(const copier &c, block_address b) {
|
|
|
|
return c.get_dest_offset() + b * c.get_block_size();
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
string src_file_;
|
|
|
|
string dest_file_;
|
2016-05-07 16:17:40 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
TEST_F(CopierTests, create_default_copier)
|
2016-06-07 15:42:27 +05:30
|
|
|
{
|
|
|
|
auto c = make_copier();
|
2021-01-17 22:41:01 +05:30
|
|
|
ASSERT_EQ(c->get_block_size(), BLOCK_SIZE);
|
|
|
|
ASSERT_EQ(c->get_src_offset(), 0u);
|
|
|
|
ASSERT_EQ(c->get_dest_offset(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CopierTests, create_copier_with_offsets)
|
|
|
|
{
|
|
|
|
sector_t src_offset = 2048;
|
|
|
|
sector_t dest_offset = 8192;
|
|
|
|
|
|
|
|
auto c = make_copier(BLOCK_SIZE, src_offset, dest_offset);
|
|
|
|
ASSERT_EQ(c->get_block_size(), BLOCK_SIZE);
|
|
|
|
ASSERT_EQ(c->get_src_offset(), src_offset);
|
|
|
|
ASSERT_EQ(c->get_dest_offset(), dest_offset);
|
2016-06-07 15:42:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CopierTests, successful_copy)
|
|
|
|
{
|
2021-01-17 22:41:01 +05:30
|
|
|
// Copy first block
|
2016-06-07 15:42:27 +05:30
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
|
|
|
|
auto c = make_copier();
|
|
|
|
issue_successful_op(*c, op1, 0);
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
TEST_F(CopierTests, successful_copy_with_offsets)
|
|
|
|
{
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
|
|
|
|
auto c = make_copier(BLOCK_SIZE, 2048, 8192);
|
|
|
|
issue_successful_op(*c, op1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify copier's error handling against unsucessful engine operations
|
|
|
|
// at different stages.
|
|
|
|
// Test the first stage (issue_read (failed) => read => issue_write => write)
|
2016-06-07 15:42:27 +05:30
|
|
|
TEST_F(CopierTests, unsuccessful_issue_read)
|
|
|
|
{
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
auto c = make_copier();
|
|
|
|
|
|
|
|
InSequence dummy;
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
// Test the second stage (issue_read => read (failed) => issue_write => write)
|
2016-06-07 15:42:27 +05:30
|
|
|
TEST_F(CopierTests, unsuccessful_read)
|
|
|
|
{
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
auto c = make_copier();
|
|
|
|
|
|
|
|
InSequence dummy;
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
c->issue(op1);
|
|
|
|
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(false, 0u)));
|
2016-06-07 15:42:27 +05:30
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
|
|
|
auto mop = c->wait();
|
|
|
|
ASSERT_EQ(c->nr_pending(), 0u);
|
|
|
|
ASSERT_FALSE(mop->success());
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
// Test the third stage (issue_read => read => issue_write (failed) => write)
|
2016-06-07 15:42:27 +05:30
|
|
|
TEST_F(CopierTests, unsuccessful_issue_write)
|
|
|
|
{
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
auto c = make_copier();
|
|
|
|
|
|
|
|
InSequence dummy;
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
c->issue(op1);
|
|
|
|
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(true, 0u)));
|
2016-06-07 15:42:27 +05:30
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::D_WRITE, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(false));
|
|
|
|
|
|
|
|
auto mop = c->wait();
|
|
|
|
ASSERT_EQ(c->nr_pending(), 0u);
|
|
|
|
ASSERT_FALSE(mop->success());
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
// Test the last stage (issue_read => read => issue_write => write (failed))
|
2016-06-07 15:42:27 +05:30
|
|
|
TEST_F(CopierTests, unsuccessful_write)
|
2016-05-07 16:17:40 +05:30
|
|
|
{
|
|
|
|
// Copy first block
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
|
2016-06-07 15:42:27 +05:30
|
|
|
auto c = make_copier();
|
|
|
|
|
|
|
|
InSequence dummy;
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
c->issue(op1);
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(true, 0u)));
|
2016-06-07 15:42:27 +05:30
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::D_WRITE, 0, BLOCK_SIZE, _, 0)).
|
2016-06-07 15:42:27 +05:30
|
|
|
WillOnce(Return(true));
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait()).
|
2016-06-14 20:57:17 +05:30
|
|
|
WillOnce(Return(make_wr(false, 0u)));
|
2016-06-07 15:42:27 +05:30
|
|
|
|
|
|
|
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);
|
2016-05-07 16:17:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 22:41:01 +05:30
|
|
|
TEST_F(CopierTests, copy_different_blocks_with_offsets)
|
|
|
|
{
|
|
|
|
sector_t src_offset = 2048;
|
|
|
|
sector_t dest_offset = 8192;
|
|
|
|
|
|
|
|
auto c = make_copier(BLOCK_SIZE, src_offset, dest_offset);
|
|
|
|
for (unsigned i = 0; i < 5000; i++) {
|
|
|
|
copy_op op(i, i + 1, i);
|
|
|
|
issue_successful_op(*c, op, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:57:17 +05:30
|
|
|
TEST_F(CopierTests, wait_can_timeout)
|
|
|
|
{
|
|
|
|
copy_op op1(0, 1, 0);
|
|
|
|
auto c = make_copier();
|
|
|
|
|
|
|
|
InSequence dummy;
|
|
|
|
EXPECT_CALL(engine_, issue_io(SRC_HANDLE, io_engine::D_READ, 0, BLOCK_SIZE, _, 0)).
|
|
|
|
WillOnce(Return(true));
|
|
|
|
c->issue(op1);
|
|
|
|
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
|
|
|
unsigned micro = 10000;
|
|
|
|
EXPECT_CALL(engine_, wait(micro)).
|
|
|
|
WillOnce(Return(make_wr(true, 0u)));
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, issue_io(DEST_HANDLE, io_engine::D_WRITE, 0, BLOCK_SIZE, _, 0)).
|
|
|
|
WillOnce(Return(true));
|
|
|
|
|
|
|
|
EXPECT_CALL(engine_, wait(micro)).
|
|
|
|
WillOnce(DoAll(SetArgReferee<0>(0u), Return(optional<wait_result>())));
|
|
|
|
|
|
|
|
auto mop = c->wait(micro);
|
|
|
|
ASSERT_FALSE(mop);
|
|
|
|
ASSERT_EQ(c->nr_pending(), 1u);
|
|
|
|
}
|
|
|
|
|
2016-05-07 16:17:40 +05:30
|
|
|
//----------------------------------------------------------------
|