]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: unit test for testing rgw_obj encoding correctness 5928/head
authorYehuda Sadeh <yehuda@redhat.com>
Thu, 10 Sep 2015 20:02:19 +0000 (13:02 -0700)
committerYehuda Sadeh <yehuda@redhat.com>
Mon, 14 Sep 2015 20:19:36 +0000 (13:19 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/test/CMakeLists.txt
src/test/Makefile-client.am
src/test/rgw/test_rgw_obj.cc [new file with mode: 0644]

index 18ea8815758e253bb2fc3b292b9b6625efa68fe8..413b4e2dad7b3b05cd5f50b3e02131af681b6ecf 100644 (file)
@@ -1463,6 +1463,36 @@ if(${WITH_RADOSGW})
   set_target_properties(test_rgw_manifest PROPERTIES COMPILE_FLAGS
     ${UNITTEST_CXX_FLAGS})
 
+  # test_rgw_obj
+  set(test_rgw_obj_srcs rgw/test_rgw_obj.cc)
+  add_executable(test_rgw_obj
+    ${test_rgw_obj_srcs}
+    $<TARGET_OBJECTS:heap_profiler_objs>
+    )
+  target_link_libraries(test_rgw_obj
+    rgw_a
+    cls_rgw_client
+    cls_lock_client
+    cls_refcount_client
+    cls_log_client
+    cls_statelog_client
+    cls_version_client
+    cls_replica_log_client
+    cls_kvs
+    cls_user_client
+    librados
+    global
+    curl
+    uuid
+    expat
+    ${CMAKE_DL_LIBS}
+    ${TCMALLOC_LIBS}
+    ${UNITTEST_LIBS}
+    ${CRYPTO_LIBS}
+    )
+  set_target_properties(test_rgw_obj PROPERTIES COMPILE_FLAGS
+    ${UNITTEST_CXX_FLAGS})
+
   # test_cls_rgw_meta
   set(test_cls_rgw_meta_srcs test_rgw_admin_meta.cc)
   add_executable(test_cls_rgw_meta
index ec40ccaab925d0a6166b63609add99d6cc1752f3..83d7cce360e7fc9733814c5660f01642d2199910 100644 (file)
@@ -533,6 +533,15 @@ ceph_test_rgw_manifest_LDADD = \
 ceph_test_rgw_manifest_CXXFLAGS = $(UNITTEST_CXXFLAGS)
 bin_DEBUGPROGRAMS += ceph_test_rgw_manifest
 
+ceph_test_rgw_obj_SOURCES = test/rgw/test_rgw_obj.cc
+ceph_test_rgw_obj_LDADD = \
+       $(LIBRADOS) $(LIBRGW) $(LIBRGW_DEPS) $(CEPH_GLOBAL) \
+       $(UNITTEST_LDADD) $(CRYPTO_LIBS) \
+       -lcurl -luuid -lexpat
+
+ceph_test_rgw_obj_CXXFLAGS = $(UNITTEST_CXXFLAGS)
+bin_DEBUGPROGRAMS += ceph_test_rgw_obj
+
 ceph_test_cls_rgw_meta_SOURCES = test/test_rgw_admin_meta.cc
 ceph_test_cls_rgw_meta_LDADD = \
        $(LIBRADOS) $(LIBRGW) $(CEPH_GLOBAL) \
diff --git a/src/test/rgw/test_rgw_obj.cc b/src/test/rgw/test_rgw_obj.cc
new file mode 100644 (file)
index 0000000..18696a6
--- /dev/null
@@ -0,0 +1,159 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+#include <iostream>
+#include "global/global_init.h"
+#include "common/ceph_json.h"
+#include "common/Formatter.h"
+#include "rgw/rgw_common.h"
+#define GTEST
+#ifdef GTEST
+#include <gtest/gtest.h>
+#else
+#define TEST(x, y) void y()
+#define ASSERT_EQ(v, s) if(v != s)cout << "Error at " << __LINE__ << "(" << #v << "!= " << #s << "\n"; \
+                                else cout << "(" << #v << "==" << #s << ") PASSED\n";
+#define EXPECT_EQ(v, s) ASSERT_EQ(v, s)
+#define ASSERT_TRUE(c) if(c)cout << "Error at " << __LINE__ << "(" << #c << ")" << "\n"; \
+                          else cout << "(" << #c << ") PASSED\n";
+#define EXPECT_TRUE(c) ASSERT_TRUE(c) 
+#endif
+using namespace std;
+
+static void init_bucket(rgw_bucket *bucket, const char *name)
+{
+  *bucket = rgw_bucket(name, ".data-pool", ".index-pool", "marker", "bucket-id", NULL);
+}
+
+void check_parsed_correctly(rgw_obj& obj, const string& name, const string& ns, const string& instance)
+{
+  /* parse_raw_oid() */
+  string parsed_name, parsed_ns, parsed_instance;
+  ASSERT_EQ(true, rgw_obj::parse_raw_oid(obj.get_object(), &parsed_name, &parsed_instance, &parsed_ns));
+
+  cout << "parsed: " << parsed_name << " ns=" << parsed_ns << " i=" << parsed_instance << std::endl;
+
+  ASSERT_EQ(name, parsed_name);
+  ASSERT_EQ(ns, parsed_ns);
+  ASSERT_EQ(instance, parsed_instance);
+
+  /* translate_raw_obj_to_obj_in_ns() */
+  string tname = obj.get_object();
+  string tns = ns + "foo";
+  string tinstance;
+  ASSERT_EQ(0, rgw_obj::translate_raw_obj_to_obj_in_ns(tname, tinstance, tns));
+  ASSERT_EQ(name, tname);
+  ASSERT_EQ(instance, tinstance);
+
+  tname = obj.get_object();
+  tns = ns;
+  ASSERT_EQ(true, rgw_obj::translate_raw_obj_to_obj_in_ns(tname, tinstance, tns));
+
+  cout << "parsed: " << parsed_name << " ns=" << parsed_ns << " i=" << parsed_instance << std::endl;
+
+  ASSERT_EQ(name, tname);
+  ASSERT_EQ(instance, tinstance);
+
+  /* strip_namespace_from_object() */
+
+  string strip_name = obj.get_object();
+  string strip_ns, strip_instance;
+
+  ASSERT_EQ(true, rgw_obj::strip_namespace_from_object(strip_name, strip_ns, strip_instance));
+
+  cout << "stripped: " << strip_name << " ns=" << strip_ns << " i=" << strip_instance << std::endl;
+
+  ASSERT_EQ(name, strip_name);
+  ASSERT_EQ(ns, strip_ns);
+  ASSERT_EQ(instance, strip_instance);
+}
+
+void test_obj(const string& name, const string& ns, const string& instance)
+{
+  rgw_bucket b;
+  init_bucket(&b, "test");
+
+  JSONFormatter *formatter = new JSONFormatter(true);
+
+  formatter->open_object_section("test");
+  rgw_obj o(b, name);
+  rgw_obj obj1(o);
+
+  if (!instance.empty()) {
+    obj1.set_instance(instance);
+  }
+  if (!ns.empty()) {
+    obj1.set_ns(ns);
+  }
+  
+  check_parsed_correctly(obj1, name, ns, instance);
+  encode_json("obj1", obj1, formatter);
+
+  bufferlist bl;
+  ::encode(obj1, bl);
+
+  rgw_obj obj2;
+  ::decode(obj2, bl);
+  check_parsed_correctly(obj2, name, ns, instance);
+
+  encode_json("obj2", obj2, formatter);
+
+  rgw_obj obj3(o);
+  bufferlist bl3;
+  ::encode(obj3, bl3);
+  ::decode(obj3, bl3);
+  encode_json("obj3", obj3, formatter);
+
+  if (!instance.empty()) {
+    obj3.set_instance(instance);
+  }
+  if (!ns.empty()) {
+    obj3.set_ns(ns);
+  }
+  check_parsed_correctly(obj3, name, ns, instance);
+
+  encode_json("obj3-2", obj3, formatter);
+
+  formatter->close_section();
+
+  formatter->flush(cout);
+
+  ASSERT_EQ(obj1, obj2);
+  ASSERT_EQ(obj1, obj3);
+
+
+  /* rgw_obj_key conversion */
+  rgw_obj_key k;
+  obj1.get_index_key(&k);
+
+  rgw_obj new_obj(b, k);
+
+  ASSERT_EQ(obj1, new_obj);
+
+  delete formatter;
+}
+
+TEST(TestRGWObj, underscore) {
+  test_obj("_obj", "", "");
+  test_obj("_obj", "ns", "");
+  test_obj("_obj", "", "v1");
+  test_obj("_obj", "ns", "v1");
+}
+
+TEST(TestRGWObj, no_underscore) {
+  test_obj("obj", "", "");
+  test_obj("obj", "ns", "");
+  test_obj("obj", "", "v1");
+  test_obj("obj", "ns", "v1");
+}
+