Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / cls_replica_log / test_cls_replica_log.cc
1 /*
2  * Ceph - scalable distributed file system
3  *
4  * This is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License version 2.1, as published by the Free Software 
7  * Foundation.  See file COPYING.
8  * Copyright 2013 Inktank
9  */
10
11 #include "gtest/gtest.h"
12 #include "test/librados/test.h"
13
14 #include "cls/replica_log/cls_replica_log_client.h"
15 #include "cls/replica_log/cls_replica_log_types.h"
16
17 class cls_replica_log_Test : public ::testing::Test {
18 public:
19   librados::Rados rados;
20   librados::IoCtx ioctx;
21   string pool_name;
22   string oid;
23   string entity;
24   string marker;
25   utime_t time;
26   list<pair<string, utime_t> > entries;
27   cls_replica_log_progress_marker progress;
28
29   void SetUp() override {
30     pool_name = get_temp_pool_name();
31     ASSERT_EQ("", create_one_pool_pp(pool_name, rados));
32     ASSERT_EQ(0, rados.ioctx_create(pool_name.c_str(), ioctx));
33     oid = "obj";
34     ASSERT_EQ(0, ioctx.create(oid, true));
35   }
36
37   void add_marker() {
38     entity = "tester_entity";
39     marker = "tester_marker1";
40     time.set_from_double(10);
41     entries.push_back(make_pair("tester_obj1", time));
42     time.set_from_double(20);
43     cls_replica_log_prepare_marker(progress, entity, marker, time, &entries);
44     librados::ObjectWriteOperation opw;
45     cls_replica_log_update_bound(opw, progress);
46     ASSERT_EQ(0, ioctx.operate(oid, &opw));
47   }
48 };
49
50 TEST_F(cls_replica_log_Test, test_set_get_marker)
51 {
52   add_marker();
53
54   string reply_position_marker;
55   utime_t reply_time;
56   list<cls_replica_log_progress_marker> return_progress_list;
57   ASSERT_EQ(0, cls_replica_log_get_bounds(ioctx, oid, reply_position_marker,
58                                           reply_time, return_progress_list));
59
60   ASSERT_EQ(reply_position_marker, marker);
61   ASSERT_EQ((double)10, (double)reply_time);
62   string response_entity;
63   string response_marker;
64   utime_t response_time;
65   list<pair<string, utime_t> > response_item_list;
66
67   cls_replica_log_extract_marker(return_progress_list.front(),
68                                  response_entity, response_marker,
69                                  response_time, response_item_list);
70   ASSERT_EQ(response_entity, entity);
71   ASSERT_EQ(response_marker, marker);
72   ASSERT_EQ(response_time, time);
73   ASSERT_EQ((unsigned)1, response_item_list.size());
74   ASSERT_EQ("tester_obj1", response_item_list.front().first);
75 }
76
77 TEST_F(cls_replica_log_Test, test_bad_update)
78 {
79   add_marker();
80
81   time.set_from_double(15);
82   cls_replica_log_progress_marker bad_marker;
83   cls_replica_log_prepare_marker(bad_marker, entity, marker, time, &entries);
84   librados::ObjectWriteOperation badw;
85   cls_replica_log_update_bound(badw, bad_marker);
86   ASSERT_EQ(-EINVAL, ioctx.operate(oid, &badw));
87 }
88
89 TEST_F(cls_replica_log_Test, test_bad_delete)
90 {
91   add_marker();
92
93   librados::ObjectWriteOperation badd;
94   cls_replica_log_delete_bound(badd, entity);
95   ASSERT_EQ(-ENOTEMPTY, ioctx.operate(oid, &badd));
96 }
97
98 TEST_F(cls_replica_log_Test, test_good_delete)
99 {
100   add_marker();
101
102   librados::ObjectWriteOperation opc;
103   progress.items.clear();
104   cls_replica_log_update_bound(opc, progress);
105   ASSERT_EQ(0, ioctx.operate(oid, &opc));
106   librados::ObjectWriteOperation opd;
107   cls_replica_log_delete_bound(opd, entity);
108   ASSERT_EQ(0, ioctx.operate(oid, &opd));
109
110   string reply_position_marker;
111   utime_t reply_time;
112   list<cls_replica_log_progress_marker> return_progress_list;
113   ASSERT_EQ(0, cls_replica_log_get_bounds(ioctx, oid, reply_position_marker,
114                                           reply_time, return_progress_list));
115   ASSERT_EQ((unsigned)0, return_progress_list.size());
116 }
117
118 TEST_F(cls_replica_log_Test, test_bad_get)
119 {
120   string reply_position_marker;
121   utime_t reply_time;
122   list<cls_replica_log_progress_marker> return_progress_list;
123   ASSERT_EQ(-ENOENT,
124             cls_replica_log_get_bounds(ioctx, oid, reply_position_marker,
125                                        reply_time, return_progress_list));
126 }
127
128 TEST_F(cls_replica_log_Test, test_double_delete)
129 {
130   add_marker();
131
132   librados::ObjectWriteOperation opc;
133   progress.items.clear();
134   cls_replica_log_update_bound(opc, progress);
135   ASSERT_EQ(0, ioctx.operate(oid, &opc));
136   librados::ObjectWriteOperation opd;
137   cls_replica_log_delete_bound(opd, entity);
138   ASSERT_EQ(0, ioctx.operate(oid, &opd));
139
140   librados::ObjectWriteOperation opd2;
141   cls_replica_log_delete_bound(opd2, entity);
142   ASSERT_EQ(0, ioctx.operate(oid, &opd2));
143
144   string reply_position_marker;
145   utime_t reply_time;
146   list<cls_replica_log_progress_marker> return_progress_list;
147   ASSERT_EQ(0, cls_replica_log_get_bounds(ioctx, oid, reply_position_marker,
148                                           reply_time, return_progress_list));
149   ASSERT_EQ((unsigned)0, return_progress_list.size());
150
151 }