blob: a4f8befc597242d1d264a61cd5f4e8161b04539b [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinler89fa0822019-07-17 13:54:30 -050016#include "extensions/openpower-pels/paths.hpp"
17#include "extensions/openpower-pels/repository.hpp"
18#include "pel_utils.hpp"
19
20#include <ext/stdio_filebuf.h>
21
22#include <filesystem>
23
24#include <gtest/gtest.h>
25
26using namespace openpower::pels;
27namespace fs = std::filesystem;
28
29/**
30 * Clean the Repo after every testcase.
31 * And because we have PEL object, also clean up
32 * the log ID.
33 */
34class RepositoryTest : public CleanLogID
35{
36 protected:
37 void SetUp() override
38 {
39 repoPath = getPELRepoPath();
40 }
41
42 void TearDown() override
43 {
44 fs::remove_all(repoPath);
45 }
46
47 fs::path repoPath;
48};
49
50TEST_F(RepositoryTest, FilenameTest)
51{
52 BCDTime date = {0x20, 0x30, 0x11, 0x28, 0x13, 0x6, 0x7, 0x8};
53
54 EXPECT_EQ(Repository::getPELFilename(0x12345678, date),
55 "2030112813060708_12345678");
56
57 EXPECT_EQ(Repository::getPELFilename(0xAABBCCDD, date),
58 "2030112813060708_AABBCCDD");
59
60 EXPECT_EQ(Repository::getPELFilename(0x3AFF1, date),
61 "2030112813060708_0003AFF1");
62
63 EXPECT_EQ(Repository::getPELFilename(100, date),
64 "2030112813060708_00000064");
65
66 EXPECT_EQ(Repository::getPELFilename(0, date), "2030112813060708_00000000");
67}
68
69TEST_F(RepositoryTest, AddTest)
70{
71 Repository repo{repoPath};
Matt Spinler42828bd2019-10-11 10:39:30 -050072 auto data = pelDataFactory(TestPELType::pelSimple);
73 auto pel = std::make_unique<PEL>(data);
Matt Spinler89fa0822019-07-17 13:54:30 -050074
75 repo.add(pel);
76
77 // Check that the PEL was stored where it was supposed to be,
78 // and that it wrote the PEL data.
Matt Spinler97d19b42019-10-29 11:34:03 -050079 const auto ts = pel->privateHeader().commitTimestamp();
Matt Spinler89fa0822019-07-17 13:54:30 -050080 auto name = Repository::getPELFilename(pel->id(), ts);
81
82 fs::path file = repoPath / "logs" / name;
83 EXPECT_TRUE(fs::exists(file));
84
85 auto newData = readPELFile(file);
86 auto pelData = pel->data();
87 EXPECT_EQ(*newData, pelData);
88}
Matt Spinler475e5742019-07-18 16:09:49 -050089
90TEST_F(RepositoryTest, RestoreTest)
91{
92 using pelID = Repository::LogID::Pel;
93 using obmcID = Repository::LogID::Obmc;
94
95 std::vector<Repository::LogID> ids;
96
97 {
98 Repository repo{repoPath};
99
100 // Add some PELs to the repository
101 {
Matt Spinler42828bd2019-10-11 10:39:30 -0500102 auto data = pelDataFactory(TestPELType::pelSimple);
103 auto pel = std::make_unique<PEL>(data, 1);
Matt Spinler475e5742019-07-18 16:09:49 -0500104 pel->assignID();
105 repo.add(pel);
106 ids.emplace_back(pelID(pel->id()), obmcID(1));
107 }
108 {
Matt Spinler42828bd2019-10-11 10:39:30 -0500109 auto data = pelDataFactory(TestPELType::pelSimple);
110 auto pel = std::make_unique<PEL>(data, 2);
Matt Spinler475e5742019-07-18 16:09:49 -0500111 pel->assignID();
112 repo.add(pel);
113 ids.emplace_back(pelID(pel->id()), obmcID(2));
114 }
115
116 // Check they're there
117 EXPECT_TRUE(repo.hasPEL(ids[0]));
118 EXPECT_TRUE(repo.hasPEL(ids[1]));
119
120 // Do some other search tests while we're here.
121
122 // Search based on PEL ID
123 Repository::LogID id(pelID(ids[0].pelID));
124 EXPECT_TRUE(repo.hasPEL(id));
125
126 // Search based on OBMC log ID
127 id.pelID.id = 0;
128 id.obmcID = ids[0].obmcID;
129 EXPECT_TRUE(repo.hasPEL(id));
130
131 // ... based on the other PEL ID
132 id.pelID = ids[1].pelID;
133 id.obmcID.id = 0;
134 EXPECT_TRUE(repo.hasPEL(id));
135
136 // Not found
137 id.pelID.id = 99;
138 id.obmcID.id = 100;
139 EXPECT_FALSE(repo.hasPEL(id));
140 }
141
142 {
143 // Restore and check they're still there, then
144 // remove them.
145 Repository repo{repoPath};
146 EXPECT_TRUE(repo.hasPEL(ids[0]));
147 EXPECT_TRUE(repo.hasPEL(ids[1]));
148
149 repo.remove(ids[0]);
150 EXPECT_FALSE(repo.hasPEL(ids[0]));
151
152 repo.remove(ids[1]);
153 EXPECT_FALSE(repo.hasPEL(ids[1]));
154 }
155}
Matt Spinler2813f362019-07-19 12:45:28 -0500156
157TEST_F(RepositoryTest, TestGetPELData)
158{
159 using ID = Repository::LogID;
160 Repository repo{repoPath};
161
162 ID badID{ID::Pel(42)};
163 auto noData = repo.getPELData(badID);
164 EXPECT_FALSE(noData);
165
166 // Add a PEL to the repo, and get the data back with getPELData.
Matt Spinler42828bd2019-10-11 10:39:30 -0500167 auto data = pelDataFactory(TestPELType::pelSimple);
168 auto dataCopy = data;
169 auto pel = std::make_unique<PEL>(data);
Matt Spinler2813f362019-07-19 12:45:28 -0500170 auto pelID = pel->id();
171 repo.add(pel);
172
173 ID id{ID::Pel(pelID)};
174 auto pelData = repo.getPELData(id);
175
176 ASSERT_TRUE(pelData);
177 EXPECT_EQ(dataCopy, *pelData);
178}
Matt Spinler1ea78802019-11-01 13:04:59 -0500179
180TEST_F(RepositoryTest, TestForEach)
181{
182 Repository repo{repoPath};
183
184 // Add 2 PELs
185 auto data = pelDataFactory(TestPELType::pelSimple);
186 auto pel = std::make_unique<PEL>(data);
187 repo.add(pel);
188
189 pel = std::make_unique<PEL>(data);
190 pel->assignID();
191 pel->setCommitTime();
192 repo.add(pel);
193
194 // Make a function that saves the IDs
195 std::vector<uint32_t> ids;
196 Repository::ForEachFunc f1 = [&ids](const PEL& pel) {
197 ids.push_back(pel.id());
198 return false;
199 };
200
201 repo.for_each(f1);
202
203 EXPECT_EQ(ids.size(), 2);
204
205 // Stop after the first time in.
206 Repository::ForEachFunc f2 = [&ids](const PEL& pel) {
207 ids.push_back(pel.id());
208 return true;
209 };
210
211 ids.clear();
212 repo.for_each(f2);
213 EXPECT_EQ(ids.size(), 1);
214}
Matt Spinler421f6532019-11-06 15:40:45 -0600215
216TEST_F(RepositoryTest, TestSubscriptions)
217{
218 std::vector<uint32_t> added;
219 std::vector<uint32_t> removed;
220
221 Repository::AddCallback ac = [&added](const PEL& pel) {
222 added.push_back(pel.id());
223 };
224
225 Repository::DeleteCallback dc = [&removed](uint32_t id) {
226 removed.push_back(id);
227 };
228
229 Repository repo{repoPath};
230 repo.subscribeToAdds("test", ac);
231 repo.subscribeToDeletes("test", dc);
232
233 auto data = pelDataFactory(TestPELType::pelSimple);
234 auto pel = std::make_unique<PEL>(data);
235 auto pelID = pel->id();
236 repo.add(pel);
237
238 EXPECT_EQ(added.size(), 1);
239
240 using ID = Repository::LogID;
241 ID id{ID::Pel(pelID)};
242 repo.remove(id);
243
244 EXPECT_EQ(removed.size(), 1);
245
246 repo.unsubscribeFromAdds("test");
247 repo.unsubscribeFromDeletes("test");
248
249 added.clear();
250 removed.clear();
251
252 repo.add(pel);
253 EXPECT_EQ(added.size(), 0);
254
255 repo.remove(id);
256 EXPECT_EQ(removed.size(), 0);
257}
Matt Spinler0ff00482019-11-06 16:19:46 -0600258
259TEST_F(RepositoryTest, TestGetAttributes)
260{
261 uint32_t pelID = 0;
262 std::bitset<16> actionFlags;
263
264 {
265 Repository repo{repoPath};
266
267 // Add a PEL to the repo
268 auto data = pelDataFactory(TestPELType::pelSimple);
269 auto pel = std::make_unique<PEL>(data);
270 repo.add(pel);
271
272 pelID = pel->id();
273 actionFlags = pel->userHeader().actionFlags();
274
275 using ID = Repository::LogID;
276 ID id{ID::Pel(pelID)};
277
278 auto a = repo.getPELAttributes(id);
279 EXPECT_TRUE(a);
280 EXPECT_EQ((*a).get().actionFlags, actionFlags);
281
282 id.pelID.id = 0;
283 a = repo.getPELAttributes(id);
284 EXPECT_FALSE(a);
285 }
286
287 {
288 // Restore the repository and check again
289 Repository repo{repoPath};
290
291 using ID = Repository::LogID;
292 ID id{ID::Pel(pelID)};
293
294 auto a = repo.getPELAttributes(id);
295 EXPECT_TRUE(a);
296 EXPECT_EQ((*a).get().actionFlags, actionFlags);
297
298 id.pelID.id = 0;
299 a = repo.getPELAttributes(id);
300 EXPECT_FALSE(a);
301 }
302}
Matt Spinler29d18c12019-11-21 13:31:27 -0600303
304TEST_F(RepositoryTest, TestSetHostState)
305{
306 // Add a PEL to the repo
307 auto data = pelDataFactory(TestPELType::pelSimple);
308 auto pel = std::make_unique<PEL>(data);
309 using ID = Repository::LogID;
310 ID id{ID::Pel(pel->id())};
311
312 {
313 Repository repo{repoPath};
314
315 repo.add(pel);
316
317 auto a = repo.getPELAttributes(id);
318 EXPECT_EQ((*a).get().hostState, TransmissionState::newPEL);
319
320 repo.setPELHostTransState(pel->id(), TransmissionState::acked);
321
322 // First, check the attributes
323 a = repo.getPELAttributes(id);
324 EXPECT_EQ((*a).get().hostState, TransmissionState::acked);
325
326 // Next, check the PEL data itself
327 auto pelData = repo.getPELData(id);
328 PEL newPEL{*pelData};
329 EXPECT_EQ(newPEL.hostTransmissionState(), TransmissionState::acked);
330 }
331
332 {
333 // Now restore, and check again
334 Repository repo{repoPath};
335
336 // First, check the attributes
337 auto a = repo.getPELAttributes(id);
338 EXPECT_EQ((*a).get().hostState, TransmissionState::acked);
339
340 // Next, check the PEL data itself
341 auto pelData = repo.getPELData(id);
342 PEL newPEL{*pelData};
343 EXPECT_EQ(newPEL.hostTransmissionState(), TransmissionState::acked);
344 }
345}
346
347TEST_F(RepositoryTest, TestSetHMCState)
348{
349 // Add a PEL to the repo
350 auto data = pelDataFactory(TestPELType::pelSimple);
351 auto pel = std::make_unique<PEL>(data);
352 using ID = Repository::LogID;
353 ID id{ID::Pel(pel->id())};
354
355 {
356 Repository repo{repoPath};
357
358 repo.add(pel);
359
360 auto a = repo.getPELAttributes(id);
361 EXPECT_EQ((*a).get().hmcState, TransmissionState::newPEL);
362
363 repo.setPELHMCTransState(pel->id(), TransmissionState::acked);
364
365 // First, check the attributes
366 a = repo.getPELAttributes(id);
367 EXPECT_EQ((*a).get().hmcState, TransmissionState::acked);
368
369 // Next, check the PEL data itself
370 auto pelData = repo.getPELData(id);
371 PEL newPEL{*pelData};
372 EXPECT_EQ(newPEL.hmcTransmissionState(), TransmissionState::acked);
373 }
374
375 {
376 // Now restore, and check again
377 Repository repo{repoPath};
378
379 // First, check the attributes
380 auto a = repo.getPELAttributes(id);
381 EXPECT_EQ((*a).get().hmcState, TransmissionState::acked);
382
383 // Next, check the PEL data itself
384 auto pelData = repo.getPELData(id);
385 PEL newPEL{*pelData};
386 EXPECT_EQ(newPEL.hmcTransmissionState(), TransmissionState::acked);
387 }
388}
Matt Spinler6d512242019-12-09 13:44:17 -0600389
390TEST_F(RepositoryTest, TestGetPELFD)
391{
392 Repository repo{repoPath};
393
394 auto data = pelDataFactory(TestPELType::pelSimple);
395 auto pel = std::make_unique<PEL>(data);
396 pel->setCommitTime();
397 pel->assignID();
398
399 repo.add(pel);
400
401 using ID = Repository::LogID;
402 ID id{ID::Pel(pel->id())};
403
404 auto fd = repo.getPELFD(id);
405
406 EXPECT_TRUE(fd);
407
408 // Get the size
409 struct stat s;
410 int r = fstat(*fd, &s);
411 ASSERT_EQ(r, 0);
412
413 auto size = s.st_size;
414
415 // Read the PEL data out of the FD
416 FILE* fp = fdopen(*fd, "r");
417 ASSERT_NE(fp, nullptr);
418
419 std::vector<uint8_t> newData;
420 newData.resize(size);
421 r = fread(newData.data(), 1, size, fp);
422 EXPECT_EQ(r, size);
423
424 PEL newPEL{newData};
425
426 EXPECT_TRUE(newPEL.valid());
427 EXPECT_EQ(newPEL.id(), pel->id());
428
429 fclose(fp);
430
431 // Call getPELFD again, this time with a bad ID
432 id.pelID.id = 42;
433 fd = repo.getPELFD(id);
434
435 EXPECT_FALSE(fd);
436}
Matt Spinlerb188f782020-07-07 11:18:12 -0500437
438// Test the repo size statistics
439TEST_F(RepositoryTest, TestRepoSizes)
440{
441 uint32_t id = 1;
442
443 Repository repo{repoPath, 10000, 500};
444
445 // All of the size stats are the sizes on disk a PEL takes up,
446 // which is different than the file size. Disk usage seems
447 // to have a granularity of 4096 bytes. This probably shouldn't
448 // be hardcoded, but I don't know how to look it up dynamically.
449
450 // All sizes are zero
451 {
452 const auto& stats = repo.getSizeStats();
453 EXPECT_EQ(stats.total, 0);
454 EXPECT_EQ(stats.bmc, 0);
455 EXPECT_EQ(stats.nonBMC, 0);
456 EXPECT_EQ(stats.bmcServiceable, 0);
457 EXPECT_EQ(stats.bmcInfo, 0);
458 EXPECT_EQ(stats.nonBMCServiceable, 0);
459 EXPECT_EQ(stats.nonBMCInfo, 0);
460 }
461
462 // Add a 2000B BMC predictive error
463 auto data = pelFactory(id++, 'O', 0x20, 0x8800, 2000);
464 auto pel = std::make_unique<PEL>(data);
465 auto pelID1 = pel->id();
466 repo.add(pel);
467
468 {
469 const auto& stats = repo.getSizeStats();
470 EXPECT_EQ(stats.total, 4096);
471 EXPECT_EQ(stats.bmc, 4096);
472 EXPECT_EQ(stats.nonBMC, 0);
473 EXPECT_EQ(stats.bmcServiceable, 4096);
474 EXPECT_EQ(stats.bmcInfo, 0);
475 EXPECT_EQ(stats.nonBMCServiceable, 0);
476 EXPECT_EQ(stats.nonBMCInfo, 0);
477 }
478
479 // Add a 5000B BMC informational error
480 data = pelFactory(id++, 'O', 0x00, 0x8800, 5000);
481 pel = std::make_unique<PEL>(data);
482 auto pelID2 = pel->id();
483 repo.add(pel);
484
485 {
486 const auto& stats = repo.getSizeStats();
487 EXPECT_EQ(stats.total, 4096 + 8192);
488 EXPECT_EQ(stats.bmc, 4096 + 8192);
489 EXPECT_EQ(stats.nonBMC, 0);
490 EXPECT_EQ(stats.bmcServiceable, 4096);
491 EXPECT_EQ(stats.bmcInfo, 8192);
492 EXPECT_EQ(stats.nonBMCServiceable, 0);
493 EXPECT_EQ(stats.nonBMCInfo, 0);
494 }
495
496 // Add a 4000B Hostboot unrecoverable error
497 data = pelFactory(id++, 'B', 0x40, 0x8800, 4000);
498 pel = std::make_unique<PEL>(data);
499 auto pelID3 = pel->id();
500 repo.add(pel);
501
502 {
503 const auto& stats = repo.getSizeStats();
504 EXPECT_EQ(stats.total, 4096 + 8192 + 4096);
505 EXPECT_EQ(stats.bmc, 4096 + 8192);
506 EXPECT_EQ(stats.nonBMC, 4096);
507 EXPECT_EQ(stats.bmcServiceable, 4096);
508 EXPECT_EQ(stats.bmcInfo, 8192);
509 EXPECT_EQ(stats.nonBMCServiceable, 4096);
510 EXPECT_EQ(stats.nonBMCInfo, 0);
511 }
512
513 // Add a 5000B Hostboot informational error
514 data = pelFactory(id++, 'B', 0x00, 0x8800, 5000);
515 pel = std::make_unique<PEL>(data);
516 auto pelID4 = pel->id();
517 repo.add(pel);
518
519 {
520 const auto& stats = repo.getSizeStats();
521 EXPECT_EQ(stats.total, 4096 + 8192 + 4096 + 8192);
522 EXPECT_EQ(stats.bmc, 4096 + 8192);
523 EXPECT_EQ(stats.nonBMC, 4096 + 8192);
524 EXPECT_EQ(stats.bmcServiceable, 4096);
525 EXPECT_EQ(stats.bmcInfo, 8192);
526 EXPECT_EQ(stats.nonBMCServiceable, 4096);
527 EXPECT_EQ(stats.nonBMCInfo, 8192);
528 }
529
530 // Remove the BMC serviceable error
531 using ID = Repository::LogID;
532 ID id1{ID::Pel(pelID1)};
533
534 repo.remove(id1);
535 {
536 const auto& stats = repo.getSizeStats();
537 EXPECT_EQ(stats.total, 8192 + 4096 + 8192);
538 EXPECT_EQ(stats.bmc, 8192);
539 EXPECT_EQ(stats.nonBMC, 4096 + 8192);
540 EXPECT_EQ(stats.bmcServiceable, 0);
541 EXPECT_EQ(stats.bmcInfo, 8192);
542 EXPECT_EQ(stats.nonBMCServiceable, 4096);
543 EXPECT_EQ(stats.nonBMCInfo, 8192);
544 }
545
546 // Remove the Hostboot informational error
547 ID id4{ID::Pel(pelID4)};
548
549 repo.remove(id4);
550 {
551 const auto& stats = repo.getSizeStats();
552 EXPECT_EQ(stats.total, 8192 + 4096);
553 EXPECT_EQ(stats.bmc, 8192);
554 EXPECT_EQ(stats.nonBMC, 4096);
555 EXPECT_EQ(stats.bmcServiceable, 0);
556 EXPECT_EQ(stats.bmcInfo, 8192);
557 EXPECT_EQ(stats.nonBMCServiceable, 4096);
558 EXPECT_EQ(stats.nonBMCInfo, 0);
559 }
560
561 // Remove the BMC informational error
562 ID id2{ID::Pel(pelID2)};
563
564 repo.remove(id2);
565 {
566 const auto& stats = repo.getSizeStats();
567 EXPECT_EQ(stats.total, 4096);
568 EXPECT_EQ(stats.bmc, 0);
569 EXPECT_EQ(stats.nonBMC, 4096);
570 EXPECT_EQ(stats.bmcServiceable, 0);
571 EXPECT_EQ(stats.bmcInfo, 0);
572 EXPECT_EQ(stats.nonBMCServiceable, 4096);
573 EXPECT_EQ(stats.nonBMCInfo, 0);
574 }
575
576 // Remove the hostboot unrecoverable error
577 ID id3{ID::Pel(pelID3)};
578
579 repo.remove(id3);
580 {
581 const auto& stats = repo.getSizeStats();
582 EXPECT_EQ(stats.total, 0);
583 EXPECT_EQ(stats.bmc, 0);
584 EXPECT_EQ(stats.nonBMC, 0);
585 EXPECT_EQ(stats.bmcServiceable, 0);
586 EXPECT_EQ(stats.bmcInfo, 0);
587 EXPECT_EQ(stats.nonBMCServiceable, 0);
588 EXPECT_EQ(stats.nonBMCInfo, 0);
589 }
590}
Matt Spinlerb0a8df52020-07-07 14:41:06 -0500591
592// Prune PELs, when no HMC/OS/PHYP acks
593TEST_F(RepositoryTest, TestPruneNoAcks)
594{
595 Repository repo{repoPath, 4096 * 20, 100};
596
597 // Add 10 4096B (on disk) PELs of BMC nonInfo, Info and nonBMC info,
598 // nonInfo errors. None of them acked by PHYP, host, or HMC.
599 for (uint32_t i = 1; i <= 10; i++)
600 {
601 // BMC predictive
602 auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
603 auto pel = std::make_unique<PEL>(data);
604 repo.add(pel);
605
606 // BMC info
607 data = pelFactory(i + 100, 'O', 0x0, 0x8800, 500);
608 pel = std::make_unique<PEL>(data);
609 repo.add(pel);
610
611 // Hostboot predictive
612 data = pelFactory(i + 200, 'B', 0x20, 0x8800, 500);
613 pel = std::make_unique<PEL>(data);
614 repo.add(pel);
615
616 // Hostboot info
617 data = pelFactory(i + 300, 'B', 0x0, 0x8800, 500);
618 pel = std::make_unique<PEL>(data);
619 repo.add(pel);
620 }
621
622 const auto& sizes = repo.getSizeStats();
623 EXPECT_EQ(sizes.total, 4096 * 40);
624
625 // Sanity check the very first PELs with IDs 1 to 4 are
626 // there so we can check they are removed after the prune.
627 for (uint32_t i = 1; i < 5; i++)
628 {
629 Repository::LogID id{Repository::LogID::Pel{i}};
630 EXPECT_TRUE(repo.getPELAttributes(id));
631 }
632
633 // Prune down to 15%/30%/15%/30% = 90% total
634 auto IDs = repo.prune();
635
636 // Check the final sizes
637 EXPECT_EQ(sizes.total, 4096 * 18); // 90% of 20 PELs
638 EXPECT_EQ(sizes.bmcInfo, 4096 * 3); // 15% of 20 PELs
639 EXPECT_EQ(sizes.bmcServiceable, 4096 * 6); // 30% of 20 PELs
640 EXPECT_EQ(sizes.nonBMCInfo, 4096 * 3); // 15% of 20 PELs
641 EXPECT_EQ(sizes.nonBMCServiceable, 4096 * 6); // 30% of 20 PELs
642
643 // Check that at least the 4 oldest, which are the oldest of
644 // each type, were removed.
645 for (uint32_t i = 1; i < 5; i++)
646 {
647 Repository::LogID id{Repository::LogID::Pel{i}};
648 EXPECT_FALSE(repo.getPELAttributes(id));
649
650 // Make sure the corresponding OpenBMC event log ID which is
651 // 500 + the PEL ID is in the list.
652 EXPECT_TRUE(std::find(IDs.begin(), IDs.end(), 500 + i) != IDs.end());
653 }
654}
655
656// Test that if filled completely with 1 type of PEL, that
657// pruning still works properly
658TEST_F(RepositoryTest, TestPruneInfoOnly)
659{
660 Repository repo{repoPath, 4096 * 22, 100};
661
662 // Fill 4096*23 bytes on disk of BMC info PELs
663 for (uint32_t i = 1; i <= 23; i++)
664 {
665 auto data = pelFactory(i, 'O', 0, 0x8800, 1000);
666 auto pel = std::make_unique<PEL>(data);
667 repo.add(pel);
668 }
669
670 const auto& sizes = repo.getSizeStats();
671 EXPECT_EQ(sizes.total, 4096 * 23);
672
673 // Pruning to 15% of 4096 * 22 will leave 3 4096B PELs.
674
675 // Sanity check the oldest 20 are there so when they
676 // get pruned below we'll know they were removed.
677 for (uint32_t i = 1; i <= 20; i++)
678 {
679 Repository::LogID id{Repository::LogID::Pel{i}};
680 EXPECT_TRUE(repo.getPELAttributes(id));
681 }
682
683 auto IDs = repo.prune();
684
685 // Check the final sizes
686 EXPECT_EQ(sizes.total, 4096 * 3);
687 EXPECT_EQ(sizes.bmcInfo, 4096 * 3);
688 EXPECT_EQ(sizes.bmcServiceable, 0);
689 EXPECT_EQ(sizes.nonBMCInfo, 0);
690 EXPECT_EQ(sizes.nonBMCServiceable, 0);
691
692 EXPECT_EQ(IDs.size(), 20);
693
694 // Can no longer find the oldest 20 PELs.
695 for (uint32_t i = 1; i <= 20; i++)
696 {
697 Repository::LogID id{Repository::LogID::Pel{i}};
698 EXPECT_FALSE(repo.getPELAttributes(id));
699 EXPECT_TRUE(std::find(IDs.begin(), IDs.end(), 500 + i) != IDs.end());
700 }
701}
702
703// Test that the HMC/OS/PHYP ack values affect the
704// pruning order.
705TEST_F(RepositoryTest, TestPruneWithAcks)
706{
707 Repository repo{repoPath, 4096 * 20, 100};
708
709 // Fill 30% worth of BMC non-info non-acked PELs
710 for (uint32_t i = 1; i <= 6; i++)
711 {
712 // BMC predictive
713 auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
714 auto pel = std::make_unique<PEL>(data);
715 repo.add(pel);
716 }
717
718 // Add another PEL to push it over the 30%, each time adding
719 // a different type that should be pruned before the above ones
720 // even though those are older.
721 for (uint32_t i = 1; i <= 3; i++)
722 {
723 auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
724 auto pel = std::make_unique<PEL>(data);
725 auto idToDelete = pel->obmcLogID();
726 repo.add(pel);
727
728 if (0 == i)
729 {
730 repo.setPELHMCTransState(pel->id(), TransmissionState::acked);
731 }
732 else if (1 == i)
733 {
734 repo.setPELHostTransState(pel->id(), TransmissionState::acked);
735 }
736 else
737 {
738 repo.setPELHostTransState(pel->id(), TransmissionState::sent);
739 }
740
741 auto IDs = repo.prune();
742 EXPECT_EQ(repo.getSizeStats().total, 4096 * 6);
743
744 // The newest PEL should be the one deleted
745 ASSERT_EQ(IDs.size(), 1);
746 EXPECT_EQ(IDs[0], idToDelete);
747 }
748}
749
750// Test that the total number of PELs limit is enforced.
751TEST_F(RepositoryTest, TestPruneTooManyPELs)
752{
753 Repository repo{repoPath, 4096 * 100, 10};
754
755 // Add 10, which is the limit and is still OK
756 for (uint32_t i = 1; i <= 10; i++)
757 {
758 auto data = pelFactory(i, 'O', 0x20, 0x8800, 500);
759 auto pel = std::make_unique<PEL>(data);
760 repo.add(pel);
761 }
762
763 auto IDs = repo.prune();
764
765 // Nothing pruned yet
766 EXPECT_TRUE(IDs.empty());
767
768 // Add 1 more PEL which will be too many.
769 {
770 auto data = pelFactory(11, 'O', 0x20, 0x8800, 500);
771 auto pel = std::make_unique<PEL>(data);
772 repo.add(pel);
773 }
774
775 // Now that's it's over the limit of 10, it will bring it down
776 // to 80%, which is 8 after it removes 3.
777 IDs = repo.prune();
778 EXPECT_EQ(repo.getSizeStats().total, 4096 * 8);
779 ASSERT_EQ(IDs.size(), 3);
780
781 // Check that it deleted the oldest ones.
782 // The OpenBMC log ID is the PEL ID + 500.
783 EXPECT_EQ(IDs[0], 500 + 1);
784 EXPECT_EQ(IDs[1], 500 + 2);
785 EXPECT_EQ(IDs[2], 500 + 3);
786}