| Lei YU | fda15a3 | 2019-09-19 14:43:02 +0800 | [diff] [blame^] | 1 | #include "version.hpp" | 
|  | 2 |  | 
|  | 3 | #include <filesystem> | 
|  | 4 | #include <fstream> | 
|  | 5 |  | 
|  | 6 | #include <gtest/gtest.h> | 
|  | 7 |  | 
|  | 8 | using phosphor::software::updater::Version; | 
|  | 9 |  | 
|  | 10 | namespace fs = std::filesystem; | 
|  | 11 |  | 
|  | 12 | namespace | 
|  | 13 | { | 
|  | 14 | constexpr auto validManifest = R"( | 
|  | 15 | purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU | 
|  | 16 | version=psu-dummy-test.v0.1 | 
|  | 17 | extended_version=model=dummy_model,manufacture=dummy_manufacture)"; | 
|  | 18 | } | 
|  | 19 |  | 
|  | 20 | class TestVersion : public ::testing::Test | 
|  | 21 | { | 
|  | 22 | public: | 
|  | 23 | TestVersion() | 
|  | 24 | { | 
|  | 25 | auto tmpPath = fs::temp_directory_path(); | 
|  | 26 | tmpDir = (tmpPath / "test_XXXXXX"); | 
|  | 27 | if (!mkdtemp(tmpDir.data())) | 
|  | 28 | { | 
|  | 29 | throw "Failed to create temp dir"; | 
|  | 30 | } | 
|  | 31 | } | 
|  | 32 | ~TestVersion() | 
|  | 33 | { | 
|  | 34 | fs::remove_all(tmpDir); | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | void writeFile(const fs::path& file, const char* data) | 
|  | 38 | { | 
|  | 39 | std::ofstream f{file}; | 
|  | 40 | f << data; | 
|  | 41 | f.close(); | 
|  | 42 | } | 
|  | 43 | std::string tmpDir; | 
|  | 44 | }; | 
|  | 45 |  | 
|  | 46 | TEST_F(TestVersion, getValuesFileNotExist) | 
|  | 47 | { | 
|  | 48 | auto ret = Version::getValues("NotExist.file", {""}); | 
|  | 49 | EXPECT_TRUE(ret.empty()); | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | TEST_F(TestVersion, getValuesOK) | 
|  | 53 | { | 
|  | 54 | auto manifestFilePath = fs::path(tmpDir) / "MANIFEST"; | 
|  | 55 | writeFile(manifestFilePath, validManifest); | 
|  | 56 | auto ret = Version::getValues(manifestFilePath.string(), | 
|  | 57 | {"purpose", "version", "extended_version"}); | 
|  | 58 | EXPECT_EQ(3u, ret.size()); | 
|  | 59 | auto purpose = ret["purpose"]; | 
|  | 60 | auto version = ret["version"]; | 
|  | 61 | auto extVersion = ret["extended_version"]; | 
|  | 62 |  | 
|  | 63 | EXPECT_EQ("xyz.openbmc_project.Software.Version.VersionPurpose.PSU", | 
|  | 64 | purpose); | 
|  | 65 | EXPECT_EQ("psu-dummy-test.v0.1", version); | 
|  | 66 | EXPECT_EQ("model=dummy_model,manufacture=dummy_manufacture", extVersion); | 
|  | 67 | } |