-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpmbus_driver_device_tests.cpp
817 lines (699 loc) · 26.2 KB
/
pmbus_driver_device_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/**
* Copyright © 2024 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mock_pmbus.hpp"
#include "mock_services.hpp"
#include "pmbus.hpp"
#include "pmbus_driver_device.hpp"
#include "rail.hpp"
#include "services.hpp"
#include "temporary_subdirectory.hpp"
#include <cstdint>
#include <exception>
#include <filesystem>
#include <format>
#include <fstream>
#include <map>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace phosphor::power::sequencer;
using namespace phosphor::pmbus;
using namespace phosphor::power::util;
namespace fs = std::filesystem;
using ::testing::Return;
using ::testing::Throw;
class PMBusDriverDeviceTests : public ::testing::Test
{
protected:
/**
* Constructor.
*
* Creates a temporary directory to use in simulating sysfs files.
*/
PMBusDriverDeviceTests() : ::testing::Test{}
{
tempDirPath = tempDir.getPath();
}
/**
* Creates a Rail object that checks for a pgood fault using STATUS_VOUT.
*
* @param name Unique name for the rail
* @param pageNum PMBus PAGE number of the rail
* @return Rail object
*/
std::unique_ptr<Rail> createRail(const std::string& name, uint8_t pageNum)
{
std::optional<std::string> presence{};
std::optional<uint8_t> page{pageNum};
bool isPowerSupplyRail{false};
bool checkStatusVout{true};
bool compareVoltageToLimit{false};
std::optional<GPIO> gpio{};
return std::make_unique<Rail>(name, presence, page, isPowerSupplyRail,
checkStatusVout, compareVoltageToLimit,
gpio);
}
/**
* Creates a file with the specified contents within the temporary
* directory.
*
* @param name File name
* @param contents File contents
*/
void createFile(const std::string& name, const std::string& contents = "")
{
fs::path path{tempDirPath / name};
std::ofstream out{path};
out << contents;
out.close();
}
/**
* Temporary subdirectory used to create simulated sysfs / hmmon files.
*/
TemporarySubDirectory tempDir;
/**
* Path to temporary subdirectory.
*/
fs::path tempDirPath;
};
TEST_F(PMBusDriverDeviceTests, Constructor)
{
// Test where works; optional parameters not specified
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
rails.emplace_back(createRail("VDD", 5));
rails.emplace_back(createRail("VIO", 7));
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
EXPECT_EQ(device.getName(), name);
EXPECT_EQ(device.getRails().size(), 2);
EXPECT_EQ(device.getRails()[0]->getName(), "VDD");
EXPECT_EQ(device.getRails()[1]->getName(), "VIO");
EXPECT_EQ(device.getBus(), bus);
EXPECT_EQ(device.getAddress(), address);
EXPECT_EQ(device.getDriverName(), "");
EXPECT_EQ(device.getInstance(), 0);
EXPECT_NE(&(device.getPMBusInterface()), nullptr);
}
// Test where works; optional parameters specified
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
rails.emplace_back(createRail("VDD", 5));
rails.emplace_back(createRail("VIO", 7));
uint8_t bus{3};
uint16_t address{0x72};
std::string driverName{"xyzdev"};
size_t instance{3};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address, driverName, instance};
EXPECT_EQ(device.getName(), name);
EXPECT_EQ(device.getRails().size(), 2);
EXPECT_EQ(device.getRails()[0]->getName(), "VDD");
EXPECT_EQ(device.getRails()[1]->getName(), "VIO");
EXPECT_EQ(device.getBus(), bus);
EXPECT_EQ(device.getAddress(), address);
EXPECT_EQ(device.getDriverName(), driverName);
EXPECT_EQ(device.getInstance(), instance);
EXPECT_NE(&(device.getPMBusInterface()), nullptr);
}
}
TEST_F(PMBusDriverDeviceTests, GetBus)
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{4};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus, address};
EXPECT_EQ(device.getBus(), bus);
}
TEST_F(PMBusDriverDeviceTests, GetAddress)
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0xab};
PMBusDriverDevice device{name, std::move(rails), services, bus, address};
EXPECT_EQ(device.getAddress(), address);
}
TEST_F(PMBusDriverDeviceTests, GetDriverName)
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
std::string driverName{"xyzdev"};
PMBusDriverDevice device{name, std::move(rails), services,
bus, address, driverName};
EXPECT_EQ(device.getDriverName(), driverName);
}
TEST_F(PMBusDriverDeviceTests, GetInstance)
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
std::string driverName{"xyzdev"};
size_t instance{3};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address, driverName, instance};
EXPECT_EQ(device.getInstance(), instance);
}
TEST_F(PMBusDriverDeviceTests, GetPMBusInterface)
{
MockServices services;
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus, address};
EXPECT_NE(&(device.getPMBusInterface()), nullptr);
}
TEST_F(PMBusDriverDeviceTests, GetGPIOValues)
{
// Test where works
{
MockServices services;
std::vector<int> gpioValues{1, 1, 1};
EXPECT_CALL(services, getGPIOValues("abc_382%#, zy"))
.Times(1)
.WillOnce(Return(gpioValues));
std::string name{"ABC_382%#, ZY"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
EXPECT_TRUE(device.getGPIOValues(services) == gpioValues);
}
// Test where fails with exception
{
MockServices services;
EXPECT_CALL(services, getGPIOValues("xyz_pseq"))
.Times(1)
.WillOnce(
Throw(std::runtime_error{"libgpiod: Unable to open chip"}));
std::string name{"XYZ_PSEQ"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
try
{
device.getGPIOValues(services);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(e.what(),
"Unable to read GPIO values from device XYZ_PSEQ "
"using label xyz_pseq: "
"libgpiod: Unable to open chip");
}
}
}
TEST_F(PMBusDriverDeviceTests, GetStatusWord)
{
// Test where works
{
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, read("status13", Type::Debug, true))
.Times(1)
.WillOnce(Return(0x1234));
uint8_t page{13};
EXPECT_EQ(device.getStatusWord(page), 0x1234);
}
// Test where fails with exception
{
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, read("status0", Type::Debug, true))
.Times(1)
.WillOnce(Throw(std::runtime_error{"File does not exist"}));
try
{
uint8_t page{0};
device.getStatusWord(page);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(
e.what(),
"Unable to read STATUS_WORD for PAGE 0 of device xyz_pseq: "
"File does not exist");
}
}
}
TEST_F(PMBusDriverDeviceTests, GetStatusVout)
{
// Test where works
{
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, read("status13_vout", Type::Debug, true))
.Times(1)
.WillOnce(Return(0xde));
uint8_t page{13};
EXPECT_EQ(device.getStatusVout(page), 0xde);
}
// Test where fails with exception
{
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, read("status0_vout", Type::Debug, true))
.Times(1)
.WillOnce(Throw(std::runtime_error{"File does not exist"}));
try
{
uint8_t page{0};
device.getStatusVout(page);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(
e.what(),
"Unable to read STATUS_VOUT for PAGE 0 of device xyz_pseq: "
"File does not exist");
}
}
}
TEST_F(PMBusDriverDeviceTests, GetReadVout)
{
// Test where works
{
// Create simulated hwmon voltage label file
createFile("in13_label"); // PAGE 9 -> file number 13
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in13_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout10")); // PAGE number 9 + 1
EXPECT_CALL(pmbus, readString("in13_input", Type::Hwmon))
.Times(1)
.WillOnce(Return("851"));
uint8_t page{9};
EXPECT_EQ(device.getReadVout(page), 0.851);
}
// Test where fails
{
// Create simulated hwmon voltage label file
createFile("in13_label"); // PAGE 8 -> file number 13
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in13_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout9")); // PAGE number 8 + 1
try
{
uint8_t page{9};
device.getReadVout(page);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(
e.what(),
"Unable to read READ_VOUT for PAGE 9 of device xyz_pseq: "
"Unable to find hwmon file number for PAGE 9 of device xyz_pseq");
}
}
}
TEST_F(PMBusDriverDeviceTests, GetVoutUVFaultLimit)
{
// Test where works
{
// Create simulated hwmon voltage label file
createFile("in1_label"); // PAGE 6 -> file number 1
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in1_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout7")); // PAGE number 6 + 1
EXPECT_CALL(pmbus, readString("in1_lcrit", Type::Hwmon))
.Times(1)
.WillOnce(Return("1329"));
uint8_t page{6};
EXPECT_EQ(device.getVoutUVFaultLimit(page), 1.329);
}
// Test where fails
{
// Create simulated hwmon voltage label file
createFile("in1_label"); // PAGE 7 -> file number 1
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in1_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout8")); // PAGE number 7 + 1
try
{
uint8_t page{6};
device.getVoutUVFaultLimit(page);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(
e.what(),
"Unable to read VOUT_UV_FAULT_LIMIT for PAGE 6 of device xyz_pseq: "
"Unable to find hwmon file number for PAGE 6 of device xyz_pseq");
}
}
}
TEST_F(PMBusDriverDeviceTests, GetPageToFileNumberMap)
{
// Test where works: No voltage label files/mappings found
{
// Create simulated hwmon files. None are valid voltage label files.
createFile("in1_input"); // Not a label file
createFile("in9_lcrit"); // Not a label file
createFile("in_label"); // Invalid voltage label file name
createFile("in9a_label"); // Invalid voltage label file name
createFile("fan3_label"); // Not a voltage label file
createFile("temp8_label"); // Not a voltage label file
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString).Times(0);
const std::map<uint8_t, unsigned int>& map =
device.getPageToFileNumberMap();
EXPECT_TRUE(map.empty());
}
// Test where works: Multiple voltage label files/mappings found
{
// Create simulated hwmon files
createFile("in9_label"); // PAGE 3 -> file number 9
createFile("in13_label"); // PAGE 7 -> file number 13
createFile("in0_label"); // PAGE 12 -> file number 0
createFile("in11_label"); // No mapping; invalid contents
createFile("in12_label"); // No mapping; invalid contents
createFile("in1_input"); // Not a label file
createFile("in7_lcrit"); // Not a label file
createFile("fan3_label"); // Not a voltage label file
createFile("temp8_label"); // Not a voltage label file
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in9_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout4")); // PAGE number 3 + 1
EXPECT_CALL(pmbus, readString("in13_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout8")); // PAGE number 7 + 1
EXPECT_CALL(pmbus, readString("in0_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout13")); // PAGE number 12 + 1
EXPECT_CALL(pmbus, readString("in11_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout")); // Invalid format
EXPECT_CALL(pmbus, readString("in12_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout13a")); // Invalid format
const std::map<uint8_t, unsigned int>& map =
device.getPageToFileNumberMap();
EXPECT_EQ(map.size(), 3);
EXPECT_EQ(map.at(uint8_t{3}), 9);
EXPECT_EQ(map.at(uint8_t{7}), 13);
EXPECT_EQ(map.at(uint8_t{12}), 0);
}
// Test where fails: hwmon directory path is actually a file
{
// Create file that will be returned as the hwmon directory path
createFile("in9_label");
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath / "in9_label"));
EXPECT_CALL(pmbus, readString).Times(0);
const std::map<uint8_t, unsigned int>& map =
device.getPageToFileNumberMap();
EXPECT_TRUE(map.empty());
}
// Test where fails: hwmon directory path does not exist
{
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath / "does_not_exist"));
EXPECT_CALL(pmbus, readString).Times(0);
const std::map<uint8_t, unsigned int>& map =
device.getPageToFileNumberMap();
EXPECT_TRUE(map.empty());
}
// Test where fails: hwmon directory path is not readable
{
// Create simulated hwmon files
createFile("in9_label");
createFile("in13_label");
createFile("in0_label");
// Change temporary directory to be unreadable
fs::permissions(tempDirPath, fs::perms::none);
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString).Times(0);
try
{
device.getPageToFileNumberMap();
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
// Error message varies
}
// Change temporary directory to be readable/writable
fs::permissions(tempDirPath, fs::perms::owner_all);
}
}
TEST_F(PMBusDriverDeviceTests, GetFileNumber)
{
// Test where works
{
// Create simulated hwmon voltage label files
createFile("in0_label"); // PAGE 6 -> file number 0
createFile("in13_label"); // PAGE 9 -> file number 13
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in0_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout7")); // PAGE number 6 + 1
EXPECT_CALL(pmbus, readString("in13_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout10")); // PAGE number 9 + 1
// Map was empty and needs to be built
uint8_t page{6};
EXPECT_EQ(device.getFileNumber(page), 0);
// Map had already been built
page = 9;
EXPECT_EQ(device.getFileNumber(page), 13);
}
// Test where fails: No mapping for specified PMBus PAGE
{
// Create simulated hwmon voltage label files
createFile("in0_label"); // PAGE 6 -> file number 0
createFile("in13_label"); // PAGE 9 -> file number 13
MockServices services;
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus,
address};
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(1)
.WillOnce(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in0_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout7")); // PAGE number 6 + 1
EXPECT_CALL(pmbus, readString("in13_label", Type::Hwmon))
.Times(1)
.WillOnce(Return("vout10")); // PAGE number 9 + 1
try
{
uint8_t page{13};
device.getFileNumber(page);
ADD_FAILURE() << "Should not have reached this line.";
}
catch (const std::exception& e)
{
EXPECT_STREQ(
e.what(),
"Unable to find hwmon file number for PAGE 13 of device xyz_pseq");
}
}
}
TEST_F(PMBusDriverDeviceTests, PrepareForPgoodFaultDetection)
{
// This is a protected method and cannot be called directly from a gtest.
// Call findPgoodFault() which calls prepareForPgoodFaultDetection().
// Create simulated hwmon voltage label file
createFile("in1_label"); // PAGE 6 -> file number 1
MockServices services;
std::vector<int> gpioValues{1, 1, 1};
EXPECT_CALL(services, getGPIOValues("xyz_pseq"))
.Times(1)
.WillOnce(Return(gpioValues));
std::string name{"xyz_pseq"};
std::vector<std::unique_ptr<Rail>> rails;
uint8_t bus{3};
uint16_t address{0x72};
PMBusDriverDevice device{name, std::move(rails), services, bus, address};
// Methods that get hwmon file info should be called twice
MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
EXPECT_CALL(pmbus, getPath(Type::Hwmon))
.Times(2)
.WillRepeatedly(Return(tempDirPath));
EXPECT_CALL(pmbus, readString("in1_label", Type::Hwmon))
.Times(2)
.WillRepeatedly(Return("vout7")); // PAGE number 6 + 1
// Map was empty and needs to be built
uint8_t page{6};
EXPECT_EQ(device.getFileNumber(page), 1);
// Call findPgoodFault() which calls prepareForPgoodFaultDetection() which
// rebuilds the map.
std::string powerSupplyError{};
std::map<std::string, std::string> additionalData{};
std::string error =
device.findPgoodFault(services, powerSupplyError, additionalData);
EXPECT_TRUE(error.empty());
EXPECT_EQ(additionalData.size(), 0);
EXPECT_EQ(device.getFileNumber(page), 1);
}