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 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 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 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 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 testGetLastBuildStateGoodFile(self): """Tests GetLastBuildState on a good file.""" osutils.SafeMakedirs(self.root) osutils.WriteFile( self.previous_build_state, '{"build_number": 1, "master_build_id": 3, "status": "pass"}') state = cbuildbot_launch.GetLastBuildState(self.root) self.assertEqual( state, build_summary.BuildSummary( build_number=1, master_build_id=3, status='pass'))
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 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)
def testGetLastBuildStateMissingBuildStatus(self): """Tests GetLastBuildState if the file doesn't have a valid status.""" osutils.SafeMakedirs(self.root) osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}') state = cbuildbot_launch.GetLastBuildState(self.root) self.assertEqual(state, build_summary.BuildSummary())
def testGetLastBuildStateBadFile(self): """Tests GetLastBuildState if the file contains invalid JSON.""" osutils.SafeMakedirs(self.root) osutils.WriteFile(self.previous_build_state, '}}') state = cbuildbot_launch.GetLastBuildState(self.root) self.assertEqual(state, build_summary.BuildSummary())
def testGetLastBuildStateNoFile(self): """Tests GetLastBuildState if the file is missing.""" osutils.SafeMakedirs(self.root) state = cbuildbot_launch.GetLastBuildState(self.root) self.assertEqual(state, build_summary.BuildSummary())