summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/migration
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-21 11:54:28 +0000
commite6918187568dbd01842d8d1d2c808ce16a894239 (patch)
tree64f88b554b444a49f656b6c656111a145cbbaa28 /src/test/librbd/migration
parentInitial commit. (diff)
downloadceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz
ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/librbd/migration')
-rw-r--r--src/test/librbd/migration/test_mock_FileStream.cc213
-rw-r--r--src/test/librbd/migration/test_mock_HttpClient.cc890
-rw-r--r--src/test/librbd/migration/test_mock_HttpStream.cc194
-rw-r--r--src/test/librbd/migration/test_mock_QCOWFormat.cc1259
-rw-r--r--src/test/librbd/migration/test_mock_RawFormat.cc523
-rw-r--r--src/test/librbd/migration/test_mock_RawSnapshot.cc255
-rw-r--r--src/test/librbd/migration/test_mock_S3Stream.cc238
-rw-r--r--src/test/librbd/migration/test_mock_Utils.cc47
8 files changed, 3619 insertions, 0 deletions
diff --git a/src/test/librbd/migration/test_mock_FileStream.cc b/src/test/librbd/migration/test_mock_FileStream.cc
new file mode 100644
index 000000000..a5bdfebe4
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_FileStream.cc
@@ -0,0 +1,213 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/FileStream.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+#include <filesystem>
+
+namespace fs = std::filesystem;
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+} // namespace librbd
+
+#include "librbd/migration/FileStream.cc"
+
+namespace librbd {
+namespace migration {
+
+using ::testing::Invoke;
+
+class TestMockMigrationFileStream : public TestMockFixture {
+public:
+ typedef FileStream<MockTestImageCtx> MockFileStream;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+ file_name = fs::temp_directory_path() / "TestMockMigrationFileStream";
+ file_name += stringify(getpid());
+ json_object["file_path"] = file_name;
+ }
+
+ void TearDown() override {
+ fs::remove(file_name);
+ TestMockFixture::TearDown();
+ }
+
+ std::string file_name;
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationFileStream, OpenClose) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist bl;
+ ASSERT_EQ(0, bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_file_stream.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, GetSize) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(128, '1'));
+ ASSERT_EQ(0, expect_bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ uint64_t size;
+ mock_file_stream.get_size(&size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(128, size);
+
+ C_SaferCond ctx3;
+ mock_file_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(128, '1'));
+ ASSERT_EQ(0, expect_bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ bufferlist bl;
+ mock_file_stream.read({{0, 128}}, &bl, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_file_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, SeekRead) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist write_bl;
+ write_bl.append(std::string(32, '1'));
+ write_bl.append(std::string(64, '2'));
+ write_bl.append(std::string(16, '3'));
+ ASSERT_EQ(0, write_bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ bufferlist bl;
+ mock_file_stream.read({{96, 16}, {32, 64}, {0, 32}}, &bl, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(16, '3'));
+ expect_bl.append(std::string(64, '2'));
+ expect_bl.append(std::string(32, '1'));
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_file_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, DNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(-ENOENT, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_file_stream.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, SeekError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist bl;
+ ASSERT_EQ(0, bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_file_stream.read({{128, 128}}, &bl, &ctx2);
+ ASSERT_EQ(-ERANGE, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_file_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationFileStream, ShortReadError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(128, '1'));
+ ASSERT_EQ(0, expect_bl.write_file(file_name.c_str()));
+
+ MockFileStream mock_file_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_file_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ bufferlist bl;
+ mock_file_stream.read({{0, 256}}, &bl, &ctx2);
+ ASSERT_EQ(-ERANGE, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_file_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_HttpClient.cc b/src/test/librbd/migration/test_mock_HttpClient.cc
new file mode 100644
index 000000000..f3888755c
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_HttpClient.cc
@@ -0,0 +1,890 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/HttpClient.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include <unistd.h>
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/beast/core.hpp>
+#include <boost/beast/http.hpp>
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace util {
+
+inline ImageCtx *get_image_ctx(MockTestImageCtx *image_ctx) {
+ return image_ctx->image_ctx;
+}
+
+} // namespace util
+} // namespace librbd
+
+#include "librbd/migration/HttpClient.cc"
+
+using EmptyHttpRequest = boost::beast::http::request<
+ boost::beast::http::empty_body>;
+using HttpResponse = boost::beast::http::response<
+ boost::beast::http::string_body>;
+
+namespace boost {
+namespace beast {
+namespace http {
+
+template <typename Body>
+bool operator==(const boost::beast::http::request<Body>& lhs,
+ const boost::beast::http::request<Body>& rhs) {
+ return (lhs.method() == rhs.method() &&
+ lhs.target() == rhs.target());
+}
+
+template <typename Body>
+bool operator==(const boost::beast::http::response<Body>& lhs,
+ const boost::beast::http::response<Body>& rhs) {
+ return (lhs.result() == rhs.result() &&
+ lhs.body() == rhs.body());
+}
+
+} // namespace http
+} // namespace beast
+} // namespace boost
+
+namespace librbd {
+namespace migration {
+
+using ::testing::Invoke;
+
+class TestMockMigrationHttpClient : public TestMockFixture {
+public:
+ typedef HttpClient<MockTestImageCtx> MockHttpClient;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ create_acceptor(false);
+ }
+
+ void TearDown() override {
+ m_acceptor.reset();
+
+ TestMockFixture::TearDown();
+ }
+
+ // if we have a racing where another thread manages to bind and listen the
+ // port picked by this acceptor, try again.
+ static constexpr int MAX_BIND_RETRIES = 60;
+
+ void create_acceptor(bool reuse) {
+ for (int retries = 0;; retries++) {
+ try {
+ m_acceptor.emplace(*m_image_ctx->asio_engine,
+ boost::asio::ip::tcp::endpoint(
+ boost::asio::ip::tcp::v4(), m_server_port), reuse);
+ // yay!
+ break;
+ } catch (const boost::system::system_error& e) {
+ if (retries == MAX_BIND_RETRIES) {
+ throw;
+ }
+ if (e.code() != boost::system::errc::address_in_use) {
+ throw;
+ }
+ }
+ // backoff a little bit
+ sleep(1);
+ }
+ m_server_port = m_acceptor->local_endpoint().port();
+ }
+
+ std::string get_local_url(UrlScheme url_scheme) {
+ std::stringstream sstream;
+ switch (url_scheme) {
+ case URL_SCHEME_HTTP:
+ sstream << "http://127.0.0.1";
+ break;
+ case URL_SCHEME_HTTPS:
+ sstream << "https://localhost";
+ break;
+ default:
+ ceph_assert(false);
+ break;
+ }
+
+ sstream << ":" << m_server_port << "/target";
+ return sstream.str();
+ }
+
+ void client_accept(boost::asio::ip::tcp::socket* socket, bool close,
+ Context* on_connect) {
+ m_acceptor->async_accept(
+ boost::asio::make_strand(m_image_ctx->asio_engine->get_executor()),
+ [socket, close, on_connect]
+ (auto ec, boost::asio::ip::tcp::socket in_socket) {
+ if (close) {
+ in_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
+ } else {
+ ASSERT_FALSE(ec) << "Unexpected error: " << ec;
+ *socket = std::move(in_socket);
+ }
+ on_connect->complete(0);
+ });
+ }
+
+ template <typename Body>
+ void client_read_request(boost::asio::ip::tcp::socket& socket,
+ boost::beast::http::request<Body>& expected_req) {
+ boost::beast::http::request<Body> req;
+ boost::beast::error_code ec;
+ boost::beast::http::read(socket, m_buffer, req, ec);
+ ASSERT_FALSE(ec) << "Unexpected errror: " << ec;
+
+ expected_req.target("/target");
+ ASSERT_EQ(expected_req, req);
+ }
+
+ void client_write_response(boost::asio::ip::tcp::socket& socket,
+ HttpResponse& expected_res) {
+ expected_res.set(boost::beast::http::field::server,
+ BOOST_BEAST_VERSION_STRING);
+ expected_res.set(boost::beast::http::field::content_type, "text/plain");
+ expected_res.content_length(expected_res.body().size());
+ expected_res.prepare_payload();
+
+ boost::beast::error_code ec;
+ boost::beast::http::write(socket, expected_res, ec);
+ ASSERT_FALSE(ec) << "Unexpected errror: " << ec;
+ }
+
+ template <typename Stream>
+ void client_ssl_handshake(Stream& stream, bool ignore_failure,
+ Context* on_handshake) {
+ stream.async_handshake(
+ boost::asio::ssl::stream_base::server,
+ [ignore_failure, on_handshake](auto ec) {
+ ASSERT_FALSE(!ignore_failure && ec) << "Unexpected error: " << ec;
+ on_handshake->complete(-ec.value());
+ });
+ }
+
+ template <typename Stream>
+ void client_ssl_shutdown(Stream& stream, Context* on_shutdown) {
+ stream.async_shutdown(
+ [on_shutdown](auto ec) {
+ ASSERT_FALSE(ec) << "Unexpected error: " << ec;
+ on_shutdown->complete(-ec.value());
+ });
+ }
+
+ void load_server_certificate(boost::asio::ssl::context& ctx) {
+ ctx.set_options(
+ boost::asio::ssl::context::default_workarounds |
+ boost::asio::ssl::context::no_sslv2 |
+ boost::asio::ssl::context::single_dh_use);
+ ctx.use_certificate_chain(
+ boost::asio::buffer(CERTIFICATE.data(), CERTIFICATE.size()));
+ ctx.use_private_key(
+ boost::asio::buffer(KEY.data(), KEY.size()),
+ boost::asio::ssl::context::file_format::pem);
+ ctx.use_tmp_dh(
+ boost::asio::buffer(DH.data(), DH.size()));
+ }
+
+ // dummy self-signed cert for localhost
+ const std::string CERTIFICATE =
+ "-----BEGIN CERTIFICATE-----\n"
+ "MIIDXzCCAkegAwIBAgIUYH6rAaq66LC6yJ3XK1WEMIfmY4cwDQYJKoZIhvcNAQEL\n"
+ "BQAwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMQ8wDQYDVQQHDAZNY0xlYW4x\n"
+ "EjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0yMDExMDIyMTM5NTVaFw00ODAzMjAyMTM5\n"
+ "NTVaMD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJWQTEPMA0GA1UEBwwGTWNMZWFu\n"
+ "MRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n"
+ "AoIBAQCeRkyxjP0eNHxzj4/R+Bg/31p7kEjB5d/LYtrzBIYNe+3DN8gdReixEpR5\n"
+ "lgTLDsl8gfk2HRz4cnAiseqYL6GKtw/cFadzLyXTbW4iavmTWiGYw/8RJlKunbhA\n"
+ "hDjM6H99ysLf0NS6t14eK+bEJIW1PiTYRR1U5I4kSIjpCX7+nJVuwMEZ2XBpN3og\n"
+ "nHhv2hZYTdzEkQEyZHz4V/ApfD7rlja5ecd/vJfPJeA8nudnGCh3Uo6f8I9TObAj\n"
+ "8hJdfRiRBvnA4NnkrMrxW9UtVjScnw9Xia11FM/IGJIgMpLQ5dqBw930p6FxMYtn\n"
+ "tRD1AF9sT+YjoCaHv0hXZvBEUEF3AgMBAAGjUzBRMB0GA1UdDgQWBBTQoIiX3+p/\n"
+ "P4Xz2vwERz6pbjPGhzAfBgNVHSMEGDAWgBTQoIiX3+p/P4Xz2vwERz6pbjPGhzAP\n"
+ "BgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCVKoYAw+D1qqWRDSh3\n"
+ "2KlKMnT6sySo7XmReGArj8FTKiZUprByj5CfAtaiDSdPOpcg3EazWbdasZbMmSQm\n"
+ "+jpe5WoKnxL9b12lwwUYHrLl6RlrDHVkIVlXLNbJFY5TpfjvZfHpwVAygF3fnbgW\n"
+ "PPuODUNAS5NDwST+t29jBZ/wwU0pyW0CS4K5d3XMGHBc13j2V/FyvmsZ5xfA4U9H\n"
+ "oEnmZ/Qm+FFK/nR40rTAZ37cuv4ysKFtwvatNgTfHGJwaBUkKFdDbcyxt9abCi6x\n"
+ "/K+ScoJtdIeVcfx8Fnc5PNtSpy8bHI3Zy4IEyw4kOqwwI1h37iBafZ2WdQkTxlAx\n"
+ "JIDj\n"
+ "-----END CERTIFICATE-----\n";
+ const std::string KEY =
+ "-----BEGIN PRIVATE KEY-----\n"
+ "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCeRkyxjP0eNHxz\n"
+ "j4/R+Bg/31p7kEjB5d/LYtrzBIYNe+3DN8gdReixEpR5lgTLDsl8gfk2HRz4cnAi\n"
+ "seqYL6GKtw/cFadzLyXTbW4iavmTWiGYw/8RJlKunbhAhDjM6H99ysLf0NS6t14e\n"
+ "K+bEJIW1PiTYRR1U5I4kSIjpCX7+nJVuwMEZ2XBpN3ognHhv2hZYTdzEkQEyZHz4\n"
+ "V/ApfD7rlja5ecd/vJfPJeA8nudnGCh3Uo6f8I9TObAj8hJdfRiRBvnA4NnkrMrx\n"
+ "W9UtVjScnw9Xia11FM/IGJIgMpLQ5dqBw930p6FxMYtntRD1AF9sT+YjoCaHv0hX\n"
+ "ZvBEUEF3AgMBAAECggEACCaYpoAbPOX5Dr5y6p47KXboIvrgNFQRPVke62rtOF6M\n"
+ "dQQ3YwKJpCzPxp8qKgbd63KKEfZX2peSHMdKzIGPcSRSRcQ7tlvUN9on1M/rgGIg\n"
+ "3swhI5H0qhdnOLNWdX73qdO6S2pmuiLdTvJ11N4IoLfNj/GnPAr1Ivs1ScL6bkQv\n"
+ "UybaNQ/g2lB0tO7vUeVe2W/AqsIb1eQlf2g+SH7xRj2bGQkr4cWTylqfiVoL/Xic\n"
+ "QVTCks3BWaZhYIhTFgvqVhXZpp52O9J+bxsWJItKQrrCBemxwp82xKbiW/KoI9L1\n"
+ "wSnKvxx7Q3RUN5EvXeOpTRR8QIpBoxP3TTeoj+EOMQKBgQDQb/VfLDlLgfYJpgRC\n"
+ "hKCLW90un9op3nA2n9Dmm9TTLYOmUyiv5ub8QDINEw/YK/NE2JsTSUk2msizqTLL\n"
+ "Z82BFbz9kPlDbJ5MgxG5zXeLvOLurAFmZk/z5JJO+65PKjf0QVLncSAJvMCeNFuC\n"
+ "2yZrEzbrItrjQsN6AedWdx6TTwKBgQDCZAsSI3lQgOh2q1GSxjuIzRAc7JnSGBvD\n"
+ "nG8+SkfKAy7BWe638772Dgx8KYO7TLI4zlm8c9Tr/nkZsGWmM5S2DMI69PWOQWNa\n"
+ "R6QzOFFwNg2JETH7ow+x8+9Q9d3WsPzROz3r5uDXgEk0glthaymVoPILFOiYpz3r\n"
+ "heUbd6mFWQKBgQCCJBVJGhyf54IOHij0u0heGrpr/QTDNY5MnNZa1hs4y2cydyOl\n"
+ "SH8aKp7ViPxQlYhriO6ySQS8YkJD4rXDSImIOmFo1Ja9oVjpHsD3iLFGf2YVbTHm\n"
+ "lKUA+8raI8x+wzZyfELeHMTLL534aWpltp0zJ6kXgQi38pyIVh3x36gogwKBgQCt\n"
+ "nba5k49VVFzLKEXqBjzD+QqMGtFjcH7TnZNJmgQ2K9OFgzIPf5atomyKNHXgQibn\n"
+ "T32cMAQaZqR4SjDvWSBX3FtZVtE+Ja57woKn8IPj6ZL7Oa1fpwpskIbM01s31cln\n"
+ "gjbSy9lC/+PiDw9YmeKBLkcfmKQJO021Xlf6yUxRuQKBgBWPODUO8oKjkuJXNI/w\n"
+ "El9hNWkd+/SZDfnt93dlVyXTtTF0M5M95tlOgqvLtWKSyB/BOnoZYWqR8luMl15d\n"
+ "bf75j5mB0lHMWtyQgvZSkFqe9Or7Zy7hfTShDlZ/w+OXK7PGesaE1F14irShXSji\n"
+ "yn5DZYAZ5pU52xreJeYvDngO\n"
+ "-----END PRIVATE KEY-----\n";
+ const std::string DH =
+ "-----BEGIN DH PARAMETERS-----\n"
+ "MIIBCAKCAQEA4+DA1j0gDWS71okwHpnvA65NmmR4mf+B3H39g163zY5S+cnWS2LI\n"
+ "dvqnUDpw13naWtQ+Nu7I4rk1XoPaxOPSTu1MTbtYOxxU9M1ceBu4kQjDeHwasPVM\n"
+ "zyEs1XXX3tsbPUxAuayX+AgW6QQAQUEjKDnv3FzVnQTFjwI49LqjnrSjbgQcoMaH\n"
+ "EdGGUc6t1/We2vtsJZx0/dbaMkzFYO8dAbEYHL4sPKQb2mLpCPJZC3vwzpFkHFCd\n"
+ "QSnLW2qRhy+66Mf8shdr6uvpoMcnKMOAvjKdXl9PBeJM9eJPz2lC4tnTiM3DqNzK\n"
+ "Hn8+Pu3KkSIFL/5uBVu1fZSq+lFIEI23wwIBAg==\n"
+ "-----END DH PARAMETERS-----\n";
+
+ librbd::ImageCtx *m_image_ctx;
+
+ std::optional<boost::asio::ip::tcp::acceptor> m_acceptor;
+ boost::beast::flat_buffer m_buffer;
+ uint64_t m_server_port = 0;
+};
+
+TEST_F(TestMockMigrationHttpClient, OpenCloseHttp) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ http_client.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, OpenCloseHttps) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTPS));
+ http_client.set_ignore_self_signed_cert(true);
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+
+ boost::asio::ssl::context ssl_context{boost::asio::ssl::context::tlsv12};
+ load_server_certificate(ssl_context);
+ boost::beast::ssl_stream<boost::beast::tcp_stream> ssl_stream{
+ std::move(socket), ssl_context};
+
+ C_SaferCond on_ssl_handshake_ctx;
+ client_ssl_handshake(ssl_stream, false, &on_ssl_handshake_ctx);
+ ASSERT_EQ(0, on_ssl_handshake_ctx.wait());
+
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ http_client.close(&ctx2);
+
+ C_SaferCond on_ssl_shutdown_ctx;
+ client_ssl_shutdown(ssl_stream, &on_ssl_shutdown_ctx);
+ ASSERT_EQ(0, on_ssl_shutdown_ctx.wait());
+
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, OpenHttpsHandshakeFail) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTPS));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+
+ boost::asio::ssl::context ssl_context{boost::asio::ssl::context::tlsv12};
+ load_server_certificate(ssl_context);
+ boost::beast::ssl_stream<boost::beast::tcp_stream> ssl_stream{
+ std::move(socket), ssl_context};
+
+ C_SaferCond on_ssl_handshake_ctx;
+ client_ssl_handshake(ssl_stream, true, &on_ssl_handshake_ctx);
+ ASSERT_NE(0, on_ssl_handshake_ctx.wait());
+ ASSERT_NE(0, ctx1.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, OpenInvalidUrl) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx, "ftp://nope/");
+
+ C_SaferCond ctx;
+ http_client.open(&ctx);
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, OpenResolveFail) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx, "http://foo.example");
+
+ C_SaferCond ctx;
+ http_client.open(&ctx);
+ ASSERT_EQ(-ENOENT, ctx.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, OpenConnectFail) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ "http://localhost:2/");
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(-ECONNREFUSED, ctx1.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssueHead) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::head);
+
+ C_SaferCond ctx2;
+ HttpResponse res;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2, &res](int r, HttpResponse&& response) mutable {
+ res = std::move(response);
+ ctx2.complete(r);
+ });
+
+ HttpResponse expected_res;
+ client_read_request(socket, req);
+ client_write_response(socket, expected_res);
+
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(expected_res, res);
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssueGet) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ HttpResponse res;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2, &res](int r, HttpResponse&& response) mutable {
+ res = std::move(response);
+ ctx2.complete(r);
+ });
+
+ HttpResponse expected_res;
+ expected_res.body() = "test";
+ client_read_request(socket, req);
+ client_write_response(socket, expected_res);
+
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(expected_res, res);
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssueSendFailed) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx1;
+ client_accept(&socket, false, &on_connect_ctx1);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx1.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ // close connection to client
+ boost::system::error_code ec;
+ socket.close(ec);
+
+ C_SaferCond on_connect_ctx2;
+ client_accept(&socket, false, &on_connect_ctx2);
+
+ // send request via closed connection
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2](int r, HttpResponse&&) mutable {
+ ctx2.complete(r);
+ });
+
+ // connection will be reset and request retried
+ ASSERT_EQ(0, on_connect_ctx2.wait());
+ HttpResponse expected_res;
+ expected_res.body() = "test";
+ client_read_request(socket, req);
+ client_write_response(socket, expected_res);
+ ASSERT_EQ(0, ctx2.wait());
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssueReceiveFailed) {
+ boost::asio::ip::tcp::socket socket1(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx1;
+ client_accept(&socket1, false, &on_connect_ctx1);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx1.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ // send request via closed connection
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2](int r, HttpResponse&&) mutable {
+ ctx2.complete(r);
+ });
+
+ // close connection to client after reading request
+ client_read_request(socket1, req);
+
+ C_SaferCond on_connect_ctx2;
+ boost::asio::ip::tcp::socket socket2(*m_image_ctx->asio_engine);
+ client_accept(&socket2, false, &on_connect_ctx2);
+
+ boost::system::error_code ec;
+ socket1.close(ec);
+ ASSERT_EQ(0, on_connect_ctx2.wait());
+
+ // connection will be reset and request retried
+ HttpResponse expected_res;
+ expected_res.body() = "test";
+ client_read_request(socket2, req);
+ client_write_response(socket2, expected_res);
+ ASSERT_EQ(0, ctx2.wait());
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssueResetFailed) {
+ m_server_port = 0;
+ create_acceptor(true);
+
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx1;
+ client_accept(&socket, false, &on_connect_ctx1);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx1.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ // send requests then close connection
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2](int r, HttpResponse&&) mutable {
+ ctx2.complete(r);
+ });
+
+ C_SaferCond ctx3;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx3](int r, HttpResponse&&) mutable {
+ ctx3.complete(r);
+ });
+
+ client_read_request(socket, req);
+ client_read_request(socket, req);
+
+ // close connection to client and verify requests are failed
+ m_acceptor.reset();
+ boost::system::error_code ec;
+ socket.close(ec);
+
+ ASSERT_EQ(-ECONNREFUSED, ctx2.wait());
+ ASSERT_EQ(-ECONNREFUSED, ctx3.wait());
+
+ // additional request will retry the failed connection
+ create_acceptor(true);
+
+ C_SaferCond on_connect_ctx2;
+ client_accept(&socket, false, &on_connect_ctx2);
+
+ C_SaferCond ctx4;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx4](int r, HttpResponse&&) mutable {
+ ctx4.complete(r);
+ });
+
+ ASSERT_EQ(0, on_connect_ctx2.wait());
+ client_read_request(socket, req);
+
+ HttpResponse expected_res;
+ expected_res.body() = "test";
+ client_write_response(socket, expected_res);
+ ASSERT_EQ(0, ctx4.wait());
+
+ C_SaferCond ctx5;
+ http_client.close(&ctx5);
+ ASSERT_EQ(0, ctx5.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssuePipelined) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ // issue two pipelined (concurrent) get requests
+ EmptyHttpRequest req1;
+ req1.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ HttpResponse res1;
+ http_client.issue(EmptyHttpRequest{req1},
+ [&ctx2, &res1](int r, HttpResponse&& response) mutable {
+ res1 = std::move(response);
+ ctx2.complete(r);
+ });
+
+ EmptyHttpRequest req2;
+ req2.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx3;
+ HttpResponse res2;
+ http_client.issue(EmptyHttpRequest{req2},
+ [&ctx3, &res2](int r, HttpResponse&& response) mutable {
+ res2 = std::move(response);
+ ctx3.complete(r);
+ });
+
+ client_read_request(socket, req1);
+ client_read_request(socket, req2);
+
+ // read the responses sequentially
+ HttpResponse expected_res1;
+ expected_res1.body() = "test";
+ client_write_response(socket, expected_res1);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(expected_res1, res1);
+
+ HttpResponse expected_res2;
+ expected_res2.body() = "test";
+ client_write_response(socket, expected_res2);
+ ASSERT_EQ(0, ctx3.wait());
+ ASSERT_EQ(expected_res2, res2);
+
+ C_SaferCond ctx4;
+ http_client.close(&ctx4);
+ ASSERT_EQ(0, ctx4.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, IssuePipelinedRestart) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx1;
+ client_accept(&socket, false, &on_connect_ctx1);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx1.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ // issue two pipelined (concurrent) get requests
+ EmptyHttpRequest req1;
+ req1.keep_alive(false);
+ req1.method(boost::beast::http::verb::get);
+
+ C_SaferCond on_connect_ctx2;
+ client_accept(&socket, false, &on_connect_ctx2);
+
+ C_SaferCond ctx2;
+ HttpResponse res1;
+ http_client.issue(EmptyHttpRequest{req1},
+ [&ctx2, &res1](int r, HttpResponse&& response) mutable {
+ res1 = std::move(response);
+ ctx2.complete(r);
+ });
+
+ EmptyHttpRequest req2;
+ req2.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx3;
+ HttpResponse res2;
+ http_client.issue(EmptyHttpRequest{req2},
+ [&ctx3, &res2](int r, HttpResponse&& response) mutable {
+ res2 = std::move(response);
+ ctx3.complete(r);
+ });
+
+ client_read_request(socket, req1);
+ client_read_request(socket, req2);
+
+ // read the responses sequentially
+ HttpResponse expected_res1;
+ expected_res1.body() = "test";
+ expected_res1.keep_alive(false);
+ client_write_response(socket, expected_res1);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(expected_res1, res1);
+
+ // second request will need to be re-sent due to 'need_eof' condition
+ ASSERT_EQ(0, on_connect_ctx2.wait());
+ client_read_request(socket, req2);
+
+ HttpResponse expected_res2;
+ expected_res2.body() = "test";
+ client_write_response(socket, expected_res2);
+ ASSERT_EQ(0, ctx3.wait());
+ ASSERT_EQ(expected_res2, res2);
+
+ C_SaferCond ctx4;
+ http_client.close(&ctx4);
+ ASSERT_EQ(0, ctx4.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, ShutdownInFlight) {
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ EmptyHttpRequest req;
+ req.method(boost::beast::http::verb::get);
+
+ C_SaferCond ctx2;
+ http_client.issue(EmptyHttpRequest{req},
+ [&ctx2](int r, HttpResponse&&) mutable {
+ ctx2.complete(r);
+ });
+
+ client_read_request(socket, req);
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+ ASSERT_EQ(-ESHUTDOWN, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, GetSize) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ uint64_t size = 0;
+ C_SaferCond ctx2;
+ http_client.get_size(&size, &ctx2);
+
+ EmptyHttpRequest expected_req;
+ expected_req.method(boost::beast::http::verb::head);
+ client_read_request(socket, expected_req);
+
+ HttpResponse expected_res;
+ expected_res.body() = std::string(123, '1');
+ client_write_response(socket, expected_res);
+
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(123, size);
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, GetSizeError) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ uint64_t size = 0;
+ C_SaferCond ctx2;
+ http_client.get_size(&size, &ctx2);
+
+ EmptyHttpRequest expected_req;
+ expected_req.method(boost::beast::http::verb::head);
+ client_read_request(socket, expected_req);
+
+ HttpResponse expected_res;
+ expected_res.result(boost::beast::http::status::internal_server_error);
+ client_write_response(socket, expected_res);
+
+ ASSERT_EQ(-EIO, ctx2.wait());
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpClient, Read) {
+ MockTestImageCtx mock_test_image_ctx(*m_image_ctx);
+ MockHttpClient http_client(&mock_test_image_ctx,
+ get_local_url(URL_SCHEME_HTTP));
+
+ boost::asio::ip::tcp::socket socket(*m_image_ctx->asio_engine);
+ C_SaferCond on_connect_ctx;
+ client_accept(&socket, false, &on_connect_ctx);
+
+ C_SaferCond ctx1;
+ http_client.open(&ctx1);
+ ASSERT_EQ(0, on_connect_ctx.wait());
+ ASSERT_EQ(0, ctx1.wait());
+
+ bufferlist bl;
+ C_SaferCond ctx2;
+ http_client.read({{0, 128}, {256, 64}}, &bl, &ctx2);
+
+ EmptyHttpRequest expected_req1;
+ expected_req1.method(boost::beast::http::verb::get);
+ expected_req1.set(boost::beast::http::field::range, "bytes=0-127");
+ client_read_request(socket, expected_req1);
+
+ EmptyHttpRequest expected_req2;
+ expected_req2.method(boost::beast::http::verb::get);
+ expected_req2.set(boost::beast::http::field::range, "bytes=256-319");
+ client_read_request(socket, expected_req2);
+
+ HttpResponse expected_res1;
+ expected_res1.result(boost::beast::http::status::partial_content);
+ expected_res1.body() = std::string(128, '1');
+ client_write_response(socket, expected_res1);
+
+ HttpResponse expected_res2;
+ expected_res2.result(boost::beast::http::status::partial_content);
+ expected_res2.body() = std::string(64, '2');
+ client_write_response(socket, expected_res2);
+
+ ASSERT_EQ(192, ctx2.wait());
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(128, '1'));
+ expect_bl.append(std::string(64, '2'));
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ http_client.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_HttpStream.cc b/src/test/librbd/migration/test_mock_HttpStream.cc
new file mode 100644
index 000000000..aff22b757
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_HttpStream.cc
@@ -0,0 +1,194 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/HttpClient.h"
+#include "librbd/migration/HttpStream.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+#include <boost/beast/http.hpp>
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace migration {
+
+template <>
+struct HttpClient<MockTestImageCtx> {
+ static HttpClient* s_instance;
+ static HttpClient* create(MockTestImageCtx*, const std::string&) {
+ ceph_assert(s_instance != nullptr);
+ return s_instance;
+ }
+
+ MOCK_METHOD1(open, void(Context*));
+ MOCK_METHOD1(close, void(Context*));
+ MOCK_METHOD2(get_size, void(uint64_t*, Context*));
+ MOCK_METHOD3(do_read, void(const io::Extents&, bufferlist*, Context*));
+ void read(io::Extents&& extents, bufferlist* bl, Context* ctx) {
+ do_read(extents, bl, ctx);
+ }
+
+ HttpClient() {
+ s_instance = this;
+ }
+};
+
+HttpClient<MockTestImageCtx>* HttpClient<MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace migration
+} // namespace librbd
+
+#include "librbd/migration/HttpStream.cc"
+
+namespace librbd {
+namespace migration {
+
+using ::testing::_;
+using ::testing::Invoke;
+using ::testing::InSequence;
+using ::testing::WithArgs;
+
+class TestMockMigrationHttpStream : public TestMockFixture {
+public:
+ typedef HttpStream<MockTestImageCtx> MockHttpStream;
+ typedef HttpClient<MockTestImageCtx> MockHttpClient;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+ json_object["url"] = "http://some.site/file";
+ }
+
+ void expect_open(MockHttpClient& mock_http_client, int r) {
+ EXPECT_CALL(mock_http_client, open(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_close(MockHttpClient& mock_http_client, int r) {
+ EXPECT_CALL(mock_http_client, close(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_get_size(MockHttpClient& mock_http_client, uint64_t size, int r) {
+ EXPECT_CALL(mock_http_client, get_size(_, _))
+ .WillOnce(Invoke([size, r](uint64_t* out_size, Context* ctx) {
+ *out_size = size;
+ ctx->complete(r);
+ }));
+ }
+
+ void expect_read(MockHttpClient& mock_http_client, io::Extents byte_extents,
+ const bufferlist& bl, int r) {
+ uint64_t len = 0;
+ for (auto [_, byte_len] : byte_extents) {
+ len += byte_len;
+ }
+ EXPECT_CALL(mock_http_client, do_read(byte_extents, _, _))
+ .WillOnce(WithArgs<1, 2>(Invoke(
+ [len, bl, r](bufferlist* out_bl, Context* ctx) {
+ *out_bl = bl;
+ ctx->complete(r < 0 ? r : len);
+ })));
+ }
+
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationHttpStream, OpenClose) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockHttpStream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_http_stream.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationHttpStream, GetSize) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ expect_get_size(*mock_http_client, 128, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockHttpStream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ uint64_t size;
+ mock_http_stream.get_size(&size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(128, size);
+
+ C_SaferCond ctx3;
+ mock_http_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationHttpStream, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(192, '1'));
+ expect_read(*mock_http_client, {{0, 128}, {256, 64}}, expect_bl, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockHttpStream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ bufferlist bl;
+ mock_http_stream.read({{0, 128}, {256, 64}}, &bl, &ctx2);
+ ASSERT_EQ(192, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_http_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_QCOWFormat.cc b/src/test/librbd/migration/test_mock_QCOWFormat.cc
new file mode 100644
index 000000000..6e7225d22
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_QCOWFormat.cc
@@ -0,0 +1,1259 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "test/librbd/mock/migration/MockStreamInterface.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/QCOWFormat.h"
+#include "librbd/migration/SourceSpecBuilder.h"
+#include "acconfig.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace migration {
+
+template<>
+struct SourceSpecBuilder<librbd::MockTestImageCtx> {
+
+ MOCK_CONST_METHOD2(build_stream, int(const json_spirit::mObject&,
+ std::shared_ptr<StreamInterface>*));
+
+};
+
+} // namespace migration
+
+bool operator==(const SnapInfo& lhs, const SnapInfo& rhs) {
+ return (lhs.name == rhs.name &&
+ lhs.size == rhs.size);
+}
+
+} // namespace librbd
+
+#include "librbd/migration/QCOWFormat.cc"
+
+using ::testing::_;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::ReturnRef;
+using ::testing::WithArg;
+using ::testing::WithArgs;
+
+namespace librbd {
+namespace migration {
+
+using ::testing::Invoke;
+
+class TestMockMigrationQCOWFormat : public TestMockFixture {
+public:
+ typedef QCOWFormat<MockTestImageCtx> MockQCOWFormat;
+ typedef SourceSpecBuilder<MockTestImageCtx> MockSourceSpecBuilder;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ json_spirit::mObject stream_obj;
+ stream_obj["type"] = "file";
+ json_object["stream"] = stream_obj;
+ }
+
+ void expect_build_stream(MockSourceSpecBuilder& mock_source_spec_builder,
+ MockStreamInterface* mock_stream_interface, int r) {
+ EXPECT_CALL(mock_source_spec_builder, build_stream(_, _))
+ .WillOnce(WithArgs<1>(Invoke([mock_stream_interface, r]
+ (std::shared_ptr<StreamInterface>* ptr) {
+ ptr->reset(mock_stream_interface);
+ return r;
+ })));
+ }
+
+ void expect_stream_open(MockStreamInterface& mock_stream_interface, int r) {
+ EXPECT_CALL(mock_stream_interface, open(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_stream_close(MockStreamInterface& mock_stream_interface, int r) {
+ EXPECT_CALL(mock_stream_interface, close(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_stream_read(MockStreamInterface& mock_stream_interface,
+ const io::Extents& byte_extents,
+ const bufferlist& bl, int r) {
+ EXPECT_CALL(mock_stream_interface, read(byte_extents, _, _))
+ .WillOnce(WithArgs<1, 2>(Invoke([bl, r]
+ (bufferlist* out_bl, Context* ctx) {
+ *out_bl = bl;
+ ctx->complete(r);
+ })));
+ }
+
+ void expect_probe_header(MockStreamInterface& mock_stream_interface,
+ uint32_t magic, uint32_t version, int r) {
+ magic = htobe32(magic);
+ version = htobe32(version);
+
+ bufferlist probe_bl;
+ probe_bl.append(std::string_view(reinterpret_cast<char*>(&magic), 4));
+ probe_bl.append(std::string_view(reinterpret_cast<char*>(&version), 4));
+ expect_stream_read(mock_stream_interface, {{0, 8}}, probe_bl, r);
+ }
+
+ void expect_read_header(MockStreamInterface& mock_stream_interface,
+ uint32_t snapshot_count, int r) {
+ QCowHeader qcow_header;
+ memset(&qcow_header, 0, sizeof(qcow_header));
+ qcow_header.magic = htobe32(QCOW_MAGIC);
+ qcow_header.version = htobe32(2);
+ qcow_header.size = htobe64(1<<30);
+ qcow_header.cluster_bits = htobe32(16);
+ qcow_header.l1_size = htobe32(2);
+ qcow_header.l1_table_offset = htobe64(1<<20);
+ if (snapshot_count > 0) {
+ qcow_header.nb_snapshots = htobe32(snapshot_count);
+ qcow_header.snapshots_offset = htobe64(1<<21);
+ }
+
+ bufferlist header_bl;
+ header_bl.append(std::string_view(reinterpret_cast<char*>(&qcow_header),
+ sizeof(qcow_header)));
+ expect_stream_read(mock_stream_interface, {{0, sizeof(qcow_header)}},
+ header_bl, r);
+ }
+
+ void expect_read_l1_table(MockStreamInterface& mock_stream_interface,
+ std::optional<std::vector<uint64_t>>&& l1_table,
+ int r) {
+ bufferlist l1_table_bl;
+ if (!l1_table) {
+ l1_table.emplace(2);
+ }
+
+ l1_table->resize(2);
+ for (size_t idx = 0; idx < l1_table->size(); ++idx) {
+ (*l1_table)[idx] = htobe64((*l1_table)[idx]);
+ }
+
+ l1_table_bl.append(
+ std::string_view(reinterpret_cast<char*>(l1_table->data()), 16));
+ expect_stream_read(mock_stream_interface, {{1<<20, 16}}, l1_table_bl, r);
+ }
+
+ void expect_read_l2_table(MockStreamInterface& mock_stream_interface,
+ uint64_t l2_table_offset,
+ std::optional<std::vector<uint64_t>>&& l2_table,
+ int r) {
+ size_t l2_table_size = 1<<(16 - 3); // cluster_bit - 3 bits for offset
+ bufferlist l2_table_bl;
+ if (!l2_table) {
+ l2_table.emplace(l2_table_size);
+ }
+
+ l2_table->resize(l2_table_size);
+ for (size_t idx = 0; idx < l2_table->size(); ++idx) {
+ (*l2_table)[idx] = htobe64((*l2_table)[idx]);
+ }
+
+ l2_table_bl.append(
+ std::string_view(reinterpret_cast<char*>(l2_table->data()),
+ l2_table->size() * sizeof(uint64_t)));
+ expect_stream_read(mock_stream_interface,
+ {{l2_table_offset, l2_table->size() * sizeof(uint64_t)}},
+ l2_table_bl, r);
+ }
+
+ void expect_read_cluster(MockStreamInterface& mock_stream_interface,
+ uint64_t cluster_offset, uint32_t offset,
+ const bufferlist& bl, int r) {
+ uint32_t cluster_size = 1<<16;
+
+ bufferlist cluster_bl;
+ if (offset > 0) {
+ cluster_size -= offset;
+ cluster_bl.append_zero(offset);
+ }
+
+ cluster_size -= bl.length();
+ cluster_bl.append(bl);
+
+ if (cluster_size > 0) {
+ cluster_bl.append_zero(cluster_size);
+ }
+
+ expect_stream_read(mock_stream_interface,
+ {{cluster_offset, cluster_bl.length()}}, cluster_bl, r);
+ }
+
+ void expect_open(MockStreamInterface& mock_stream_interface,
+ std::optional<std::vector<uint64_t>>&& l1_table) {
+ expect_stream_open(mock_stream_interface, 0);
+
+ expect_probe_header(mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(mock_stream_interface, 0, 0);
+
+ expect_read_l1_table(mock_stream_interface, std::move(l1_table), 0);
+ }
+
+ void expect_read_snapshot_header(MockStreamInterface& mock_stream_interface,
+ const std::string& id,
+ const std::string& name,
+ uint64_t l1_table_offset,
+ uint64_t* snapshot_offset, int r) {
+ QCowSnapshotHeader snapshot_header;
+ memset(&snapshot_header, 0, sizeof(snapshot_header));
+ snapshot_header.id_str_size = htobe16(id.size());
+ snapshot_header.name_size = htobe16(name.size());
+ snapshot_header.extra_data_size = htobe32(16);
+ snapshot_header.l1_size = htobe32(1);
+ snapshot_header.l1_table_offset = htobe64(l1_table_offset);
+
+ bufferlist snapshot_header_bl;
+ snapshot_header_bl.append(
+ std::string_view(reinterpret_cast<char*>(&snapshot_header),
+ sizeof(snapshot_header)));
+ expect_stream_read(mock_stream_interface,
+ {{*snapshot_offset, sizeof(snapshot_header)}},
+ snapshot_header_bl, r);
+ *snapshot_offset += sizeof(snapshot_header);
+ }
+
+ void expect_read_snapshot_header_extra(
+ MockStreamInterface& mock_stream_interface,
+ const std::string& id, const std::string& name, uint64_t size,
+ uint64_t* snapshot_offset, int r) {
+ QCowSnapshotExtraData snapshot_header_extra;
+ memset(&snapshot_header_extra, 0, sizeof(snapshot_header_extra));
+ snapshot_header_extra.disk_size = htobe64(size);
+
+ bufferlist snapshot_header_extra_bl;
+ snapshot_header_extra_bl.append(
+ std::string_view(reinterpret_cast<char*>(&snapshot_header_extra), 16));
+ snapshot_header_extra_bl.append(id);
+ snapshot_header_extra_bl.append(name);
+ expect_stream_read(mock_stream_interface,
+ {{*snapshot_offset, 16 + id.size() + name.size()}},
+ snapshot_header_extra_bl, r);
+
+ *snapshot_offset += 16 + id.size() + name.size();
+ *snapshot_offset = p2roundup(*snapshot_offset, static_cast<uint64_t>(8));
+ }
+
+ void expect_read_snapshot_l1_table(MockStreamInterface& mock_stream_interface,
+ uint64_t l1_table_offset, int r) {
+ uint64_t l2_table_cluster = htobe64(l1_table_offset);
+
+ bufferlist snapshot_l1_table_bl;
+ snapshot_l1_table_bl.append(
+ std::string_view(reinterpret_cast<char*>(&l2_table_cluster), 8));
+ expect_stream_read(mock_stream_interface, {{l1_table_offset, 8}},
+ snapshot_l1_table_bl, r);
+ }
+
+
+ void expect_read_snapshot(MockStreamInterface& mock_stream_interface,
+ const std::string& id, const std::string& name,
+ uint64_t size, uint64_t l1_table_offset,
+ uint64_t* snapshot_offset) {
+ expect_read_snapshot_header(mock_stream_interface, id, name,
+ l1_table_offset, snapshot_offset, 0);
+ expect_read_snapshot_header_extra(mock_stream_interface, id, name,
+ size, snapshot_offset, 0);
+ expect_read_snapshot_l1_table(mock_stream_interface, l1_table_offset, 0);
+ }
+
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationQCOWFormat, OpenCloseV1) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ int expected_open_ret_val = 0;
+ QCowHeaderV1 qcow_header;
+ memset(&qcow_header, 0, sizeof(qcow_header));
+ qcow_header.magic = htobe32(QCOW_MAGIC);
+ qcow_header.version = htobe32(1);
+ qcow_header.size = htobe64(1<<30);
+ qcow_header.l1_table_offset = htobe64(1<<20);
+ qcow_header.cluster_bits = 16;
+ qcow_header.l2_bits = 13;
+
+ bufferlist probe_bl;
+ probe_bl.append(std::string_view(reinterpret_cast<char*>(&qcow_header), 8));
+ expect_stream_read(*mock_stream_interface, {{0, 8}}, probe_bl, 0);
+
+#ifdef WITH_RBD_MIGRATION_FORMAT_QCOW_V1
+
+ bufferlist header_bl;
+ header_bl.append(std::string_view(reinterpret_cast<char*>(&qcow_header),
+ sizeof(qcow_header)));
+ expect_stream_read(*mock_stream_interface, {{0, sizeof(qcow_header)}},
+ header_bl, 0);
+
+ bufferlist l1_table_bl;
+ l1_table_bl.append_zero(16);
+ expect_stream_read(*mock_stream_interface, {{1<<20, 16}}, l1_table_bl, 0);
+
+#else // WITH_RBD_MIGRATION_FORMAT_QCOW_V1
+
+ expected_open_ret_val = -ENOTSUP;
+
+#endif // WITH_RBD_MIGRATION_FORMAT_QCOW_V1
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(expected_open_ret_val, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, OpenCloseV2) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {});
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ProbeInvalidMagic) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, 0, 2, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EINVAL, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ProbeInvalidVersion) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 0, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EINVAL, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ProbeError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, -EIO);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EIO, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+#ifdef WITH_RBD_MIGRATION_FORMAT_QCOW_V1
+
+TEST_F(TestMockMigrationQCOWFormat, ReadHeaderV1Error) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 1, 0);
+
+ QCowHeaderV1 qcow_header;
+ memset(&qcow_header, 0, sizeof(qcow_header));
+ qcow_header.magic = htobe32(QCOW_MAGIC);
+ qcow_header.version = htobe32(1);
+ qcow_header.size = htobe64(1<<30);
+ qcow_header.l1_table_offset = htobe64(1<<20);
+ qcow_header.cluster_bits = 16;
+ qcow_header.l2_bits = 13;
+
+ bufferlist header_bl;
+ header_bl.append(std::string_view(reinterpret_cast<char*>(&qcow_header),
+ sizeof(qcow_header)));
+ expect_stream_read(*mock_stream_interface, {{0, sizeof(qcow_header)}},
+ header_bl, -EPERM);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EPERM, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+#endif // WITH_RBD_MIGRATION_FORMAT_QCOW_V1
+
+TEST_F(TestMockMigrationQCOWFormat, ReadHeaderV2Error) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 0, -EIO);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EIO, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadL1TableError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 0, 0);
+
+ expect_read_l1_table(*mock_stream_interface, {}, -EPERM);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EPERM, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, Snapshots) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 2, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot(*mock_stream_interface, "1", "snap1", 1<<29, 1<<22,
+ &snapshot_offset);
+ expect_read_snapshot(*mock_stream_interface, "2", "snap2", 1<<29, 1<<22,
+ &snapshot_offset);
+
+ expect_read_l1_table(*mock_stream_interface, {}, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ FormatInterface::SnapInfos snap_infos;
+ C_SaferCond ctx2;
+ mock_qcow_format.get_snapshots(&snap_infos, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ FormatInterface::SnapInfos expected_snap_infos{
+ {1, {"snap1", cls::rbd::UserSnapshotNamespace{}, 1<<29, {}, 0, 0, {}}},
+ {2, {"snap2", cls::rbd::UserSnapshotNamespace{}, 1<<29, {}, 0, 0, {}}}};
+ ASSERT_EQ(expected_snap_infos, snap_infos);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, SnapshotHeaderError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 2, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot_header(*mock_stream_interface, "1", "snap1",
+ 1<<22, &snapshot_offset, -EINVAL);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EINVAL, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, SnapshotExtraError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 2, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot_header(*mock_stream_interface, "1", "snap1",
+ 1<<22, &snapshot_offset, 0);
+ expect_read_snapshot_header_extra(*mock_stream_interface, "1", "snap1",
+ 1<<29, &snapshot_offset, -EINVAL);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EINVAL, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, SnapshotL1TableError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 2, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot_header(*mock_stream_interface, "1", "snap1",
+ 1<<22, &snapshot_offset, 0);
+ expect_read_snapshot_header_extra(*mock_stream_interface, "1", "snap1",
+ 1<<29, &snapshot_offset, 0);
+ expect_read_snapshot_l1_table(*mock_stream_interface, 1<<22, -EINVAL);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(-EINVAL, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_qcow_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, GetImageSize) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {});
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ uint64_t size = 0;
+ C_SaferCond ctx2;
+ mock_qcow_format.get_image_size(CEPH_NOSNAP, &size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(1<<30, size);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, GetImageSizeSnap) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 1, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot(*mock_stream_interface, "1", "snap1", 1<<29, 1<<22,
+ &snapshot_offset);
+
+ expect_read_l1_table(*mock_stream_interface, {}, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ uint64_t size = 0;
+ C_SaferCond ctx2;
+ mock_qcow_format.get_image_size(1U, &size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(1<<29, size);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, GetImageSizeSnapDNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {});
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ uint64_t size = 0;
+ C_SaferCond ctx2;
+ mock_qcow_format.get_image_size(1U, &size, &ctx2);
+ ASSERT_EQ(-ENOENT, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, 1ULL<<32}}, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(123, '1'));
+ expect_read_cluster(*mock_stream_interface, 1ULL<<32, 123, expect_bl, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadL1DNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {});
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{234, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ bufferlist expect_bl;
+ expect_bl.append_zero(123);
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadL2DNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, 1ULL<<32}}, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{234, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ bufferlist expect_bl;
+ expect_bl.append_zero(123);
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadZero) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, QCOW_OFLAG_ZERO}}, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ bufferlist expect_bl;
+ expect_bl.append_zero(123);
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadSnap) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 1, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot(*mock_stream_interface, "1", "snap1", 1<<29, 1<<22,
+ &snapshot_offset);
+
+ expect_read_l1_table(*mock_stream_interface, {}, 0);
+
+ expect_read_l2_table(*mock_stream_interface, 1<<22,
+ {{0, 1ULL<<32}}, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(123, '1'));
+ expect_read_cluster(*mock_stream_interface, 1ULL<<32, 123, expect_bl, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, 1, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadSnapDNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, 1, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(-ENOENT, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadClusterCacheHit) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, 1ULL<<32}}, 0);
+
+ bufferlist expect_bl1;
+ expect_bl1.append(std::string(123, '1'));
+
+ bufferlist expect_bl2;
+ expect_bl2.append(std::string(234, '2'));
+
+ bufferlist cluster_bl;
+ cluster_bl.append_zero(123);
+ cluster_bl.append(expect_bl1);
+ cluster_bl.append_zero(234);
+ cluster_bl.append(expect_bl2);
+
+ expect_read_cluster(*mock_stream_interface, 1ULL<<32, 0, cluster_bl, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp1 = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl1;
+ io::ReadResult read_result1{&bl1};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp1, CEPH_NOSNAP, {{65659, 123}},
+ std::move(read_result1), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ ASSERT_EQ(expect_bl1, bl1);
+
+ C_SaferCond ctx3;
+ auto aio_comp2 = io::AioCompletion::create_and_start(
+ &ctx3, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl2;
+ io::ReadResult read_result2{&bl2};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp2, CEPH_NOSNAP, {{66016, 234}},
+ std::move(read_result2), 0, 0, {}));
+ ASSERT_EQ(234, ctx3.wait());
+ ASSERT_EQ(expect_bl2, bl2);
+
+ C_SaferCond ctx4;
+ mock_qcow_format.close(&ctx4);
+ ASSERT_EQ(0, ctx4.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadClusterError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, 1ULL<<32}}, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(123, '1'));
+ expect_read_cluster(*mock_stream_interface, 1ULL<<32, 123, expect_bl, -EIO);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(-EIO, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ReadL2TableError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_open(*mock_stream_interface, {{1ULL<<40}});
+
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<40,
+ {{0, 1ULL<<32}}, -EIO);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_qcow_format.read(aio_comp, CEPH_NOSNAP, {{65659, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(-EIO, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationQCOWFormat, ListSnaps) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+
+ expect_probe_header(*mock_stream_interface, QCOW_MAGIC, 2, 0);
+
+ expect_read_header(*mock_stream_interface, 2, 0);
+
+ uint64_t snapshot_offset = 1<<21;
+ expect_read_snapshot(*mock_stream_interface, "1", "snap1", 1<<29, 1<<22,
+ &snapshot_offset);
+ expect_read_snapshot(*mock_stream_interface, "2", "snap2", 1<<29, 1<<23,
+ &snapshot_offset);
+
+ expect_read_l1_table(*mock_stream_interface, {{1ULL<<28}}, 0);
+
+ io::SparseExtents sparse_extents_1;
+ sparse_extents_1.insert(0, 196608, {io::SPARSE_EXTENT_STATE_DATA, 196608});
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<22,
+ {{1ULL<<23, 1ULL<<24, 1ULL<<25}}, 0);
+
+ io::SparseExtents sparse_extents_2;
+ sparse_extents_2.insert(0, 65536, {io::SPARSE_EXTENT_STATE_DATA, 65536});
+ sparse_extents_2.insert(65536, 65536,
+ {io::SPARSE_EXTENT_STATE_ZEROED, 65536});
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<23,
+ {{1ULL<<26, QCOW_OFLAG_ZERO, 1ULL<<25}}, 0);
+
+ io::SparseExtents sparse_extents_head;
+ sparse_extents_head.insert(131072, 65536,
+ {io::SPARSE_EXTENT_STATE_DATA, 65536});
+ expect_read_l2_table(*mock_stream_interface, 1ULL<<28,
+ {{1ULL<<26, QCOW_OFLAG_ZERO, 1ULL<<27}}, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockQCOWFormat mock_qcow_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_qcow_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ io::SnapshotDelta snapshot_delta;
+ mock_qcow_format.list_snaps({{0, 196608}}, {1, CEPH_NOSNAP}, 0,
+ &snapshot_delta, {}, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ io::SnapshotDelta expected_snapshot_delta;
+ expected_snapshot_delta[{1, 1}] = sparse_extents_1;
+ expected_snapshot_delta[{CEPH_NOSNAP, 2}] = sparse_extents_2;
+ expected_snapshot_delta[{CEPH_NOSNAP, CEPH_NOSNAP}] = sparse_extents_head;
+ ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
+
+ C_SaferCond ctx3;
+ mock_qcow_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_RawFormat.cc b/src/test/librbd/migration/test_mock_RawFormat.cc
new file mode 100644
index 000000000..6b69bf20c
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_RawFormat.cc
@@ -0,0 +1,523 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "test/librbd/mock/migration/MockSnapshotInterface.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/RawFormat.h"
+#include "librbd/migration/SourceSpecBuilder.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace migration {
+
+template<>
+struct SourceSpecBuilder<librbd::MockTestImageCtx> {
+
+ MOCK_CONST_METHOD3(build_snapshot, int(const json_spirit::mObject&, uint64_t,
+ std::shared_ptr<SnapshotInterface>*));
+
+};
+
+} // namespace migration
+} // namespace librbd
+
+#include "librbd/migration/RawFormat.cc"
+
+using ::testing::_;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::ReturnRef;
+using ::testing::WithArg;
+using ::testing::WithArgs;
+
+namespace librbd {
+namespace migration {
+
+using ::testing::Invoke;
+
+class TestMockMigrationRawFormat : public TestMockFixture {
+public:
+ typedef RawFormat<MockTestImageCtx> MockRawFormat;
+ typedef SourceSpecBuilder<MockTestImageCtx> MockSourceSpecBuilder;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ json_spirit::mObject stream_obj;
+ stream_obj["type"] = "file";
+ json_object["stream"] = stream_obj;
+ }
+
+ void expect_build_snapshot(MockSourceSpecBuilder& mock_source_spec_builder,
+ uint64_t index,
+ MockSnapshotInterface* mock_snapshot_interface,
+ int r) {
+ EXPECT_CALL(mock_source_spec_builder, build_snapshot(_, index, _))
+ .WillOnce(WithArgs<2>(Invoke([mock_snapshot_interface, r]
+ (std::shared_ptr<SnapshotInterface>* ptr) {
+ ptr->reset(mock_snapshot_interface);
+ return r;
+ })));
+ }
+
+ void expect_snapshot_open(MockSnapshotInterface& mock_snapshot_interface,
+ int r) {
+ EXPECT_CALL(mock_snapshot_interface, open(_, _))
+ .WillOnce(WithArg<1>(Invoke([r](Context* ctx) { ctx->complete(r); })));
+ }
+
+ void expect_snapshot_close(MockSnapshotInterface& mock_snapshot_interface,
+ int r) {
+ EXPECT_CALL(mock_snapshot_interface, close(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_snapshot_get_info(MockSnapshotInterface& mock_snapshot_interface,
+ const SnapInfo& snap_info) {
+ EXPECT_CALL(mock_snapshot_interface, get_snap_info())
+ .WillOnce(ReturnRef(snap_info));
+ }
+
+ void expect_snapshot_read(MockSnapshotInterface& mock_snapshot_interface,
+ const io::Extents& image_extents,
+ const bufferlist& bl, int r) {
+ EXPECT_CALL(mock_snapshot_interface, read(_, image_extents, _))
+ .WillOnce(WithArgs<0, 2>(Invoke([bl, image_extents, r]
+ (io::AioCompletion* aio_comp, io::ReadResult& read_result) {
+ aio_comp->read_result = std::move(read_result);
+ aio_comp->read_result.set_image_extents(image_extents);
+ aio_comp->set_request_count(1);
+ auto ctx = new io::ReadResult::C_ImageReadRequest(aio_comp, 0,
+ image_extents);
+ ctx->bl = std::move(bl);
+ ctx->complete(r);
+ })));
+ }
+
+ void expect_snapshot_list_snap(MockSnapshotInterface& mock_snapshot_interface,
+ const io::Extents& image_extents,
+ const io::SparseExtents& sparse_extents,
+ int r) {
+ EXPECT_CALL(mock_snapshot_interface, list_snap(image_extents, _, _))
+ .WillOnce(WithArgs<1, 2>(Invoke(
+ [sparse_extents, r](io::SparseExtents* out_sparse_extents,
+ Context* ctx) {
+ out_sparse_extents->insert(sparse_extents);
+ ctx->complete(r);
+ })));
+ }
+
+ void expect_close(MockTestImageCtx &mock_image_ctx, int r) {
+ EXPECT_CALL(*mock_image_ctx.state, close(_))
+ .WillOnce(Invoke([r](Context* ctx) {
+ ctx->complete(r);
+ }));
+ }
+
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationRawFormat, OpenClose) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_raw_format.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, OpenError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, -ENOENT);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+ expect_close(mock_image_ctx, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx;
+ mock_raw_format.open(&ctx);
+ ASSERT_EQ(-ENOENT, ctx.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, OpenSnapshotError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface_head = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface_head, 0);
+
+ auto mock_snapshot_interface_1 = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, 1,
+ mock_snapshot_interface_1, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface_1, -ENOENT);
+ expect_snapshot_open(*mock_snapshot_interface_head, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface_1, 0);
+ expect_snapshot_close(*mock_snapshot_interface_head, 0);
+ expect_close(mock_image_ctx, 0);
+
+ json_spirit::mArray snapshots;
+ snapshots.push_back(json_spirit::mObject{});
+ json_object["snapshots"] = snapshots;
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx;
+ mock_raw_format.open(&ctx);
+ ASSERT_EQ(-ENOENT, ctx.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, GetSnapshots) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ FormatInterface::SnapInfos snap_infos;
+ mock_raw_format.get_snapshots(&snap_infos, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_TRUE(snap_infos.empty());
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, GetImageSize) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ SnapInfo snap_info{{}, {}, 123, {}, 0, 0, {}};
+ expect_snapshot_get_info(*mock_snapshot_interface, snap_info);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ uint64_t size;
+ mock_raw_format.get_image_size(CEPH_NOSNAP, &size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(size, 123);
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, GetImageSizeSnapshotDNE) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ uint64_t size;
+ mock_raw_format.get_image_size(0, &size, &ctx2);
+ ASSERT_EQ(-ENOENT, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(123, '1'));
+ expect_snapshot_read(*mock_snapshot_interface, {{123, 123}}, expect_bl, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ ASSERT_TRUE(mock_raw_format.read(aio_comp, CEPH_NOSNAP, {{123, 123}},
+ std::move(read_result), 0, 0, {}));
+ ASSERT_EQ(123, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, ListSnaps) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ SnapInfo snap_info{{}, {}, 123, {}, 0, 0, {}};
+ expect_snapshot_get_info(*mock_snapshot_interface, snap_info);
+ io::SparseExtents sparse_extents;
+ sparse_extents.insert(0, 123, {io::SPARSE_EXTENT_STATE_DATA, 123});
+ expect_snapshot_list_snap(*mock_snapshot_interface, {{0, 123}},
+ sparse_extents, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ io::SnapshotDelta snapshot_delta;
+ mock_raw_format.list_snaps({{0, 123}}, {CEPH_NOSNAP}, 0, &snapshot_delta, {},
+ &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ io::SnapshotDelta expected_snapshot_delta;
+ expected_snapshot_delta[{CEPH_NOSNAP, CEPH_NOSNAP}] = sparse_extents;
+ ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, ListSnapsError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface, 0);
+
+
+ expect_snapshot_open(*mock_snapshot_interface, 0);
+
+ SnapInfo snap_info{{}, {}, 123, {}, 0, 0, {}};
+ expect_snapshot_get_info(*mock_snapshot_interface, snap_info);
+ io::SparseExtents sparse_extents;
+ sparse_extents.insert(0, 123, {io::SPARSE_EXTENT_STATE_DATA, 123});
+ expect_snapshot_list_snap(*mock_snapshot_interface, {{0, 123}},
+ sparse_extents, -EINVAL);
+
+ expect_snapshot_close(*mock_snapshot_interface, 0);
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ io::SnapshotDelta snapshot_delta;
+ mock_raw_format.list_snaps({{0, 123}}, {CEPH_NOSNAP}, 0, &snapshot_delta, {},
+ &ctx2);
+ ASSERT_EQ(-EINVAL, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawFormat, ListSnapsMerge) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_snapshot_interface_head = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, CEPH_NOSNAP,
+ mock_snapshot_interface_head, 0);
+
+ auto mock_snapshot_interface_1 = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, 1,
+ mock_snapshot_interface_1, 0);
+
+ auto mock_snapshot_interface_2 = new MockSnapshotInterface();
+ expect_build_snapshot(mock_source_spec_builder, 2,
+ mock_snapshot_interface_2, 0);
+
+
+ expect_snapshot_open(*mock_snapshot_interface_1, 0);
+ expect_snapshot_open(*mock_snapshot_interface_2, 0);
+ expect_snapshot_open(*mock_snapshot_interface_head, 0);
+
+ SnapInfo snap_info_head{{}, {}, 256, {}, 0, 0, {}};
+ SnapInfo snap_info_1{snap_info_head};
+ snap_info_1.size = 123;
+ expect_snapshot_get_info(*mock_snapshot_interface_1, snap_info_1);
+ io::SparseExtents sparse_extents_1;
+ sparse_extents_1.insert(0, 123, {io::SPARSE_EXTENT_STATE_DATA, 123});
+ expect_snapshot_list_snap(*mock_snapshot_interface_1, {{0, 123}},
+ sparse_extents_1, 0);
+
+ SnapInfo snap_info_2{snap_info_head};
+ snap_info_2.size = 64;
+ expect_snapshot_get_info(*mock_snapshot_interface_2, snap_info_2);
+ io::SparseExtents sparse_extents_2;
+ sparse_extents_2.insert(0, 32, {io::SPARSE_EXTENT_STATE_DATA, 32});
+ expect_snapshot_list_snap(*mock_snapshot_interface_2, {{0, 123}},
+ sparse_extents_2, 0);
+
+ expect_snapshot_get_info(*mock_snapshot_interface_head, snap_info_head);
+ io::SparseExtents sparse_extents_head;
+ sparse_extents_head.insert(0, 16, {io::SPARSE_EXTENT_STATE_DATA, 16});
+ expect_snapshot_list_snap(*mock_snapshot_interface_head, {{0, 123}},
+ sparse_extents_head, 0);
+
+ expect_snapshot_close(*mock_snapshot_interface_1, 0);
+ expect_snapshot_close(*mock_snapshot_interface_2, 0);
+ expect_snapshot_close(*mock_snapshot_interface_head, 0);
+
+ json_spirit::mArray snapshots;
+ snapshots.push_back(json_spirit::mObject{});
+ snapshots.push_back(json_spirit::mObject{});
+ json_object["snapshots"] = snapshots;
+
+ MockRawFormat mock_raw_format(&mock_image_ctx, json_object,
+ &mock_source_spec_builder);
+
+ C_SaferCond ctx1;
+ mock_raw_format.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ io::SnapshotDelta snapshot_delta;
+ mock_raw_format.list_snaps({{0, 123}}, {1, CEPH_NOSNAP}, 0, &snapshot_delta,
+ {}, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ io::SnapshotDelta expected_snapshot_delta;
+ expected_snapshot_delta[{1, 1}] = sparse_extents_1;
+ sparse_extents_2.erase(0, 16);
+ sparse_extents_2.insert(64, 59, {io::SPARSE_EXTENT_STATE_ZEROED, 59});
+ expected_snapshot_delta[{CEPH_NOSNAP, 2}] = sparse_extents_2;
+ expected_snapshot_delta[{CEPH_NOSNAP, CEPH_NOSNAP}] = sparse_extents_head;
+ ASSERT_EQ(expected_snapshot_delta, snapshot_delta);
+
+ C_SaferCond ctx3;
+ mock_raw_format.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_RawSnapshot.cc b/src/test/librbd/migration/test_mock_RawSnapshot.cc
new file mode 100644
index 000000000..3ce4b5c9d
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_RawSnapshot.cc
@@ -0,0 +1,255 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "test/librbd/mock/migration/MockStreamInterface.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/FileStream.h"
+#include "librbd/migration/RawSnapshot.h"
+#include "librbd/migration/SourceSpecBuilder.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace migration {
+
+template<>
+struct SourceSpecBuilder<librbd::MockTestImageCtx> {
+
+ MOCK_CONST_METHOD2(build_stream, int(const json_spirit::mObject&,
+ std::shared_ptr<StreamInterface>*));
+
+};
+
+} // namespace migration
+} // namespace librbd
+
+#include "librbd/migration/RawSnapshot.cc"
+
+using ::testing::_;
+using ::testing::InSequence;
+using ::testing::Invoke;
+using ::testing::WithArgs;
+
+namespace librbd {
+namespace migration {
+
+using ::testing::Invoke;
+
+class TestMockMigrationRawSnapshot : public TestMockFixture {
+public:
+ typedef RawSnapshot<MockTestImageCtx> MockRawSnapshot;
+ typedef SourceSpecBuilder<MockTestImageCtx> MockSourceSpecBuilder;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+
+ json_spirit::mObject stream_obj;
+ stream_obj["type"] = "file";
+ json_object["stream"] = stream_obj;
+ }
+
+ void expect_build_stream(MockSourceSpecBuilder& mock_source_spec_builder,
+ MockStreamInterface* mock_stream_interface, int r) {
+ EXPECT_CALL(mock_source_spec_builder, build_stream(_, _))
+ .WillOnce(WithArgs<1>(Invoke([mock_stream_interface, r]
+ (std::shared_ptr<StreamInterface>* ptr) {
+ ptr->reset(mock_stream_interface);
+ return r;
+ })));
+ }
+
+ void expect_stream_open(MockStreamInterface& mock_stream_interface, int r) {
+ EXPECT_CALL(mock_stream_interface, open(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_stream_close(MockStreamInterface& mock_stream_interface, int r) {
+ EXPECT_CALL(mock_stream_interface, close(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_stream_get_size(MockStreamInterface& mock_stream_interface,
+ uint64_t size, int r) {
+ EXPECT_CALL(mock_stream_interface, get_size(_, _))
+ .WillOnce(Invoke([size, r](uint64_t* out_size, Context* ctx) {
+ *out_size = size;
+ ctx->complete(r);
+ }));
+ }
+
+ void expect_stream_read(MockStreamInterface& mock_stream_interface,
+ const io::Extents& byte_extents,
+ const bufferlist& bl, int r) {
+ EXPECT_CALL(mock_stream_interface, read(byte_extents, _, _))
+ .WillOnce(WithArgs<1, 2>(Invoke([bl, r]
+ (bufferlist* out_bl, Context* ctx) {
+ *out_bl = bl;
+ ctx->complete(r);
+ })));
+ }
+
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationRawSnapshot, OpenClose) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+ expect_stream_get_size(*mock_stream_interface, 123, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ json_object["name"] = "snap1";
+ MockRawSnapshot mock_raw_snapshot(&mock_image_ctx, json_object,
+ &mock_source_spec_builder, 1);
+
+ C_SaferCond ctx1;
+ mock_raw_snapshot.open(nullptr, &ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ auto snap_info = mock_raw_snapshot.get_snap_info();
+ ASSERT_EQ("snap1", snap_info.name);
+ ASSERT_EQ(123, snap_info.size);
+
+ C_SaferCond ctx2;
+ mock_raw_snapshot.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationRawSnapshot, OpenError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, -ENOENT);
+
+ MockRawSnapshot mock_raw_snapshot(&mock_image_ctx, json_object,
+ &mock_source_spec_builder, 0);
+
+ C_SaferCond ctx;
+ mock_raw_snapshot.open(nullptr, &ctx);
+ ASSERT_EQ(-ENOENT, ctx.wait());
+}
+
+TEST_F(TestMockMigrationRawSnapshot, GetSizeError) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+ expect_stream_get_size(*mock_stream_interface, 0, -EINVAL);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockRawSnapshot mock_raw_snapshot(&mock_image_ctx, json_object,
+ &mock_source_spec_builder, 0);
+
+ C_SaferCond ctx;
+ mock_raw_snapshot.open(nullptr, &ctx);
+ ASSERT_EQ(-EINVAL, ctx.wait());
+}
+
+TEST_F(TestMockMigrationRawSnapshot, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+ expect_stream_get_size(*mock_stream_interface, 0, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(123, '1'));
+ expect_stream_read(*mock_stream_interface, {{123, 123}}, expect_bl, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockRawSnapshot mock_raw_snapshot(&mock_image_ctx, json_object,
+ &mock_source_spec_builder, 0);
+
+ C_SaferCond ctx1;
+ mock_raw_snapshot.open(nullptr, &ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ auto aio_comp = io::AioCompletion::create_and_start(
+ &ctx2, m_image_ctx, io::AIO_TYPE_READ);
+ bufferlist bl;
+ io::ReadResult read_result{&bl};
+ mock_raw_snapshot.read(aio_comp, {{123, 123}}, std::move(read_result), 0, 0,
+ {});
+ ASSERT_EQ(123, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_raw_snapshot.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationRawSnapshot, ListSnap) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+ MockSourceSpecBuilder mock_source_spec_builder;
+
+ auto mock_stream_interface = new MockStreamInterface();
+ expect_build_stream(mock_source_spec_builder, mock_stream_interface, 0);
+
+ expect_stream_open(*mock_stream_interface, 0);
+ expect_stream_get_size(*mock_stream_interface, 0, 0);
+
+ expect_stream_close(*mock_stream_interface, 0);
+
+ MockRawSnapshot mock_raw_snapshot(&mock_image_ctx, json_object,
+ &mock_source_spec_builder, 0);
+
+ C_SaferCond ctx1;
+ mock_raw_snapshot.open(nullptr, &ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ io::SparseExtents sparse_extents;
+ mock_raw_snapshot.list_snap({{0, 123}}, 0, &sparse_extents, {}, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+
+ C_SaferCond ctx3;
+ mock_raw_snapshot.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_S3Stream.cc b/src/test/librbd/migration/test_mock_S3Stream.cc
new file mode 100644
index 000000000..2f2097f79
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_S3Stream.cc
@@ -0,0 +1,238 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "include/rbd_types.h"
+#include "common/ceph_mutex.h"
+#include "librbd/migration/HttpClient.h"
+#include "librbd/migration/S3Stream.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "json_spirit/json_spirit.h"
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/beast/http.hpp>
+
+namespace librbd {
+namespace {
+
+struct MockTestImageCtx : public MockImageCtx {
+ MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
+ }
+};
+
+} // anonymous namespace
+
+namespace migration {
+
+template <>
+struct HttpClient<MockTestImageCtx> {
+ static HttpClient* s_instance;
+ static HttpClient* create(MockTestImageCtx*, const std::string&) {
+ ceph_assert(s_instance != nullptr);
+ return s_instance;
+ }
+
+ HttpProcessorInterface* http_processor = nullptr;
+ void set_http_processor(HttpProcessorInterface* http_processor) {
+ this->http_processor = http_processor;
+ }
+
+ MOCK_METHOD1(open, void(Context*));
+ MOCK_METHOD1(close, void(Context*));
+ MOCK_METHOD2(get_size, void(uint64_t*, Context*));
+ MOCK_METHOD3(do_read, void(const io::Extents&, bufferlist*, Context*));
+ void read(io::Extents&& extents, bufferlist* bl, Context* ctx) {
+ do_read(extents, bl, ctx);
+ }
+
+ HttpClient() {
+ s_instance = this;
+ }
+};
+
+HttpClient<MockTestImageCtx>* HttpClient<MockTestImageCtx>::s_instance = nullptr;
+
+} // namespace migration
+} // namespace librbd
+
+#include "librbd/migration/S3Stream.cc"
+
+namespace librbd {
+namespace migration {
+
+using ::testing::_;
+using ::testing::Invoke;
+using ::testing::InSequence;
+using ::testing::WithArgs;
+
+class TestMockMigrationS3Stream : public TestMockFixture {
+public:
+ typedef S3Stream<MockTestImageCtx> MockS3Stream;
+ typedef HttpClient<MockTestImageCtx> MockHttpClient;
+
+ using EmptyBody = boost::beast::http::empty_body;
+ using EmptyRequest = boost::beast::http::request<EmptyBody>;
+
+ librbd::ImageCtx *m_image_ctx;
+
+ void SetUp() override {
+ TestMockFixture::SetUp();
+
+ ASSERT_EQ(0, open_image(m_image_name, &m_image_ctx));
+ json_object["url"] = "http://some.site/bucket/file";
+ json_object["access_key"] = "0555b35654ad1656d804";
+ json_object["secret_key"] = "h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==";
+ }
+
+ void expect_open(MockHttpClient& mock_http_client, int r) {
+ EXPECT_CALL(mock_http_client, open(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_close(MockHttpClient& mock_http_client, int r) {
+ EXPECT_CALL(mock_http_client, close(_))
+ .WillOnce(Invoke([r](Context* ctx) { ctx->complete(r); }));
+ }
+
+ void expect_get_size(MockHttpClient& mock_http_client, uint64_t size, int r) {
+ EXPECT_CALL(mock_http_client, get_size(_, _))
+ .WillOnce(Invoke([size, r](uint64_t* out_size, Context* ctx) {
+ *out_size = size;
+ ctx->complete(r);
+ }));
+ }
+
+ void expect_read(MockHttpClient& mock_http_client, io::Extents byte_extents,
+ const bufferlist& bl, int r) {
+ uint64_t len = 0;
+ for (auto [_, byte_len] : byte_extents) {
+ len += byte_len;
+ }
+ EXPECT_CALL(mock_http_client, do_read(byte_extents, _, _))
+ .WillOnce(WithArgs<1, 2>(Invoke(
+ [len, bl, r](bufferlist* out_bl, Context* ctx) {
+ *out_bl = bl;
+ ctx->complete(r < 0 ? r : len);
+ })));
+ }
+
+ json_spirit::mObject json_object;
+};
+
+TEST_F(TestMockMigrationS3Stream, OpenClose) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockS3Stream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ mock_http_stream.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+TEST_F(TestMockMigrationS3Stream, GetSize) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ expect_get_size(*mock_http_client, 128, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockS3Stream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ uint64_t size;
+ mock_http_stream.get_size(&size, &ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+ ASSERT_EQ(128, size);
+
+ C_SaferCond ctx3;
+ mock_http_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationS3Stream, Read) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ bufferlist expect_bl;
+ expect_bl.append(std::string(192, '1'));
+ expect_read(*mock_http_client, {{0, 128}, {256, 64}}, expect_bl, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockS3Stream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ C_SaferCond ctx2;
+ bufferlist bl;
+ mock_http_stream.read({{0, 128}, {256, 64}}, &bl, &ctx2);
+ ASSERT_EQ(192, ctx2.wait());
+ ASSERT_EQ(expect_bl, bl);
+
+ C_SaferCond ctx3;
+ mock_http_stream.close(&ctx3);
+ ASSERT_EQ(0, ctx3.wait());
+}
+
+TEST_F(TestMockMigrationS3Stream, ProcessRequest) {
+ MockTestImageCtx mock_image_ctx(*m_image_ctx);
+
+ InSequence seq;
+
+ auto mock_http_client = new MockHttpClient();
+ expect_open(*mock_http_client, 0);
+
+ expect_close(*mock_http_client, 0);
+
+ MockS3Stream mock_http_stream(&mock_image_ctx, json_object);
+
+ C_SaferCond ctx1;
+ mock_http_stream.open(&ctx1);
+ ASSERT_EQ(0, ctx1.wait());
+
+ EmptyRequest request;
+ request.method(boost::beast::http::verb::get);
+ request.target("/bucket/resource");
+ mock_http_client->http_processor->process_request(request);
+
+ // basic test for date and known portion of authorization
+ ASSERT_EQ(1U, request.count(boost::beast::http::field::date));
+ ASSERT_EQ(1U, request.count(boost::beast::http::field::authorization));
+ ASSERT_TRUE(boost::algorithm::starts_with(
+ request[boost::beast::http::field::authorization],
+ "AWS 0555b35654ad1656d804:"));
+
+ C_SaferCond ctx2;
+ mock_http_stream.close(&ctx2);
+ ASSERT_EQ(0, ctx2.wait());
+}
+
+} // namespace migration
+} // namespace librbd
diff --git a/src/test/librbd/migration/test_mock_Utils.cc b/src/test/librbd/migration/test_mock_Utils.cc
new file mode 100644
index 000000000..917c191dd
--- /dev/null
+++ b/src/test/librbd/migration/test_mock_Utils.cc
@@ -0,0 +1,47 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "test/librbd/test_mock_fixture.h"
+#include "test/librbd/test_support.h"
+#include "librbd/migration/Utils.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+
+namespace librbd {
+namespace migration {
+namespace util {
+
+class TestMockMigrationUtils : public TestMockFixture {
+public:
+};
+
+TEST_F(TestMockMigrationUtils, ParseUrl) {
+ UrlSpec url_spec;
+ ASSERT_EQ(-EINVAL, parse_url(g_ceph_context, "", &url_spec));
+ ASSERT_EQ(-EINVAL, parse_url(g_ceph_context, "jttp://google.com/path",
+ &url_spec));
+ ASSERT_EQ(-EINVAL, parse_url(g_ceph_context, "http://google.com:absd/path",
+ &url_spec));
+
+ ASSERT_EQ(0, parse_url(g_ceph_context, "ceph.io/path", &url_spec));
+ ASSERT_EQ(UrlSpec(URL_SCHEME_HTTP, "ceph.io", "80", "/path"), url_spec);
+
+ ASSERT_EQ(0, parse_url(g_ceph_context, "http://google.com/path", &url_spec));
+ ASSERT_EQ(UrlSpec(URL_SCHEME_HTTP, "google.com", "80", "/path"), url_spec);
+
+ ASSERT_EQ(0, parse_url(g_ceph_context, "https://ceph.io/", &url_spec));
+ ASSERT_EQ(UrlSpec(URL_SCHEME_HTTPS, "ceph.io", "443", "/"), url_spec);
+
+ ASSERT_EQ(0, parse_url(g_ceph_context,
+ "http://google.com:1234/some/other/path", &url_spec));
+ ASSERT_EQ(UrlSpec(URL_SCHEME_HTTP, "google.com", "1234", "/some/other/path"),
+ url_spec);
+
+ ASSERT_EQ(0, parse_url(g_ceph_context,
+ "http://1.2.3.4/", &url_spec));
+ ASSERT_EQ(UrlSpec(URL_SCHEME_HTTP, "1.2.3.4", "80", "/"), url_spec);
+}
+
+} // namespace util
+} // namespace migration
+} // namespace librbd