Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / cls / lock / cls_lock_ops.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) 2004-2006 Sage Weil <sage@newdream.net>
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
15 #include "msg/msg_types.h"
16 #include "common/Formatter.h"
17
18 #include "cls/lock/cls_lock_ops.h"
19
20 using namespace rados::cls::lock;
21
22 static void generate_lock_id(locker_id_t& i, int n, const string& cookie)
23 {
24   i.locker = entity_name_t(entity_name_t::CLIENT(n));
25   i.cookie = cookie;
26 }
27
28 void cls_lock_lock_op::dump(Formatter *f) const
29 {
30   f->dump_string("name", name);
31   f->dump_string("type", cls_lock_type_str(type));
32   f->dump_string("cookie", cookie);
33   f->dump_string("tag", tag);
34   f->dump_string("description", description);
35   f->dump_stream("duration") << duration;
36   f->dump_int("flags", (int)flags);
37 }
38
39 void cls_lock_lock_op::generate_test_instances(list<cls_lock_lock_op*>& o)
40 {
41   cls_lock_lock_op *i = new cls_lock_lock_op;
42   i->name = "name";
43   i->type = LOCK_SHARED;
44   i->cookie = "cookie";
45   i->tag = "tag";
46   i->description = "description";
47   i->duration = utime_t(5, 0);
48   i->flags = LOCK_FLAG_RENEW;
49   o.push_back(i);
50   o.push_back(new cls_lock_lock_op);
51 }
52
53 void cls_lock_unlock_op::dump(Formatter *f) const
54 {
55   f->dump_string("name", name);
56   f->dump_string("cookie", cookie);
57 }
58
59 void cls_lock_unlock_op::generate_test_instances(list<cls_lock_unlock_op*>& o)
60 {
61   cls_lock_unlock_op *i = new cls_lock_unlock_op;
62   i->name = "name";
63   i->cookie = "cookie";
64   o.push_back(i);
65   o.push_back(new cls_lock_unlock_op);
66 }
67
68 void cls_lock_break_op::dump(Formatter *f) const
69 {
70   f->dump_string("name", name);
71   f->dump_string("cookie", cookie);
72   f->dump_stream("locker") << locker;
73 }
74
75 void cls_lock_break_op::generate_test_instances(list<cls_lock_break_op*>& o)
76 {
77   cls_lock_break_op *i = new cls_lock_break_op;
78   i->name = "name";
79   i->cookie = "cookie";
80   i->locker =  entity_name_t(entity_name_t::CLIENT(1));
81   o.push_back(i);
82   o.push_back(new cls_lock_break_op);
83 }
84
85 void cls_lock_get_info_op::dump(Formatter *f) const
86 {
87   f->dump_string("name", name);
88 }
89
90 void cls_lock_get_info_op::generate_test_instances(list<cls_lock_get_info_op*>& o)
91 {
92   cls_lock_get_info_op *i = new cls_lock_get_info_op;
93   i->name = "name";
94   o.push_back(i);
95   o.push_back(new cls_lock_get_info_op);
96 }
97
98 static void generate_test_addr(entity_addr_t& a, int nonce, int port)
99 {
100   a.set_type(entity_addr_t::TYPE_LEGACY);
101   a.set_nonce(nonce);
102   a.set_family(AF_INET);
103   a.set_in4_quad(0, 127);
104   a.set_in4_quad(1, 0);
105   a.set_in4_quad(2, 1);
106   a.set_in4_quad(3, 2);
107   a.set_port(port);
108 }
109
110 void cls_lock_get_info_reply::dump(Formatter *f) const
111 {
112   f->dump_string("lock_type", cls_lock_type_str(lock_type));
113   f->dump_string("tag", tag);
114   f->open_array_section("lockers");
115   map<locker_id_t, locker_info_t>::const_iterator iter;
116   for (iter = lockers.begin(); iter != lockers.end(); ++iter) {
117     const locker_id_t& id = iter->first;
118     const locker_info_t& info = iter->second;
119     f->open_object_section("object");
120     f->dump_stream("locker") << id.locker;
121     f->dump_string("description", info.description);
122     f->dump_string("cookie", id.cookie);
123     f->dump_stream("expiration") << info.expiration;
124     f->dump_stream("addr") << info.addr;
125     f->close_section();
126   }
127   f->close_section();
128 }
129
130 void cls_lock_get_info_reply::generate_test_instances(list<cls_lock_get_info_reply*>& o)
131 {
132   cls_lock_get_info_reply *i = new cls_lock_get_info_reply;
133   i->lock_type = LOCK_SHARED;
134   i->tag = "tag";
135   locker_id_t id1, id2;
136   entity_addr_t addr1, addr2;
137   generate_lock_id(id1, 1, "cookie1");
138   generate_test_addr(addr1, 10, 20);
139   i->lockers[id1] = locker_info_t(utime_t(10, 0), addr1, "description1");
140   generate_lock_id(id2, 2, "cookie2");
141   generate_test_addr(addr2, 30, 40);
142   i->lockers[id2] = locker_info_t(utime_t(20, 0), addr2, "description2");
143
144   o.push_back(i);
145   o.push_back(new cls_lock_get_info_reply);
146 }
147
148 void cls_lock_list_locks_reply::dump(Formatter *f) const
149 {
150   list<string>::const_iterator iter;
151   f->open_array_section("locks");
152   for (iter = locks.begin(); iter != locks.end(); ++iter) {
153     f->open_array_section("object");
154     f->dump_string("lock", *iter);
155     f->close_section();
156   }
157   f->close_section();
158 }
159
160 void cls_lock_list_locks_reply::generate_test_instances(list<cls_lock_list_locks_reply*>& o)
161 {
162   cls_lock_list_locks_reply *i = new cls_lock_list_locks_reply;
163   i->locks.push_back("lock1");
164   i->locks.push_back("lock2");
165   i->locks.push_back("lock3");
166
167   o.push_back(i);
168   o.push_back(new cls_lock_list_locks_reply);
169 }
170
171 void cls_lock_assert_op::dump(Formatter *f) const
172 {
173   f->dump_string("name", name);
174   f->dump_string("type", cls_lock_type_str(type));
175   f->dump_string("cookie", cookie);
176   f->dump_string("tag", tag);
177 }
178
179 void cls_lock_assert_op::generate_test_instances(list<cls_lock_assert_op*>& o)
180 {
181   cls_lock_assert_op *i = new cls_lock_assert_op;
182   i->name = "name";
183   i->type = LOCK_SHARED;
184   i->cookie = "cookie";
185   i->tag = "tag";
186   o.push_back(i);
187   o.push_back(new cls_lock_assert_op);
188 }
189
190 void cls_lock_set_cookie_op::dump(Formatter *f) const
191 {
192   f->dump_string("name", name);
193   f->dump_string("type", cls_lock_type_str(type));
194   f->dump_string("cookie", cookie);
195   f->dump_string("tag", tag);
196   f->dump_string("new_cookie", new_cookie);
197 }
198
199 void cls_lock_set_cookie_op::generate_test_instances(list<cls_lock_set_cookie_op*>& o)
200 {
201   cls_lock_set_cookie_op *i = new cls_lock_set_cookie_op;
202   i->name = "name";
203   i->type = LOCK_SHARED;
204   i->cookie = "cookie";
205   i->tag = "tag";
206   i->new_cookie = "new cookie";
207   o.push_back(i);
208   o.push_back(new cls_lock_set_cookie_op);
209 }
210