]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/rgw/test_rgw_obj.cc
bump version to 18.2.2-pve1
[ceph.git] / 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
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 eNovance SAS <licensing@enovance.com>
7 *
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.
12 *
13 */
14 #include <iostream>
15 #include "common/ceph_json.h"
16 #include "common/Formatter.h"
17 #include "rgw_common.h"
18 #include "rgw_rados.h"
19 #include "services/svc_tier_rados.h"
20 #include "test_rgw_common.h"
21 #include <gtest/gtest.h>
22
23 using namespace std;
24
25 void check_parsed_correctly(rgw_obj& obj, const string& name, const string& ns, const string& instance)
26 {
27 /* parse_raw_oid() */
28 rgw_obj_key parsed_key;
29 ASSERT_EQ(true, rgw_obj_key::parse_raw_oid(obj.get_oid(), &parsed_key));
30
31 cout << "parsed: " << parsed_key << std::endl;
32
33 ASSERT_EQ(name, parsed_key.name);
34 ASSERT_EQ(ns, parsed_key.ns);
35 ASSERT_EQ(instance, parsed_key.instance);
36
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));
41
42 tkey = rgw_obj_key();
43 tns = ns;
44 ASSERT_EQ(true, rgw_obj_key::oid_to_key_in_ns(obj.get_oid(), &tkey, tns));
45
46 cout << "parsed: " << tkey << std::endl;
47
48 ASSERT_EQ(obj.key, tkey);
49
50 /* strip_namespace_from_object() */
51
52 string strip_name = obj.get_oid();
53 string strip_ns, strip_instance;
54
55 ASSERT_EQ(true, rgw_obj_key::strip_namespace_from_name(strip_name, strip_ns, strip_instance));
56
57 cout << "stripped: " << strip_name << " ns=" << strip_ns << " i=" << strip_instance << std::endl;
58
59 ASSERT_EQ(name, strip_name);
60 ASSERT_EQ(ns, strip_ns);
61 ASSERT_EQ(instance, strip_instance);
62 }
63
64 void test_obj(const string& name, const string& ns, const string& instance)
65 {
66 rgw_bucket b;
67 test_rgw_init_bucket(&b, "test");
68
69 JSONFormatter *formatter = new JSONFormatter(true);
70
71 formatter->open_object_section("test");
72 rgw_obj o(b, name);
73 rgw_obj obj1(o);
74
75 if (!instance.empty()) {
76 obj1.key.instance = instance;
77 }
78 if (!ns.empty()) {
79 obj1.key.ns = ns;
80 }
81
82 check_parsed_correctly(obj1, name, ns, instance);
83 encode_json("obj1", obj1, formatter);
84
85 bufferlist bl;
86 encode(obj1, bl);
87
88 rgw_obj obj2;
89 decode(obj2, bl);
90 check_parsed_correctly(obj2, name, ns, instance);
91
92 encode_json("obj2", obj2, formatter);
93
94 rgw_obj obj3(o);
95 bufferlist bl3;
96 encode(obj3, bl3);
97 decode(obj3, bl3);
98 encode_json("obj3", obj3, formatter);
99
100 if (!instance.empty()) {
101 obj3.key.instance = instance;
102 }
103 if (!ns.empty()) {
104 obj3.key.ns = ns;
105 }
106 check_parsed_correctly(obj3, name, ns, instance);
107
108 encode_json("obj3-2", obj3, formatter);
109
110 formatter->close_section();
111
112 formatter->flush(cout);
113
114 ASSERT_EQ(obj1, obj2);
115 ASSERT_EQ(obj1, obj3);
116
117
118 /* rgw_obj_key conversion */
119 rgw_obj_index_key k;
120 obj1.key.get_index_key(&k);
121
122 rgw_obj new_obj(b, k);
123
124 ASSERT_EQ(obj1, new_obj);
125
126 delete formatter;
127 }
128
129 TEST(TestRGWObj, underscore) {
130 test_obj("_obj", "", "");
131 test_obj("_obj", "ns", "");
132 test_obj("_obj", "", "v1");
133 test_obj("_obj", "ns", "v1");
134 }
135
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");
141 }
142
143 template <class T>
144 void dump(JSONFormatter& f, const string& name, const T& entity)
145 {
146 f.open_object_section(name.c_str());
147 ::encode_json(name.c_str(), entity, &f);
148 f.close_section();
149 f.flush(cout);
150 }
151
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)
155 {
156 JSONFormatter f(true);
157 dump(f, "bucket", b);
158 rgw_obj obj = test_rgw_create_obj(b, name, instance, ns);
159 dump(f, "obj", obj);
160
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);
164
165 if (!placement_id.empty()) {
166 ASSERT_EQ(raw_obj.pool, env.get_placement(placement_id).data_pool);
167 } else {
168 ASSERT_EQ(raw_obj.pool, b.explicit_placement.data_pool);
169 }
170 ASSERT_EQ(raw_obj.oid, test_rgw_get_obj_oid(obj));
171
172 rgw_obj new_obj;
173 RGWSI_Tier_RADOS::raw_obj_to_obj(b, raw_obj, &new_obj);
174
175 dump(f, "new_obj", new_obj);
176
177 ASSERT_EQ(obj, new_obj);
178
179 }
180
181 TEST(TestRGWObj, obj_to_raw) {
182 test_rgw_env env;
183
184 rgw_bucket b;
185 test_rgw_init_bucket(&b, "test");
186
187 rgw_bucket eb;
188 test_rgw_init_explicit_placement_bucket(&eb, "ebtest");
189
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());
195 }
196 }
197 }
198 }
199
200 TEST(TestRGWObj, old_to_raw) {
201 JSONFormatter f(true);
202 test_rgw_env env;
203
204 old_rgw_bucket eb;
205 test_rgw_init_old_bucket(&eb, "ebtest");
206
207 for (auto name : { "myobj", "_myobj", "_myobj_"}) {
208 for (string inst : { "", "inst"}) {
209 for (string ns : { "", "ns"}) {
210 old_rgw_obj old(eb, name);
211 if (!inst.empty()) {
212 old.set_instance(inst);
213 }
214 if (!ns.empty()) {
215 old.set_ns(ns);
216 }
217
218 bufferlist bl;
219
220 encode(old, bl);
221
222 rgw_obj new_obj;
223 rgw_raw_obj raw_obj;
224
225 try {
226 auto iter = bl.cbegin();
227 decode(new_obj, iter);
228
229 iter = bl.begin();
230 decode(raw_obj, iter);
231 } catch (buffer::error& err) {
232 ASSERT_TRUE(false);
233 }
234
235 bl.clear();
236
237 rgw_obj new_obj2;
238 rgw_raw_obj raw_obj2;
239
240 encode(new_obj, bl);
241
242 dump(f, "raw_obj", raw_obj);
243 dump(f, "new_obj", new_obj);
244 cout << "raw=" << raw_obj << std::endl;
245
246 try {
247 auto iter = bl.cbegin();
248 decode(new_obj2, iter);
249
250 /*
251 can't decode raw obj here, because we didn't encode an old versioned
252 object
253 */
254
255 bl.clear();
256 encode(raw_obj, bl);
257 iter = bl.begin();
258 decode(raw_obj2, iter);
259 } catch (buffer::error& err) {
260 ASSERT_TRUE(false);
261 }
262
263 dump(f, "raw_obj2", raw_obj2);
264 dump(f, "new_obj2", new_obj2);
265 cout << "raw2=" << raw_obj2 << std::endl;
266
267 ASSERT_EQ(new_obj, new_obj2);
268 ASSERT_EQ(raw_obj, raw_obj2);
269 }
270 }
271 }
272 }