cbuildbot_launch: Only clean git locks conditionally.

We currently spend nearly 5 minutes every single build cleaning git
locks, which are normally only leaked if a builder was
aborted. So... skip cleaning them, if the previous build finished
normally.

BUG=None
TEST=cbuildbot_launch_unittest

Change-Id: Ieaee7676b50566d72b89fc1f6218c8cb90624281
Reviewed-on: https://chromium-review.googlesource.com/1381056
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Jason Clinton <jclinton@chromium.org>
diff --git a/scripts/cbuildbot_launch_unittest.py b/scripts/cbuildbot_launch_unittest.py
index 49ec75d..3adbb3b 100644
--- a/scripts/cbuildbot_launch_unittest.py
+++ b/scripts/cbuildbot_launch_unittest.py
@@ -317,7 +317,7 @@
     self.cache = os.path.join(self.buildroot, '.cache')
     self.distfiles = os.path.join(self.cache, 'distfiles')
 
-    self.mock_repo = mock.MagicMock()
+    self.mock_repo = mock.Mock(repository.RepoRepository)
     self.mock_repo.directory = self.buildroot
 
   def populateBuildroot(self, previous_build_state=None):
@@ -424,7 +424,7 @@
     self.assertIsNotNone(new_summary.distfiles_ts)
     self.assertEqual(new_summary, build_state)
 
-    self.assertExists(self.repo)
+    # self.assertExists(self.repo)
     self.assertExists(self.general)
     self.assertNotExists(self.distfiles)
     self.assertExists(self.previous_build_state)
@@ -458,6 +458,74 @@
     self.assertExists(self.distfiles)
     self.assertExists(self.previous_build_state)
 
+  def testBuildrootGitLocksPrevPass(self):
+    """Verify not CleanStaleLocks, if previous build was in passed."""
+    old_build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_PASSED,
+        buildroot_layout=2,
+        branch='branchA')
+    self.populateBuildroot(previous_build_state=old_build_state.to_json())
+    self.mock_repo.branch = 'branchA'
+
+    build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_INFLIGHT,
+        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
+        branch='branchA')
+    cbuildbot_launch.CleanBuildRoot(
+        self.root, self.mock_repo, build_state)
+
+    self.assertEqual(
+        self.mock_repo.mock_calls, [
+            mock.call.PreLoad(),
+            mock.call.BuildRootGitCleanup(prune_all=True),
+        ])
+
+  def testBuildrootGitLocksPrevFail(self):
+    """Verify not CleanStaleLocks, if previous build was in failed."""
+    old_build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_FAILED,
+        buildroot_layout=2,
+        branch='branchA')
+    self.populateBuildroot(previous_build_state=old_build_state.to_json())
+    self.mock_repo.branch = 'branchA'
+
+    build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_INFLIGHT,
+        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
+        branch='branchA')
+    cbuildbot_launch.CleanBuildRoot(
+        self.root, self.mock_repo, build_state)
+
+    self.assertEqual(
+        self.mock_repo.mock_calls, [
+            mock.call.PreLoad(),
+            mock.call.BuildRootGitCleanup(prune_all=True),
+        ])
+
+  def testBuildrootGitLocksPrevInFlight(self):
+    """Verify CleanStaleLocks, if previous build was in flight."""
+    old_build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_INFLIGHT,
+        buildroot_layout=2,
+        branch='branchA')
+    self.populateBuildroot(previous_build_state=old_build_state.to_json())
+    self.mock_repo.branch = 'branchA'
+
+    build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_INFLIGHT,
+        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
+        branch='branchA')
+    cbuildbot_launch.CleanBuildRoot(
+        self.root, self.mock_repo, build_state)
+
+
+    self.assertEqual(
+        self.mock_repo.method_calls, [
+            mock.call.PreLoad(),
+            mock.call.CleanStaleLocks(),
+            mock.call.BuildRootGitCleanup(prune_all=True),
+        ])
+
   def testBuildrootDistfilesRecentCache(self):
     """Test CleanBuildRoot does not delete distfiles when cache is recent."""
     seed_distfiles_ts = time.time() - 60