Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / objectstore / store_test_fixture.cc
1 #include <stdlib.h>
2 #include <string>
3 #include <iostream>
4 #include <gtest/gtest.h>
5
6 #include "common/errno.h"
7 #include "os/ObjectStore.h"
8 #if defined(HAVE_LIBAIO)
9 #include "os/bluestore/BlueStore.h"
10 #endif
11 #include "store_test_fixture.h"
12
13 static void rm_r(const string& path) {
14   string cmd = string("rm -r ") + path;
15   cout << "==> " << cmd << std::endl;
16   int r = ::system(cmd.c_str());
17   if (r) {
18     if (r == -1) {
19       r = errno;
20       cerr << "system() failed to fork() " << cpp_strerror(r)
21            << ", continuing anyway" << std::endl;
22     } else {
23       cerr << "failed with exit code " << r
24            << ", continuing anyway" << std::endl;
25     }
26   }
27 }
28
29 void StoreTestFixture::SetUp() {
30   int r = ::mkdir(data_dir.c_str(), 0777);
31   if (r < 0) {
32     r = -errno;
33     cerr << __func__ << ": unable to create " << data_dir << ": " << cpp_strerror(r) << std::endl;
34   }
35   ASSERT_EQ(0, r);
36
37   store.reset(ObjectStore::create(g_ceph_context,
38                                   type,
39                                   data_dir,
40                                   string("store_test_temp_journal")));
41   if (!store) {
42     cerr << __func__ << ": objectstore type " << type << " doesn't exist yet!" << std::endl;
43   }
44   ASSERT_TRUE(store);
45 #if defined(HAVE_LIBAIO)
46   if (type == "bluestore") {
47     BlueStore *s = static_cast<BlueStore*>(store.get());
48     // better test coverage!
49     s->set_cache_shards(5);
50   }
51 #endif
52   ASSERT_EQ(0, store->mkfs());
53   ASSERT_EQ(0, store->mount());
54 }
55
56 void StoreTestFixture::TearDown() {
57   if (store) {
58     int r = store->umount();
59     EXPECT_EQ(0, r);
60     rm_r(data_dir);
61   }
62 }