]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cls_statelog: improve unitest
authorYehuda Sadeh <yehuda@inktank.com>
Tue, 18 Jun 2013 22:14:07 +0000 (15:14 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Tue, 18 Jun 2013 22:14:07 +0000 (15:14 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/test/cls_statelog/test_cls_statelog.cc

index d1ace1e31c92f8eaebb02d126f90b05c915464d7..ac90157aa9e3385d29b036428e37c2e416eee8f1 100644 (file)
@@ -24,6 +24,10 @@ static librados::ObjectReadOperation *new_rop() {
   return new librados::ObjectReadOperation();
 }
 
+static void reset_op(librados::ObjectWriteOperation **pop) {
+  delete *pop;
+  *pop = new_op();
+}
 static void reset_rop(librados::ObjectReadOperation **pop) {
   delete *pop;
   *pop = new_rop();
@@ -45,13 +49,46 @@ void next_op_id(string& op_id, int *id)
   (*id)++;
 }
 
-string get_name(int i)
+static string get_obj_name(int num)
 {
-  string name_prefix = "data-source";
-
   char buf[16];
-  snprintf(buf, sizeof(buf), "%d", i);
-  return name_prefix + buf;
+  snprintf(buf, sizeof(buf), "obj-%d", num);
+  return string(buf);
+}
+
+static void get_entries_by_object(librados::IoCtx& ioctx, string& oid,
+                                  list<cls_statelog_entry>& entries, string& object, string& op_id, int expected)
+{
+  /* search everything */
+  string empty_str, marker;
+
+  librados::ObjectReadOperation *rop = new_rop();
+  bufferlist obl;
+  bool truncated;
+  cls_statelog_list(*rop, empty_str, op_id, object, marker, 0, entries, &marker, &truncated);
+  ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+  ASSERT_EQ(expected, (int)entries.size());
+}
+
+static void get_entries_by_client_id(librados::IoCtx& ioctx, string& oid,
+                                     list<cls_statelog_entry>& entries, string& client_id, string& op_id, int expected)
+{
+  /* search everything */
+  string empty_str, marker;
+
+  librados::ObjectReadOperation *rop = new_rop();
+  bufferlist obl;
+  bool truncated;
+  cls_statelog_list(*rop, client_id, op_id, empty_str, marker, 0, entries, &marker, &truncated);
+  ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+  ASSERT_EQ(expected, (int)entries.size());
+}
+
+static void get_all_entries(librados::IoCtx& ioctx, string& oid, list<cls_statelog_entry>& entries, int expected)
+{
+  /* search everything */
+  string object, op_id;
+  get_entries_by_object(ioctx, oid, entries, object, op_id, expected);
 }
 
 TEST(cls_rgw, test_statelog_basic)
@@ -79,12 +116,8 @@ TEST(cls_rgw, test_statelog_basic)
 
   for (int i = 0; i < num_ops; i++) {
     next_op_id(op_ids[i], &id);
-    char buf[16];
-    snprintf(buf, sizeof(buf), "obj-%d", i / 2);
-
+    string obj = get_obj_name(i / 2);
     string cid = client_id[i / (num_ops / 2)];
-
-    string obj = buf;
     add_log(op, cid, op_ids[i], obj, i /* just for testing */); 
   }
   ASSERT_EQ(0, ioctx.operate(oid, op));
@@ -96,63 +129,77 @@ TEST(cls_rgw, test_statelog_basic)
 
   /* check list by client_id */
 
-  string marker;
-
-  string obj;
-  string cid = client_id[0];
-  string op_id;
-
-  cls_statelog_list(*rop, cid, op_id, obj, marker, 0, entries, &marker, &truncated);
-
-  bufferlist obl;
-  ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
-
-  ASSERT_EQ(5, (int)entries.size());
-  ASSERT_EQ(0, (int)truncated);
-#if 0
-  list<cls_log_entry>::iterator iter;
-
-  /* need to sort returned entries, all were using the same time as key */
-  map<int, cls_log_entry> check_ents;
-
-  for (iter = entries.begin(); iter != entries.end(); ++iter) {
-    cls_log_entry& entry = *iter;
-
-    int num;
-    ASSERT_EQ(0, read_bl(entry.data, &num));
-
-    check_ents[num] = entry;
+  int total_count = 0;
+  for (int j = 0; j < 2; j++) {
+    string marker;
+    string obj;
+    string cid = client_id[j];
+    string op_id;
+
+    bufferlist obl;
+
+    cls_statelog_list(*rop, cid, op_id, obj, marker, 1, entries, &marker, &truncated);
+    ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+    ASSERT_EQ(1, (int)entries.size());
+
+    reset_rop(&rop);
+    marker.clear();
+    cls_statelog_list(*rop, cid, op_id, obj, marker, 0, entries, &marker, &truncated);
+    obl.clear();
+    ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+
+    ASSERT_EQ(5, (int)entries.size());
+    ASSERT_EQ(0, (int)truncated);
+
+    map<string, string> emap;
+    for (list<cls_statelog_entry>::iterator iter = entries.begin(); iter != entries.end(); ++iter) {
+      ASSERT_EQ(cid, iter->client_id);
+      emap[iter->op_id] = iter->object;
+    }
+    ASSERT_EQ(5, (int)emap.size());
+    /* verify correct object */
+    for (int i = 0; i < num_ops / 2; i++, total_count++) {
+      string ret_obj = emap[op_ids[total_count]];
+      string obj = get_obj_name(total_count / 2);
+      ASSERT_EQ(0, ret_obj.compare(obj));
+    }
   }
 
-  ASSERT_EQ(10, (int)check_ents.size());
-
-  map<int, cls_log_entry>::iterator ei;
-
-  /* verify entries are as expected */
-
-  int i;
-
-  for (i = 0, ei = check_ents.begin(); i < 10; i++, ++ei) {
-    cls_log_entry& entry = ei->second;
-
-    ASSERT_EQ(i, ei->first);
-    check_entry(entry, start_time, i, false);
+  entries.clear();
+  /* now search by object */
+  total_count = 0;
+  for (int i = 0; i < num_ops; i++) {
+    string marker;
+    string obj = get_obj_name(i / 2);
+    string cid;
+    string op_id;
+    bufferlist obl;
+
+    reset_rop(&rop);
+    cls_statelog_list(*rop, cid, op_id, obj, marker, 0, entries, &marker, &truncated);
+    ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+    ASSERT_EQ(2, (int)entries.size());
   }
 
-  reset_rop(&rop);
+  /* search everything */
 
-  /* check list again, now want to be truncated*/
+  get_all_entries(ioctx, oid, entries, 10);
 
-  marker.clear();
+  /* now remove an entry */
+  cls_statelog_entry e = entries.front();
+  entries.pop_front();
 
-  cls_log_list(*rop, start_time, to_time, marker, 1, entries, &marker, &truncated);
+  reset_op(&op);
+  cls_statelog_remove_by_client(*op, e.client_id, e.op_id);
+  ASSERT_EQ(0, ioctx.operate(oid, op));
 
-  ASSERT_EQ(0, ioctx.operate(oid, rop, &obl));
+  get_all_entries(ioctx, oid, entries, 9);
 
-  ASSERT_EQ(1, (int)entries.size());
-  ASSERT_EQ(1, (int)truncated);
+  get_entries_by_object(ioctx, oid, entries, e.object, e.op_id, 0);
+  get_entries_by_client_id(ioctx, oid, entries, e.client_id, e.op_id, 0);
 
-  delete rop;
-#endif
+  string empty_str;
+  get_entries_by_client_id(ioctx, oid, entries, e.client_id, empty_str, 4);
+  get_entries_by_object(ioctx, oid, entries, e.object, empty_str, 1);
 }