Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / journal / test_Journaler.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 "include/stringify.h"
5
6 #include "journal/Journaler.h"
7 #include "journal/Settings.h"
8
9 #include "test/librados/test.h"
10 #include "test/journal/RadosTestFixture.h"
11
12 #include "gtest/gtest.h"
13
14 // reinclude our assert to clobber the system one
15 #include "include/assert.h"
16
17 class TestJournaler : public RadosTestFixture {
18 public:
19
20   static const std::string CLIENT_ID;
21
22   static std::string get_temp_journal_id() {
23     return stringify(++_journal_id);
24   }
25
26   void SetUp() override {
27     RadosTestFixture::SetUp();
28     m_journal_id = get_temp_journal_id();
29     m_journaler = new journal::Journaler(m_work_queue, m_timer, &m_timer_lock,
30                                          m_ioctx, m_journal_id, CLIENT_ID, {});
31   }
32
33   void TearDown() override {
34     delete m_journaler;
35     RadosTestFixture::TearDown();
36   }
37
38   int create_journal(uint8_t order, uint8_t splay_width) {
39     C_SaferCond cond;
40     m_journaler->create(order, splay_width, -1, &cond);
41     return cond.wait();
42   }
43
44   int init_journaler() {
45     C_SaferCond cond;
46     m_journaler->init(&cond);
47     return cond.wait();
48   }
49
50   int shut_down_journaler() {
51     C_SaferCond ctx;
52     m_journaler->shut_down(&ctx);
53     return ctx.wait();
54   }
55
56   int register_client(const std::string &client_id, const std::string &desc) {
57     journal::Journaler journaler(m_work_queue, m_timer, &m_timer_lock,
58                                  m_ioctx, m_journal_id, client_id, {});
59     bufferlist data;
60     data.append(desc);
61     C_SaferCond cond;
62     journaler.register_client(data, &cond);
63     return cond.wait();
64   }
65
66   int update_client(const std::string &client_id, const std::string &desc) {
67     journal::Journaler journaler(m_work_queue, m_timer, &m_timer_lock,
68                                  m_ioctx, m_journal_id, client_id, {});
69     bufferlist data;
70     data.append(desc);
71     C_SaferCond cond;
72     journaler.update_client(data, &cond);
73     return cond.wait();
74   }
75
76   int unregister_client(const std::string &client_id) {
77     journal::Journaler journaler(m_work_queue, m_timer, &m_timer_lock,
78                                  m_ioctx, m_journal_id, client_id, {});
79     C_SaferCond cond;
80     journaler.unregister_client(&cond);
81     return cond.wait();
82   }
83
84   static uint64_t _journal_id;
85
86   std::string m_journal_id;
87   journal::Journaler *m_journaler;
88 };
89
90 const std::string TestJournaler::CLIENT_ID = "client1";
91 uint64_t TestJournaler::_journal_id = 0;
92
93 TEST_F(TestJournaler, Create) {
94   ASSERT_EQ(0, create_journal(12, 8));
95 }
96
97 TEST_F(TestJournaler, CreateDuplicate) {
98   ASSERT_EQ(0, create_journal(12, 8));
99   ASSERT_EQ(-EEXIST, create_journal(12, 8));
100 }
101
102 TEST_F(TestJournaler, CreateInvalidParams) {
103   ASSERT_EQ(-EDOM, create_journal(1, 8));
104   ASSERT_EQ(-EDOM, create_journal(123, 8));
105   ASSERT_EQ(-EINVAL, create_journal(12, 0));
106 }
107
108 TEST_F(TestJournaler, Init) {
109   ASSERT_EQ(0, create_journal(12, 8));
110   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
111   ASSERT_EQ(0, init_journaler());
112   ASSERT_EQ(0, shut_down_journaler());
113 }
114
115 TEST_F(TestJournaler, InitDNE) {
116   ASSERT_EQ(-ENOENT, init_journaler());
117   ASSERT_EQ(0, shut_down_journaler());
118 }
119
120 TEST_F(TestJournaler, RegisterClientDuplicate) {
121   ASSERT_EQ(0, create_journal(12, 8));
122   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
123   ASSERT_EQ(-EEXIST, register_client(CLIENT_ID, "foo2"));
124 }
125
126 TEST_F(TestJournaler, UpdateClient) {
127   ASSERT_EQ(0, create_journal(12, 8));
128   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
129   ASSERT_EQ(0, update_client(CLIENT_ID, "foo2"));
130 }
131
132 TEST_F(TestJournaler, UpdateClientDNE) {
133   ASSERT_EQ(0, create_journal(12, 8));
134   ASSERT_EQ(-ENOENT, update_client(CLIENT_ID, "foo"));
135 }
136
137 TEST_F(TestJournaler, UnregisterClient) {
138   ASSERT_EQ(0, create_journal(12, 8));
139   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
140   ASSERT_EQ(0, unregister_client(CLIENT_ID));
141   // Test it does not exist and can be registered again
142   ASSERT_EQ(-ENOENT, update_client(CLIENT_ID, "foo"));
143   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
144 }
145
146 TEST_F(TestJournaler, UnregisterClientDNE) {
147   ASSERT_EQ(0, create_journal(12, 8));
148   ASSERT_EQ(-ENOENT, unregister_client(CLIENT_ID));
149 }
150
151 TEST_F(TestJournaler, AllocateTag) {
152   ASSERT_EQ(0, create_journal(12, 8));
153
154   cls::journal::Tag tag;
155
156   bufferlist data;
157   data.append(std::string(128, '1'));
158
159   // allocate a new tag class
160   C_SaferCond ctx1;
161   m_journaler->allocate_tag(data, &tag, &ctx1);
162   ASSERT_EQ(0, ctx1.wait());
163   ASSERT_EQ(cls::journal::Tag(0, 0, data), tag);
164
165   // re-use an existing tag class
166   C_SaferCond ctx2;
167   m_journaler->allocate_tag(tag.tag_class, bufferlist(), &tag, &ctx2);
168   ASSERT_EQ(0, ctx2.wait());
169   ASSERT_EQ(cls::journal::Tag(1, 0, bufferlist()), tag);
170 }
171
172 TEST_F(TestJournaler, GetTags) {
173   ASSERT_EQ(0, create_journal(12, 8));
174   ASSERT_EQ(0, register_client(CLIENT_ID, "foo"));
175
176   std::list<cls::journal::Tag> expected_tags;
177   for (size_t i = 0; i < 256; ++i) {
178     C_SaferCond ctx;
179     cls::journal::Tag tag;
180     if (i < 2) {
181       m_journaler->allocate_tag(bufferlist(), &tag, &ctx);
182     } else {
183       m_journaler->allocate_tag(i % 2, bufferlist(), &tag, &ctx);
184     }
185     ASSERT_EQ(0, ctx.wait());
186
187     if (i % 2 == 0) {
188       expected_tags.push_back(tag);
189     }
190   }
191
192   std::list<cls::journal::Tag> tags;
193   C_SaferCond ctx;
194   m_journaler->get_tags(0, &tags, &ctx);
195   ASSERT_EQ(0, ctx.wait());
196   ASSERT_EQ(expected_tags, tags);
197 }