Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librados_test_stub / TestMemRadosClient.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "test/librados_test_stub/TestMemRadosClient.h"
5 #include "test/librados_test_stub/TestMemCluster.h"
6 #include "test/librados_test_stub/TestMemIoCtxImpl.h"
7 #include <errno.h>
8 #include <sstream>
9
10 namespace librados {
11
12 TestMemRadosClient::TestMemRadosClient(CephContext *cct,
13                                        TestMemCluster *test_mem_cluster)
14   : TestRadosClient(cct, test_mem_cluster->get_watch_notify()),
15     m_mem_cluster(test_mem_cluster) {
16   m_mem_cluster->allocate_client(&m_nonce, &m_global_id);
17 }
18
19 TestMemRadosClient::~TestMemRadosClient() {
20   m_mem_cluster->deallocate_client(m_nonce);
21 }
22
23 TestIoCtxImpl *TestMemRadosClient::create_ioctx(int64_t pool_id,
24                                                 const std::string &pool_name) {
25   return new TestMemIoCtxImpl(this, pool_id, pool_name,
26                               m_mem_cluster->get_pool(pool_name));
27 }
28
29 void TestMemRadosClient::object_list(int64_t pool_id,
30                                      std::list<librados::TestRadosClient::Object> *list) {
31   list->clear();
32
33   auto pool = m_mem_cluster->get_pool(pool_id);
34   if (pool != nullptr) {
35     RWLock::RLocker file_locker(pool->file_lock);
36     for (auto &file_pair : pool->files) {
37       Object obj;
38       obj.oid = file_pair.first;
39       list->push_back(obj);
40     }
41   }
42 }
43
44 int TestMemRadosClient::pool_create(const std::string &pool_name) {
45   if (is_blacklisted()) {
46     return -EBLACKLISTED;
47   }
48   return m_mem_cluster->pool_create(pool_name);
49 }
50
51 int TestMemRadosClient::pool_delete(const std::string &pool_name) {
52   if (is_blacklisted()) {
53     return -EBLACKLISTED;
54   }
55   return m_mem_cluster->pool_delete(pool_name);
56 }
57
58 int TestMemRadosClient::pool_get_base_tier(int64_t pool_id, int64_t* base_tier) {
59   // TODO
60   *base_tier = pool_id;
61   return 0;
62 }
63
64 int TestMemRadosClient::pool_list(std::list<std::pair<int64_t, std::string> >& v) {
65   return m_mem_cluster->pool_list(v);
66 }
67
68 int64_t TestMemRadosClient::pool_lookup(const std::string &pool_name) {
69   return m_mem_cluster->pool_lookup(pool_name);
70 }
71
72 int TestMemRadosClient::pool_reverse_lookup(int64_t id, std::string *name) {
73   return m_mem_cluster->pool_reverse_lookup(id, name);
74 }
75
76 int TestMemRadosClient::watch_flush() {
77   get_watch_notify()->flush(this);
78   return 0;
79 }
80
81 bool TestMemRadosClient::is_blacklisted() const {
82   return m_mem_cluster->is_blacklisted(m_nonce);
83 }
84
85 int TestMemRadosClient::blacklist_add(const std::string& client_address,
86                                       uint32_t expire_seconds) {
87   if (is_blacklisted()) {
88     return -EBLACKLISTED;
89   }
90
91   // extract the nonce to use as a unique key to the client
92   auto idx = client_address.find("/");
93   if (idx == std::string::npos || idx + 1 >= client_address.size()) {
94     return -EINVAL;
95   }
96
97   std::stringstream nonce_ss(client_address.substr(idx + 1));
98   uint32_t nonce;
99   nonce_ss >> nonce;
100   if (!nonce_ss) {
101     return -EINVAL;
102   }
103
104   m_mem_cluster->blacklist(nonce);
105   return 0;
106 }
107
108 void TestMemRadosClient::transaction_start(const std::string &oid) {
109   m_mem_cluster->transaction_start(oid);
110 }
111
112 void TestMemRadosClient::transaction_finish(const std::string &oid) {
113   m_mem_cluster->transaction_finish(oid);
114 }
115
116 } // namespace librados