1774f9af9SEddie James #include "occ_manager.hpp"
2774f9af9SEddie James
3774f9af9SEddie James #include <stdlib.h>
4774f9af9SEddie James
5774f9af9SEddie James #include <filesystem>
6774f9af9SEddie James #include <fstream>
7774f9af9SEddie James
8774f9af9SEddie James #include <gtest/gtest.h>
9774f9af9SEddie James
10774f9af9SEddie James constexpr auto num_error_files = 8;
11774f9af9SEddie James constexpr auto device = "occ-hwmon.1";
12774f9af9SEddie James constexpr auto error = "occ_error";
13774f9af9SEddie James constexpr auto errorMem = "occ_mem_throttle";
14774f9af9SEddie James constexpr auto errorPower = "occ_dvfs_power";
15774f9af9SEddie James constexpr auto errorTemp = "occ_dvfs_overtemp";
16774f9af9SEddie James constexpr auto legacyDevice = "occ-hwmon.2";
17774f9af9SEddie James constexpr auto legacyErrorTemp = "occ_dvfs_ot";
18774f9af9SEddie James constexpr auto noError = "0";
19774f9af9SEddie James
20bcef3b48SGeorge Liu namespace fs = std::filesystem;
21774f9af9SEddie James using namespace open_power::occ;
22774f9af9SEddie James
23774f9af9SEddie James class ErrorFiles : public ::testing::Test
24774f9af9SEddie James {
25774f9af9SEddie James public:
ErrorFiles()26774f9af9SEddie James ErrorFiles() :
27f3b7514eSGeorge Liu rc(sd_event_default(&event)), pEvent(event), manager(pEvent),
2889e54fabSPatrick Williams status(pEvent, "/dummy1", manager
2989e54fabSPatrick Williams #ifdef POWER10
3089e54fabSPatrick Williams ,
3189e54fabSPatrick Williams powerMode
3289e54fabSPatrick Williams #endif
3389e54fabSPatrick Williams )
34774f9af9SEddie James {
35774f9af9SEddie James EXPECT_GE(rc, 0);
36774f9af9SEddie James event = nullptr;
37774f9af9SEddie James }
38774f9af9SEddie James
SetUp()39774f9af9SEddie James virtual void SetUp()
40774f9af9SEddie James {
41774f9af9SEddie James fs::path files[num_error_files];
42774f9af9SEddie James char tmpDirTemplate[64];
43774f9af9SEddie James
44774f9af9SEddie James strcpy(tmpDirTemplate, "/tmp/occXXXXXX");
45774f9af9SEddie James auto path = mkdtemp(tmpDirTemplate);
46774f9af9SEddie James assert(path != nullptr);
47774f9af9SEddie James
48774f9af9SEddie James occPath = path;
49774f9af9SEddie James devicePath = occPath / device;
50774f9af9SEddie James legacyDevicePath = occPath / legacyDevice;
51774f9af9SEddie James
52774f9af9SEddie James fs::create_directory(devicePath);
53774f9af9SEddie James fs::create_directory(legacyDevicePath);
54774f9af9SEddie James
55774f9af9SEddie James files[0] = devicePath / error;
56774f9af9SEddie James files[1] = devicePath / errorMem;
57774f9af9SEddie James files[2] = devicePath / errorPower;
58774f9af9SEddie James files[3] = devicePath / errorTemp;
59774f9af9SEddie James files[4] = legacyDevicePath / error;
60774f9af9SEddie James files[5] = legacyDevicePath / errorMem;
61774f9af9SEddie James files[6] = legacyDevicePath / errorPower;
62774f9af9SEddie James files[7] = legacyDevicePath / legacyErrorTemp;
63774f9af9SEddie James
64774f9af9SEddie James for (const fs::path& f : files)
65774f9af9SEddie James {
66774f9af9SEddie James auto stream = std::ofstream(f.c_str());
67774f9af9SEddie James
68774f9af9SEddie James if (stream)
69774f9af9SEddie James {
70774f9af9SEddie James stream << noError;
71774f9af9SEddie James }
72774f9af9SEddie James }
73774f9af9SEddie James }
74774f9af9SEddie James
TearDown()75774f9af9SEddie James virtual void TearDown()
76774f9af9SEddie James {
77774f9af9SEddie James fs::remove_all(occPath);
78774f9af9SEddie James }
79774f9af9SEddie James
80774f9af9SEddie James sd_event* event;
81774f9af9SEddie James int rc;
82774f9af9SEddie James open_power::occ::EventPtr pEvent;
8389e54fabSPatrick Williams #ifdef POWER10
8489e54fabSPatrick Williams std::unique_ptr<powermode::PowerMode> powerMode = nullptr;
8589e54fabSPatrick Williams #endif
86774f9af9SEddie James
87774f9af9SEddie James Manager manager;
88774f9af9SEddie James Status status;
89774f9af9SEddie James
90774f9af9SEddie James fs::path devicePath;
91774f9af9SEddie James fs::path legacyDevicePath;
92774f9af9SEddie James fs::path occPath;
93774f9af9SEddie James };
94774f9af9SEddie James
TEST_F(ErrorFiles,AddDeviceErrorWatch)95774f9af9SEddie James TEST_F(ErrorFiles, AddDeviceErrorWatch)
96774f9af9SEddie James {
9789e54fabSPatrick Williams Device occDevice(pEvent, devicePath, manager, status
9889e54fabSPatrick Williams #ifdef POWER10
9989e54fabSPatrick Williams ,
10089e54fabSPatrick Williams powerMode
10189e54fabSPatrick Williams #endif
10289e54fabSPatrick Williams );
103774f9af9SEddie James
104774f9af9SEddie James occDevice.addErrorWatch(false);
105774f9af9SEddie James occDevice.removeErrorWatch();
106774f9af9SEddie James }
107774f9af9SEddie James
108*720a3841SChris Cain // legacy OCC devices not supported on POWER10
109*720a3841SChris Cain #ifndef POWER10
TEST_F(ErrorFiles,AddLegacyDeviceErrorWatch)110774f9af9SEddie James TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
111774f9af9SEddie James {
112*720a3841SChris Cain Device legacyOccDevice(pEvent, legacyDevicePath, manager, status);
113774f9af9SEddie James
114774f9af9SEddie James legacyOccDevice.addErrorWatch(false);
115774f9af9SEddie James legacyOccDevice.removeErrorWatch();
116774f9af9SEddie James }
117*720a3841SChris Cain #endif
118