1 // -*- mode:C; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "include/types.h"
5 #include "cls/version/cls_version_types.h"
6 #include "cls/version/cls_version_client.h"
8 #include "gtest/gtest.h"
9 #include "test/librados/test.h"
15 static librados::ObjectWriteOperation *new_op() {
16 return new librados::ObjectWriteOperation();
19 static librados::ObjectReadOperation *new_rop() {
20 return new librados::ObjectReadOperation();
23 TEST(cls_rgw, test_version_inc_read)
25 librados::Rados rados;
26 librados::IoCtx ioctx;
27 string pool_name = get_temp_pool_name();
30 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
31 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
39 ASSERT_EQ(0, ioctx.create(oid, true));
43 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
44 ASSERT_EQ(0, (long long)ver.ver);
45 ASSERT_EQ(0, (int)ver.tag.size());
49 librados::ObjectWriteOperation *op = new_op();
51 ASSERT_EQ(0, ioctx.operate(oid, op));
53 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
54 ASSERT_GT((long long)ver.ver, 0);
55 ASSERT_NE(0, (int)ver.tag.size());
57 /* inc version again! */
61 ASSERT_EQ(0, ioctx.operate(oid, op));
65 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
66 ASSERT_GT((long long)ver2.ver, (long long)ver.ver);
67 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
73 librados::ObjectReadOperation *rop = new_rop();
74 cls_version_read(*rop, &ver3);
76 ASSERT_EQ(0, ioctx.operate(oid, rop, &outbl));
77 ASSERT_EQ(ver2.ver, ver3.ver);
78 ASSERT_EQ(1, (long long)ver2.compare(&ver3));
84 TEST(cls_rgw, test_version_set)
86 librados::Rados rados;
87 librados::IoCtx ioctx;
88 string pool_name = get_temp_pool_name();
91 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
92 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
100 ASSERT_EQ(0, ioctx.create(oid, true));
104 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
105 ASSERT_EQ(0, (long long)ver.ver);
106 ASSERT_EQ(0, (int)ver.tag.size());
113 librados::ObjectWriteOperation *op = new_op();
114 cls_version_set(*op, ver);
115 ASSERT_EQ(0, ioctx.operate(oid, op));
120 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
121 ASSERT_EQ((long long)ver2.ver, (long long)ver.ver);
122 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
127 TEST(cls_rgw, test_version_inc_cond)
129 librados::Rados rados;
130 librados::IoCtx ioctx;
131 string pool_name = get_temp_pool_name();
134 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
135 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
142 ASSERT_EQ(0, ioctx.create(oid, true));
146 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
147 ASSERT_EQ(0, (long long)ver.ver);
148 ASSERT_EQ(0, (int)ver.tag.size());
151 librados::ObjectWriteOperation *op = new_op();
152 cls_version_inc(*op);
153 ASSERT_EQ(0, ioctx.operate(oid, op));
155 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
156 ASSERT_GT((long long)ver.ver, 0);
157 ASSERT_NE(0, (int)ver.tag.size());
159 obj_version cond_ver = ver;
162 /* inc version again! */
165 cls_version_inc(*op);
166 ASSERT_EQ(0, ioctx.operate(oid, op));
170 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
171 ASSERT_GT((long long)ver2.ver, (long long)ver.ver);
172 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
175 /* now check various condition tests */
176 cls_version_inc(*op, cond_ver, VER_COND_NONE);
177 ASSERT_EQ(0, ioctx.operate(oid, op));
179 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
180 ASSERT_GT((long long)ver2.ver, (long long)ver.ver);
181 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
183 /* a bunch of conditions that should fail */
186 cls_version_inc(*op, cond_ver, VER_COND_EQ);
187 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, op));
191 cls_version_inc(*op, cond_ver, VER_COND_LT);
192 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, op));
196 cls_version_inc(*op, cond_ver, VER_COND_LE);
197 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, op));
201 cls_version_inc(*op, cond_ver, VER_COND_TAG_NE);
202 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, op));
204 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
205 ASSERT_GT((long long)ver2.ver, (long long)ver.ver);
206 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
208 /* a bunch of conditions that should succeed */
211 cls_version_inc(*op, ver2, VER_COND_EQ);
212 ASSERT_EQ(0, ioctx.operate(oid, op));
216 cls_version_inc(*op, cond_ver, VER_COND_GT);
217 ASSERT_EQ(0, ioctx.operate(oid, op));
221 cls_version_inc(*op, cond_ver, VER_COND_GE);
222 ASSERT_EQ(0, ioctx.operate(oid, op));
226 cls_version_inc(*op, cond_ver, VER_COND_TAG_EQ);
227 ASSERT_EQ(0, ioctx.operate(oid, op));
232 TEST(cls_rgw, test_version_inc_check)
234 librados::Rados rados;
235 librados::IoCtx ioctx;
236 string pool_name = get_temp_pool_name();
239 ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
240 ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
248 ASSERT_EQ(0, ioctx.create(oid, true));
252 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
253 ASSERT_EQ(0, (long long)ver.ver);
254 ASSERT_EQ(0, (int)ver.tag.size());
257 librados::ObjectWriteOperation *op = new_op();
258 cls_version_inc(*op);
259 ASSERT_EQ(0, ioctx.operate(oid, op));
261 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver));
262 ASSERT_GT((long long)ver.ver, 0);
263 ASSERT_NE(0, (int)ver.tag.size());
265 obj_version cond_ver = ver;
267 /* a bunch of conditions that should succeed */
268 librados::ObjectReadOperation *rop = new_rop();
269 cls_version_check(*rop, cond_ver, VER_COND_EQ);
271 ASSERT_EQ(0, ioctx.operate(oid, rop, &bl));
275 cls_version_check(*rop, cond_ver, VER_COND_GE);
276 ASSERT_EQ(0, ioctx.operate(oid, rop, &bl));
280 cls_version_check(*rop, cond_ver, VER_COND_LE);
281 ASSERT_EQ(0, ioctx.operate(oid, rop, &bl));
285 cls_version_check(*rop, cond_ver, VER_COND_TAG_EQ);
286 ASSERT_EQ(0, ioctx.operate(oid, rop, &bl));
292 cls_version_inc(*op);
293 ASSERT_EQ(0, ioctx.operate(oid, op));
295 ASSERT_EQ(0, cls_version_read(ioctx, oid, &ver2));
296 ASSERT_GT((long long)ver2.ver, (long long)ver.ver);
297 ASSERT_EQ(0, (int)ver2.tag.compare(ver.tag));
301 /* a bunch of conditions that should fail */
304 cls_version_check(*rop, ver, VER_COND_LT);
305 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, rop, &bl));
309 cls_version_check(*rop, cond_ver, VER_COND_LE);
310 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, rop, &bl));
314 cls_version_check(*rop, cond_ver, VER_COND_TAG_NE);
315 ASSERT_EQ(-ECANCELED, ioctx.operate(oid, rop, &bl));