API
sysMonitor_test.cpp
Go to the documentation of this file.
1 /****
2  * Test following parsing functions
3  * int parseCPUTemperatures(std::string, std::vector<float>&);
4  * int parseCPULoads(std::string, float&);
5  * int parseDiskTemperature(std::string, float&);
6  * int parseDiskUsage(std::string, float&);
7  * int parseRamUsage(std::string, float&);
8  * To use:
9  * In ~MagAOX/tests, compile this file with:
10  * `make -f singleTest.mk testfile=../apps/sysMonitor/tests/sysMonitor_test.cpp`
11  * and run program with:
12  * `./singleTest`
13  ****/
14 
15 #include "../../../tests/catch2/catch.hpp"
16 #include "../../tests/testMacrosINDI.hpp"
17 
18 #include "../sysMonitor.hpp"
19 
20 using namespace MagAOX::app;
21 
22 SCENARIO( "System monitor is constructed and CPU temperature results are passed in", "[sysMonitor]" )
23 {
24  GIVEN("A default constructed system monitor object and an empty vector for temperatures")
25  {
27  int rv;
28  float temps = -1;
29 
30  // Fails with whitespace in front, but is this necessary to correct for?
31 
32  WHEN("Correct line is given")
33  {
34  rv = sm.parseCPUTemperatures(temps, "Core 0: +42.0°C (high = +100.0°C, crit = +100.0°C)");
35  REQUIRE(rv == 0);
36  REQUIRE(temps == 42);
37  }
38 
39  WHEN("Correct line is given")
40  {
41  rv = sm.parseCPUTemperatures(temps, "Core 1: +45.0°C (high = +100.0°C, crit = +100.0°C)");
42  REQUIRE(rv == 0);
43  REQUIRE(temps == 45);
44  }
45 
46  WHEN("Correct line is given")
47  {
48  rv = sm.parseCPUTemperatures(temps, "Core 2: +91.0°C (high = +100.0°C, crit = +100.0°C)");
49  REQUIRE(rv == 0);
50  REQUIRE(temps == 91);
51  }
52 
53  WHEN("Blank line is given")
54  {
55  rv = sm.parseCPUTemperatures(temps, "");
56  REQUIRE(rv == -1);
57  REQUIRE(temps == -999);
58  }
59 
60  WHEN("Incorrect line is given")
61  {
62  rv = sm.parseCPUTemperatures(temps, "coretemp-isa-0000");
63  REQUIRE(rv == -1);
64  REQUIRE(temps == -999);
65  }
66 
67  WHEN("Corrupted line is given")
68  {
69  rv = sm.parseCPUTemperatures(temps, "Core 3:+91.0° XXXXXXX");
70  REQUIRE(rv == -1);
71  REQUIRE(temps == -999);
72  }
73 
74  WHEN("Corrupted line is given")
75  {
76  rv = sm.parseCPUTemperatures(temps, "Core2: +91.0°C(high =+100.0°C, crit= +100.0°C)");
77  REQUIRE(rv == -1);
78  REQUIRE(temps == -999);
79  }
80  }
81 }
82 
83 SCENARIO( "System monitor is constructed and CPU load results are passed in", "[sysMonitor]" )
84 {
85  GIVEN("A default constructed system monitor object and an empty vector for loads")
86  {
88  int rv;
89  float loads = -1;
90 
91  // Fails with whitespace in front, but is this necessary to correct for?
92 
93  WHEN("Correct line is given")
94  {
95  rv = sm.parseCPULoads(loads, "02:35:43 PM 0 6.57 0.02 1.32 0.24 0.00 0.00 0.00 0.00 0.00 91.85");
96  REQUIRE(rv == 0);
97  REQUIRE((loads - 0.0815) < 0.0005);
98  }
99 
100  WHEN("Correct line is given")
101  {
102  rv = sm.parseCPULoads(loads, "10:32:28 AM 1 6.54 0.21 2.75 24.64 0.00 0.06 0.00 0.00 0.00 65.81");
103  REQUIRE(rv == 0);
104  REQUIRE((loads - 0.3419) < 0.0005);
105  }
106 
107  WHEN("Correct line is given")
108  {
109  rv = sm.parseCPULoads(loads, "10:32:28 AM 3 4.24 0.03 1.97 5.52 0.00 0.00 0.00 0.00 0.00 88.24");
110  REQUIRE(rv == 0);
111  REQUIRE((loads - 0.1176) < 0.0005);
112  }
113 
114  WHEN("Blank line is given")
115  {
116  rv = sm.parseCPULoads(loads, "");
117  REQUIRE(rv == -1);
118  REQUIRE(loads == -1);
119  }
120 
121  WHEN("Incorrect line is given")
122  {
123  rv = sm.parseCPULoads(loads, "02:35:43 PM CPU %%usr %%nice %%sys %%iowait %%irq %%soft %%steal %%guest %%gnice %%idle");
124  REQUIRE(rv == -1);
125  REQUIRE(loads == -1);
126  }
127 
128  WHEN("Corrupted line is given")
129  {
130  rv = sm.parseCPULoads(loads, "10:32:28AM 2 5.24 0.14 2.70_1.41 0.00 0.00 0.00 0.00 0.00 80.50 ncawd vexing");
131  REQUIRE(rv == -1);
132  REQUIRE(loads == -1);
133  }
134  }
135 }
136 
137 SCENARIO( "System monitor is constructed and disk temperature result is passed in", "[sysMonitor]" )
138 {
139  GIVEN("A default constructed system monitor object and an empty float for temperature")
140  {
142  int rv;
143  float hdd_temp = -1;
144  std::string dname;
145 
146  // Fails with whitespace in front, but is this necessary to correct for?
147 
148  WHEN("Correct line is given for hard drive")
149  {
150  rv = sm.parseDiskTemperature(dname, hdd_temp, "/dev/sda: ST1000LM024 HN-M101MBB: 31°C");
151  REQUIRE(rv == 0);
152  REQUIRE(dname == "sda");
153  REQUIRE(hdd_temp == 31);
154  }
155 
156  WHEN("Correct line is given for ssd")
157  {
158  rv = sm.parseDiskTemperature(dname, hdd_temp, "/dev/sda: Samsung SSD 860 EVO 500GB: 27°C");
159  REQUIRE(rv == 0);
160  REQUIRE(dname == "sda");
161  REQUIRE(hdd_temp == 27);
162  }
163 
164  WHEN("Correct line is given for ssd")
165  {
166  rv = sm.parseDiskTemperature(dname, hdd_temp, "/dev/sdd: Samsung SSD 860 EVO 1TB: 100°C");
167  REQUIRE(rv == 0);
168  REQUIRE(dname == "sdd");
169  REQUIRE(hdd_temp == 100);
170  }
171 
172  WHEN("Blank line is given")
173  {
174  rv = sm.parseDiskTemperature(dname, hdd_temp,"");
175  REQUIRE(rv == -1);
176  REQUIRE(dname == "");
177  REQUIRE(hdd_temp == -999);
178  }
179 
180  WHEN("Incorrect line is given")
181  {
182  rv = sm.parseDiskTemperature(dname, hdd_temp,"/dev/sda: ST1000LM024_HN-M101MBB: 999999");
183  REQUIRE(rv == -1);
184  REQUIRE(dname == "");
185  REQUIRE(hdd_temp == -999);
186  }
187 
188  WHEN("Corrupted line is given")
189  {
190  rv = sm.parseDiskTemperature(dname, hdd_temp, "/dev/sdaT10 00L M0 24N-M101 MBB:31°CMBB");
191  REQUIRE(rv == -1);
192  REQUIRE(dname == "");
193  REQUIRE(hdd_temp == -999);
194  }
195  }
196 }
197 
198 SCENARIO( "System monitor is constructed and disk usage result is passed in", "[sysMonitor]" )
199 {
200  GIVEN("A default constructed system monitor object and an empty float for usage")
201  {
203  int rv;
204  float rootUsage = -1;
205  float dataUsage = -1;
206  float bootUsage = -1;
207 
208  // Fails with whitespace in front, but is this necessary to correct for?
209 
210  WHEN("Correct line is given for root")
211  {
212  rv = sm.parseDiskUsage("/dev/mapper/cl-root 52403200 12321848 40081352 24% /", rootUsage, dataUsage, bootUsage);
213  REQUIRE(rv == 0);
214  REQUIRE((rootUsage - 0.24f) < 0.0005);
215  }
216 
217  WHEN("Correct line for /data is given")
218  {
219  rv = sm.parseDiskUsage("/dev/md124 1952297568 81552 1952216016 1% /data", rootUsage, dataUsage, bootUsage);
220  REQUIRE(rv == 0);
221  REQUIRE((dataUsage - 0.01f) < 0.0005);
222  }
223 
224  WHEN("Correct line for /boot is given")
225  {
226  rv = sm.parseDiskUsage("/dev/md126 484004 289264 194740 60% /boot", rootUsage, dataUsage, bootUsage);
227  REQUIRE(rv == 0);
228  REQUIRE((bootUsage - 0.6f) < 0.0005);
229  }
230 
231  WHEN("Blank line is given")
232  {
233  rv = sm.parseDiskUsage("", rootUsage, dataUsage, bootUsage);
234  REQUIRE(rv == -1);
235  REQUIRE(rootUsage == -1);
236  REQUIRE(dataUsage == -1);
237  REQUIRE(bootUsage == -1);
238  }
239 
240  WHEN("Incorrect line is given")
241  {
242  rv = sm.parseDiskUsage("/dev/mapper/cl-root2403200 12321848 40081352 24% / 23e32 dwwe", rootUsage, dataUsage, bootUsage);
243  REQUIRE(rv == -1);
244  REQUIRE(rootUsage == -1);
245  REQUIRE(dataUsage == -1);
246  REQUIRE(bootUsage == -1);
247  }
248 
249  WHEN("Corrupted line is given")
250  {
251  rv = sm.parseDiskUsage("/dev/mapper/cl-root 52403200 12321848 40081352 aa% /", rootUsage, dataUsage, bootUsage);
252  REQUIRE(rv == -1);
253  REQUIRE(rootUsage == -1);
254  REQUIRE(dataUsage == -1);
255  REQUIRE(bootUsage == -1);
256  }
257  }
258 }
259 
260 SCENARIO( "System monitor is constructed and ram usage result is passed in", "[sysMonitor]" )
261 {
262  GIVEN("A default constructed system monitor object and an float for usage")
263  {
265  int rv;
266  float ramUsage = -1;
267 
268  // Fails with whitespace in front, but is this necessary to correct for?
269 
270  WHEN("Correct line is given")
271  {
272  rv = sm.parseRamUsage("Mem: 7714 1308 4550 288 1855 5807", ramUsage);
273  REQUIRE(rv == 0);
274  REQUIRE(ramUsage == (float) 1308/7714);
275  }
276 
277  WHEN("Correct line is given")
278  {
279  rv = sm.parseRamUsage("Mem: 7777 7700 4550 288 1855 5807", ramUsage);
280  REQUIRE(rv == 0);
281  REQUIRE(ramUsage == (float)7700/7777);
282  }
283 
284  WHEN("Blank line is given")
285  {
286  rv = sm.parseRamUsage("", ramUsage);
287  REQUIRE(rv == -1);
288  REQUIRE(ramUsage == -1);
289  }
290 
291  WHEN("Incorrect line is given")
292  {
293  rv = sm.parseRamUsage("Swap: 7935 0 7935", ramUsage);
294  REQUIRE(rv == -1);
295  REQUIRE(ramUsage == -1);
296  }
297 
298 
299  WHEN("Corrupted line is given")
300  {
301  rv = sm.parseRamUsage("Mem: 1308 7714 4550 288 1855 5807", ramUsage);
302  REQUIRE(rv == -1);
303  REQUIRE(ramUsage == -1);
304  }
305  }
306 }
307 
308 namespace SYSMONTEST
309 {
310 
312 {
313 
314 public:
315  sysMonitor_test(const std::string device)
316  {
317  m_configName = device;
318 
320  }
321 };
322 
323 SCENARIO( "INDI Callbacks", "[sysMonitor]" )
324 {
326 }
327 
328 }//namespace SYSMONTEST
#define GIVEN(desc)
Definition: catch.hpp:17763
#define WHEN(desc)
Definition: catch.hpp:17765
#define REQUIRE(...)
Definition: catch.hpp:17676
int parseDiskTemperature(std::string &driveName, float &temp, const std::string &line)
Parses string from system call to find drive temperatures.
Definition: sysMonitor.hpp:925
int parseCPUTemperatures(float &temp, const std::string &line)
Parses string from system call to find CPU temperatures.
Definition: sysMonitor.hpp:628
int parseCPULoads(float &, const std::string &)
Parses string from system call to find CPU usage loads.
Definition: sysMonitor.hpp:848
int parseDiskUsage(std::string, float &, float &, float &)
Parses string from system call to find drive usage space.
int parseRamUsage(std::string, float &)
Parses string from system call to find RAM usage.
sysMonitor_test(const std::string device)
#define XWCTEST_INDI_NEW_CALLBACK(testclass, propname)
Catch-2 tests for whether a NEW callback properly validates the input property properly.
SCENARIO("System monitor is constructed and CPU temperature results are passed in", "[sysMonitor]")
#define XWCTEST_SETUP_INDI_NEW_PROP(propname)