]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
11fdf7f2 | 7 | #include <linux/kdev_t.h> |
7c673cae FG |
8 | |
9 | #include "include/types.h" | |
10 | #include "common/blkdev.h" | |
11 | ||
12 | #include "gtest/gtest.h" | |
11fdf7f2 | 13 | #include "gmock/gmock.h" |
7c673cae FG |
14 | #include <iostream> |
15 | ||
11fdf7f2 TL |
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))); | |
7c673cae | 58 | } |
7c673cae | 59 | |
11fdf7f2 TL |
60 | virtual void TearDown() { |
61 | delete root; | |
62 | } | |
63 | ||
64 | MockBlkDev sda, sdb; | |
65 | }; | |
7c673cae | 66 | |
11fdf7f2 TL |
67 | TEST_F(BlockDevTest, device_model) |
68 | { | |
7c673cae | 69 | char model[1000] = {0}; |
11fdf7f2 | 70 | int rc = sda.model(model, sizeof(model)); |
7c673cae | 71 | ASSERT_EQ(0, rc); |
11fdf7f2 | 72 | ASSERT_STREQ(model, "myfancymodel"); |
7c673cae FG |
73 | } |
74 | ||
11fdf7f2 | 75 | TEST_F(BlockDevTest, discard) |
7c673cae | 76 | { |
11fdf7f2 TL |
77 | EXPECT_TRUE(sda.support_discard()); |
78 | EXPECT_TRUE(sdb.support_discard()); | |
7c673cae FG |
79 | } |
80 | ||
11fdf7f2 TL |
81 | TEST_F(BlockDevTest, is_rotational) |
82 | { | |
83 | EXPECT_FALSE(sda.is_rotational()); | |
84 | EXPECT_TRUE(sdb.is_rotational()); | |
7c673cae FG |
85 | } |
86 | ||
11fdf7f2 TL |
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 | }; | |
7c673cae | 95 | |
11fdf7f2 TL |
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 | } | |
20effc67 TL |
102 | |
103 | TEST(blkdev, get_device_id) | |
104 | { | |
105 | // this doesn't really test anything; it's just a way to exercise the | |
106 | // get_device_id() code. | |
107 | for (char c = 'a'; c < 'z'; ++c) { | |
108 | char devname[4] = {'s', 'd', c, 0}; | |
109 | std::string err; | |
110 | auto i = get_device_id(devname, &err); | |
111 | cout << "devname " << devname << " -> '" << i | |
112 | << "' (" << err << ")" << std::endl; | |
113 | } | |
114 | } |