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(
        status=constants.BUILDER_STATUS_PASSED,
        buildroot_layout=1,
        branch='branchA')
    self.populateBuildroot(previous_build_state=old_build_state.to_json())
    self.mock_repo.branch = 'branchA'
    self.mock_repo.BuildRootGitCleanup.side_effect = Exception

    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')
    self.assertIsNotNone(new_summary.distfiles_ts)
    self.assertEqual(new_summary, build_state)

    self.assertNotExists(self.repo)
    self.assertNotExists(self.chroot)
    self.assertNotExists(self.general)
    self.assertNotExists(self.distfiles)
    self.assertExists(self.previous_build_state)
  def testBuildrootBranchChange(self):
    """Test CleanBuildRoot with a change in branches."""
    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 = 'branchB'
    m = self.PatchObject(cros_sdk_lib, 'CleanupChrootMount')

    build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_INFLIGHT,
        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
        branch='branchB')
    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, 'branchB')
    self.assertIsNotNone(new_summary.distfiles_ts)
    self.assertEqual(new_summary, build_state)

    # self.assertExists(self.repo)
    self.assertExists(self.general)
    self.assertNotExists(self.distfiles)
    self.assertExists(self.previous_build_state)
    m.assert_called_with(self.chroot, delete=True)
  def testBuildrootDistfilesCacheExpired(self):
    """Test CleanBuildRoot when the distfiles cache is too old."""
    old_build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_PASSED,
        buildroot_layout=2,
        branch='branchA',
        distfiles_ts=100.0)
    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, self.cache, build_state)

    new_summary = cbuildbot_launch.GetLastBuildState(self.root)
    self.assertEqual(new_summary.buildroot_layout, 2)
    self.assertEqual(new_summary.branch, 'branchA')
    self.assertIsNotNone(new_summary.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 testBuildrootFormatMismatch(self):
    """Test CleanBuildRoot with buildroot layout mismatch."""
    old_build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_PASSED,
        buildroot_layout=1,
        branch='master')
    self.populateBuildroot(previous_build_state=old_build_state.to_json())
    self.mock_repo.branch = 'master'

    build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_INFLIGHT,
        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
        branch='master')
    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, 'master')
    self.assertIsNotNone(new_summary.distfiles_ts)
    self.assertEqual(new_summary, build_state)

    self.assertNotExists(self.repo)
    self.assertNotExists(self.chroot)
    self.assertNotExists(self.general)
    self.assertNotExists(self.distfiles)
    self.assertExists(self.previous_build_state)
  def testBuildrootDistfilesRecentCache(self):
    """Test CleanBuildRoot does not delete distfiles when cache is recent."""
    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'

    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.metrics, 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.assertExists(self.distfiles)
    self.assertExists(self.previous_build_state)
  def testNoBuildroot(self):
    """Test CleanBuildRoot with no history."""
    self.mock_repo.branch = 'master'

    build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_INFLIGHT,
        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
        branch='master')
    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, 'master')
    self.assertIsNotNone(new_summary.distfiles_ts)
    self.assertEqual(new_summary, build_state)

    self.assertExists(self.previous_build_state)
    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, self.cache,
                                        build_state)

        self.assertEqual(self.mock_repo.mock_calls, [
            mock.call.PreLoad(),
            mock.call.BuildRootGitCleanup(prune_all=True),
        ])
  def testBuildrootNoState(self):
    """Test CleanBuildRoot with no state information."""
    self.populateBuildroot()
    self.mock_repo.branch = 'master'

    build_state = build_summary.BuildSummary(
        status=constants.BUILDER_STATUS_INFLIGHT,
        buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
        branch='master')
    cbuildbot_launch.CleanBuildRoot(
        self.root, self.mock_repo, self.metrics, build_state)

    new_summary = cbuildbot_launch.GetLastBuildState(self.root)
    self.assertEqual(new_summary.buildroot_layout, 2)
    self.assertEqual(new_summary.branch, 'master')
    self.assertIsNotNone(new_summary.distfiles_ts)
    self.assertEqual(new_summary, build_state)

    self.assertNotExists(self.repo)
    self.assertNotExists(self.chroot)
    self.assertNotExists(self.general)
    self.assertNotExists(self.distfiles)
    self.assertExists(self.previous_build_state)