blob: 118cb12e2478e9d7f1821f201887845e4b7939ab [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}