blob: 6f3f5562aed05c839ffcf606012316efc420e403 [file] [log] [blame]
Chris Sosadad0d322011-01-31 16:37:33 -08001#!/usr/bin/python
Mike Frysinger6cb624a2012-05-24 18:17:38 -04002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosadad0d322011-01-31 16:37:33 -08003# 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
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Mike Frysinger6cb624a2012-05-24 18:17:38 -040010import os
Chris Sosadad0d322011-01-31 16:37:33 -080011import sys
Chris Sosadad0d322011-01-31 16:37:33 -080012
Mike Frysinger6cb624a2012-05-24 18:17:38 -040013sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
14 '..', '..'))
J. Richard Barnetted422f622011-11-17 09:39:46 -080015from chromite.lib import cros_build_lib
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040016from chromite.lib import cros_build_lib_unittest
Brian Harringc92788f2012-09-21 18:07:15 -070017from chromite.lib import cros_test_lib
David James97d95872012-11-16 15:09:56 -080018from chromite.lib import git
David James59a0a2b2013-03-22 14:04:44 -070019from chromite.lib import osutils
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040020from chromite.lib import parallel_unittest
21from chromite.lib import partial_mock
Mike Frysinger6cb624a2012-05-24 18:17:38 -040022from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080023
Mike Frysingeref6ccb22014-11-07 14:59:48 -050024# TODO(build): Finish test wrapper (http://crosbug.com/37517).
25# Until then, this has to be after the chromite imports.
26import mock
Chris Sosa62ad8522011-03-08 17:46:17 -080027
Mike Frysingeref6ccb22014-11-07 14:59:48 -050028
29# pylint: disable=W0212
30
31
Brian Harringc92788f2012-09-21 18:07:15 -070032class NonClassTests(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070033 """Test the flow for pushing a change."""
Chris Sosadad0d322011-01-31 16:37:33 -080034 def setUp(self):
David James3a373092011-11-18 15:56:31 -080035 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080036 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070037 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080038
Matt Tennantcb522052013-11-25 14:23:43 -080039 def _TestPushChange(self, bad_cls):
Chris Sosadad0d322011-01-31 16:37:33 -080040 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
41 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
42 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
43 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
44 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
David James97d95872012-11-16 15:09:56 -080045 self.mox.StubOutWithMock(git, 'PushWithRetry')
46 self.mox.StubOutWithMock(git, 'GetTrackingBranch')
47 self.mox.StubOutWithMock(git, 'SyncPushBranch')
48 self.mox.StubOutWithMock(git, 'CreatePushBranch')
49 self.mox.StubOutWithMock(git, 'RunGit')
Chris Sosadad0d322011-01-31 16:37:33 -080050
Matt Tennantcb522052013-11-25 14:23:43 -080051 # Run the flow.
Chris Sosadad0d322011-01-31 16:37:33 -080052 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040053 self._branch, self._target_manifest_branch, '.').AndReturn(True)
David James97d95872012-11-16 15:09:56 -080054 git.GetTrackingBranch('.', for_push=True).AndReturn(
Brian Harring609dc4e2012-05-07 02:17:44 -070055 ['gerrit', 'refs/remotes/gerrit/master'])
David James97d95872012-11-16 15:09:56 -080056 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070057 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070058 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Matt Tennantcb522052013-11-25 14:23:43 -080059
60 # Look for bad CLs.
61 cmd = ['log', '--format=short', '--perl-regexp', '--author',
62 '^(?!chrome-bot)', 'refs/remotes/gerrit/master..%s' % self._branch]
63
64 if bad_cls:
65 result = cros_build_lib.CommandResult(output='Found bad stuff')
66 git.RunGit('.', cmd).AndReturn(result)
67 else:
68 result = cros_build_lib.CommandResult(output='\n')
69 git.RunGit('.', cmd).AndReturn(result)
70 result = cros_build_lib.CommandResult(output=git_log)
71 cmd = ['log', '--format=format:%s%n%n%b',
72 'refs/remotes/gerrit/master..%s' % self._branch]
73 git.RunGit('.', cmd).AndReturn(result)
74 git.CreatePushBranch('merge_branch', '.')
75 git.RunGit('.', ['merge', '--squash', self._branch])
76 git.RunGit('.', ['commit', '-m', fake_description])
77 git.RunGit('.', ['config', 'push.default', 'tracking'])
78 git.PushWithRetry('merge_branch', '.', dryrun=False)
79
Chris Sosadad0d322011-01-31 16:37:33 -080080 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040081 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
82 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080083 self.mox.VerifyAll()
84
Matt Tennantcb522052013-11-25 14:23:43 -080085 def testPushChange(self):
86 self._TestPushChange(bad_cls=False)
87
88 def testPushChangeBadCls(self):
89 self.assertRaises(AssertionError, self._TestPushChange, bad_cls=True)
90
Chris Sosadad0d322011-01-31 16:37:33 -080091
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040092class CleanStalePackagesTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070093 """Tests for cros_mark_as_stable.CleanStalePackages."""
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040094
David James59a0a2b2013-03-22 14:04:44 -070095 def setUp(self):
96 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
97
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040098 def testNormalClean(self):
99 """Clean up boards/packages with normal success"""
100 cros_mark_as_stable.CleanStalePackages(('board1', 'board2'), ['cow', 'car'])
101
102 def testNothingToUnmerge(self):
103 """Clean up packages that don't exist (portage will exit 1)"""
104 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=1)
105 cros_mark_as_stable.CleanStalePackages((), ['no/pkg'])
106
107 def testUnmergeError(self):
108 """Make sure random exit errors are not ignored"""
109 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=123)
110 with parallel_unittest.ParallelMock():
111 self.assertRaises(cros_build_lib.RunCommandError,
David James59a0a2b2013-03-22 14:04:44 -0700112 cros_mark_as_stable.CleanStalePackages,
113 (), ['no/pkg'])
Mike Frysingerde5ab0e2013-03-21 20:48:36 -0400114
115
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500116class GitBranchTest(cros_test_lib.MockTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700117 """Tests for cros_mark_as_stable.GitBranch."""
Chris Sosadad0d322011-01-31 16:37:33 -0800118
119 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -0800120 # Always stub RunCommmand out as we use it in every method.
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500121 self.rc_mock = self.PatchObject(cros_build_lib, 'RunCommand')
122
Chris Sosadad0d322011-01-31 16:37:33 -0800123 self._branch_name = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -0700124 self._target_manifest_branch = 'cros/test'
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500125 self._branch = cros_mark_as_stable.GitBranch(
126 branch_name=self._branch_name,
127 tracking_branch=self._target_manifest_branch,
128 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -0800129
130 def testCheckoutCreate(self):
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500131 """Test init with no previous branch existing."""
132 self.PatchObject(self._branch, 'Exists', return_value=False)
Chris Sosadad0d322011-01-31 16:37:33 -0800133 cros_mark_as_stable.GitBranch.Checkout(self._branch)
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500134 self.rc_mock.assert_call(mock.call(
135 ['repo', 'start', self._branch_name, '.'],
136 print_cmd=False, cwd='.', capture_output=True))
Chris Sosadad0d322011-01-31 16:37:33 -0800137
138 def testCheckoutNoCreate(self):
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500139 """Test init with previous branch existing."""
140 self.PatchObject(self._branch, 'Exists', return_value=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800141 cros_mark_as_stable.GitBranch.Checkout(self._branch)
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500142 self.rc_mock.assert_call(mock.call(
143 ['git', 'checkout', '-f', self._branch_name],
144 print_cmd=False, cwd='.', capture_output=True))
Chris Sosadad0d322011-01-31 16:37:33 -0800145
Chris Sosadad0d322011-01-31 16:37:33 -0800146 def testExists(self):
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500147 """Test if branch exists that is created."""
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400148 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysingeref6ccb22014-11-07 14:59:48 -0500149 self.PatchObject(git, 'RunGit', return_value=result)
150 self.assertTrue(self._branch.Exists())
Chris Sosadad0d322011-01-31 16:37:33 -0800151
152
Chris Sosadad0d322011-01-31 16:37:33 -0800153if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700154 cros_test_lib.main()