From f1f6407221a0670cf8f0fd883d44531222aa3182 Mon Sep 17 00:00:00 2001 From: Josh Durgin Date: Sun, 31 Mar 2013 23:13:51 -0700 Subject: [PATCH] test_librbd: add diff_iterate test including discard Signed-off-by: Josh Durgin --- src/test/librbd/test_librbd.cc | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/test/librbd/test_librbd.cc b/src/test/librbd/test_librbd.cc index c24f41ec95343..ae65bd5027d08 100644 --- a/src/test/librbd/test_librbd.cc +++ b/src/test/librbd/test_librbd.cc @@ -1579,6 +1579,103 @@ TEST(LibRBD, DiffIterate) ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados)); } +struct diff_extent { + diff_extent(uint64_t offset, uint64_t length, bool exists) : + offset(offset), length(length), exists(exists) {} + uint64_t offset; + uint64_t length; + bool exists; + bool operator==(const diff_extent& o) const { + return offset == o.offset && length == o.length && exists == o.exists; + } +}; + +ostream& operator<<(ostream & o, const diff_extent& e) { + return o << '(' << e.offset << '~' << e.length << ' ' << (e.exists ? "true" : "false") << ')'; +} + +int vector_iterate_cb(uint64_t off, size_t len, int exists, void *arg) +{ + cout << "iterate_cb " << off << "~" << len << std::endl; + vector *diff = static_cast *>(arg); + diff->push_back(diff_extent(off, len, exists)); + return 0; +} + +TEST(LibRBD, DiffIterateDiscard) +{ + librados::Rados rados; + librados::IoCtx ioctx; + string pool_name = get_temp_pool_name(); + + ASSERT_EQ("", create_one_pool_pp(pool_name, rados)); + ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx)); + + { + librbd::RBD rbd; + librbd::Image image; + int order = 0; + const char *name = "testimg"; + uint64_t size = 20 << 20; + + ASSERT_EQ(0, create_image_pp(rbd, ioctx, name, size, &order)); + ASSERT_EQ(0, rbd.open(ioctx, image, name, NULL)); + + vector extents; + ceph::bufferlist bl; + + ASSERT_EQ(0, image.diff_iterate(NULL, 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(0u, extents.size()); + + char data[256]; + bl.append(data, 256); + ASSERT_EQ(256, image.write(0, 256, bl)); + ASSERT_EQ(0, image.diff_iterate(NULL, 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(1u, extents.size()); + ASSERT_EQ(diff_extent(0, 256, true), extents[0]); + + int obj_ofs = 256; + ASSERT_EQ(obj_ofs, image.discard(0, obj_ofs)); + + extents.clear(); + ASSERT_EQ(0, image.diff_iterate(NULL, 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(0u, extents.size()); + + ASSERT_EQ(0, image.snap_create("snap1")); + ASSERT_EQ(256, image.write(0, 256, bl)); + ASSERT_EQ(0, image.diff_iterate(NULL, 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(1u, extents.size()); + ASSERT_EQ(diff_extent(0, 256, true), extents[0]); + ASSERT_EQ(0, image.snap_create("snap2")); + + ASSERT_EQ(obj_ofs, image.discard(0, obj_ofs)); + + extents.clear(); + ASSERT_EQ(0, image.snap_set("snap2")); + ASSERT_EQ(0, image.diff_iterate("snap1", 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(1u, extents.size()); + ASSERT_EQ(diff_extent(0, 256, true), extents[0]); + + ASSERT_EQ(0, image.snap_set(NULL)); + ASSERT_EQ(1 << order, image.discard(0, 1 << order)); + ASSERT_EQ(0, image.snap_create("snap3")); + ASSERT_EQ(0, image.snap_set("snap3")); + + extents.clear(); + ASSERT_EQ(0, image.diff_iterate("snap1", 0, size, + vector_iterate_cb, (void *) &extents)); + ASSERT_EQ(1u, extents.size()); + ASSERT_EQ(diff_extent(0, 256, false), extents[0]); + } + ioctx.close(); + ASSERT_EQ(0, destroy_one_pool_pp(pool_name, rados)); +} + TEST(LibRBD, DiffIterateStress) { librados::Rados rados; -- 2.39.5