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::experimental::filesystem;
21 using namespace open_power::occ;
22 
23 class ErrorFiles : public ::testing::Test
24 {
25   public:
26     ErrorFiles() :
27         bus(sdbusplus::bus::new_default()), rc(sd_event_default(&event)),
28         pEvent(event), manager(bus, pEvent),
29         status(bus, pEvent, "/dummy1", manager)
30     {
31         EXPECT_GE(rc, 0);
32         event = nullptr;
33     }
34 
35     virtual void SetUp()
36     {
37         fs::path files[num_error_files];
38         char tmpDirTemplate[64];
39 
40         strcpy(tmpDirTemplate, "/tmp/occXXXXXX");
41         auto path = mkdtemp(tmpDirTemplate);
42         assert(path != nullptr);
43 
44         occPath = path;
45         devicePath = occPath / device;
46         legacyDevicePath = occPath / legacyDevice;
47 
48         fs::create_directory(devicePath);
49         fs::create_directory(legacyDevicePath);
50 
51         files[0] = devicePath / error;
52         files[1] = devicePath / errorMem;
53         files[2] = devicePath / errorPower;
54         files[3] = devicePath / errorTemp;
55         files[4] = legacyDevicePath / error;
56         files[5] = legacyDevicePath / errorMem;
57         files[6] = legacyDevicePath / errorPower;
58         files[7] = legacyDevicePath / legacyErrorTemp;
59 
60         for (const fs::path& f : files)
61         {
62             auto stream = std::ofstream(f.c_str());
63 
64             if (stream)
65             {
66                 stream << noError;
67             }
68         }
69     }
70 
71     virtual void TearDown()
72     {
73         fs::remove_all(occPath);
74     }
75 
76     sdbusplus::bus::bus bus;
77     sd_event* event;
78     int rc;
79     open_power::occ::EventPtr pEvent;
80 
81     Manager manager;
82     Status status;
83 
84     fs::path devicePath;
85     fs::path legacyDevicePath;
86     fs::path occPath;
87 };
88 
89 TEST_F(ErrorFiles, AddDeviceErrorWatch)
90 {
91     Device occDevice(pEvent, devicePath, manager, status);
92 
93     occDevice.addErrorWatch(false);
94     occDevice.removeErrorWatch();
95 }
96 
97 TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
98 {
99     Device legacyOccDevice(pEvent, legacyDevicePath, manager, status);
100 
101     legacyOccDevice.addErrorWatch(false);
102     legacyOccDevice.removeErrorWatch();
103 }
104