From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/test/common/test_blkdev.cc | 109 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/test/common/test_blkdev.cc (limited to 'src/test/common/test_blkdev.cc') diff --git a/src/test/common/test_blkdev.cc b/src/test/common/test_blkdev.cc new file mode 100644 index 00000000..9dec5297 --- /dev/null +++ b/src/test/common/test_blkdev.cc @@ -0,0 +1,109 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include +#include +#include +#include + +#include "include/types.h" +#include "common/blkdev.h" + +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include + +using namespace std; +using namespace testing; + +class MockBlkDev : public BlkDev { + public: + // pass 0 as fd, so it won't try to use the empty devname + MockBlkDev() : BlkDev(0) {}; + virtual ~MockBlkDev() {} + + MOCK_CONST_METHOD0(sysfsdir, const char*()); + MOCK_CONST_METHOD2(wholedisk, int(char* device, size_t max)); +}; + + +class BlockDevTest : public ::testing::Test { +public: + string *root; + +protected: + virtual void SetUp() { + const char *sda_name = "sda"; + const char *sdb_name = "sdb"; + const char* env = getenv("CEPH_ROOT"); + ASSERT_NE(env, nullptr) << "Environment Variable CEPH_ROOT not found!"; + root = new string(env); + *root += "/src/test/common/test_blkdev_sys_block/sys"; + + EXPECT_CALL(sda, sysfsdir()) + .WillRepeatedly(Return(root->c_str())); + EXPECT_CALL(sda, wholedisk(NotNull(), Ge(0ul))) + .WillRepeatedly( + DoAll( + SetArrayArgument<0>(sda_name, sda_name + strlen(sda_name) + 1), + Return(0))); + + EXPECT_CALL(sdb, sysfsdir()) + .WillRepeatedly(Return(root->c_str())); + EXPECT_CALL(sdb, wholedisk(NotNull(), Ge(0ul))) + .WillRepeatedly( + DoAll( + SetArrayArgument<0>(sdb_name, sdb_name + strlen(sdb_name) + 1), + Return(0))); + } + + virtual void TearDown() { + delete root; + } + + MockBlkDev sda, sdb; +}; + +TEST_F(BlockDevTest, device_model) +{ + char model[1000] = {0}; + int rc = sda.model(model, sizeof(model)); + ASSERT_EQ(0, rc); + ASSERT_STREQ(model, "myfancymodel"); +} + +TEST_F(BlockDevTest, discard) +{ + EXPECT_TRUE(sda.support_discard()); + EXPECT_TRUE(sdb.support_discard()); +} + +TEST_F(BlockDevTest, is_nvme) +{ + // It would be nice to have a positive NVME test too, but I don't have any + // examples for the canned data. + EXPECT_FALSE(sda.is_nvme()); + EXPECT_FALSE(sdb.is_nvme()); +} + +TEST_F(BlockDevTest, is_rotational) +{ + EXPECT_FALSE(sda.is_rotational()); + EXPECT_TRUE(sdb.is_rotational()); +} + +TEST(blkdev, _decode_model_enc) +{ + + const char *foo[][2] = { + { "WDC\\x20WDS200T2B0A-00SM50\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20\\x20", + "WDC_WDS200T2B0A-00SM50" }, + { 0, 0}, + }; + + for (unsigned i = 0; foo[i][0]; ++i) { + std::string d = _decode_model_enc(foo[i][0]); + cout << " '" << foo[i][0] << "' -> '" << d << "'" << std::endl; + ASSERT_EQ(std::string(foo[i][1]), d); + } +} -- cgit v1.2.3