blob: c412e63ac4906090d5fed6af041e06543449311d [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
20namespace fs = std::experimental::filesystem;
21using 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),
28 status(pEvent, "/dummy1", manager)
Eddie James774f9af2019-03-19 20:58:53 +000029 {
30 EXPECT_GE(rc, 0);
31 event = nullptr;
32 }
33
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
70 virtual void TearDown()
71 {
72 fs::remove_all(occPath);
73 }
74
Eddie James774f9af2019-03-19 20:58:53 +000075 sd_event* event;
76 int rc;
77 open_power::occ::EventPtr pEvent;
78
79 Manager manager;
80 Status status;
81
82 fs::path devicePath;
83 fs::path legacyDevicePath;
84 fs::path occPath;
85};
86
87TEST_F(ErrorFiles, AddDeviceErrorWatch)
88{
89 Device occDevice(pEvent, devicePath, manager, status);
90
91 occDevice.addErrorWatch(false);
92 occDevice.removeErrorWatch();
93}
94
95TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
96{
97 Device legacyOccDevice(pEvent, legacyDevicePath, manager, status);
98
99 legacyOccDevice.addErrorWatch(false);
100 legacyOccDevice.removeErrorWatch();
101}