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