]>
git.proxmox.com Git - ceph.git/blob - ceph/src/test/rgw/test_rgw_obj.cc
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 "common/ceph_json.h"
16 #include "common/Formatter.h"
17 #include "rgw/rgw_common.h"
18 #include "rgw/rgw_rados.h"
19 #include "rgw/services/svc_tier_rados.h"
20 #include "test_rgw_common.h"
21 #include <gtest/gtest.h>
25 void check_parsed_correctly(rgw_obj
& obj
, const string
& name
, const string
& ns
, const string
& instance
)
28 rgw_obj_key parsed_key
;
29 ASSERT_EQ(true, rgw_obj_key::parse_raw_oid(obj
.get_oid(), &parsed_key
));
31 cout
<< "parsed: " << parsed_key
<< std::endl
;
33 ASSERT_EQ(name
, parsed_key
.name
);
34 ASSERT_EQ(ns
, parsed_key
.ns
);
35 ASSERT_EQ(instance
, parsed_key
.instance
);
37 /* translate_raw_obj_to_obj_in_ns() */
38 rgw_obj_key tkey
= parsed_key
;
39 string tns
= ns
+ "foo";
40 ASSERT_EQ(0, rgw_obj_key::oid_to_key_in_ns(obj
.get_oid(), &tkey
, tns
));
44 ASSERT_EQ(true, rgw_obj_key::oid_to_key_in_ns(obj
.get_oid(), &tkey
, tns
));
46 cout
<< "parsed: " << tkey
<< std::endl
;
48 ASSERT_EQ(obj
.key
, tkey
);
50 /* strip_namespace_from_object() */
52 string strip_name
= obj
.get_oid();
53 string strip_ns
, strip_instance
;
55 ASSERT_EQ(true, rgw_obj_key::strip_namespace_from_name(strip_name
, strip_ns
, strip_instance
));
57 cout
<< "stripped: " << strip_name
<< " ns=" << strip_ns
<< " i=" << strip_instance
<< std::endl
;
59 ASSERT_EQ(name
, strip_name
);
60 ASSERT_EQ(ns
, strip_ns
);
61 ASSERT_EQ(instance
, strip_instance
);
64 void test_obj(const string
& name
, const string
& ns
, const string
& instance
)
67 test_rgw_init_bucket(&b
, "test");
69 JSONFormatter
*formatter
= new JSONFormatter(true);
71 formatter
->open_object_section("test");
75 if (!instance
.empty()) {
76 obj1
.key
.instance
= instance
;
82 check_parsed_correctly(obj1
, name
, ns
, instance
);
83 encode_json("obj1", obj1
, formatter
);
90 check_parsed_correctly(obj2
, name
, ns
, instance
);
92 encode_json("obj2", obj2
, formatter
);
98 encode_json("obj3", obj3
, formatter
);
100 if (!instance
.empty()) {
101 obj3
.key
.instance
= instance
;
106 check_parsed_correctly(obj3
, name
, ns
, instance
);
108 encode_json("obj3-2", obj3
, formatter
);
110 formatter
->close_section();
112 formatter
->flush(cout
);
114 ASSERT_EQ(obj1
, obj2
);
115 ASSERT_EQ(obj1
, obj3
);
118 /* rgw_obj_key conversion */
120 obj1
.key
.get_index_key(&k
);
122 rgw_obj
new_obj(b
, k
);
124 ASSERT_EQ(obj1
, new_obj
);
129 TEST(TestRGWObj
, underscore
) {
130 test_obj("_obj", "", "");
131 test_obj("_obj", "ns", "");
132 test_obj("_obj", "", "v1");
133 test_obj("_obj", "ns", "v1");
136 TEST(TestRGWObj
, no_underscore
) {
137 test_obj("obj", "", "");
138 test_obj("obj", "ns", "");
139 test_obj("obj", "", "v1");
140 test_obj("obj", "ns", "v1");
144 void dump(JSONFormatter
& f
, const string
& name
, const T
& entity
)
146 f
.open_object_section(name
.c_str());
147 ::encode_json(name
.c_str(), entity
, &f
);
152 static void test_obj_to_raw(test_rgw_env
& env
, const rgw_bucket
& b
,
153 const string
& name
, const string
& instance
, const string
& ns
,
154 const string
& placement_id
)
156 JSONFormatter
f(true);
157 dump(f
, "bucket", b
);
158 rgw_obj obj
= test_rgw_create_obj(b
, name
, instance
, ns
);
161 rgw_obj_select
s(obj
);
162 rgw_raw_obj raw_obj
= s
.get_raw_obj(env
.zonegroup
, env
.zone_params
);
163 dump(f
, "raw_obj", raw_obj
);
165 if (!placement_id
.empty()) {
166 ASSERT_EQ(raw_obj
.pool
, env
.get_placement(placement_id
).data_pool
);
168 ASSERT_EQ(raw_obj
.pool
, b
.explicit_placement
.data_pool
);
170 ASSERT_EQ(raw_obj
.oid
, test_rgw_get_obj_oid(obj
));
173 RGWSI_Tier_RADOS::raw_obj_to_obj(b
, raw_obj
, &new_obj
);
175 dump(f
, "new_obj", new_obj
);
177 ASSERT_EQ(obj
, new_obj
);
181 TEST(TestRGWObj
, obj_to_raw
) {
185 test_rgw_init_bucket(&b
, "test");
188 test_rgw_init_explicit_placement_bucket(&eb
, "ebtest");
190 for (auto name
: { "myobj", "_myobj", "_myobj_"}) {
191 for (auto inst
: { "", "inst"}) {
192 for (auto ns
: { "", "ns"}) {
193 test_obj_to_raw(env
, b
, name
, inst
, ns
, env
.zonegroup
.default_placement
.name
);
194 test_obj_to_raw(env
, eb
, name
, inst
, ns
, string());
200 TEST(TestRGWObj
, old_to_raw
) {
201 JSONFormatter
f(true);
205 test_rgw_init_old_bucket(&eb
, "ebtest");
207 for (auto name
: { "myobj", "_myobj", "_myobj_"}) {
208 for (string inst
: { "", "inst"}) {
209 for (string ns
: { "", "ns"}) {
210 old_rgw_obj
old(eb
, name
);
212 old
.set_instance(inst
);
226 auto iter
= bl
.cbegin();
227 decode(new_obj
, iter
);
230 decode(raw_obj
, iter
);
231 } catch (buffer::error
& err
) {
238 rgw_raw_obj raw_obj2
;
242 dump(f
, "raw_obj", raw_obj
);
243 dump(f
, "new_obj", new_obj
);
244 cout
<< "raw=" << raw_obj
<< std::endl
;
247 auto iter
= bl
.cbegin();
248 decode(new_obj2
, iter
);
251 can't decode raw obj here, because we didn't encode an old versioned
258 decode(raw_obj2
, iter
);
259 } catch (buffer::error
& err
) {
263 dump(f
, "raw_obj2", raw_obj2
);
264 dump(f
, "new_obj2", new_obj2
);
265 cout
<< "raw2=" << raw_obj2
<< std::endl
;
267 ASSERT_EQ(new_obj
, new_obj2
);
268 ASSERT_EQ(raw_obj
, raw_obj2
);