]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/objectstore/store_test_fixture.cc
Import ceph 15.2.8
[ceph.git] / ceph / src / test / objectstore / store_test_fixture.cc
1 #include <stdlib.h>
2 #include <string>
3 #include <iostream>
4 #include <assert.h>
5 #include <gtest/gtest.h>
6
7 #include "common/errno.h"
8 #include "common/config.h"
9 #include "os/ObjectStore.h"
10
11 #if defined(WITH_BLUESTORE)
12 #include "os/bluestore/BlueStore.h"
13 #endif
14 #include "store_test_fixture.h"
15
16 static void rm_r(const string& path)
17 {
18 string cmd = string("rm -r ") + path;
19 cout << "==> " << cmd << std::endl;
20 int r = ::system(cmd.c_str());
21 if (r) {
22 if (r == -1) {
23 r = errno;
24 cerr << "system() failed to fork() " << cpp_strerror(r)
25 << ", continuing anyway" << std::endl;
26 } else {
27 cerr << "failed with exit code " << r
28 << ", continuing anyway" << std::endl;
29 }
30 }
31 }
32
33 void StoreTestFixture::SetUp()
34 {
35
36 int r = ::mkdir(data_dir.c_str(), 0777);
37 if (r < 0) {
38 r = -errno;
39 cerr << __func__ << ": unable to create " << data_dir << ": " << cpp_strerror(r) << std::endl;
40 }
41 ASSERT_EQ(0, r);
42
43 store.reset(ObjectStore::create(g_ceph_context,
44 type,
45 data_dir,
46 string("store_test_temp_journal")));
47 if (!store) {
48 cerr << __func__ << ": objectstore type " << type << " doesn't exist yet!" << std::endl;
49 }
50 ASSERT_TRUE(store);
51 #if defined(WITH_BLUESTORE)
52 if (type == "bluestore") {
53 BlueStore *s = static_cast<BlueStore*>(store.get());
54 // better test coverage!
55 s->set_cache_shards(5);
56 }
57 #endif
58 ASSERT_EQ(0, store->mkfs());
59 ASSERT_EQ(0, store->mount());
60
61 // we keep this stuff 'unsafe' out of test case scope to be able to update ANY
62 // config settings. Hence setting it to 'safe' here to proceed with the test
63 // case
64 g_conf().set_safe_to_start_threads();
65 }
66
67 void StoreTestFixture::TearDown()
68 {
69 // we keep this stuff 'unsafe' out of test case scope to be able to update ANY
70 // config settings. Hence setting it to 'unsafe' here as test case is closing.
71 g_conf()._clear_safe_to_start_threads();
72 PopSettings(0);
73 if (!orig_death_test_style.empty()) {
74 ::testing::FLAGS_gtest_death_test_style = orig_death_test_style;
75 orig_death_test_style.clear();
76 }
77 if (store) {
78 int r = store->umount();
79 EXPECT_EQ(0, r);
80 rm_r(data_dir);
81 }
82 }
83
84 void StoreTestFixture::SetVal(ConfigProxy& _conf, const char* key, const char* val)
85 {
86 ceph_assert(!conf || conf == &_conf);
87 conf = &_conf;
88 std::string skey(key);
89 std::string prev_val;
90 conf->get_val(skey, &prev_val);
91 conf->set_val_or_die(key, val);
92 saved_settings.emplace(skey, prev_val);
93 }
94
95 void StoreTestFixture::PopSettings(size_t pos)
96 {
97 if (conf) {
98 ceph_assert(pos == 0 || pos <= saved_settings.size()); // for sanity
99 while(pos < saved_settings.size())
100 {
101 auto& e = saved_settings.top();
102 conf->set_val_or_die(e.first, e.second);
103 saved_settings.pop();
104 }
105 conf->apply_changes(NULL);
106 }
107 }