blob: 6f9f5deeb6cb53a58dab548dde04a5f57cfa5956 [file] [log] [blame]
Eddie James774f9af2019-03-19 20:58:53 +00001#include "occ_manager.hpp"
2
3#include <stdlib.h>
4
5#include <filesystem>
6#include <fstream>
7
8#include <gtest/gtest.h>
9
10constexpr auto num_error_files = 8;
11constexpr auto device = "occ-hwmon.1";
12constexpr auto error = "occ_error";
13constexpr auto errorMem = "occ_mem_throttle";
14constexpr auto errorPower = "occ_dvfs_power";
15constexpr auto errorTemp = "occ_dvfs_overtemp";
16constexpr auto legacyDevice = "occ-hwmon.2";
17constexpr auto legacyErrorTemp = "occ_dvfs_ot";
18constexpr auto noError = "0";
19
George Liubcef3b42021-09-10 12:39:02 +080020namespace fs = std::filesystem;
Eddie James774f9af2019-03-19 20:58:53 +000021using namespace open_power::occ;
22
23class ErrorFiles : public ::testing::Test
24{
25 public:
26 ErrorFiles() :
George Liuf3b75142021-06-10 11:22:50 +080027 rc(sd_event_default(&event)), pEvent(event), manager(pEvent),
Patrick Williams89e54fa2024-02-28 11:32:51 -060028 status(pEvent, "/dummy1", manager
29#ifdef POWER10
30 ,
31 powerMode
32#endif
33 )
Eddie James774f9af2019-03-19 20:58:53 +000034 {
35 EXPECT_GE(rc, 0);
36 event = nullptr;
37 }
38
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
75 virtual void TearDown()
76 {
77 fs::remove_all(occPath);
78 }
79
Eddie James774f9af2019-03-19 20:58:53 +000080 sd_event* event;
81 int rc;
82 open_power::occ::EventPtr pEvent;
Patrick Williams89e54fa2024-02-28 11:32:51 -060083#ifdef POWER10
84 std::unique_ptr<powermode::PowerMode> powerMode = nullptr;
85#endif
Eddie James774f9af2019-03-19 20:58:53 +000086
87 Manager manager;
88 Status status;
89
90 fs::path devicePath;
91 fs::path legacyDevicePath;
92 fs::path occPath;
93};
94
95TEST_F(ErrorFiles, AddDeviceErrorWatch)
96{
Patrick Williams89e54fa2024-02-28 11:32:51 -060097 Device occDevice(pEvent, devicePath, manager, status
98#ifdef POWER10
99 ,
100 powerMode
101#endif
102 );
Eddie James774f9af2019-03-19 20:58:53 +0000103
104 occDevice.addErrorWatch(false);
105 occDevice.removeErrorWatch();
106}
107
108TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
109{
Patrick Williams89e54fa2024-02-28 11:32:51 -0600110 Device legacyOccDevice(pEvent, legacyDevicePath, manager, status
111#ifdef POWER10
112 ,
113 powerMode
114#endif
115 );
Eddie James774f9af2019-03-19 20:58:53 +0000116
117 legacyOccDevice.addErrorWatch(false);
118 legacyOccDevice.removeErrorWatch();
119}