bmc: firmware updateStarted: stat(session)

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: I3d34f52aacbb004bccf18748c8f8cd591dbb0490
diff --git a/firmware_handler.cpp b/firmware_handler.cpp
index d446f66..13e5d72 100644
--- a/firmware_handler.cpp
+++ b/firmware_handler.cpp
@@ -202,19 +202,22 @@
 
     if (item->second->activePath == verifyBlobId)
     {
-        VerifyCheckResponses value;
+        VerifyCheckResponses value = VerifyCheckResponses::other;
 
         switch (state)
         {
             case UpdateState::verificationPending:
                 value = VerifyCheckResponses::other;
                 break;
+            case UpdateState::verificationStarted:
+                value = verification->status();
+                lastVerificationResponse = value;
+                break;
             case UpdateState::verificationCompleted:
                 value = lastVerificationResponse;
                 break;
             default:
-                value = verification->checkVerificationState();
-                lastVerificationResponse = value;
+                break;
         }
 
         meta->metadata.push_back(static_cast<std::uint8_t>(value));
@@ -240,18 +243,36 @@
     }
     else if (item->second->activePath == updateBlobId)
     {
-        UpdateStatus value;
+        UpdateStatus value = UpdateStatus::unknown;
 
         switch (state)
         {
             case UpdateState::updatePending:
                 value = UpdateStatus::unknown;
                 break;
+            case UpdateState::updateStarted:
+                value = update->status();
+                break;
             default:
                 break;
         }
 
         meta->metadata.push_back(static_cast<std::uint8_t>(value));
+
+        if (value == UpdateStatus::success || value == UpdateStatus::failed)
+        {
+            state = UpdateState::updateCompleted;
+            item->second->flags &= ~blobs::StateFlags::committing;
+
+            if (value == UpdateStatus::success)
+            {
+                item->second->flags |= blobs::StateFlags::committed;
+            }
+            else
+            {
+                item->second->flags |= blobs::StateFlags::commit_error;
+            }
+        }
     }
 
     /* The blobState here relates to an active sesion, so we should return the
diff --git a/firmware_handler.hpp b/firmware_handler.hpp
index b2f5a64..67fb82b 100644
--- a/firmware_handler.hpp
+++ b/firmware_handler.hpp
@@ -104,7 +104,7 @@
         /** The update process has started. */
         updateStarted,
         /** The update has completed (optional state to reach) */
-        updatedCompleted,
+        updateCompleted,
     };
 
     /**
diff --git a/test/firmware_state_updatestarted_unittest.cpp b/test/firmware_state_updatestarted_unittest.cpp
index 8638953..e2348e4 100644
--- a/test/firmware_state_updatestarted_unittest.cpp
+++ b/test/firmware_state_updatestarted_unittest.cpp
@@ -157,6 +157,59 @@
 /*
  * stat(session) - this will trigger a check, and the state may change.
  */
