blob: 97019e87e80b6808df0e956c0429184fdc76d7bf [file] [log] [blame]
Matt Spinler89fa0822019-07-17 13:54:30 -05001#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
11using namespace openpower::pels;
12namespace 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 */
19class 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
35TEST_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
54TEST_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}