blob: cba599fb313d4139b79b48f284f87782c934b907 [file] [log] [blame]
Lei YUfda15a32019-09-19 14:43:02 +08001#include "version.hpp"
2
3#include <filesystem>
4#include <fstream>
5
6#include <gtest/gtest.h>
7
8using phosphor::software::updater::Version;
9
10namespace fs = std::filesystem;
11
12namespace
13{
14constexpr auto validManifest = R"(
15purpose=xyz.openbmc_project.Software.Version.VersionPurpose.PSU
16version=psu-dummy-test.v0.1
17extended_version=model=dummy_model,manufacture=dummy_manufacture)";
18}
19
20class 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
46TEST_F(TestVersion, getValuesFileNotExist)
47{
48 auto ret = Version::getValues("NotExist.file", {""});
49 EXPECT_TRUE(ret.empty());
50}
51
52TEST_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}