cbuildbot_launch: Clean .cache.

Some builds are having problems accessing .cache, because of
permission problems.

Move the process of defining and validating .cache into
cbuildbot_launch, which will create the directory, and cleanup invalid
contents, then pass an explicit cache directory path to cbuildbot.

BUG=chromium:936123
TEST=run_tests

Change-Id: Ibc6db01bf4fe3700091241e5172207454f58f65c
Reviewed-on: https://chromium-review.googlesource.com/c/1491938
Tested-by: Don Garrett <dgarrett@chromium.org>
Trybot-Ready: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Evan Hernandez <evanhernandez@chromium.org>
Commit-Queue: Don Garrett <dgarrett@chromium.org>
diff --git a/scripts/cbuildbot_launch_unittest.py b/scripts/cbuildbot_launch_unittest.py
index 3adbb3b..fc7aa1d 100644
--- a/scripts/cbuildbot_launch_unittest.py
+++ b/scripts/cbuildbot_launch_unittest.py
@@ -170,7 +170,7 @@
 
     # Ensure we clean, as expected.
     self.assertEqual(mock_clean.mock_calls, [
-        mock.call('/root', mock_repo,
+        mock.call('/root', mock_repo, '/root/repository/.cache',
                   expected_build_state)])
 
     # Ensure we checkout, as expected.
@@ -184,6 +184,9 @@
             'config',
             '-r', '/root/repository',
             '--workspace', '/root/workspace',
+            '--cache-dir', '/root/repository/.cache',
+            # The duplication is a bug, but not harmful.
+            '--cache-dir', '/root/repository/.cache',
             '--ts-mon-task-num', '1',
         ],
         extra_env={'PATH': mock.ANY},
@@ -233,6 +236,7 @@
     argv = ['--buildroot', '/root',
             '--branch', 'branch',
             '--git-cache-dir', '/git-cache',
+            '--cache-dir', '/cache',
             '--remote-trybot',
             '--master-build-id', '123456789',
             '--buildnumber', '314',
@@ -253,6 +257,7 @@
     self.assertEqual(mock_clean.mock_calls, [
         mock.call('/root',
                   mock_repo,
+                  '/cache',
                   build_summary.BuildSummary(
                       build_number=314,
                       master_build_id=123456789,
@@ -273,6 +278,7 @@
             '--buildroot', '/root/repository',
             '--branch', 'branch',
             '--git-cache-dir', '/git-cache',
+            '--cache-dir', '/cache',
             '--remote-trybot',
             '--master-build-id', '123456789',
             '--buildnumber', '314',
@@ -281,6 +287,7 @@
             'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg'
             'ImJyYW5jaCI6ICJicmFuY2gifQ==',
             '--workspace', '/root/workspace',
+            '--cache-dir', '/cache',
             '--ts-mon-task-num', '1',
         ],
         extra_env={'PATH': mock.ANY},
@@ -339,7 +346,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='master')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -359,7 +366,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='master')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -387,7 +394,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='master')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -416,7 +423,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchB')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -444,7 +451,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -472,7 +479,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     self.assertEqual(
         self.mock_repo.mock_calls, [
@@ -494,7 +501,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     self.assertEqual(
         self.mock_repo.mock_calls, [
@@ -516,7 +523,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
 
     self.assertEqual(
@@ -542,7 +549,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -572,7 +579,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)
@@ -586,6 +593,39 @@
     self.assertNotExists(self.distfiles)
     self.assertExists(self.previous_build_state)
 
+  def testRootOwnedCache(self):
+    """Test CleanBuildRoot with no history."""
+    seed_distfiles_ts = time.time() - 60
+    old_build_state = build_summary.BuildSummary(
+        status=constants.BUILDER_STATUS_PASSED,
+        buildroot_layout=2,
+        branch='branchA',
+        distfiles_ts=seed_distfiles_ts)
+    self.populateBuildroot(previous_build_state=old_build_state.to_json())
+    self.mock_repo.branch = 'branchA'
+
+    osutils.Chown(self.cache, 'root', 'root')
+
+    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, self.cache, build_state)
+
+    new_summary = cbuildbot_launch.GetLastBuildState(self.root)
+    self.assertEqual(new_summary.buildroot_layout, 2)
+    self.assertEqual(new_summary.branch, 'branchA')
+    # Same cache creation timestamp is rewritten to state.
+    self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
+    self.assertEqual(new_summary, build_state)
+
+    self.assertExists(self.repo)
+    self.assertExists(self.chroot)
+    self.assertExists(self.general)
+    self.assertNotExists(self.distfiles)
+    self.assertExists(self.previous_build_state)
+
   def testBuildrootRepoCleanFailure(self):
     """Test CleanBuildRoot with repo checkout failure."""
     old_build_state = build_summary.BuildSummary(
@@ -601,7 +641,7 @@
         buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
         branch='branchA')
     cbuildbot_launch.CleanBuildRoot(
-        self.root, self.mock_repo, build_state)
+        self.root, self.mock_repo, self.cache, build_state)
 
     new_summary = cbuildbot_launch.GetLastBuildState(self.root)
     self.assertEqual(new_summary.buildroot_layout, 2)