1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "global/global_init.h"
16 #include "common/ceph_json.h"
17 #include "common/Formatter.h"
18 #include "rgw/rgw_common.h"
19 #include "rgw/rgw_rados.h"
20 #include "test_rgw_common.h"
23 #include <gtest/gtest.h>
25 #define TEST(x, y) void y()
26 #define ASSERT_EQ(v, s) if(v != s)cout << "Error at " << __LINE__ << "(" << #v << "!= " << #s << "\n"; \
27 else cout << "(" << #v << "==" << #s << ") PASSED\n";
28 #define EXPECT_EQ(v, s) ASSERT_EQ(v, s)
29 #define ASSERT_TRUE(c) if(c)cout << "Error at " << __LINE__ << "(" << #c << ")" << "\n"; \
30 else cout << "(" << #c << ") PASSED\n";
31 #define EXPECT_TRUE(c) ASSERT_TRUE(c)
35 void check_parsed_correctly(rgw_obj& obj, const string& name, const string& ns, const string& instance)
38 rgw_obj_key parsed_key;
39 ASSERT_EQ(true, rgw_obj_key::parse_raw_oid(obj.get_oid(), &parsed_key));
41 cout << "parsed: " << parsed_key << std::endl;
43 ASSERT_EQ(name, parsed_key.name);
44 ASSERT_EQ(ns, parsed_key.ns);
45 ASSERT_EQ(instance, parsed_key.instance);
47 /* translate_raw_obj_to_obj_in_ns() */
48 rgw_obj_key tkey = parsed_key;
49 string tns = ns + "foo";
50 ASSERT_EQ(0, rgw_obj_key::oid_to_key_in_ns(obj.get_oid(), &tkey, tns));
54 ASSERT_EQ(true, rgw_obj_key::oid_to_key_in_ns(obj.get_oid(), &tkey, tns));
56 cout << "parsed: " << tkey << std::endl;
58 ASSERT_EQ(obj.key, tkey);
60 /* strip_namespace_from_object() */
62 string strip_name = obj.get_oid();
63 string strip_ns, strip_instance;
65 ASSERT_EQ(true, rgw_obj_key::strip_namespace_from_name(strip_name, strip_ns, strip_instance));
67 cout << "stripped: " << strip_name << " ns=" << strip_ns << " i=" << strip_instance << std::endl;
69 ASSERT_EQ(name, strip_name);
70 ASSERT_EQ(ns, strip_ns);
71 ASSERT_EQ(instance, strip_instance);
74 void test_obj(const string& name, const string& ns, const string& instance)
77 test_rgw_init_bucket(&b, "test");
79 JSONFormatter *formatter = new JSONFormatter(true);
81 formatter->open_object_section("test");
85 if (!instance.empty()) {
86 obj1.key.instance = instance;
92 check_parsed_correctly(obj1, name, ns, instance);
93 encode_json("obj1", obj1, formatter);
100 check_parsed_correctly(obj2, name, ns, instance);
102 encode_json("obj2", obj2, formatter);
108 encode_json("obj3", obj3, formatter);
110 if (!instance.empty()) {
111 obj3.key.instance = instance;
116 check_parsed_correctly(obj3, name, ns, instance);
118 encode_json("obj3-2", obj3, formatter);
120 formatter->close_section();
122 formatter->flush(cout);
124 ASSERT_EQ(obj1, obj2);
125 ASSERT_EQ(obj1, obj3);
128 /* rgw_obj_key conversion */
130 obj1.key.get_index_key(&k);
132 rgw_obj new_obj(b, k);
134 ASSERT_EQ(obj1, new_obj);
139 TEST(TestRGWObj, underscore) {
140 test_obj("_obj", "", "");
141 test_obj("_obj", "ns", "");
142 test_obj("_obj", "", "v1");
143 test_obj("_obj", "ns", "v1");
146 TEST(TestRGWObj, no_underscore) {
147 test_obj("obj", "", "");
148 test_obj("obj", "ns", "");
149 test_obj("obj", "", "v1");
150 test_obj("obj", "ns", "v1");
154 void dump(JSONFormatter& f, const string& name, const T& entity)
156 f.open_object_section(name.c_str());
157 ::encode_json(name.c_str(), entity, &f);
162 static void test_obj_to_raw(test_rgw_env& env, const rgw_bucket& b,
163 const string& name, const string& instance, const string& ns,
164 const string& placement_id)
166 JSONFormatter f(true);
167 dump(f, "bucket", b);
168 rgw_obj obj = test_rgw_create_obj(b, name, instance, ns);
171 rgw_obj_select s(obj);
172 rgw_raw_obj raw_obj = s.get_raw_obj(env.zonegroup, env.zone_params);
173 dump(f, "raw_obj", raw_obj);
175 if (!placement_id.empty()) {
176 ASSERT_EQ(raw_obj.pool, env.get_placement(placement_id).data_pool);
178 ASSERT_EQ(raw_obj.pool, b.explicit_placement.data_pool);
180 ASSERT_EQ(raw_obj.oid, test_rgw_get_obj_oid(obj));
183 rgw_raw_obj_to_obj(b, raw_obj, &new_obj);
185 dump(f, "new_obj", new_obj);
187 ASSERT_EQ(obj, new_obj);
191 TEST(TestRGWObj, obj_to_raw) {
195 test_rgw_init_bucket(&b, "test");
198 test_rgw_init_explicit_placement_bucket(&eb, "ebtest");
200 for (auto name : { "myobj", "_myobj", "_myobj_"}) {
201 for (auto inst : { "", "inst"}) {
202 for (auto ns : { "", "ns"}) {
203 test_obj_to_raw(env, b, name, inst, ns, env.zonegroup.default_placement);
204 test_obj_to_raw(env, eb, name, inst, ns, string());
210 TEST(TestRGWObj, old_to_raw) {
211 JSONFormatter f(true);
215 test_rgw_init_old_bucket(&eb, "ebtest");
217 for (auto name : { "myobj", "_myobj", "_myobj_"}) {
218 for (string inst : { "", "inst"}) {
219 for (string ns : { "", "ns"}) {
220 old_rgw_obj old(eb, name);
222 old.set_instance(inst);
236 bufferlist::iterator iter = bl.begin();
237 ::decode(new_obj, iter);
240 ::decode(raw_obj, iter);
241 } catch (buffer::error& err) {
248 rgw_raw_obj raw_obj2;
250 ::encode(new_obj, bl);
252 dump(f, "raw_obj", raw_obj);
253 dump(f, "new_obj", new_obj);
254 cout << "raw=" << raw_obj << std::endl;
257 bufferlist::iterator iter = bl.begin();
258 ::decode(new_obj2, iter);
261 can't decode raw obj here, because we didn't encode an old versioned
266 ::encode(raw_obj, bl);
268 ::decode(raw_obj2, iter);
269 } catch (buffer::error& err) {
273 dump(f, "raw_obj2", raw_obj2);
274 dump(f, "new_obj2", new_obj2);
275 cout << "raw2=" << raw_obj2 << std::endl;
277 ASSERT_EQ(new_obj, new_obj2);
278 ASSERT_EQ(raw_obj, raw_obj2);