HardwareState: EMMC data reader

Ported EMMC driver from
cla-sysrepo@19163e50ab062a8b5b75f754b30c22fdbd62b03a.

In cla-sysrepo, these drivers provided a property-based API, i.e., you
asked for a specific property (i.e., a filename) and the contents of the
file was returned.

In velia, we do not need this one-by-one property access. We provide all
attributes with a single call.

Change-Id: Icdb1129e84c2c7d8db50e192caa7c6ddce1441d7
diff --git a/tests/hardware_emmc.cpp b/tests/hardware_emmc.cpp
new file mode 100644
index 0000000..91b06c0
--- /dev/null
+++ b/tests/hardware_emmc.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
+ *
+*/
+
+#include "trompeloeil_doctest.h"
+#include <filesystem>
+#include "ietf-hardware/sysfs/EMMC.h"
+#include "pretty_printers.h"
+#include "test_log_setup.h"
+#include "tests/configure.cmake.h"
+
+using namespace std::literals;
+
+namespace {
+
+/** @short Remove directory tree at 'rootDir' path (if exists) */
+void removeDirectoryTreeIfExists(const std::string& rootDir)
+{
+    if (std::filesystem::exists(rootDir)) {
+        std::filesystem::remove_all(rootDir);
+    }
+}
+}
+
+TEST_CASE("EMMC driver")
+{
+    TEST_INIT_LOGS;
+
+    const auto fakeRoot = CMAKE_CURRENT_BINARY_DIR + "/tests/emmc/"s;
+    removeDirectoryTreeIfExists(fakeRoot);
+
+    SECTION("Test correct structure")
+    {
+        std::string sourceDir;
+        velia::ietf_hardware::sysfs::EMMC::Attributes expected;
+
+        SECTION("device1")
+        {
+            sourceDir = CMAKE_CURRENT_SOURCE_DIR + "/tests/sysfs/emmc/device1"s;
+            expected = {
+                {"date", "02/2015"},
+                {"serial", "0x00a8808d"},
+                {"name", "8GME4R"},
+                // life_time: 0x01 0x02 (i.e., 0-10% and 10-20%)
+                // pre_eol_info: 0x01 (i.e., normal)
+                {"life_time", "10"}};
+        }
+
+        SECTION("device2")
+        {
+            sourceDir = CMAKE_CURRENT_SOURCE_DIR + "/tests/sysfs/emmc/device2"s;
+            expected = {
+                {"date", "02/2015"},
+                {"serial", "0x00a8808d"},
+                {"name", "8GME4R"},
+                // life_time: 0x0B 0x02 (i.e., 100-?% and 10-20%)
+                // pre_eol_info: 0x03 (i.e., urgent)
+                {"life_time", "100"}};
+        }
+
+        std::filesystem::copy(sourceDir, fakeRoot, std::filesystem::copy_options::recursive);
+        auto emmcAttrs = velia::ietf_hardware::sysfs::EMMC(fakeRoot);
+        REQUIRE(emmcAttrs.attributes() == expected);
+    }
+
+    SECTION("Test emmc (<5) / device3")
+    {
+        std::filesystem::copy(CMAKE_CURRENT_SOURCE_DIR + "/tests/sysfs/emmc/device3"s, fakeRoot, std::filesystem::copy_options::recursive);
+
+        // health reporting missing (emmc < 5). When one file is missing, the attributes method invocation throws
+        REQUIRE_THROWS_AS(velia::ietf_hardware::sysfs::EMMC(fakeRoot).attributes(), std::invalid_argument);
+    }
+}
diff --git a/tests/pretty_printers.h b/tests/pretty_printers.h
new file mode 100644
index 0000000..8fea584
--- /dev/null
+++ b/tests/pretty_printers.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016-2018 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Jan Kundrát <jan.kundrat@cesnet.cz>
+ *
+*/
+
+#pragma once
+
+#include <doctest/doctest.h>
+#include <map>
+#include <sstream>
+#include <trompeloeil.hpp>
+
+
+namespace doctest {
+
+template <>
+struct StringMaker<std::map<std::string, std::string>> {
+    static String convert(const std::map<std::string, std::string>& map)
+    {
+        std::ostringstream os;
+        os << "{" << std::endl;
+        for (const auto& [key, value] : map) {
+            os << "  \"" << key << "\": \"" << value << "\"," << std::endl;
+        }
+        os << "}";
+        return os.str().c_str();
+    }
+};
+
+}
diff --git a/tests/sysfs/emmc/device1/date b/tests/sysfs/emmc/device1/date
new file mode 100644
index 0000000..f4eea3a
--- /dev/null
+++ b/tests/sysfs/emmc/device1/date
@@ -0,0 +1 @@
+02/2015
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device1/life_time b/tests/sysfs/emmc/device1/life_time
new file mode 100644
index 0000000..365c1d7
--- /dev/null
+++ b/tests/sysfs/emmc/device1/life_time
@@ -0,0 +1 @@
+0x01 0x02
diff --git a/tests/sysfs/emmc/device1/name b/tests/sysfs/emmc/device1/name
new file mode 100644
index 0000000..504b965
--- /dev/null
+++ b/tests/sysfs/emmc/device1/name
@@ -0,0 +1 @@
+8GME4R
diff --git a/tests/sysfs/emmc/device1/pre_eol_info b/tests/sysfs/emmc/device1/pre_eol_info
new file mode 100644
index 0000000..160915c
--- /dev/null
+++ b/tests/sysfs/emmc/device1/pre_eol_info
@@ -0,0 +1 @@
+0x01
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device1/serial b/tests/sysfs/emmc/device1/serial
new file mode 100644
index 0000000..20d039f
--- /dev/null
+++ b/tests/sysfs/emmc/device1/serial
@@ -0,0 +1 @@
+0x00a8808d
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device2/date b/tests/sysfs/emmc/device2/date
new file mode 100644
index 0000000..f4eea3a
--- /dev/null
+++ b/tests/sysfs/emmc/device2/date
@@ -0,0 +1 @@
+02/2015
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device2/life_time b/tests/sysfs/emmc/device2/life_time
new file mode 100644
index 0000000..55d1e6b
--- /dev/null
+++ b/tests/sysfs/emmc/device2/life_time
@@ -0,0 +1 @@
+0x0B 0x02
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device2/name b/tests/sysfs/emmc/device2/name
new file mode 100644
index 0000000..504b965
--- /dev/null
+++ b/tests/sysfs/emmc/device2/name
@@ -0,0 +1 @@
+8GME4R
diff --git a/tests/sysfs/emmc/device2/pre_eol_info b/tests/sysfs/emmc/device2/pre_eol_info
new file mode 100644
index 0000000..6bc286e
--- /dev/null
+++ b/tests/sysfs/emmc/device2/pre_eol_info
@@ -0,0 +1 @@
+0x03
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device2/serial b/tests/sysfs/emmc/device2/serial
new file mode 100644
index 0000000..20d039f
--- /dev/null
+++ b/tests/sysfs/emmc/device2/serial
@@ -0,0 +1 @@
+0x00a8808d
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device3/date b/tests/sysfs/emmc/device3/date
new file mode 100644
index 0000000..f4eea3a
--- /dev/null
+++ b/tests/sysfs/emmc/device3/date
@@ -0,0 +1 @@
+02/2015
\ No newline at end of file
diff --git a/tests/sysfs/emmc/device3/name b/tests/sysfs/emmc/device3/name
new file mode 100644
index 0000000..504b965
--- /dev/null
+++ b/tests/sysfs/emmc/device3/name
@@ -0,0 +1 @@
+8GME4R
diff --git a/tests/sysfs/emmc/device3/serial b/tests/sysfs/emmc/device3/serial
new file mode 100644
index 0000000..20d039f
--- /dev/null
+++ b/tests/sysfs/emmc/device3/serial
@@ -0,0 +1 @@
+0x00a8808d
\ No newline at end of file
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/name b/tests/sysfs/hwmon/device1/hwmon/hwmon0/name
new file mode 100644
index 0000000..a29f406
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/name
@@ -0,0 +1 @@
+acpitz
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_crit b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_crit
new file mode 100644
index 0000000..9c2aad9
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_crit
@@ -0,0 +1 @@
+666777
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_input b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_input
new file mode 100644
index 0000000..f352f47
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_input
@@ -0,0 +1 @@
+66600
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_label b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_label
new file mode 100644
index 0000000..6cd60cd
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp10_label
@@ -0,0 +1 @@
+satan 1
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp11_input b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp11_input
new file mode 100644
index 0000000..6d2e2fb
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp11_input
@@ -0,0 +1 @@
+111222333444555
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_crit b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_crit
new file mode 100644
index 0000000..971c604
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_crit
@@ -0,0 +1 @@
+105000
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_input b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_input
new file mode 100644
index 0000000..f352f47
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp1_input
@@ -0,0 +1 @@
+66600
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_crit b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_crit
new file mode 100644
index 0000000..971c604
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_crit
@@ -0,0 +1 @@
+105000
diff --git a/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_input b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_input
new file mode 100644
index 0000000..7cb3518
--- /dev/null
+++ b/tests/sysfs/hwmon/device1/hwmon/hwmon0/temp2_input
@@ -0,0 +1 @@
+29800
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/name b/tests/sysfs/hwmon/device2/hwmon/hwmon33/name
new file mode 100755
index 0000000..2ec2faf
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/name
@@ -0,0 +1 @@
+satan temperatures
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit
new file mode 100755
index 0000000..363ca95
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit
@@ -0,0 +1 @@
+1000000000000000000005666
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit_alarm b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit_alarm
new file mode 100755
index 0000000..573541a
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_crit_alarm
@@ -0,0 +1 @@
+0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_input b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_input
new file mode 100755
index 0000000..acab4b6
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_input
@@ -0,0 +1 @@
+-34000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_label b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_label
new file mode 100755
index 0000000..f93d047
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_label
@@ -0,0 +1 @@
+Physical id 0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_max b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_max
new file mode 100755
index 0000000..146cf04
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp1_max
@@ -0,0 +1 @@
+80000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit
new file mode 100755
index 0000000..0277850
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit
@@ -0,0 +1 @@
+nazdar bazar carodej Merlin
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit_alarm b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit_alarm
new file mode 100755
index 0000000..573541a
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_crit_alarm
@@ -0,0 +1 @@
+0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_input b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_input
new file mode 100755
index 0000000..782a512
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_input
@@ -0,0 +1 @@
+-1234567891234567896666
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_label b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_label
new file mode 100755
index 0000000..09f8cbd
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_label
@@ -0,0 +1 @@
+Core 0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_max b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_max
new file mode 100755
index 0000000..146cf04
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp2_max
@@ -0,0 +1 @@
+80000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit
new file mode 100755
index 0000000..f7393e8
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit
@@ -0,0 +1 @@
+100000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit_alarm b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit_alarm
new file mode 100755
index 0000000..573541a
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_crit_alarm
@@ -0,0 +1 @@
+0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_input b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_input
new file mode 100755
index 0000000..3a05c8b
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_input
@@ -0,0 +1 @@
+30000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_label b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_label
new file mode 100755
index 0000000..e98a1da
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_label
@@ -0,0 +1 @@
+Core 1
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_max b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_max
new file mode 100755
index 0000000..146cf04
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp3_max
@@ -0,0 +1 @@
+80000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit
new file mode 100755
index 0000000..f7393e8
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit
@@ -0,0 +1 @@
+100000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit_alarm b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit_alarm
new file mode 100755
index 0000000..573541a
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_crit_alarm
@@ -0,0 +1 @@
+0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_input b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_input
new file mode 100755
index 0000000..afe1f8b
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_input
@@ -0,0 +1 @@
+26000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_label b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_label
new file mode 100755
index 0000000..18c8a0b
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_label
@@ -0,0 +1 @@
+Core 2
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_max b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_max
new file mode 100755
index 0000000..146cf04
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp4_max
@@ -0,0 +1 @@
+80000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit
new file mode 100755
index 0000000..f7393e8
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit
@@ -0,0 +1 @@
+100000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit_alarm b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit_alarm
new file mode 100755
index 0000000..573541a
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_crit_alarm
@@ -0,0 +1 @@
+0
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_input b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_input
new file mode 100755
index 0000000..446a9a5
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_input
@@ -0,0 +1 @@
+29000
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_label b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_label
new file mode 100755
index 0000000..5ec08ee
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_label
@@ -0,0 +1 @@
+Core 3
diff --git a/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_max b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_max
new file mode 100755
index 0000000..146cf04
--- /dev/null
+++ b/tests/sysfs/hwmon/device2/hwmon/hwmon33/temp5_max
@@ -0,0 +1 @@
+80000
diff --git a/tests/sysfs/hwmon/device3/hwmon/hwmon0/name b/tests/sysfs/hwmon/device3/hwmon/hwmon0/name
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/sysfs/hwmon/device3/hwmon/hwmon0/name
diff --git a/tests/sysfs/hwmon/device3/hwmon/hwmon5/name b/tests/sysfs/hwmon/device3/hwmon/hwmon5/name
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/sysfs/hwmon/device3/hwmon/hwmon5/name
diff --git a/tests/sysfs/hwmon/device4/hwmon/blah/name b/tests/sysfs/hwmon/device4/hwmon/blah/name
new file mode 100755
index 0000000..2615405
--- /dev/null
+++ b/tests/sysfs/hwmon/device4/hwmon/blah/name
@@ -0,0 +1 @@
+wrong_directory_name
\ No newline at end of file
diff --git a/tests/sysfs/hwmon/device4/hwmon/blah/temp1_input b/tests/sysfs/hwmon/device4/hwmon/blah/temp1_input
new file mode 100755
index 0000000..acab4b6
--- /dev/null
+++ b/tests/sysfs/hwmon/device4/hwmon/blah/temp1_input
@@ -0,0 +1 @@
+-34000