xref: /openbmc/telemetry/tests/src/test_persistent_json_storage.cpp (revision 583ba441654657bb4ba9d051b747144a7258c159)
1d2238194SKrzysztof Grobelny #include "helpers.hpp"
273da6906SKrzysztof Grobelny #include "persistent_json_storage.hpp"
373da6906SKrzysztof Grobelny 
4a06626d1SKrzysztof Grobelny #include <fstream>
5a06626d1SKrzysztof Grobelny 
673da6906SKrzysztof Grobelny #include "gmock/gmock.h"
773da6906SKrzysztof Grobelny #include "gtest/gtest.h"
873da6906SKrzysztof Grobelny 
973da6906SKrzysztof Grobelny using namespace testing;
1073da6906SKrzysztof Grobelny 
1173da6906SKrzysztof Grobelny class TestPersistentJsonStorage : public Test
1273da6906SKrzysztof Grobelny {
1373da6906SKrzysztof Grobelny   public:
1473da6906SKrzysztof Grobelny     using FilePath = interfaces::JsonStorage::FilePath;
1573da6906SKrzysztof Grobelny     using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
1673da6906SKrzysztof Grobelny 
SetUpTestSuite()1773da6906SKrzysztof Grobelny     static void SetUpTestSuite()
1873da6906SKrzysztof Grobelny     {
1973da6906SKrzysztof Grobelny         ASSERT_FALSE(std::filesystem::exists(directory));
2073da6906SKrzysztof Grobelny     }
2173da6906SKrzysztof Grobelny 
TearDown()2273da6906SKrzysztof Grobelny     void TearDown() override
2373da6906SKrzysztof Grobelny     {
2473da6906SKrzysztof Grobelny         if (std::filesystem::exists(directory))
2573da6906SKrzysztof Grobelny         {
2673da6906SKrzysztof Grobelny             std::filesystem::remove_all(directory);
2773da6906SKrzysztof Grobelny         }
2873da6906SKrzysztof Grobelny     }
2973da6906SKrzysztof Grobelny 
3073da6906SKrzysztof Grobelny     const FilePath fileName = FilePath("report/1/file.txt");
3173da6906SKrzysztof Grobelny 
3273da6906SKrzysztof Grobelny     static const DirectoryPath directory;
3373da6906SKrzysztof Grobelny     PersistentJsonStorage sut{directory};
3473da6906SKrzysztof Grobelny };
3573da6906SKrzysztof Grobelny 
3673da6906SKrzysztof Grobelny const interfaces::JsonStorage::DirectoryPath
3773da6906SKrzysztof Grobelny     TestPersistentJsonStorage::directory =
38596a9945SWludzik, Jozef         interfaces::JsonStorage::DirectoryPath(
39596a9945SWludzik, Jozef             std::filesystem::temp_directory_path() / "telemetry-tests");
4073da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,storesJsonData)4173da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, storesJsonData)
4273da6906SKrzysztof Grobelny {
4373da6906SKrzysztof Grobelny     nlohmann::json data = nlohmann::json::object();
4473da6906SKrzysztof Grobelny     data["name"] = "kevin";
4573da6906SKrzysztof Grobelny     data["lastname"] = "mc calister";
4673da6906SKrzysztof Grobelny 
4773da6906SKrzysztof Grobelny     sut.store(fileName, data);
4873da6906SKrzysztof Grobelny 
4973da6906SKrzysztof Grobelny     ASSERT_THAT(sut.load(fileName), Eq(data));
5073da6906SKrzysztof Grobelny }
5173da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,emptyListWhenNoReportsCreated)5273da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, emptyListWhenNoReportsCreated)
5373da6906SKrzysztof Grobelny {
54e2362796SWludzik, Jozef     EXPECT_THAT(sut.list(), SizeIs(0u));
5573da6906SKrzysztof Grobelny }
5673da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,listSavedReports)5773da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, listSavedReports)
5873da6906SKrzysztof Grobelny {
5973da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
6073da6906SKrzysztof Grobelny               nlohmann::json("data-1a"));
6173da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
6273da6906SKrzysztof Grobelny               nlohmann::json("data-2a"));
6373da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
6473da6906SKrzysztof Grobelny               nlohmann::json("data-2b"));
6573da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
6673da6906SKrzysztof Grobelny               nlohmann::json("data-3a"));
6773da6906SKrzysztof Grobelny 
68e2362796SWludzik, Jozef     EXPECT_THAT(
69e2362796SWludzik, Jozef         sut.list(),
70e2362796SWludzik, Jozef         UnorderedElementsAre(FilePath("report/domain-1/name-1/conf-1.json"),
71e2362796SWludzik, Jozef                              FilePath("report/domain-1/name-2/conf-1.json"),
72e2362796SWludzik, Jozef                              FilePath("report/domain-1/name-2/conf-2.json"),
73e2362796SWludzik, Jozef                              FilePath("report/domain-2/name-1/conf-1.json")));
7473da6906SKrzysztof Grobelny }
7573da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,listSavedReportsWithoutRemovedOnes)7673da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, listSavedReportsWithoutRemovedOnes)
7773da6906SKrzysztof Grobelny {
7873da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-1/conf-1.json"),
7973da6906SKrzysztof Grobelny               nlohmann::json("data-1a"));
8073da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-2/conf-1.json"),
8173da6906SKrzysztof Grobelny               nlohmann::json("data-2a"));
8273da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-1/name-2/conf-2.json"),
8373da6906SKrzysztof Grobelny               nlohmann::json("data-2b"));
8473da6906SKrzysztof Grobelny     sut.store(FilePath("report/domain-2/name-1/conf-1.json"),
8573da6906SKrzysztof Grobelny               nlohmann::json("data-3a"));
8673da6906SKrzysztof Grobelny     sut.remove(FilePath("report/domain-1/name-1/conf-1.json"));
8773da6906SKrzysztof Grobelny     sut.remove(FilePath("report/domain-1/name-2/conf-2.json"));
8873da6906SKrzysztof Grobelny 
89e2362796SWludzik, Jozef     EXPECT_THAT(
90e2362796SWludzik, Jozef         sut.list(),
91e2362796SWludzik, Jozef         UnorderedElementsAre(FilePath("report/domain-1/name-2/conf-1.json"),
92e2362796SWludzik, Jozef                              FilePath("report/domain-2/name-1/conf-1.json")));
9373da6906SKrzysztof Grobelny }
9473da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,removesStoredJson)9573da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, removesStoredJson)
9673da6906SKrzysztof Grobelny {
9773da6906SKrzysztof Grobelny     nlohmann::json data = nlohmann::json::object();
9873da6906SKrzysztof Grobelny     data["name"] = "kevin";
9973da6906SKrzysztof Grobelny     data["lastname"] = "mc calister";
10073da6906SKrzysztof Grobelny 
10173da6906SKrzysztof Grobelny     sut.store(fileName, data);
10273da6906SKrzysztof Grobelny 
10373da6906SKrzysztof Grobelny     ASSERT_THAT(sut.remove(fileName), Eq(true));
10473da6906SKrzysztof Grobelny     ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
10573da6906SKrzysztof Grobelny }
10673da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,returnsFalseWhenDeletingNonExistingFile)10773da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, returnsFalseWhenDeletingNonExistingFile)
10873da6906SKrzysztof Grobelny {
10973da6906SKrzysztof Grobelny     ASSERT_THAT(sut.remove(fileName), Eq(false));
11073da6906SKrzysztof Grobelny }
11173da6906SKrzysztof Grobelny 
TEST_F(TestPersistentJsonStorage,returnsNulloptWhenFileDoesntExist)11273da6906SKrzysztof Grobelny TEST_F(TestPersistentJsonStorage, returnsNulloptWhenFileDoesntExist)
11373da6906SKrzysztof Grobelny {
11473da6906SKrzysztof Grobelny     ASSERT_THAT(sut.load(fileName), Eq(std::nullopt));
11573da6906SKrzysztof Grobelny }
116a06626d1SKrzysztof Grobelny 
1174d1c2ce2SSzymon Dompke struct TestFileSymlink
1184d1c2ce2SSzymon Dompke {
setupSymlinksTestFileSymlink119*583ba441SPatrick Williams     static interfaces::JsonStorage::FilePath setupSymlinks(
120*583ba441SPatrick Williams         const std::filesystem::path& originalFile,
1214d1c2ce2SSzymon Dompke         const interfaces::JsonStorage::DirectoryPath& directory)
1224d1c2ce2SSzymon Dompke     {
123*583ba441SPatrick Williams         auto linkPath =
124*583ba441SPatrick Williams             std::filesystem::path(directory) / "report/symlink.json";
125*583ba441SPatrick Williams         std::filesystem::create_directories(
126*583ba441SPatrick Williams             std::filesystem::path(directory) / "report");
1274d1c2ce2SSzymon Dompke         std::filesystem::create_symlink(originalFile, linkPath);
1284d1c2ce2SSzymon Dompke         return interfaces::JsonStorage::FilePath(linkPath);
1294d1c2ce2SSzymon Dompke     }
1304d1c2ce2SSzymon Dompke };
1314d1c2ce2SSzymon Dompke 
1324d1c2ce2SSzymon Dompke struct TestDirectorySymlink
1334d1c2ce2SSzymon Dompke {
setupSymlinksTestDirectorySymlink134*583ba441SPatrick Williams     static interfaces::JsonStorage::FilePath setupSymlinks(
135*583ba441SPatrick Williams         const std::filesystem::path& originalFile,
1364d1c2ce2SSzymon Dompke         const interfaces::JsonStorage::DirectoryPath& directory)
1374d1c2ce2SSzymon Dompke     {
1384d1c2ce2SSzymon Dompke         auto linkPath = std::filesystem::path(directory) / "reportLink";
139*583ba441SPatrick Williams         std::filesystem::create_directories(
140*583ba441SPatrick Williams             std::filesystem::path(directory) / "report");
1414d1c2ce2SSzymon Dompke         std::filesystem::create_directory_symlink(originalFile.parent_path(),
1424d1c2ce2SSzymon Dompke                                                   linkPath);
143*583ba441SPatrick Williams         return interfaces::JsonStorage::FilePath(
144*583ba441SPatrick Williams             linkPath / originalFile.filename());
1454d1c2ce2SSzymon Dompke     }
1464d1c2ce2SSzymon Dompke };
1474d1c2ce2SSzymon Dompke 
1484d1c2ce2SSzymon Dompke template <typename T>
149a06626d1SKrzysztof Grobelny class TestPersistentJsonStorageWithSymlink : public TestPersistentJsonStorage
150a06626d1SKrzysztof Grobelny {
151a06626d1SKrzysztof Grobelny   public:
TestPersistentJsonStorageWithSymlink()152a06626d1SKrzysztof Grobelny     TestPersistentJsonStorageWithSymlink()
153a06626d1SKrzysztof Grobelny     {
154a06626d1SKrzysztof Grobelny         std::ofstream file(dummyReportPath);
155a06626d1SKrzysztof Grobelny         file << "{}";
156a06626d1SKrzysztof Grobelny         file.close();
157a06626d1SKrzysztof Grobelny 
1584d1c2ce2SSzymon Dompke         linkPath = T::setupSymlinks(dummyReportPath, directory);
159a06626d1SKrzysztof Grobelny     }
160a06626d1SKrzysztof Grobelny 
SetUpTestSuite()161a06626d1SKrzysztof Grobelny     static void SetUpTestSuite()
162a06626d1SKrzysztof Grobelny     {
163a06626d1SKrzysztof Grobelny         TestPersistentJsonStorage::SetUpTestSuite();
164a06626d1SKrzysztof Grobelny         ASSERT_FALSE(std::filesystem::exists(dummyReportPath));
165a06626d1SKrzysztof Grobelny     }
166a06626d1SKrzysztof Grobelny 
TearDown()167a06626d1SKrzysztof Grobelny     void TearDown() override
168a06626d1SKrzysztof Grobelny     {
169a06626d1SKrzysztof Grobelny         TestPersistentJsonStorage::TearDown();
170a06626d1SKrzysztof Grobelny         if (std::filesystem::exists(dummyReportPath))
171a06626d1SKrzysztof Grobelny         {
172a06626d1SKrzysztof Grobelny             std::filesystem::remove(dummyReportPath);
173a06626d1SKrzysztof Grobelny         }
174a06626d1SKrzysztof Grobelny     }
175a06626d1SKrzysztof Grobelny 
176a06626d1SKrzysztof Grobelny     static const std::filesystem::path dummyReportPath;
1774d1c2ce2SSzymon Dompke     interfaces::JsonStorage::FilePath linkPath;
178a06626d1SKrzysztof Grobelny };
179a06626d1SKrzysztof Grobelny 
1804d1c2ce2SSzymon Dompke template <typename T>
181a06626d1SKrzysztof Grobelny const std::filesystem::path
1824d1c2ce2SSzymon Dompke     TestPersistentJsonStorageWithSymlink<T>::dummyReportPath =
1834d1c2ce2SSzymon Dompke         std::filesystem::temp_directory_path() / "report.json";
184a06626d1SKrzysztof Grobelny 
1854d1c2ce2SSzymon Dompke using SymlinkTypes = Types<TestFileSymlink, TestDirectorySymlink>;
1864d1c2ce2SSzymon Dompke TYPED_TEST_SUITE(TestPersistentJsonStorageWithSymlink, SymlinkTypes);
1874d1c2ce2SSzymon Dompke 
TYPED_TEST(TestPersistentJsonStorageWithSymlink,symlinksAreNotListed)1884d1c2ce2SSzymon Dompke TYPED_TEST(TestPersistentJsonStorageWithSymlink, symlinksAreNotListed)
189a06626d1SKrzysztof Grobelny {
1904d1c2ce2SSzymon Dompke     ASSERT_THAT(TestPersistentJsonStorage::sut.list(), UnorderedElementsAre());
191a06626d1SKrzysztof Grobelny }
192a06626d1SKrzysztof Grobelny 
TYPED_TEST(TestPersistentJsonStorageWithSymlink,throwsWhenStoreTargetIsSymlink)1934d1c2ce2SSzymon Dompke TYPED_TEST(TestPersistentJsonStorageWithSymlink, throwsWhenStoreTargetIsSymlink)
194a06626d1SKrzysztof Grobelny {
1954d1c2ce2SSzymon Dompke     ASSERT_THROW(TestPersistentJsonStorage::sut.store(TestFixture::linkPath,
1964d1c2ce2SSzymon Dompke                                                       nlohmann::json("data")),
197a06626d1SKrzysztof Grobelny                  std::runtime_error);
198a06626d1SKrzysztof Grobelny 
1994d1c2ce2SSzymon Dompke     ASSERT_THAT(TestPersistentJsonStorage::sut.list(), UnorderedElementsAre());
200a06626d1SKrzysztof Grobelny }
201a06626d1SKrzysztof Grobelny 
TYPED_TEST(TestPersistentJsonStorageWithSymlink,returnsNulloptWhenFileIsSymlink)2024d1c2ce2SSzymon Dompke TYPED_TEST(TestPersistentJsonStorageWithSymlink,
2034d1c2ce2SSzymon Dompke            returnsNulloptWhenFileIsSymlink)
204a06626d1SKrzysztof Grobelny {
2054d1c2ce2SSzymon Dompke     ASSERT_THAT(TestPersistentJsonStorage::sut.load(TestFixture::linkPath),
2064d1c2ce2SSzymon Dompke                 Eq(std::nullopt));
207a06626d1SKrzysztof Grobelny }
208a06626d1SKrzysztof Grobelny 
TYPED_TEST(TestPersistentJsonStorageWithSymlink,returnsFalseWhenTryingToDeleteSymlink)2094d1c2ce2SSzymon Dompke TYPED_TEST(TestPersistentJsonStorageWithSymlink,
210a06626d1SKrzysztof Grobelny            returnsFalseWhenTryingToDeleteSymlink)
211a06626d1SKrzysztof Grobelny {
2124d1c2ce2SSzymon Dompke     EXPECT_THAT(TestPersistentJsonStorage::sut.remove(TestFixture::linkPath),
2134d1c2ce2SSzymon Dompke                 Eq(false));
2144d1c2ce2SSzymon Dompke     EXPECT_TRUE(std::filesystem::exists(TestFixture::linkPath));
2154d1c2ce2SSzymon Dompke     EXPECT_TRUE(std::filesystem::exists(TestFixture::dummyReportPath));
216a06626d1SKrzysztof Grobelny }
217