blob: 63eb0d83da8eb5a11191f38a8dd85ec6ceacc29e [file] [log] [blame]
Tomáš Peckaefd88432020-06-17 15:31:39 +02001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
5 *
6*/
7
8#include "trompeloeil_doctest.h"
9#include <filesystem>
10#include <fstream>
Tomáš Pecka261c8862020-11-05 11:23:08 +010011#include "health/outputs/LedSysfsDriver.h"
Tomáš Peckaefd88432020-06-17 15:31:39 +020012#include "test_log_setup.h"
13#include "tests/configure.cmake.h"
14
15namespace {
16
17uint32_t readFile(const std::filesystem::path& path)
18{
19 std::ifstream ifs(path);
20 uint32_t ret;
21 if (!(ifs >> ret)) {
22 throw std::invalid_argument("Failed reading '" + std::string(path) + "'.");
23 }
24 return ret;
25}
26
27/** @short Remove directory tree at 'rootDir' path (if exists) */
28void removeDirectoryTreeIfExists(const std::filesystem::path& rootDir)
29{
30 if (std::filesystem::exists(rootDir)) {
31 std::filesystem::remove_all(rootDir);
32 }
33}
34
35}
36
37TEST_CASE("SysFS LED driver")
38{
39 using namespace std::literals;
40 TEST_INIT_LOGS;
41
Jan Kundrátaf905722021-12-01 20:00:05 +010042 auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/health_output-led/"s};
Tomáš Peckaefd88432020-06-17 15:31:39 +020043 auto fakeBrightnessFile = fakeSysfsDir / "brightness";
44 removeDirectoryTreeIfExists(fakeSysfsDir);
45
46 SECTION("Basic usage test")
47 {
Tomáš Peckad62d3ec2021-04-20 13:06:58 +020048 std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/leds/line:green/"s, fakeSysfsDir, std::filesystem::copy_options::recursive);
Tomáš Pecka261c8862020-11-05 11:23:08 +010049 velia::health::LedSysfsDriver led(fakeSysfsDir);
Tomáš Peckaefd88432020-06-17 15:31:39 +020050
51 led.set(0);
52 REQUIRE(readFile(fakeBrightnessFile) == 0);
53
54 led.set(1);
55 REQUIRE(readFile(fakeBrightnessFile) == 1);
56
57 led.set(42);
58 REQUIRE(readFile(fakeBrightnessFile) == 42);
59
60 led.set(0);
61 REQUIRE(readFile(fakeBrightnessFile) == 0);
62 }
63
64 SECTION("Invalid directory")
65 {
Tomáš Peckad62d3ec2021-04-20 13:06:58 +020066 std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/leds/line:red/"s, fakeSysfsDir, std::filesystem::copy_options::recursive);
Tomáš Pecka261c8862020-11-05 11:23:08 +010067 REQUIRE_THROWS_AS(velia::health::LedSysfsDriver(fakeSysfsDir), std::invalid_argument);
Tomáš Peckaefd88432020-06-17 15:31:39 +020068 }
69}