]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_blkdev.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / test / common / test_blkdev.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <string.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <linux/kdev_t.h>
8
9 #include "include/types.h"
10 #include "common/blkdev.h"
11
12 #include "gtest/gtest.h"
13 #include "gmock/gmock.h"
14 #include <iostream>
15
16 using namespace std;
17 using namespace testing;
18
19 class MockBlkDev : public BlkDev {
20 public:
21 // pass 0 as fd, so it won't try to use the empty devname
22 MockBlkDev() : BlkDev(0) {};
23 virtual ~MockBlkDev() {}
24
25 MOCK_CONST_METHOD0(sysfsdir, const char*());
26 MOCK_CONST_METHOD2(wholedisk, int(char* device, size_t max));
27 };
28
29
30 class BlockDevTest : public ::testing::Test {
31 public:
32 string *root;
33
34 protected:
35 virtual void SetUp() {
36 const char *sda_name = "sda";
37 const char *sdb_name = "sdb";
38 const char* env = getenv("CEPH_ROOT");
39 ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!";
40 root = new string(env);
41 *root += "/src/test/common/test_blkdev_sys_block/sys";
42
43 EXPECT_CALL(sda, sysfsdir())
44 .WillRepeatedly(Return(root->c_str()));
45 EXPECT_CALL(sda, wholedisk(NotNull(), Ge(0ul)))
46 .WillRepeatedly(
47 DoAll(
48 SetArrayArgument<0>(sda_name, sda_name + strlen(sda_name) + 1),
49 Return(0)));
50
51 EXPECT_CALL(sdb, sysfsdir())
52 .WillRepeatedly(Return(root->c_str()));
53 EXPECT_CALL(sdb, wholedisk(NotNull(), Ge(0ul)))
54 .WillRepeatedly(
55 DoAll(
56 SetArrayArgument<0>(sdb_name, sdb_name + strlen(sdb_name) + 1),
57 Return(0)));
58 }
59
60 virtual void TearDown() {
61 delete root;
62 }
63
64 MockBlkDev sda, sdb;
65 };
66
67 TEST_F(BlockDevTest, device_model)
68 {
69 char model[1000] = {0};
70 int rc = sda.model(model, sizeof(model));
71 ASSERT_EQ(0, rc);
72 ASSERT_STREQ(model, "myfancymodel");
73 }
74
75 TEST_F(BlockDevTest, discard)
76 {
77 EXPECT_TRUE(sda.support_discard());
78 EXPECT_TRUE(sdb.support_discard());
79 }
80
81 TEST_F(BlockDevTest, is_rotational)
82 {
83 EXPECT_FALSE(sda.is_rotational());
84 EXPECT_TRUE(sdb.is_rotational());
85 }
86
87 TEST(blkdev, _decode_model_enc)
88 {
89
90 const char *foo[][2] = {
91 { "WDC\\x20WDS200T2B0A-00SM50\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20",
92 "WDC_WDS200T2B0A-00SM50" },
93 { 0, 0},
94 };
95
96 for (unsigned i = 0; foo[i][0]; ++i) {
97 std::string d = _decode_model_enc(foo[i][0]);
98 cout << " '" << foo[i][0] << "' -> '" << d << "'" << std::endl;
99 ASSERT_EQ(std::string(foo[i][1]), d);
100 }
101 }