Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librados / stat.cc
1 #include "include/rados/librados.h"
2 #include "include/rados/librados.hpp"
3 #include "test/librados/test.h"
4 #include "test/librados/TestCase.h"
5
6 #include "common/ceph_time.h"
7
8 #include <algorithm>
9 #include <errno.h>
10 #include "gtest/gtest.h"
11
12 using namespace librados;
13
14 typedef RadosTest LibRadosStat;
15 typedef RadosTestPP LibRadosStatPP;
16 typedef RadosTestEC LibRadosStatEC;
17 typedef RadosTestECPP LibRadosStatECPP;
18
19 TEST_F(LibRadosStat, Stat) {
20   char buf[128];
21   memset(buf, 0xcc, sizeof(buf));
22   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
23   uint64_t size;
24   time_t mtime;
25   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
26   ASSERT_EQ(sizeof(buf), size);
27   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
28 }
29
30 TEST_F(LibRadosStatPP, StatPP) {
31   char buf[128];
32   memset(buf, 0xcc, sizeof(buf));
33   bufferlist bl;
34   bl.append(buf, sizeof(buf));
35   ASSERT_EQ(0, ioctx.write("foo", bl, sizeof(buf), 0));
36   uint64_t size;
37   time_t mtime;
38   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
39   ASSERT_EQ(sizeof(buf), size);
40   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
41 }
42
43 TEST_F(LibRadosStatPP, Stat2Mtime2PP) {
44   char buf[128];
45   memset(buf, 0xcc, sizeof(buf));
46   bufferlist bl;
47   bl.append(buf, sizeof(buf));
48   librados::ObjectWriteOperation op;
49   struct timespec ts;
50   ts.tv_sec = 1457129052;
51   ts.tv_nsec = 123456789;
52   op.mtime2(&ts);
53   op.write(0, bl);
54   ASSERT_EQ(0, ioctx.operate("foo", &op));
55
56   /* XXX time comparison asserts could spuriously fail */
57
58   uint64_t size;
59   time_t mtime;
60   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
61   ASSERT_EQ(sizeof(buf), size);
62   ASSERT_EQ(mtime, ts.tv_sec);
63
64   struct timespec ts2;
65   ASSERT_EQ(0, ioctx.stat2("foo", &size, &ts2));
66   ASSERT_EQ(sizeof(buf), size);
67   ASSERT_EQ(ts2.tv_sec, ts.tv_sec);
68   ASSERT_EQ(ts2.tv_nsec, ts.tv_nsec);
69
70   ASSERT_EQ(-ENOENT, ioctx.stat2("nonexistent", &size, &ts2));
71 }
72
73 TEST_F(LibRadosStat, StatNS) {
74   char buf[128];
75   memset(buf, 0xcc, sizeof(buf));
76   rados_ioctx_set_namespace(ioctx, "");
77   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
78   ASSERT_EQ(0, rados_write(ioctx, "foo2", buf, sizeof(buf), 0));
79
80   char buf2[64];
81   memset(buf2, 0xcc, sizeof(buf2));
82   rados_ioctx_set_namespace(ioctx, "nspace");
83   ASSERT_EQ(0, rados_write(ioctx, "foo", buf2, sizeof(buf2), 0));
84
85   uint64_t size;
86   time_t mtime;
87   rados_ioctx_set_namespace(ioctx, "");
88   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
89   ASSERT_EQ(sizeof(buf), size);
90   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
91
92   rados_ioctx_set_namespace(ioctx, "nspace");
93   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
94   ASSERT_EQ(sizeof(buf2), size);
95   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
96   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "foo2", &size, &mtime));
97 }
98
99 TEST_F(LibRadosStatPP, StatPPNS) {
100   char buf[128];
101   memset(buf, 0xcc, sizeof(buf));
102   bufferlist bl;
103   bl.append(buf, sizeof(buf));
104   ioctx.set_namespace("");
105   ASSERT_EQ(0, ioctx.write("foo", bl, sizeof(buf), 0));
106   ASSERT_EQ(0, ioctx.write("foo2", bl, sizeof(buf), 0));
107
108   char buf2[64];
109   memset(buf2, 0xbb, sizeof(buf2));
110   bufferlist bl2;
111   bl2.append(buf2, sizeof(buf2));
112   ioctx.set_namespace("nspace");
113   ASSERT_EQ(0, ioctx.write("foo", bl2, sizeof(buf2), 0));
114
115   uint64_t size;
116   time_t mtime;
117   ioctx.set_namespace("");
118   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
119   ASSERT_EQ(sizeof(buf), size);
120   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
121
122   ioctx.set_namespace("nspace");
123   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
124   ASSERT_EQ(sizeof(buf2), size);
125   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
126   ASSERT_EQ(-ENOENT, ioctx.stat("foo2", &size, &mtime));
127 }
128
129 TEST_F(LibRadosStat, ClusterStat) {
130   struct rados_cluster_stat_t result;
131   ASSERT_EQ(0, rados_cluster_stat(cluster, &result));
132 }
133
134 TEST_F(LibRadosStatPP, ClusterStatPP) {
135   cluster_stat_t cstat;
136   ASSERT_EQ(0, cluster.cluster_stat(cstat));
137 }
138
139 TEST_F(LibRadosStat, PoolStat) {
140   char buf[128];
141   char actual_pool_name[80];
142   unsigned l = rados_ioctx_get_pool_name(ioctx, actual_pool_name, sizeof(actual_pool_name));
143   ASSERT_EQ(strlen(actual_pool_name), l);
144   ASSERT_EQ(0, strcmp(actual_pool_name, pool_name.c_str()));
145   memset(buf, 0xff, sizeof(buf));
146   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
147   struct rados_pool_stat_t stats;
148   memset(&stats, 0, sizeof(stats));
149   ASSERT_EQ(0, rados_ioctx_pool_stat(ioctx, &stats));
150 }
151
152 TEST_F(LibRadosStatPP, PoolStatPP) {
153   std::string n = ioctx.get_pool_name();
154   ASSERT_EQ(n, pool_name);
155   char buf[128];
156   memset(buf, 0xff, sizeof(buf));
157   bufferlist bl1;
158   bl1.append(buf, sizeof(buf));
159   ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
160   std::list<std::string> v;
161   std::map<std::string,stats_map> stats;
162   ASSERT_EQ(0, cluster.get_pool_stats(v, stats));
163 }
164
165 TEST_F(LibRadosStatEC, Stat) {
166   char buf[128];
167   memset(buf, 0xcc, sizeof(buf));
168   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
169   uint64_t size;
170   time_t mtime;
171   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
172   ASSERT_EQ(sizeof(buf), size);
173   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
174 }
175
176 TEST_F(LibRadosStatECPP, StatPP) {
177   char buf[128];
178   memset(buf, 0xcc, sizeof(buf));
179   bufferlist bl;
180   bl.append(buf, sizeof(buf));
181   ASSERT_EQ(0, ioctx.write("foo", bl, sizeof(buf), 0));
182   uint64_t size;
183   time_t mtime;
184   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
185   ASSERT_EQ(sizeof(buf), size);
186   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
187 }
188
189 TEST_F(LibRadosStatEC, StatNS) {
190   char buf[128];
191   memset(buf, 0xcc, sizeof(buf));
192   rados_ioctx_set_namespace(ioctx, "");
193   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
194   ASSERT_EQ(0, rados_write(ioctx, "foo2", buf, sizeof(buf), 0));
195
196   char buf2[64];
197   memset(buf2, 0xcc, sizeof(buf2));
198   rados_ioctx_set_namespace(ioctx, "nspace");
199   ASSERT_EQ(0, rados_write(ioctx, "foo", buf2, sizeof(buf2), 0));
200
201   uint64_t size;
202   time_t mtime;
203   rados_ioctx_set_namespace(ioctx, "");
204   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
205   ASSERT_EQ(sizeof(buf), size);
206   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
207
208   rados_ioctx_set_namespace(ioctx, "nspace");
209   ASSERT_EQ(0, rados_stat(ioctx, "foo", &size, &mtime));
210   ASSERT_EQ(sizeof(buf2), size);
211   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "nonexistent", &size, &mtime));
212   ASSERT_EQ(-ENOENT, rados_stat(ioctx, "foo2", &size, &mtime));
213 }
214
215 TEST_F(LibRadosStatECPP, StatPPNS) {
216   char buf[128];
217   memset(buf, 0xcc, sizeof(buf));
218   bufferlist bl;
219   bl.append(buf, sizeof(buf));
220   ioctx.set_namespace("");
221   ASSERT_EQ(0, ioctx.write("foo", bl, sizeof(buf), 0));
222   ASSERT_EQ(0, ioctx.write("foo2", bl, sizeof(buf), 0));
223
224   char buf2[64];
225   memset(buf2, 0xbb, sizeof(buf2));
226   bufferlist bl2;
227   bl2.append(buf2, sizeof(buf2));
228   ioctx.set_namespace("nspace");
229   ASSERT_EQ(0, ioctx.write("foo", bl2, sizeof(buf2), 0));
230
231   uint64_t size;
232   time_t mtime;
233   ioctx.set_namespace("");
234   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
235   ASSERT_EQ(sizeof(buf), size);
236   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
237
238   ioctx.set_namespace("nspace");
239   ASSERT_EQ(0, ioctx.stat("foo", &size, &mtime));
240   ASSERT_EQ(sizeof(buf2), size);
241   ASSERT_EQ(-ENOENT, ioctx.stat("nonexistent", &size, &mtime));
242   ASSERT_EQ(-ENOENT, ioctx.stat("foo2", &size, &mtime));
243 }
244
245 TEST_F(LibRadosStatEC, ClusterStat) {
246   struct rados_cluster_stat_t result;
247   ASSERT_EQ(0, rados_cluster_stat(cluster, &result));
248 }
249
250 TEST_F(LibRadosStatECPP, ClusterStatPP) {
251   cluster_stat_t cstat;
252   ASSERT_EQ(0, cluster.cluster_stat(cstat));
253 }
254
255 TEST_F(LibRadosStatEC, PoolStat) {
256   char buf[128];
257   char actual_pool_name[80];
258   unsigned l = rados_ioctx_get_pool_name(ioctx, actual_pool_name, sizeof(actual_pool_name));
259   ASSERT_EQ(strlen(actual_pool_name), l);
260   ASSERT_EQ(0, strcmp(actual_pool_name, pool_name.c_str()));
261   memset(buf, 0xff, sizeof(buf));
262   ASSERT_EQ(0, rados_write(ioctx, "foo", buf, sizeof(buf), 0));
263   struct rados_pool_stat_t stats;
264   memset(&stats, 0, sizeof(stats));
265   ASSERT_EQ(0, rados_ioctx_pool_stat(ioctx, &stats));
266 }
267
268 TEST_F(LibRadosStatECPP, PoolStatPP) {
269   std::string n = ioctx.get_pool_name();
270   ASSERT_EQ(n, pool_name);
271   char buf[128];
272   memset(buf, 0xff, sizeof(buf));
273   bufferlist bl1;
274   bl1.append(buf, sizeof(buf));
275   ASSERT_EQ(0, ioctx.write("foo", bl1, sizeof(buf), 0));
276   std::list<std::string> v;
277   std::map<std::string,stats_map> stats;
278   ASSERT_EQ(0, cluster.get_pool_stats(v, stats));
279 }