+TEST_F(FirmwareHandlerUpdateStartedTest,
+       CallStatChecksUpdateStatusReturnsRunningDoesNotChangeState)
+{
+    getToUpdateStarted();
+    EXPECT_CALL(*updateMockPtr, status())
+        .WillOnce(Return(UpdateStatus::running));
+
+    blobs::BlobMeta meta, expectedMeta = {};
+    expectedMeta.size = 0;
+    expectedMeta.blobState = flags | blobs::StateFlags::committing;
+    expectedMeta.metadata.push_back(
+        static_cast<std::uint8_t>(UpdateStatus::running));
+
+    EXPECT_TRUE(handler->stat(session, &meta));
+    EXPECT_EQ(expectedMeta, meta);
+    expectedState(FirmwareBlobHandler::UpdateState::updateStarted);
+}
+
+TEST_F(FirmwareHandlerUpdateStartedTest,
+       CallStatChecksUpdateStatusReturnsFailedChangesStateToCompleted)
+{
+    getToUpdateStarted();
+    EXPECT_CALL(*updateMockPtr, status())
+        .WillOnce(Return(UpdateStatus::failed));
+
+    blobs::BlobMeta meta, expectedMeta = {};
+    expectedMeta.size = 0;
+    expectedMeta.blobState = flags | blobs::StateFlags::commit_error;
+    expectedMeta.metadata.push_back(
+        static_cast<std::uint8_t>(UpdateStatus::failed));
+
+    EXPECT_TRUE(handler->stat(session, &meta));
+    EXPECT_EQ(expectedMeta, meta);
+    expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
+}
+
+TEST_F(FirmwareHandlerUpdateStartedTest,
+       CallStatChecksUpdateStatusReturnsSuccessChangesStateToCompleted)
+{
+    getToUpdateStarted();
+    EXPECT_CALL(*updateMockPtr, status())
+        .WillOnce(Return(UpdateStatus::success));
+
+    blobs::BlobMeta meta, expectedMeta = {};
+    expectedMeta.size = 0;
+    expectedMeta.blobState = flags | blobs::StateFlags::committed;
+    expectedMeta.metadata.push_back(
+        static_cast<std::uint8_t>(UpdateStatus::success));
+
+    EXPECT_TRUE(handler->stat(session, &meta));
+    EXPECT_EQ(expectedMeta, meta);
+    expectedState(FirmwareBlobHandler::UpdateState::updateCompleted);
+}
 
 /*
  * TODO: close(session) - this will abort.
diff --git a/test/firmware_state_verificationcompleted_unittest.cpp b/test/firmware_state_verificationcompleted_unittest.cpp
index f36427d..3c9f34d 100644
--- a/test/firmware_state_verificationcompleted_unittest.cpp
+++ b/test/firmware_state_verificationcompleted_unittest.cpp
@@ -151,7 +151,7 @@
      * until it's completed.
      */
     getToVerificationCompleted(VerifyCheckResponses::success);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState()).Times(0);
+    EXPECT_CALL(*verifyMockPtr, status()).Times(0);
 
     blobs::BlobMeta meta, expectedMeta = {};
     expectedMeta.size = 0;
@@ -168,7 +168,7 @@
        SessionStatOnVerifyAfterFailureDoesNothing)
 {
     getToVerificationCompleted(VerifyCheckResponses::failed);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState()).Times(0);
+    EXPECT_CALL(*verifyMockPtr, status()).Times(0);
 
     blobs::BlobMeta meta, expectedMeta = {};
     expectedMeta.size = 0;
diff --git a/test/firmware_state_verificationpending_unittest.cpp b/test/firmware_state_verificationpending_unittest.cpp
index 4452f17..b96ecb3 100644
--- a/test/firmware_state_verificationpending_unittest.cpp
+++ b/test/firmware_state_verificationpending_unittest.cpp
@@ -210,7 +210,7 @@
     getToVerificationPending(staticLayoutBlobId);
     EXPECT_TRUE(handler->open(session, flags, verifyBlobId));
     EXPECT_CALL(*verifyMockPtr, triggerVerification()).Times(0);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState()).Times(0);
+    EXPECT_CALL(*verifyMockPtr, status()).Times(0);
 
     blobs::BlobMeta meta, expectedMeta = {};
     expectedMeta.size = 0;
diff --git a/test/firmware_state_verificationstarted_unittest.cpp b/test/firmware_state_verificationstarted_unittest.cpp
index 9072ab9..e031fe3 100644
--- a/test/firmware_state_verificationstarted_unittest.cpp
+++ b/test/firmware_state_verificationstarted_unittest.cpp
@@ -70,7 +70,7 @@
        StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsRunning)
 {
     getToVerificationStarted(staticLayoutBlobId);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState())
+    EXPECT_CALL(*verifyMockPtr, status())
         .WillOnce(Return(VerifyCheckResponses::running));
 
     blobs::BlobMeta meta, expectedMeta = {};
