summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarisuddin Mohamed Isa <harisuddin@gmail.com>2020-02-06 17:32:38 +0800
committerMatt Spinler <spinler@us.ibm.com>2020-02-07 16:02:00 +0000
commite2d1bf317c0ddf25ddd305aa8f3c6f09118f799c (patch)
treede330de70c2a62ce6785bca2dae1faee0f89a50e
parent6fd0c1e70c1f63c44ffa69308569b1be144451ce (diff)
downloadphosphor-logging-e2d1bf317c0ddf25ddd305aa8f3c6f09118f799c.tar.gz
phosphor-logging-e2d1bf317c0ddf25ddd305aa8f3c6f09118f799c.zip
PEL: Print Failing MTMS section into JSON
Remove any trailing whitespace for serial number. "Failing MTMS": { "Section Version": "1", "Sub-section type": "0", "Created by": "0x4552", "Machine Type Model": "OPWR-131", "Serial Number": "1318ABA" }, Testing: Manually run peltool and verified output. All unit tests passed. Signed-off-by: Harisuddin Mohamed Isa <harisuddin@gmail.com> Change-Id: I7a05790bdf406ef6d7946ffae831a45ee6dd5cda
-rw-r--r--extensions/openpower-pels/failing_mtms.cpp15
-rw-r--r--extensions/openpower-pels/failing_mtms.hpp6
-rw-r--r--extensions/openpower-pels/json_utils.cpp10
-rw-r--r--extensions/openpower-pels/json_utils.hpp7
-rw-r--r--extensions/openpower-pels/tools/peltool.cpp15
-rw-r--r--test/openpower-pels/Makefile.include10
-rw-r--r--test/openpower-pels/json_utils_test.cpp50
7 files changed, 98 insertions, 15 deletions
diff --git a/extensions/openpower-pels/failing_mtms.cpp b/extensions/openpower-pels/failing_mtms.cpp
index d4cdf37..b45c05d 100644
--- a/extensions/openpower-pels/failing_mtms.cpp
+++ b/extensions/openpower-pels/failing_mtms.cpp
@@ -15,6 +15,7 @@
*/
#include "failing_mtms.hpp"
+#include "json_utils.hpp"
#include "pel_types.hpp"
#include <phosphor-logging/log.hpp>
@@ -85,5 +86,19 @@ void FailingMTMS::unflatten(Stream& stream)
stream >> _header >> _mtms;
}
+std::optional<std::string> FailingMTMS::getJSON() const
+{
+ std::string json;
+ jsonInsert(json, "Section Version", getNumberString("%d", _header.version),
+ 1);
+ jsonInsert(json, "Sub-section type", getNumberString("%d", _header.subType),
+ 1);
+ jsonInsert(json, "Created by", getNumberString("0x%X", _header.componentID),
+ 1);
+ jsonInsert(json, "Machine Type Model", _mtms.machineTypeAndModel(), 1);
+ jsonInsert(json, "Serial Number", trimEnd(_mtms.machineSerialNumber()), 1);
+ json.erase(json.size() - 2);
+ return json;
+}
} // namespace pels
} // namespace openpower
diff --git a/extensions/openpower-pels/failing_mtms.hpp b/extensions/openpower-pels/failing_mtms.hpp
index 65186eb..8f5cd44 100644
--- a/extensions/openpower-pels/failing_mtms.hpp
+++ b/extensions/openpower-pels/failing_mtms.hpp
@@ -81,6 +81,12 @@ class FailingMTMS : public Section
return _mtms.machineSerialNumber();
}
+ /**
+ * @brief Get section in JSON.
+ * @return std::optional<std::string> - Failing MTMS section in JSON
+ */
+ std::optional<std::string> getJSON() const override;
+
private:
/**
* @brief Validates the section contents
diff --git a/extensions/openpower-pels/json_utils.cpp b/extensions/openpower-pels/json_utils.cpp
index cd41804..871bd32 100644
--- a/extensions/openpower-pels/json_utils.cpp
+++ b/extensions/openpower-pels/json_utils.cpp
@@ -193,5 +193,15 @@ void jsonInsertArray(std::string& jsonStr, const std::string& fieldName,
jsonStr.append("[],\n");
}
}
+
+std::string trimEnd(std::string s)
+{
+ const char* t = " \t\n\r\f\v";
+ if (s.find_last_not_of(t) != std::string::npos)
+ {
+ s.erase(s.find_last_not_of(t) + 1);
+ }
+ return s;
+}
} // namespace pels
} // namespace openpower
diff --git a/extensions/openpower-pels/json_utils.hpp b/extensions/openpower-pels/json_utils.hpp
index f3fb767..e122f47 100644
--- a/extensions/openpower-pels/json_utils.hpp
+++ b/extensions/openpower-pels/json_utils.hpp
@@ -76,5 +76,12 @@ std::string getNumberString(const char* format, T number)
return numString;
}
+/**
+ * @brief helper function to trim trailing whitespaces
+ * @return std::string - trimmed string
+ * @param[in] std::string - string to trim
+ */
+std::string trimEnd(std::string s);
+
} // namespace pels
} // namespace openpower
diff --git a/extensions/openpower-pels/tools/peltool.cpp b/extensions/openpower-pels/tools/peltool.cpp
index f1d5f59..ed6daa2 100644
--- a/extensions/openpower-pels/tools/peltool.cpp
+++ b/extensions/openpower-pels/tools/peltool.cpp
@@ -182,21 +182,6 @@ std::vector<uint8_t> getFileData(const std::string& name)
}
}
-/**
- * @brief helper function to trim trailing whitespaces
- * @return std::string - trimmed string
- * @param[in] std::string - string to trim
- */
-const char* ws = " \t\n\r\f\v";
-std::string trim(std::string s, const char* t = ws)
-{
- if (s.find_last_not_of(t) != std::string::npos)
- {
- s.erase(s.find_last_not_of(t) + 1);
- }
- return s;
-}
-
template <typename T>
std::string genPELJSON(T itr, bool hidden, message::Registry& registry)
{
diff --git a/test/openpower-pels/Makefile.include b/test/openpower-pels/Makefile.include
index 2bed701..a700a50 100644
--- a/test/openpower-pels/Makefile.include
+++ b/test/openpower-pels/Makefile.include
@@ -9,6 +9,7 @@ check_PROGRAMS += \
fru_identity_test \
generic_section_test \
host_notifier_test \
+ json_utils_test \
log_id_test \
mru_test \
mtms_test \
@@ -187,6 +188,7 @@ failing_mtms_test_CXXFLAGS = $(test_cxxflags)
failing_mtms_test_LDADD = \
$(test_ldadd) \
$(top_builddir)/extensions/openpower-pels/failing_mtms.o \
+ $(top_builddir)/extensions/openpower-pels/json_utils.o \
$(top_builddir)/extensions/openpower-pels/mtms.o
failing_mtms_test_LDFLAGS = $(test_ldflags)
@@ -343,3 +345,11 @@ host_notifier_test_LDADD = \
$(top_builddir)/extensions/openpower-pels/host_notifier.o \
$(top_builddir)/extensions/openpower-pels/repository.o
host_notifier_test_LDFLAGS = $(test_ldflags) $(SDEVENTPLUS_LIBS)
+
+json_utils_test_SOURCES = %reldir%/json_utils_test.cpp
+json_utils_test_CPPFLAGS = $(test_cppflags)
+json_utils_test_CXXFLAGS = $(test_cxxflags)
+json_utils_test_LDADD = \
+ $(test_ldadd) \
+ $(top_builddir)/extensions/openpower-pels/json_utils.o
+json_utils_test_LDFLAGS = $(test_ldflags)
diff --git a/test/openpower-pels/json_utils_test.cpp b/test/openpower-pels/json_utils_test.cpp
new file mode 100644
index 0000000..60ca00d
--- /dev/null
+++ b/test/openpower-pels/json_utils_test.cpp
@@ -0,0 +1,50 @@
+/**
+ * Copyright © 2019 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "extensions/openpower-pels/json_utils.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace openpower::pels;
+
+TEST(JsonUtilsTest, TrimEndTest)
+{
+ std::string testStr("Test string 1");
+ EXPECT_EQ(trimEnd(testStr), "Test string 1");
+ testStr = "Test string 2 ";
+ EXPECT_EQ(trimEnd(testStr), "Test string 2");
+ testStr = " Test string 3 ";
+ EXPECT_EQ(trimEnd(testStr), " Test string 3");
+}
+
+TEST(JsonUtilsTest, NumberToStringTest)
+{
+ size_t number = 123;
+ EXPECT_EQ(getNumberString("%d", number), "123");
+ EXPECT_EQ(getNumberString("%03X", number), "07B");
+ EXPECT_EQ(getNumberString("0x%X", number), "0x7B");
+ ASSERT_EXIT((getNumberString("%123", number), exit(0)),
+ ::testing::KilledBySignal(SIGSEGV), ".*");
+}
+
+TEST(JsonUtilsTest, JsonInsertTest)
+{
+ std::string json;
+ jsonInsert(json, "Key", "Value1", 1);
+ EXPECT_EQ(json, " \"Key\": \"Value1\",\n");
+ jsonInsert(json, "Keyxxxxxxxxxxxxxxxxxxxxxxxxxx", "Value2", 2);
+ EXPECT_EQ(json, " \"Key\": \"Value1\",\n"
+ " \"Keyxxxxxxxxxxxxxxxxxxxxxxxxxx\": \"Value2\",\n");
+}
OpenPOWER on IntegriCloud