Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 16 | #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 | |
| 26 | using namespace openpower::pels; |
| 27 | namespace 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 | */ |
| 34 | class 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 | |
| 50 | TEST_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 | |
| 69 | TEST_F(RepositoryTest, AddTest) |
| 70 | { |
| 71 | Repository repo{repoPath}; |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 72 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 73 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 74 | |
| 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 Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 79 | const auto ts = pel->privateHeader().commitTimestamp(); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 80 | 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 Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 89 | |
| 90 | TEST_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 Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 102 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 103 | auto pel = std::make_unique<PEL>(data, 1); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 104 | pel->assignID(); |
| 105 | repo.add(pel); |
| 106 | ids.emplace_back(pelID(pel->id()), obmcID(1)); |
| 107 | } |
| 108 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 109 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 110 | auto pel = std::make_unique<PEL>(data, 2); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 111 | 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 Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 156 | |
| 157 | TEST_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 Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 167 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 168 | auto dataCopy = data; |
| 169 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 170 | 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 Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 179 | |
| 180 | TEST_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 | } |