From 84e44dd0122cc4e532206bad01a25629deac3d9f Mon Sep 17 00:00:00 2001 From: iamamyth Date: Fri, 17 Jan 2025 02:48:48 -0800 Subject: tests: Fix tools::is_hdd unit tests Correct the unit tests for tools::is_hdd to avoid making assumptions about the configuration of a particular device based solely on the value of the __GLIBC__ preprocessor flag. Instead, rely on the test invoker to provide paths for devices of specific types via the process environment, thereby avoiding faulty assumptions and improving the specificity of test assertions. To ensure appropriate devices exist, add a script, tests/create_test_disks.sh, which configures loopback devices mirroring relevant configurations. --- tests/unit_tests/is_hdd.cpp | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'tests/unit_tests/is_hdd.cpp') diff --git a/tests/unit_tests/is_hdd.cpp b/tests/unit_tests/is_hdd.cpp index 040af4f47..55f759ed9 100644 --- a/tests/unit_tests/is_hdd.cpp +++ b/tests/unit_tests/is_hdd.cpp @@ -1,17 +1,36 @@ #include "common/util.h" +#include #include #include +#include /* required to output boost::optional in assertions */ + +#ifndef GTEST_SKIP +#include +#define SKIP_TEST(reason) do {std::cerr << "Skipping test: " << reason << std::endl; return;} while(0) +#else +#define SKIP_TEST(reason) GTEST_SKIP() << reason +#endif #if defined(__GLIBC__) -TEST(is_hdd, linux_os_root) -{ - std::string path = "/"; - EXPECT_TRUE(tools::is_hdd(path.c_str()) != boost::none); +TEST(is_hdd, rotational_drive) { + const char *hdd = std::getenv("MONERO_TEST_DEVICE_HDD"); + if (hdd == nullptr) + SKIP_TEST("No rotational disk device configured"); + EXPECT_EQ(tools::is_hdd(hdd), boost::optional(true)); } -#else -TEST(is_hdd, unknown_os) -{ - std::string path = ""; - EXPECT_FALSE(tools::is_hdd(path.c_str()) != boost::none); + +TEST(is_hdd, ssd) { + const char *ssd = std::getenv("MONERO_TEST_DEVICE_SSD"); + if (ssd == nullptr) + SKIP_TEST("No SSD device configured"); + EXPECT_EQ(tools::is_hdd(ssd), boost::optional(false)); +} + +TEST(is_hdd, unknown_attrs) { + EXPECT_EQ(tools::is_hdd("/dev/null"), boost::none); } #endif +TEST(is_hdd, stability) +{ + EXPECT_NO_THROW(tools::is_hdd("")); +} -- cgit v1.2.3