1 #include "occ_manager.hpp"
2
3 #include <stdlib.h>
4
5 #include <filesystem>
6 #include <fstream>
7
8 #include <gtest/gtest.h>
9
10 constexpr auto num_error_files = 8;
11 constexpr auto device = "occ-hwmon.1";
12 constexpr auto error = "occ_error";
13 constexpr auto errorMem = "occ_mem_throttle";
14 constexpr auto errorPower = "occ_dvfs_power";
15 constexpr auto errorTemp = "occ_dvfs_overtemp";
16 constexpr auto legacyDevice = "occ-hwmon.2";
17 constexpr auto legacyErrorTemp = "occ_dvfs_ot";
18 constexpr auto noError = "0";
19
20 namespace fs = std::filesystem;
21 using namespace open_power::occ;
22
23 class ErrorFiles : public ::testing::Test
24 {
25 public:
ErrorFiles()26 ErrorFiles() :
27 rc(sd_event_default(&event)), pEvent(event), manager(pEvent),
28 status(pEvent, "/dummy1", manager, powerMode)
29 {
30 EXPECT_GE(rc, 0);
31 event = nullptr;
32 }
33
SetUp()34 virtual void SetUp()
35 {
36 fs::path files[num_error_files];
37 char tmpDirTemplate[64];
38
39 strcpy(tmpDirTemplate, "/tmp/occXXXXXX");
40 auto path = mkdtemp(tmpDirTemplate);
41 assert(path != nullptr);
42
43 occPath = path;
44 devicePath = occPath / device;
45 legacyDevicePath = occPath / legacyDevice;
46
47 fs::create_directory(devicePath);
48 fs::create_directory(legacyDevicePath);
49
50 files[0] = devicePath / error;
51 files[1] = devicePath / errorMem;
52 files[2] = devicePath / errorPower;
53 files[3] = devicePath / errorTemp;
54 files[4] = legacyDevicePath / error;
55 files[5] = legacyDevicePath / errorMem;
56 files[6] = legacyDevicePath / errorPower;
57 files[7] = legacyDevicePath / legacyErrorTemp;
58
59 for (const fs::path& f : files)
60 {
61 auto stream = std::ofstream(f.c_str());
62
63 if (stream)
64 {
65 stream << noError;
66 }
67 }
68 }
69
TearDown()70 virtual void TearDown()
71 {
72 fs::remove_all(occPath);
73 }
74
75 sd_event* event;
76 int rc;
77 open_power::occ::EventPtr pEvent;
78 std::unique_ptr<powermode::PowerMode> powerMode = nullptr;
79
80 Manager manager;
81 Status status;
82
83 fs::path devicePath;
84 fs::path legacyDevicePath;
85 fs::path occPath;
86 };
87
TEST_F(ErrorFiles,AddDeviceErrorWatch)88 TEST_F(ErrorFiles, AddDeviceErrorWatch)
89 {
90 Device occDevice(pEvent, devicePath, manager, status, powerMode);
91
92 occDevice.addErrorWatch(false);
93 occDevice.removeErrorWatch();
94 }
95