Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / rbd_mirror / pool_watcher / test_mock_RefreshImagesRequest.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "test/rbd_mirror/test_mock_fixture.h"
5 #include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
6 #include "test/librados_test_stub/MockTestMemRadosClient.h"
7 #include "test/librbd/mock/MockImageCtx.h"
8 #include "tools/rbd_mirror/pool_watcher/RefreshImagesRequest.h"
9 #include "include/stringify.h"
10
11 namespace librbd {
12 namespace {
13
14 struct MockTestImageCtx : public librbd::MockImageCtx {
15   MockTestImageCtx(librbd::ImageCtx &image_ctx)
16     : librbd::MockImageCtx(image_ctx) {
17   }
18 };
19
20 } // anonymous namespace
21 } // namespace librbd
22
23 // template definitions
24 #include "tools/rbd_mirror/pool_watcher/RefreshImagesRequest.cc"
25 template class rbd::mirror::pool_watcher::RefreshImagesRequest<librbd::MockTestImageCtx>;
26
27 namespace rbd {
28 namespace mirror {
29 namespace pool_watcher {
30
31 using ::testing::_;
32 using ::testing::DoAll;
33 using ::testing::InSequence;
34 using ::testing::Invoke;
35 using ::testing::Return;
36 using ::testing::StrEq;
37 using ::testing::WithArg;
38
39 class TestMockPoolWatcherRefreshImagesRequest : public TestMockFixture {
40 public:
41   typedef RefreshImagesRequest<librbd::MockTestImageCtx> MockRefreshImagesRequest;
42
43   void expect_mirror_image_list(librados::IoCtx &io_ctx,
44                                 const std::map<std::string, std::string> &ids,
45                                 int r) {
46     bufferlist bl;
47     ::encode(ids, bl);
48
49     EXPECT_CALL(get_mock_io_ctx(io_ctx),
50                 exec(RBD_MIRRORING, _, StrEq("rbd"), StrEq("mirror_image_list"), _, _, _))
51       .WillOnce(DoAll(WithArg<5>(Invoke([bl](bufferlist *out_bl) {
52                                           *out_bl = bl;
53                                         })),
54                       Return(r)));
55   }
56
57 };
58
59 TEST_F(TestMockPoolWatcherRefreshImagesRequest, Success) {
60   InSequence seq;
61   expect_mirror_image_list(m_remote_io_ctx, {{"local id", "global id"}}, 0);
62
63   C_SaferCond ctx;
64   ImageIds image_ids;
65   MockRefreshImagesRequest *req = new MockRefreshImagesRequest(
66     m_remote_io_ctx, &image_ids, &ctx);
67
68   req->send();
69   ASSERT_EQ(0, ctx.wait());
70
71   ImageIds expected_image_ids = {{"global id", "local id"}};
72   ASSERT_EQ(expected_image_ids, image_ids);
73 }
74
75 TEST_F(TestMockPoolWatcherRefreshImagesRequest, LargeDirectory) {
76   InSequence seq;
77   std::map<std::string, std::string> mirror_list;
78   ImageIds expected_image_ids;
79   for (uint32_t idx = 1; idx <= 1024; ++idx) {
80     mirror_list.insert(std::make_pair("local id " + stringify(idx),
81                                       "global id " + stringify(idx)));
82     expected_image_ids.insert({{"global id " + stringify(idx),
83                                 "local id " + stringify(idx)}});
84   }
85
86   expect_mirror_image_list(m_remote_io_ctx, mirror_list, 0);
87   expect_mirror_image_list(m_remote_io_ctx, {{"local id", "global id"}}, 0);
88
89   C_SaferCond ctx;
90   ImageIds image_ids;
91   MockRefreshImagesRequest *req = new MockRefreshImagesRequest(
92     m_remote_io_ctx, &image_ids, &ctx);
93
94   req->send();
95   ASSERT_EQ(0, ctx.wait());
96
97   expected_image_ids.insert({"global id", "local id"});
98   ASSERT_EQ(expected_image_ids, image_ids);
99 }
100
101 TEST_F(TestMockPoolWatcherRefreshImagesRequest, MirrorImageListError) {
102   InSequence seq;
103   expect_mirror_image_list(m_remote_io_ctx, {}, -EINVAL);
104
105   C_SaferCond ctx;
106   ImageIds image_ids;
107   MockRefreshImagesRequest *req = new MockRefreshImagesRequest(
108     m_remote_io_ctx, &image_ids, &ctx);
109
110   req->send();
111   ASSERT_EQ(-EINVAL, ctx.wait());
112 }
113
114 } // namespace pool_watcher
115 } // namespace mirror
116 } // namespace rbd