]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/driver/dbstore/tests/dbstore_mgr_tests.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rgw / driver / dbstore / tests / dbstore_mgr_tests.cc
CommitLineData
1e59de90
TL
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include "common/ceph_context.h"
5#include "rgw/driver/dbstore/dbstore_mgr.h"
6
7#include <filesystem>
8#include <gtest/gtest.h>
9#include <memory>
10
11using namespace rgw;
12namespace fs = std::filesystem;
13const static std::string TEST_DIR = "rgw_dbstore_tests";
14
15bool endsWith(const std::string &mainStr, const std::string &toMatch)
16{
17 if(mainStr.size() >= toMatch.size() &&
18 mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)
19 return true;
20 else
21 return false;
22}
23
24class TestDBStoreManager : public ::testing::Test {
25protected:
26 void SetUp() override {
27 ctx_ = std::make_shared<CephContext>(CEPH_ENTITY_TYPE_CLIENT);
28 g_ceph_context = ctx_.get();
29 fs::current_path(fs::temp_directory_path());
30 fs::create_directory(TEST_DIR);
31 }
32
33 void TearDown() override {
34 fs::current_path(fs::temp_directory_path());
35 fs::remove_all(TEST_DIR);
36 }
37
38 std::string getTestDir() const {
39 auto test_dir = fs::temp_directory_path() / TEST_DIR;
40 return test_dir.string();
41 }
42
43 fs::path getDBFullPath(const std::string & base_dir,
44 const std::string & tenant) const {
45 auto db_path = ctx_->_conf.get_val<std::string>("dbstore_db_dir");
46 const auto& db_name = ctx_->_conf.get_val<std::string>("dbstore_db_name_prefix") + "-" + tenant + ".db";
47
48 auto db_full_path = std::filesystem::path(db_path) / db_name;
49 auto db_full_path_test = fs::path(base_dir) / db_full_path;
50 return db_full_path_test;
51 }
52
53 std::string getDBTenant(const std::string & base_dir,
54 const std::string & tenant) const {
55 auto db_name = ctx_->_conf.get_val<std::string>("dbstore_db_name_prefix");
56 db_name += "-" + tenant;
57 auto db_full_path = fs::path(base_dir) / db_name;
58 return db_full_path.string();
59 }
60
61 std::string getDBTenant(const std::string & tenant = default_tenant) const {
62 return getDBTenant(getTestDir(), tenant);
63 }
64
65 fs::path getDBFullPath(const std::string & tenant) const {
66 return getDBFullPath(getTestDir(), tenant);
67 }
68
69 fs::path getLogFilePath(const std::string & log_file) {
70 return fs::temp_directory_path() / log_file;
71 }
72
73 std::shared_ptr<CephContext> getContext() const {
74 return ctx_;
75 }
76
77 private:
78 std::shared_ptr<CephContext> ctx_;
79};
80
81TEST_F(TestDBStoreManager, BasicInstantiateUsingDBDir) {
82 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
83
84 EXPECT_FALSE(fs::exists(getDBFullPath(default_tenant)));
85 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
86 EXPECT_TRUE(fs::exists(getDBFullPath(default_tenant)));
87}
88
89TEST_F(TestDBStoreManager, DBNamePrefix) {
90 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
91 std::string prefix = "testprefix";
92 getContext()->_conf.set_val("dbstore_db_name_prefix", prefix);
93
94 EXPECT_FALSE(fs::exists(getDBFullPath(default_tenant)));
95 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
96 EXPECT_TRUE(fs::exists(getDBFullPath(default_tenant)));
97
98 // check that the database name contains the given prefix
99 std::string expected_db_name = prefix + "-" + default_tenant + ".db";
100 EXPECT_TRUE(endsWith(getDBFullPath(default_tenant), expected_db_name));
101}
102
103TEST_F(TestDBStoreManager, BasicInstantiateSecondConstructor) {
104 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
105
106 EXPECT_FALSE(fs::exists(getDBFullPath(default_tenant)));
107 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get(), getLogFilePath("test.log").string(), 10);
108 EXPECT_TRUE(fs::exists(getDBFullPath(default_tenant)));
109}
110
111TEST_F(TestDBStoreManager, TestDBName) {
112 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
113
114 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
115 auto db = dbstore_mgr->getDB(default_tenant, false);
116 ASSERT_NE(nullptr, db);
117 EXPECT_EQ(getDBTenant(), db->getDBname());
118}
119
120
121TEST_F(TestDBStoreManager, TestDBNameDefaultDB) {
122 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
123
124 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
125 // passing an empty tenant should return the default_db
126 auto db = dbstore_mgr->getDB("", false);
127 ASSERT_NE(nullptr, db);
128 EXPECT_EQ(getDBTenant(), db->getDBname());
129}
130
131TEST_F(TestDBStoreManager, TestDBBadTenant) {
132 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
133
134 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
135 auto db = dbstore_mgr->getDB("does-not-exist", false);
136 ASSERT_EQ(nullptr, db);
137}
138
139TEST_F(TestDBStoreManager, TestGetNewDB) {
140 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
141
142 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
143
144 auto new_tenant_path = "new_tenant";
145 auto db = dbstore_mgr->getDB(new_tenant_path, true);
146 ASSERT_NE(nullptr, db);
147 EXPECT_EQ(getDBTenant(new_tenant_path), db->getDBname());
148}
149
150TEST_F(TestDBStoreManager, TestDelete) {
151 getContext()->_conf.set_val("dbstore_db_dir", getTestDir());
152
153 auto dbstore_mgr = std::make_shared<DBStoreManager>(getContext().get());
154 dbstore_mgr->deleteDB(default_tenant);
155 auto db = dbstore_mgr->getDB(default_tenant, false);
156 ASSERT_EQ(nullptr, db);
157}