summaryrefslogtreecommitdiffstats
path: root/src/test/objectstore/store_test_fixture.cc
blob: e343f96156da7d272d995a5a31aead064eb228ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdlib.h>
#include <string>
#include <iostream>
#include <assert.h>
#include <gtest/gtest.h>

#include "common/errno.h"
#include "common/config.h"
#include "os/ObjectStore.h"

#if defined(WITH_BLUESTORE)
#include "os/bluestore/BlueStore.h"
#endif
#include "store_test_fixture.h"

static void rm_r(const string& path)
{
  string cmd = string("rm -r ") + path;
  cout << "==> " << cmd << std::endl;
  int r = ::system(cmd.c_str());
  if (r) {
    if (r == -1) {
      r = errno;
      cerr << "system() failed to fork() " << cpp_strerror(r)
           << ", continuing anyway" << std::endl;
    } else {
      cerr << "failed with exit code " << r
           << ", continuing anyway" << std::endl;
    }
  }
}

void StoreTestFixture::SetUp()
{

  int r = ::mkdir(data_dir.c_str(), 0777);
  if (r < 0) {
    r = -errno;
    cerr << __func__ << ": unable to create " << data_dir << ": " << cpp_strerror(r) << std::endl;
  }
  ASSERT_EQ(0, r);

  store.reset(ObjectStore::create(g_ceph_context,
                                  type,
                                  data_dir,
                                  string("store_test_temp_journal")));
  if (!store) {
    cerr << __func__ << ": objectstore type " << type << " doesn't exist yet!" << std::endl;
  }
  ASSERT_TRUE(store);
#if defined(WITH_BLUESTORE)
  if (type == "bluestore") {
    BlueStore *s = static_cast<BlueStore*>(store.get());
    // better test coverage!
    s->set_cache_shards(5);
  }
#endif
  ASSERT_EQ(0, store->mkfs());
  ASSERT_EQ(0, store->mount());

  // we keep this stuff 'unsafe' out of test case scope to be able to update ANY
  // config settings. Hence setting it to 'safe' here to proceed with the test
  // case
  g_conf().set_safe_to_start_threads();
}

void StoreTestFixture::TearDown()
{
  // we keep this stuff 'unsafe' out of test case scope to be able to update ANY
  // config settings. Hence setting it to 'unsafe' here as test case is closing.
  g_conf()._clear_safe_to_start_threads();
  PopSettings(0);
  if (!orig_death_test_style.empty()) {
    ::testing::FLAGS_gtest_death_test_style = orig_death_test_style;
    orig_death_test_style.clear();
  }
  if (store) {
    int r = store->umount();
    EXPECT_EQ(0, r);
    rm_r(data_dir);
  }
}

void StoreTestFixture::SetVal(ConfigProxy& _conf, const char* key, const char* val)
{
  ceph_assert(!conf || conf == &_conf);
  conf = &_conf;
  std::string skey(key);
  std::string prev_val;
  conf->get_val(skey, &prev_val);
  conf->set_val_or_die(key, val);
  saved_settings.emplace(skey, prev_val);
}

void StoreTestFixture::PopSettings(size_t pos)
{
  if (conf) {
    ceph_assert(pos == 0 || pos <= saved_settings.size()); // for sanity
    while(pos < saved_settings.size())
    {
      auto& e = saved_settings.top();
      conf->set_val_or_die(e.first, e.second);
      saved_settings.pop();
    }
    conf->apply_changes(NULL);
  }
}

void StoreTestFixture::CloseAndReopen() {
  ceph_assert(store != nullptr);
  g_conf()._clear_safe_to_start_threads();
  int r = store->umount();
  EXPECT_EQ(0, r);
  ch.reset(nullptr);
  store.reset(nullptr);
  store.reset(ObjectStore::create(g_ceph_context,
                                  type,
                                  data_dir,
                                  string("store_test_temp_journal")));
  if (!store) {
    cerr << __func__ << ": objectstore type " << type << " failed to reopen!" << std::endl;
  }
  ASSERT_TRUE(store);
#if defined(WITH_BLUESTORE)
  if (type == "bluestore") {
    BlueStore *s = static_cast<BlueStore*>(store.get());
    // better test coverage!
    s->set_cache_shards(5);
  }
#endif
  ASSERT_EQ(0, store->mount());
  g_conf().set_safe_to_start_threads();
}