cbuildbot_launch: If repo cleanup fails, wipe buildroot.

The is the same logic used in the Sync stages, except for being a bit
more straight forward, and triggering on any exception. This does NOT
preserve cbuildbot_logs, trybot markers or other files that aren't
relevent on the builders.

BUG=chromium:722619
TEST=Unittests + local builds.

Change-Id: I0cdee84aac3317c4faa9996b2f3c594983189587
Reviewed-on: https://chromium-review.googlesource.com/508853
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Ningning Xia <nxia@chromium.org>
Commit-Queue: Don Garrett <dgarrett@chromium.org>
diff --git a/scripts/cbuildbot_launch_unittest.py b/scripts/cbuildbot_launch_unittest.py
index 1f94e0f..873ed9a 100644
--- a/scripts/cbuildbot_launch_unittest.py
+++ b/scripts/cbuildbot_launch_unittest.py
@@ -20,6 +20,10 @@
 EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal'  # pylint: disable=line-too-long
 
 
+class FakeException(Exception):
+  """Test exception to raise during tests."""
+
+
 class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
   """Tests for cbuildbot_launch script."""
 
@@ -79,6 +83,28 @@
         mock.call().Sync(detach=True),
     ])
 
+  def testInitialCheckoutCleanupError(self):
+    """Test we wipe buildroot when cleanup fails."""
+    mock_clean = self.PatchObject(
+        repository.RepoRepository, 'BuildRootGitCleanup', autospec=True,
+        side_effect=FakeException)
+    mock_sync = self.PatchObject(
+        repository.RepoRepository, 'Sync', autospec=True)
+    mock_remove = self.PatchObject(
+        repository, 'ClearBuildRoot', autospec=True)
+
+    cbuildbot_launch.InitialCheckout('master', '/buildroot', None)
+
+    self.assertEqual(mock_clean.mock_calls, [
+        mock.call(mock.ANY, prune_all=True),
+    ])
+    self.assertEqual(mock_sync.mock_calls, [
+        mock.call(mock.ANY, detach=True),
+    ])
+    self.assertEqual(mock_remove.mock_calls, [
+        mock.call('/buildroot'),
+    ])
+
   def testConfigureGlobalEnvironment(self):
     """Ensure that we can setup our global runtime environment correctly."""
     cbuildbot_launch.ConfigureGlobalEnvironment()