@@ -87,7 +87,7 @@
        StatOnVerifyBlobIdAfterCommitChecksStateAndReturnsOther)
 {
     getToVerificationStarted(staticLayoutBlobId);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState())
+    EXPECT_CALL(*verifyMockPtr, status())
         .WillOnce(Return(VerifyCheckResponses::other));
 
     blobs::BlobMeta meta, expectedMeta = {};
@@ -107,7 +107,7 @@
      * commit_error and transitions to verificationCompleted.
      */
     getToVerificationStarted(staticLayoutBlobId);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState())
+    EXPECT_CALL(*verifyMockPtr, status())
         .WillOnce(Return(VerifyCheckResponses::failed));
 
     blobs::BlobMeta meta, expectedMeta = {};
@@ -128,7 +128,7 @@
      * committed and transitions to verificationCompleted.
      */
     getToVerificationStarted(staticLayoutBlobId);
-    EXPECT_CALL(*verifyMockPtr, checkVerificationState())
+    EXPECT_CALL(*verifyMockPtr, status())
         .WillOnce(Return(VerifyCheckResponses::success));
 
     blobs::BlobMeta meta, expectedMeta = {};
diff --git a/test/firmware_unittest.hpp b/test/firmware_unittest.hpp
index d49e9f5..d077818 100644
--- a/test/firmware_unittest.hpp
+++ b/test/firmware_unittest.hpp
@@ -79,8 +79,7 @@
     {
         getToVerificationStarted(staticLayoutBlobId);
 
-        EXPECT_CALL(*verifyMockPtr, checkVerificationState())
-            .WillOnce(Return(checkResponse));
+        EXPECT_CALL(*verifyMockPtr, status()).WillOnce(Return(checkResponse));
         blobs::BlobMeta meta;
         EXPECT_TRUE(handler->stat(session, &meta));
         expectedState(FirmwareBlobHandler::UpdateState::verificationCompleted);
diff --git a/test/verification_mock.hpp b/test/verification_mock.hpp
index e027f00..b3071ef 100644
--- a/test/verification_mock.hpp
+++ b/test/verification_mock.hpp
@@ -16,7 +16,7 @@
   public:
     MOCK_METHOD0(triggerVerification, bool());
     MOCK_METHOD0(abortVerification, void());
-    MOCK_METHOD0(checkVerificationState, VerifyCheckResponses());
+    MOCK_METHOD0(status, VerifyCheckResponses());
 };
 
 std::unique_ptr<VerificationInterface> CreateVerifyMock()
diff --git a/verify.hpp b/verify.hpp
index 59407c6..d7574de 100644
--- a/verify.hpp
+++ b/verify.hpp
@@ -24,7 +24,7 @@
     virtual void abortVerification() = 0;
 
     /** Check the current state of the verification process. */
-    virtual VerifyCheckResponses checkVerificationState() = 0;
+    virtual VerifyCheckResponses status() = 0;
 };
 
 } // namespace ipmi_flash
diff --git a/verify_systemd.cpp b/verify_systemd.cpp
index 2cd418b..11386e0 100644
--- a/verify_systemd.cpp
+++ b/verify_systemd.cpp
@@ -67,7 +67,7 @@
     /* TODO: Implement this. */
 }
 
-VerifyCheckResponses SystemdVerification::checkVerificationState()
+VerifyCheckResponses SystemdVerification::status()
 {
     VerifyCheckResponses result = VerifyCheckResponses::other;
 
diff --git a/verify_systemd.hpp b/verify_systemd.hpp
index d64d8c8..5e63e63 100644
--- a/verify_systemd.hpp
+++ b/verify_systemd.hpp
@@ -47,7 +47,7 @@
 
     bool triggerVerification() override;
     void abortVerification() override;
-    VerifyCheckResponses checkVerificationState() override;
+    VerifyCheckResponses status() override;
 
   private:
     sdbusplus::bus::bus bus;