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         rc(sd_event_default(&event)), pEvent(event), manager(pEvent),
28         status(pEvent, "/dummy1", manager)
29     {
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 
75     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 
87 TEST_F(ErrorFiles, AddDeviceErrorWatch)
88 {
89     Device occDevice(pEvent, devicePath, manager, status);
90 
91     occDevice.addErrorWatch(false);
92     occDevice.removeErrorWatch();
93 }
94 
95 TEST_F(ErrorFiles, AddLegacyDeviceErrorWatch)
96 {
97     Device legacyOccDevice(pEvent, legacyDevicePath, manager, status);
98 
99     legacyOccDevice.addErrorWatch(false);
100     legacyOccDevice.removeErrorWatch();
101 }
102