| /* |
| * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| * |
| * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| * |
| */ |
| |
| #include "trompeloeil_doctest.h" |
| #include <filesystem> |
| #include <fstream> |
| #include "health/outputs/LedSysfsDriver.h" |
| #include "test_log_setup.h" |
| #include "tests/configure.cmake.h" |
| |
| namespace { |
| |
| uint32_t readFile(const std::filesystem::path& path) |
| { |
| std::ifstream ifs(path); |
| uint32_t ret; |
| if (!(ifs >> ret)) { |
| throw std::invalid_argument("Failed reading '" + std::string(path) + "'."); |
| } |
| return ret; |
| } |
| |
| /** @short Remove directory tree at 'rootDir' path (if exists) */ |
| void removeDirectoryTreeIfExists(const std::filesystem::path& rootDir) |
| { |
| if (std::filesystem::exists(rootDir)) { |
| std::filesystem::remove_all(rootDir); |
| } |
| } |
| |
| } |
| |
| TEST_CASE("SysFS LED driver") |
| { |
| using namespace std::literals; |
| TEST_INIT_LOGS; |
| |
| auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/leds/"s}; |
| auto fakeBrightnessFile = fakeSysfsDir / "brightness"; |
| removeDirectoryTreeIfExists(fakeSysfsDir); |
| |
| SECTION("Basic usage test") |
| { |
| std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/leds/line:green/"s, fakeSysfsDir, std::filesystem::copy_options::recursive); |
| velia::health::LedSysfsDriver led(fakeSysfsDir); |
| |
| led.set(0); |
| REQUIRE(readFile(fakeBrightnessFile) == 0); |
| |
| led.set(1); |
| REQUIRE(readFile(fakeBrightnessFile) == 1); |
| |
| led.set(42); |
| REQUIRE(readFile(fakeBrightnessFile) == 42); |
| |
| led.set(0); |
| REQUIRE(readFile(fakeBrightnessFile) == 0); |
| } |
| |
| SECTION("Invalid directory") |
| { |
| std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/leds/line:red/"s, fakeSysfsDir, std::filesystem::copy_options::recursive); |
| REQUIRE_THROWS_AS(velia::health::LedSysfsDriver(fakeSysfsDir), std::invalid_argument); |
| } |
| } |