Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / cls_statelog / test_cls_statelog.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/types.h"
5 #include "cls/statelog/cls_statelog_types.h"
6 #include "cls/statelog/cls_statelog_client.h"
7
8 #include "include/utime.h"
9 #include "common/Clock.h"
10 #include "global/global_context.h"
11
12 #include "gtest/gtest.h"
13 #include "test/librados/test.h"
14
15 #include <errno.h>
16 #include <string>
17 #include <vector>
18
19 static librados::ObjectWriteOperation *new_op() {
20   return new librados::ObjectWriteOperation();
21 }
22
23 static librados::ObjectReadOperation *new_rop() {
24   return new librados::ObjectReadOperation();
25 }
26
27 static void reset_op(librados::ObjectWriteOperation **pop) {
28   delete *pop;
29   *pop = new_op();
30 }
31 static void reset_rop(librados::ObjectReadOperation **pop) {
32   delete *pop;
33   *pop = new_rop();
34 }
35
36 void add_log(librados::ObjectWriteOperation *op, const string& client_id, const string& op_id, string& obj, uint32_t state)
37 {
38   bufferlist bl;
39   ::encode(state, bl);
40
41   utime_t ts = ceph_clock_now();
42
43   cls_statelog_add(*op, client_id, op_id, obj, ts, state, bl);
44 }
45
46 void next_op_id(string& op_id, int *id)
47 {
48   char buf[16];
49   snprintf(buf, sizeof(buf), "%d", *id);
50   op_id = buf;
51   (*id)++;
52 }
53
54 static string get_obj_name(int num)
55 {
56   char buf[16];
57   snprintf(buf, sizeof(buf), "obj-%d", num);
58   return string(buf);
59 }
60
61 static void get_entries_by_object(librados::IoCtx& ioctx, string& oid,
62                                   list<cls_statelog_entry>& entries, string& object, string& op_id, int expected)
63 {
64   /* search everything */
65   string empty_str, marker;
66
67   librados::ObjectReadOperation *rop = new_rop();
68   bufferlist obl;
69   bool truncated;
70   cls_statelog_list(*rop, empty_str, op_id, object, marker, 0, entries, &marker, &truncated);
71   ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
72   ASSERT_EQ(expected, (int)entries.size());
73   delete rop;
74 }
75
76 static void get_entries_by_client_id(librados::IoCtx& ioctx, string& oid,
77                                      list<cls_statelog_entry>& entries, string& client_id, string& op_id, int expected)
78 {
79   /* search everything */
80   string empty_str, marker;
81
82   librados::ObjectReadOperation *rop = new_rop();
83   bufferlist obl;
84   bool truncated;
85   cls_statelog_list(*rop, client_id, op_id, empty_str, marker, 0, entries, &marker, &truncated);
86   ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
87   ASSERT_EQ(expected, (int)entries.size());
88   delete rop;
89 }
90
91 static void get_all_entries(librados::IoCtx& ioctx, string& oid, list<cls_statelog_entry>& entries, int expected)
92 {
93   /* search everything */
94   string object, op_id;
95   get_entries_by_object(ioctx, oid, entries, object, op_id, expected);
96 }
97
98 TEST(cls_rgw, test_statelog_basic)
99 {
100   librados::Rados rados;
101   librados::IoCtx ioctx;
102   string pool_name = get_temp_pool_name();
103
104   /* create pool */
105   ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
106   ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
107
108   string oid = "obj";
109
110   /* create object */
111   ASSERT_EQ(0, ioctx.create(oid, true));
112
113   int id = 0;
114   string client_id[] = { "client-1", "client-2" };
115
116   const int num_ops = 10;
117   string op_ids[num_ops];
118
119   librados::ObjectWriteOperation *op = new_op();
120
121   for (int i = 0; i < num_ops; i++) {
122     next_op_id(op_ids[i], &id);
123     string obj = get_obj_name(i / 2);
124     string cid = client_id[i / (num_ops / 2)];
125     add_log(op, cid, op_ids[i], obj, i /* just for testing */); 
126   }
127   ASSERT_EQ(0, ioctx.operate(oid, op));
128
129   librados::ObjectReadOperation *rop = new_rop();
130
131   list<cls_statelog_entry> entries;
132   bool truncated;
133
134   /* check list by client_id */
135
136   int total_count = 0;
137   for (int j = 0; j < 2; j++) {
138     string marker;
139     string obj;
140     string cid = client_id[j];
141     string op_id;
142
143     bufferlist obl;
144
145     cls_statelog_list(*rop, cid, op_id, obj, marker, 1, entries, &marker, &truncated);
146     ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
147     ASSERT_EQ(1, (int)entries.size());
148
149     reset_rop(&rop);
150     marker.clear();
151     cls_statelog_list(*rop, cid, op_id, obj, marker, 0, entries, &marker, &truncated);
152     obl.clear();
153     ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
154
155     ASSERT_EQ(5, (int)entries.size());
156     ASSERT_EQ(0, (int)truncated);
157
158     map<string, string> emap;
159     for (list<cls_statelog_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
160       ASSERT_EQ(cid, iter->client_id);
161       emap[iter->op_id] = iter->object;
162     }
163     ASSERT_EQ(5, (int)emap.size());
164     /* verify correct object */
165     for (int i = 0; i < num_ops / 2; i++, total_count++) {
166       string ret_obj = emap[op_ids[total_count]];
167       string obj = get_obj_name(total_count / 2);
168       ASSERT_EQ(0, ret_obj.compare(obj));
169     }
170   }
171
172   entries.clear();
173   /* now search by object */
174   total_count = 0;
175   for (int i = 0; i < num_ops; i++) {
176     string marker;
177     string obj = get_obj_name(i / 2);
178     string cid;
179     string op_id;
180     bufferlist obl;
181
182     reset_rop(&rop);
183     cls_statelog_list(*rop, cid, op_id, obj, marker, 0, entries, &marker, &truncated);
184     ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
185     ASSERT_EQ(2, (int)entries.size());
186   }
187
188   /* search everything */
189
190   get_all_entries(ioctx, oid, entries, 10);
191
192   /* now remove an entry */
193   cls_statelog_entry e = entries.front();
194   entries.pop_front();
195
196   reset_op(&op);
197   cls_statelog_remove_by_client(*op, e.client_id, e.op_id);
198   ASSERT_EQ(0, ioctx.operate(oid, op));
199
200   get_all_entries(ioctx, oid, entries, 9);
201
202   get_entries_by_object(ioctx, oid, entries, e.object, e.op_id, 0);
203   get_entries_by_client_id(ioctx, oid, entries, e.client_id, e.op_id, 0);
204
205   string empty_str;
206   get_entries_by_client_id(ioctx, oid, entries, e.client_id, empty_str, 4);
207   get_entries_by_object(ioctx, oid, entries, e.object, empty_str, 1);
208   delete op;
209   delete rop;
210 }
211