]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/common/test_blkdev.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / test / common / test_blkdev.cc
CommitLineData
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
16using namespace std;
17using namespace testing;
18
19class 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
30class BlockDevTest : public ::testing::Test {
31public:
32 string *root;
33
34protected:
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
67TEST_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 75TEST_F(BlockDevTest, discard)
7c673cae 76{
11fdf7f2
TL
77 EXPECT_TRUE(sda.support_discard());
78 EXPECT_TRUE(sdb.support_discard());
7c673cae
FG
79}
80
11fdf7f2 81TEST_F(BlockDevTest, is_nvme)
7c673cae 82{
11fdf7f2
TL
83 // It would be nice to have a positive NVME test too, but I don't have any
84 // examples for the canned data.
85 EXPECT_FALSE(sda.is_nvme());
86 EXPECT_FALSE(sdb.is_nvme());
87}
7c673cae 88
11fdf7f2
TL
89TEST_F(BlockDevTest, is_rotational)
90{
91 EXPECT_FALSE(sda.is_rotational());
92 EXPECT_TRUE(sdb.is_rotational());
7c673cae
FG
93}
94
11fdf7f2
TL
95TEST(blkdev, _decode_model_enc)
96{
97
98 const char *foo[][2] = {
99 { "WDC\\x20WDS200T2B0A-00SM50\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20",
100 "WDC_WDS200T2B0A-00SM50" },
101 { 0, 0},
102 };
7c673cae 103
11fdf7f2
TL
104 for (unsigned i = 0; foo[i][0]; ++i) {
105 std::string d = _decode_model_enc(foo[i][0]);
106 cout << " '" << foo[i][0] << "' -> '" << d << "'" << std::endl;
107 ASSERT_EQ(std::string(foo[i][1]), d);
108 }
109}