3 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Unit tests for cros_mark_as_stable.py."""
12 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 from chromite.lib import cros_build_lib
15 from chromite.lib import cros_build_lib_unittest
16 from chromite.lib import cros_test_lib
17 from chromite.lib import git
18 from chromite.lib import osutils
19 from chromite.lib import parallel_unittest
20 from chromite.lib import partial_mock
21 from chromite.scripts import cros_mark_as_stable
24 # pylint: disable=W0212,R0904
25 class NonClassTests(cros_test_lib.MoxTestCase):
26 """Test the flow for pushing a change."""
28 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
29 self._branch = 'test_branch'
30 self._target_manifest_branch = 'cros/master'
32 def _TestPushChange(self, bad_cls):
33 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
34 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
35 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
36 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
37 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
38 self.mox.StubOutWithMock(git, 'PushWithRetry')
39 self.mox.StubOutWithMock(git, 'GetTrackingBranch')
40 self.mox.StubOutWithMock(git, 'SyncPushBranch')
41 self.mox.StubOutWithMock(git, 'CreatePushBranch')
42 self.mox.StubOutWithMock(git, 'RunGit')
45 cros_mark_as_stable._DoWeHaveLocalCommits(
46 self._branch, self._target_manifest_branch, '.').AndReturn(True)
47 git.GetTrackingBranch('.', for_push=True).AndReturn(
48 ['gerrit', 'refs/remotes/gerrit/master'])
49 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
50 cros_mark_as_stable._DoWeHaveLocalCommits(
51 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
54 cmd = ['log', '--format=short', '--perl-regexp', '--author',
55 '^(?!chrome-bot)', 'refs/remotes/gerrit/master..%s' % self._branch]
58 result = cros_build_lib.CommandResult(output='Found bad stuff')
59 git.RunGit('.', cmd).AndReturn(result)
61 result = cros_build_lib.CommandResult(output='\n')
62 git.RunGit('.', cmd).AndReturn(result)
63 result = cros_build_lib.CommandResult(output=git_log)
64 cmd = ['log', '--format=format:%s%n%n%b',
65 'refs/remotes/gerrit/master..%s' % self._branch]
66 git.RunGit('.', cmd).AndReturn(result)
67 git.CreatePushBranch('merge_branch', '.')
68 git.RunGit('.', ['merge', '--squash', self._branch])
69 git.RunGit('.', ['commit', '-m', fake_description])
70 git.RunGit('.', ['config', 'push.default', 'tracking'])
71 git.PushWithRetry('merge_branch', '.', dryrun=False)
74 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
78 def testPushChange(self):
79 self._TestPushChange(bad_cls=False)
81 def testPushChangeBadCls(self):
82 self.assertRaises(AssertionError, self._TestPushChange, bad_cls=True)
85 class CleanStalePackagesTest(cros_build_lib_unittest.RunCommandTestCase):
86 """Tests for cros_mark_as_stable.CleanStalePackages."""
89 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
91 def testNormalClean(self):
92 """Clean up boards/packages with normal success"""
93 cros_mark_as_stable.CleanStalePackages(('board1', 'board2'), ['cow', 'car'])
95 def testNothingToUnmerge(self):
96 """Clean up packages that don't exist (portage will exit 1)"""
97 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=1)
98 cros_mark_as_stable.CleanStalePackages((), ['no/pkg'])
100 def testUnmergeError(self):
101 """Make sure random exit errors are not ignored"""
102 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=123)
103 with parallel_unittest.ParallelMock():
104 self.assertRaises(cros_build_lib.RunCommandError,
105 cros_mark_as_stable.CleanStalePackages,
109 class GitBranchTest(cros_test_lib.MoxTestCase):
110 """Tests for cros_mark_as_stable.GitBranch."""
113 # Always stub RunCommmand out as we use it in every method.
114 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
115 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
116 self._branch_name = 'test_branch'
117 self._branch.branch_name = self._branch_name
118 self._target_manifest_branch = 'cros/test'
119 self._branch.tracking_branch = self._target_manifest_branch
120 self._branch.cwd = '.'
122 def testCheckoutCreate(self):
123 # Test init with no previous branch existing.
124 self._branch.Exists(self._branch_name).AndReturn(False)
125 cros_build_lib.RunCommand(['repo', 'start', self._branch_name, '.'],
126 print_cmd=False, cwd='.', capture_output=True)
128 cros_mark_as_stable.GitBranch.Checkout(self._branch)
131 def testCheckoutNoCreate(self):
132 # Test init with previous branch existing.
133 self._branch.Exists(self._branch_name).AndReturn(True)
134 cros_build_lib.RunCommand(['git', 'checkout', '-f', self._branch_name],
135 print_cmd=False, cwd='.', capture_output=True)
137 cros_mark_as_stable.GitBranch.Checkout(self._branch)
140 def testExists(self):
141 branch = cros_mark_as_stable.GitBranch(self._branch_name,
142 self._target_manifest_branch, '.')
143 # Test if branch exists that is created
144 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
145 git.RunGit('.', ['branch']).AndReturn(result)
147 self.assertTrue(branch.Exists())
151 if __name__ == '__main__':