tools: add cleanup blob on failure

If the update process fails, call the cleanup blob and try to cleanup
artifacts automatically.

Signed-off-by: Patrick Venture <venture@google.com>
Change-Id: Ib57e5cc7f13b73e2d7371116058469a4d4bfd0c4
diff --git a/tools/test/tools_updater_unittest.cpp b/tools/test/tools_updater_unittest.cpp
index e54e613..5b3cb5b 100644
--- a/tools/test/tools_updater_unittest.cpp
+++ b/tools/test/tools_updater_unittest.cpp
@@ -1,5 +1,6 @@
 #include "data_interface_mock.hpp"
 #include "status.hpp"
+#include "tool_errors.hpp"
 #include "updater.hpp"
 #include "updater_mock.hpp"
 #include "util.hpp"
@@ -138,4 +139,20 @@
     updaterMain(&handler, image, signature);
 }
 
+TEST_F(UpdaterTest, UpdateMainCleansUpOnFailure)
+{
+    const std::string image = "image.bin";
+    const std::string signature = "signature.bin";
+    UpdateHandlerMock handler;
+
+    EXPECT_CALL(handler, checkAvailable(_)).WillOnce(Return(true));
+    EXPECT_CALL(handler, sendFile(_, image)).WillOnce(Return());
+    EXPECT_CALL(handler, sendFile(_, signature)).WillOnce(Return());
+    EXPECT_CALL(handler, verifyFile(ipmi_flash::verifyBlobId))
+        .WillOnce(Return(false));
+    EXPECT_CALL(handler, cleanArtifacts()).WillOnce(Return());
+
+    EXPECT_THROW(updaterMain(&handler, image, signature), ToolException);
+}
+
 } // namespace host_tool