Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "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"
21 #define GTEST
22 #ifdef GTEST
23 #include <gtest/gtest.h>
24 #else
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) 
32 #endif
33 using namespace std;
34
35 void check_parsed_correctly(rgw_obj& obj, const string& name, const string& ns, const string& instance)
36 {
37   /* parse_raw_oid() */
38   rgw_obj_key parsed_key;
39   ASSERT_EQ(true, rgw_obj_key::parse_raw_oid(obj.get_oid(), &parsed_key));
40
41   cout << "parsed: " << parsed_key << std::endl;
42
43   ASSERT_EQ(name, parsed_key.name);
44   ASSERT_EQ(ns, parsed_key.ns);
45   ASSERT_EQ(instance, parsed_key.instance);
46
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));
51
52   tkey = rgw_obj_key();
53   tns = ns;
54   ASSERT_EQ(true, rgw_obj_key::oid_to_key_in_ns(obj.get_oid(), &tkey, tns));
55
56   cout << "parsed: " << tkey << std::endl;
57
58   ASSERT_EQ(obj.key, tkey);
59
60   /* strip_namespace_from_object() */
61
62   string strip_name = obj.get_oid();
63   string strip_ns, strip_instance;
64
65   ASSERT_EQ(true, rgw_obj_key::strip_namespace_from_name(strip_name, strip_ns, strip_instance));
66
67   cout << "stripped: " << strip_name << " ns=" << strip_ns << " i=" << strip_instance << std::endl;
68
69   ASSERT_EQ(name, strip_name);
70   ASSERT_EQ(ns, strip_ns);
71   ASSERT_EQ(instance, strip_instance);
72 }
73
74 void test_obj(const string& name, const string& ns, const string& instance)
75 {
76   rgw_bucket b;
77   test_rgw_init_bucket(&b, "test");
78
79   JSONFormatter *formatter = new JSONFormatter(true);
80
81   formatter->open_object_section("test");
82   rgw_obj o(b, name);
83   rgw_obj obj1(o);
84
85   if (!instance.empty()) {
86     obj1.key.instance = instance;
87   }
88   if (!ns.empty()) {
89     obj1.key.ns = ns;
90   }
91   
92   check_parsed_correctly(obj1, name, ns, instance);
93   encode_json("obj1", obj1, formatter);
94
95   bufferlist bl;
96   ::encode(obj1, bl);
97
98   rgw_obj obj2;
99   ::decode(obj2, bl);
100   check_parsed_correctly(obj2, name, ns, instance);
101
102   encode_json("obj2", obj2, formatter);
103
104   rgw_obj obj3(o);
105   bufferlist bl3;
106   ::encode(obj3, bl3);
107   ::decode(obj3, bl3);
108   encode_json("obj3", obj3, formatter);
109
110   if (!instance.empty()) {
111     obj3.key.instance = instance;
112   }
113   if (!ns.empty()) {
114     obj3.key.ns = ns;
115   }
116   check_parsed_correctly(obj3, name, ns, instance);
117
118   encode_json("obj3-2", obj3, formatter);
119
120   formatter->close_section();
121
122   formatter->flush(cout);
123
124   ASSERT_EQ(obj1, obj2);
125   ASSERT_EQ(obj1, obj3);
126
127
128   /* rgw_obj_key conversion */
129   rgw_obj_index_key k;
130   obj1.key.get_index_key(&k);
131
132   rgw_obj new_obj(b, k);
133
134   ASSERT_EQ(obj1, new_obj);
135
136   delete formatter;
137 }
138
139 TEST(TestRGWObj, underscore) {
140   test_obj("_obj", "", "");
141   test_obj("_obj", "ns", "");
142   test_obj("_obj", "", "v1");
143   test_obj("_obj", "ns", "v1");
144 }
145
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");
151 }
152
153 template <class T>
154 void dump(JSONFormatter& f, const string& name, const T& entity)
155 {
156   f.open_object_section(name.c_str());
157   ::encode_json(name.c_str(), entity, &f);
158   f.close_section();
159   f.flush(cout);
160 }
161
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)
165 {
166   JSONFormatter f(true);
167   dump(f, "bucket", b);
168   rgw_obj obj = test_rgw_create_obj(b, name, instance, ns);
169   dump(f, "obj", obj);
170
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);
174
175   if (!placement_id.empty()) {
176     ASSERT_EQ(raw_obj.pool, env.get_placement(placement_id).data_pool);
177   } else {
178     ASSERT_EQ(raw_obj.pool, b.explicit_placement.data_pool);
179   }
180   ASSERT_EQ(raw_obj.oid, test_rgw_get_obj_oid(obj));
181
182   rgw_obj new_obj;
183   rgw_raw_obj_to_obj(b, raw_obj, &new_obj);
184
185   dump(f, "new_obj", new_obj);
186
187   ASSERT_EQ(obj, new_obj);
188
189 }
190
191 TEST(TestRGWObj, obj_to_raw) {
192   test_rgw_env env;
193
194   rgw_bucket b;
195   test_rgw_init_bucket(&b, "test");
196
197   rgw_bucket eb;
198   test_rgw_init_explicit_placement_bucket(&eb, "ebtest");
199
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());
205       }
206     }
207   }
208 }
209
210 TEST(TestRGWObj, old_to_raw) {
211   JSONFormatter f(true);
212   test_rgw_env env;
213
214   old_rgw_bucket eb;
215   test_rgw_init_old_bucket(&eb, "ebtest");
216
217   for (auto name : { "myobj", "_myobj", "_myobj_"}) {
218     for (string inst : { "", "inst"}) {
219       for (string ns : { "", "ns"}) {
220         old_rgw_obj old(eb, name);
221         if (!inst.empty()) {
222           old.set_instance(inst);
223         }
224         if (!ns.empty()) {
225           old.set_ns(ns);
226         }
227
228         bufferlist bl;
229
230         ::encode(old, bl);
231
232         rgw_obj new_obj;
233         rgw_raw_obj raw_obj;
234
235         try {
236           bufferlist::iterator iter = bl.begin();
237           ::decode(new_obj, iter);
238
239           iter = bl.begin();
240           ::decode(raw_obj, iter);
241         } catch (buffer::error& err) {
242           ASSERT_TRUE(false);
243         }
244
245         bl.clear();
246
247         rgw_obj new_obj2;
248         rgw_raw_obj raw_obj2;
249
250         ::encode(new_obj, bl);
251
252         dump(f, "raw_obj", raw_obj);
253         dump(f, "new_obj", new_obj);
254         cout << "raw=" << raw_obj << std::endl;
255
256         try {
257           bufferlist::iterator iter = bl.begin();
258           ::decode(new_obj2, iter);
259
260           /*
261             can't decode raw obj here, because we didn't encode an old versioned
262             object
263            */
264
265           bl.clear();
266           ::encode(raw_obj, bl);
267           iter = bl.begin();
268           ::decode(raw_obj2, iter);
269         } catch (buffer::error& err) {
270           ASSERT_TRUE(false);
271         }
272
273         dump(f, "raw_obj2", raw_obj2);
274         dump(f, "new_obj2", new_obj2);
275         cout << "raw2=" << raw_obj2 << std::endl;
276
277         ASSERT_EQ(new_obj, new_obj2);
278         ASSERT_EQ(raw_obj, raw_obj2);
279       }
280     }
281   }
282 }