bmc: json configuration: update: add mode parameter

Add the mode parameter as an optional configuration option for the
update systemd approach.  Previously this was hard-coded as "replace"
but now the user can specify something else.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I2bcb61bd9ac5733df6da70f901516d26bd31e2a9
diff --git a/bmc/buildjson.cpp b/bmc/buildjson.cpp
index aec9a09..d8a9ea1 100644
--- a/bmc/buildjson.cpp
+++ b/bmc/buildjson.cpp
@@ -112,8 +112,17 @@
             else if (updateType == "systemd")
             {
                 const auto& unit = update.at("unit");
+
+                /* the mode parameter is optional. */
+                std::string systemdMode = "replace";
+                const auto& mode = update.find("mode");
+                if (mode != update.end())
+                {
+                    systemdMode = update.at("mode").get<std::string>();
+                }
+
                 pack->update = SystemdUpdateMechanism::CreateSystemdUpdate(
-                    sdbusplus::bus::new_default(), unit, "replace");
+                    sdbusplus::bus::new_default(), unit, systemdMode);
             }
             else
             {
diff --git a/bmc/test/firmware_json_unittest.cpp b/bmc/test/firmware_json_unittest.cpp
index 704243b..04c0f09 100644
--- a/bmc/test/firmware_json_unittest.cpp
+++ b/bmc/test/firmware_json_unittest.cpp
@@ -1,4 +1,5 @@
 #include "buildjson.hpp"
+#include "update_systemd.hpp"
 
 #include <nlohmann/json.hpp>
 
@@ -423,6 +424,49 @@
     EXPECT_FALSE(h[0].actions->preparation == nullptr);
     EXPECT_FALSE(h[0].actions->verification == nullptr);
     EXPECT_FALSE(h[0].actions->update == nullptr);
+    auto updater =
+        reinterpret_cast<SystemdUpdateMechanism*>(h[0].actions->update.get());
+    EXPECT_THAT(updater->getMode(), "replace");
+}
+
+TEST(FirmwareJsonTest, VerifyValidUpdateWithMode)
+{
+    auto j2 = R"(
+        [{
+            "blob" : "/flash/image",
+            "handler" : {
+                "type" : "file",
+                "path" : "/run/initramfs/bmc-image"
+            },
+            "actions" : {
+                "preparation" : {
+                    "type" : "systemd",
+                    "unit" : "phosphor-ipmi-flash-bmc-prepare.target"
+                },
+                "verification" : {
+                    "type" : "fileSystemdVerify",
+                    "unit" : "phosphor-ipmi-flash-bmc-verify.target",
+                    "path" : "/tmp/bmc.verify"
+                },
+                "update" : {
+                    "type" : "systemd",
+                    "mode" : "replace-fake",
+                    "unit" : "phosphor-ipmi-flash-bmc-update.target"
+                }
+            }
+         }]
+    )"_json;
+
+    auto h = buildHandlerFromJson(j2);
+    EXPECT_EQ(h[0].blobId, "/flash/image");
+    EXPECT_FALSE(h[0].handler == nullptr);
+    EXPECT_FALSE(h[0].actions == nullptr);
+    EXPECT_FALSE(h[0].actions->preparation == nullptr);
+    EXPECT_FALSE(h[0].actions->verification == nullptr);
+    EXPECT_FALSE(h[0].actions->update == nullptr);
+    auto updater =
+        reinterpret_cast<SystemdUpdateMechanism*>(h[0].actions->update.get());
+    EXPECT_THAT(updater->getMode(), "replace-fake");
 }
 
 } // namespace
diff --git a/bmc/update_systemd.cpp b/bmc/update_systemd.cpp
index 92c97c8..afd4316 100644
--- a/bmc/update_systemd.cpp
+++ b/bmc/update_systemd.cpp
@@ -69,4 +69,9 @@
     return ActionStatus::running;
 }
 
+const std::string SystemdUpdateMechanism::getMode() const
+{
+    return mode;
+}
+
 } // namespace ipmi_flash
diff --git a/bmc/update_systemd.hpp b/bmc/update_systemd.hpp
index fe0ab33..a8dd8fc 100644
--- a/bmc/update_systemd.hpp
+++ b/bmc/update_systemd.hpp
@@ -36,6 +36,8 @@
     void abort() override;
     ActionStatus status() override;
 
+    const std::string getMode() const;
+
   private:
     sdbusplus::bus::bus bus;
     const std::string target;