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
29 #ifdef POWER10
30                ,
31                powerMode
32 #endif
33         )
34     {
35         EXPECT_GE(rc, 0);
36         event = nullptr;
37     }
38 
SetUp()39     virtual void SetUp()
40     {
41         fs::path files[num_error_files];
42         char tmpDirTemplate[64];
43 
44         strcpy(tmpDirTemplate, "/tmp/occXXXXXX");
45         auto path = mkdtemp(tmpDirTemplate);
46         assert(path != nullptr);
47 
48         occPath = path;
49         devicePath = occPath / device;
50         legacyDevicePath = occPath / legacyDevice;
51 
52         fs::create_directory(devicePath);
53         fs::create_directory(legacyDevicePath);
54 
55         files[0] = devicePath / error;
56         files[1] = devicePath / errorMem;
57         files[2] = devicePath / errorPower;
58         files[3] = devicePath / errorTemp;
59         files[4] = legacyDevicePath / error;
60         files[5] = legacyDevicePath / errorMem;
61         files[6] = legacyDevicePath / errorPower;
62         files[7] = legacyDevicePath / legacyErrorTemp;
63 
64         for (const fs::path& f : files)
65         {
66             auto stream = std::ofstream(f.c_str());
67 
68             if (stream)
69             {
70                 stream << noError;
71             }
72         }
73     }
74 
TearDown()75     virtual void TearDown()
76     {
77         fs::remove_all(occPath);
78     }
79 
80     sd_event* event;
81     int rc;
82     open_power::occ::EventPtr pEvent;
83 #ifdef POWER10
84     std::unique_ptr<powermode::PowerMode> powerMode = nullptr;
85 #endif
86 
87     Manager manager;
88     Status status;
89 
90     fs::path devicePath;
91     fs::path legacyDevicePath;
92     fs::path occPath;
93 };
94 
TEST_F(ErrorFiles,AddDeviceErrorWatch)95 TEST_F(ErrorFiles, AddDeviceErrorWatch)
96 {
97     Device occDevice(pEvent, devicePath, manager, status
98 #ifdef POWER10
99                      ,
100                      powerMode
101 #endif
102     );
103 
104     occDevice.addErrorWatch(false);
105     occDevice.removeErrorWatch();
106 }
107 
TEST_F(ErrorFiles,AddLegacyDeviceErrorWatch)108 TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
109 {
110     Device legacyOccDevice(pEvent, legacyDevicePath, manager, status
111 #ifdef POWER10
112                            ,
113                            powerMode
114 #endif
115     );
116 
117     legacyOccDevice.addErrorWatch(false);
118     legacyOccDevice.removeErrorWatch();
119 }
120