1 #include "extensions/openpower-pels/paths.hpp"
2 #include "extensions/openpower-pels/repository.hpp"
3 #include "pel_utils.hpp"
4 
5 #include <ext/stdio_filebuf.h>
6 
7 #include <filesystem>
8 
9 #include <gtest/gtest.h>
10 
11 using namespace openpower::pels;
12 namespace fs = std::filesystem;
13 
14 /**
15  * Clean the Repo after every testcase.
16  * And because we have PEL object, also clean up
17  * the log ID.
18  */
19 class RepositoryTest : public CleanLogID
20 {
21   protected:
22     void SetUp() override
23     {
24         repoPath = getPELRepoPath();
25     }
26 
27     void TearDown() override
28     {
29         fs::remove_all(repoPath);
30     }
31 
32     fs::path repoPath;
33 };
34 
35 TEST_F(RepositoryTest, FilenameTest)
36 {
37     BCDTime date = {0x20, 0x30, 0x11, 0x28, 0x13, 0x6, 0x7, 0x8};
38 
39     EXPECT_EQ(Repository::getPELFilename(0x12345678, date),
40               "2030112813060708_12345678");
41 
42     EXPECT_EQ(Repository::getPELFilename(0xAABBCCDD, date),
43               "2030112813060708_AABBCCDD");
44 
45     EXPECT_EQ(Repository::getPELFilename(0x3AFF1, date),
46               "2030112813060708_0003AFF1");
47 
48     EXPECT_EQ(Repository::getPELFilename(100, date),
49               "2030112813060708_00000064");
50 
51     EXPECT_EQ(Repository::getPELFilename(0, date), "2030112813060708_00000000");
52 }
53 
54 TEST_F(RepositoryTest, AddTest)
55 {
56     Repository repo{repoPath};
57     auto data = pelDataFactory(TestPelType::pelSimple);
58     auto pel = std::make_unique<PEL>(*data);
59 
60     repo.add(pel);
61 
62     // Check that the PEL was stored where it was supposed to be,
63     // and that it wrote the PEL data.
64     const auto& ts = pel->privateHeader()->commitTimestamp();
65     auto name = Repository::getPELFilename(pel->id(), ts);
66 
67     fs::path file = repoPath / "logs" / name;
68     EXPECT_TRUE(fs::exists(file));
69 
70     auto newData = readPELFile(file);
71     auto pelData = pel->data();
72     EXPECT_EQ(*newData, pelData);
73 }
74 
75 TEST_F(RepositoryTest, RestoreTest)
76 {
77     using pelID = Repository::LogID::Pel;
78     using obmcID = Repository::LogID::Obmc;
79 
80     std::vector<Repository::LogID> ids;
81 
82     {
83         Repository repo{repoPath};
84 
85         // Add some PELs to the repository
86         {
87             auto data = pelDataFactory(TestPelType::pelSimple);
88             auto pel = std::make_unique<PEL>(*data, 1);
89             pel->assignID();
90             repo.add(pel);
91             ids.emplace_back(pelID(pel->id()), obmcID(1));
92         }
93         {
94             auto data = pelDataFactory(TestPelType::pelSimple);
95             auto pel = std::make_unique<PEL>(*data, 2);
96             pel->assignID();
97             repo.add(pel);
98             ids.emplace_back(pelID(pel->id()), obmcID(2));
99         }
100 
101         // Check they're there
102         EXPECT_TRUE(repo.hasPEL(ids[0]));
103         EXPECT_TRUE(repo.hasPEL(ids[1]));
104 
105         // Do some other search tests while we're here.
106 
107         // Search based on PEL ID
108         Repository::LogID id(pelID(ids[0].pelID));
109         EXPECT_TRUE(repo.hasPEL(id));
110 
111         // Search based on OBMC log ID
112         id.pelID.id = 0;
113         id.obmcID = ids[0].obmcID;
114         EXPECT_TRUE(repo.hasPEL(id));
115 
116         // ... based on the other PEL ID
117         id.pelID = ids[1].pelID;
118         id.obmcID.id = 0;
119         EXPECT_TRUE(repo.hasPEL(id));
120 
121         // Not found
122         id.pelID.id = 99;
123         id.obmcID.id = 100;
124         EXPECT_FALSE(repo.hasPEL(id));
125     }
126 
127     {
128         // Restore and check they're still there, then
129         // remove them.
130         Repository repo{repoPath};
131         EXPECT_TRUE(repo.hasPEL(ids[0]));
132         EXPECT_TRUE(repo.hasPEL(ids[1]));
133 
134         repo.remove(ids[0]);
135         EXPECT_FALSE(repo.hasPEL(ids[0]));
136 
137         repo.remove(ids[1]);
138         EXPECT_FALSE(repo.hasPEL(ids[1]));
139     }
140 }
141 
142 TEST_F(RepositoryTest, TestGetPELData)
143 {
144     using ID = Repository::LogID;
145     Repository repo{repoPath};
146 
147     ID badID{ID::Pel(42)};
148     auto noData = repo.getPELData(badID);
149     EXPECT_FALSE(noData);
150 
151     // Add a PEL to the repo, and get the data back with getPELData.
152     auto data = pelDataFactory(TestPelType::pelSimple);
153     auto dataCopy = *data;
154     auto pel = std::make_unique<PEL>(*data);
155     auto pelID = pel->id();
156     repo.add(pel);
157 
158     ID id{ID::Pel(pelID)};
159     auto pelData = repo.getPELData(id);
160 
161     ASSERT_TRUE(pelData);
162     EXPECT_EQ(dataCopy, *pelData);
163 }
164