]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/objectstore/store_test_fixture.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / objectstore / store_test_fixture.cc
CommitLineData
7c673cae
FG
1#include <stdlib.h>
2#include <string>
3#include <iostream>
11fdf7f2 4#include <assert.h>
7c673cae
FG
5#include <gtest/gtest.h>
6
7#include "common/errno.h"
11fdf7f2 8#include "common/config.h"
7c673cae 9#include "os/ObjectStore.h"
11fdf7f2
TL
10
11#if defined(WITH_BLUESTORE)
7c673cae
FG
12#include "os/bluestore/BlueStore.h"
13#endif
14#include "store_test_fixture.h"
15
11fdf7f2
TL
16static void rm_r(const string& path)
17{
7c673cae
FG
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
11fdf7f2
TL
33void StoreTestFixture::SetUp()
34{
35
7c673cae
FG
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);
11fdf7f2 51#if defined(WITH_BLUESTORE)
7c673cae
FG
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());
11fdf7f2
TL
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();
7c673cae
FG
65}
66
11fdf7f2
TL
67void 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);
7c673cae
FG
73 if (store) {
74 int r = store->umount();
75 EXPECT_EQ(0, r);
76 rm_r(data_dir);
77 }
78}
11fdf7f2
TL
79
80void StoreTestFixture::SetVal(ConfigProxy& _conf, const char* key, const char* val)
81{
82 ceph_assert(!conf || conf == &_conf);
83 conf = &_conf;
84 std::string skey(key);
85 std::string prev_val;
86 conf->get_val(skey, &prev_val);
87 conf->set_val_or_die(key, val);
88 saved_settings.emplace(skey, prev_val);
89}
90
91void StoreTestFixture::PopSettings(size_t pos)
92{
93 if (conf) {
94 ceph_assert(pos == 0 || pos <= saved_settings.size()); // for sanity
95 while(pos < saved_settings.size())
96 {
97 auto& e = saved_settings.top();
98 conf->set_val_or_die(e.first, e.second);
99 saved_settings.pop();
100 }
101 conf->apply_changes(NULL);
102 }
103}