blob: 7c65fe2a451a5c7f1cc9436909ed7830100bd054 [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 Frysinger6cb624a2012-05-24 18:17:38 -04008import os
Chris Sosadad0d322011-01-31 16:37:33 -08009import sys
Chris Sosadad0d322011-01-31 16:37:33 -080010
Mike Frysinger6cb624a2012-05-24 18:17:38 -040011sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
12 '..', '..'))
J. Richard Barnetted422f622011-11-17 09:39:46 -080013from chromite.lib import cros_build_lib
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040014from chromite.lib import cros_build_lib_unittest
Brian Harringc92788f2012-09-21 18:07:15 -070015from chromite.lib import cros_test_lib
David James97d95872012-11-16 15:09:56 -080016from chromite.lib import git
David James59a0a2b2013-03-22 14:04:44 -070017from chromite.lib import osutils
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040018from chromite.lib import parallel_unittest
19from chromite.lib import partial_mock
Mike Frysinger6cb624a2012-05-24 18:17:38 -040020from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080021
Chris Sosa62ad8522011-03-08 17:46:17 -080022
David Jamesfba499d2011-10-19 10:47:21 -070023# pylint: disable=W0212,R0904
Brian Harringc92788f2012-09-21 18:07:15 -070024class NonClassTests(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070025 """Test the flow for pushing a change."""
Chris Sosadad0d322011-01-31 16:37:33 -080026 def setUp(self):
David James3a373092011-11-18 15:56:31 -080027 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080028 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070029 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080030
Matt Tennantcb522052013-11-25 14:23:43 -080031 def _TestPushChange(self, bad_cls):
Chris Sosadad0d322011-01-31 16:37:33 -080032 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
33 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
34 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
35 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
36 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
David James97d95872012-11-16 15:09:56 -080037 self.mox.StubOutWithMock(git, 'PushWithRetry')
38 self.mox.StubOutWithMock(git, 'GetTrackingBranch')
39 self.mox.StubOutWithMock(git, 'SyncPushBranch')
40 self.mox.StubOutWithMock(git, 'CreatePushBranch')
41 self.mox.StubOutWithMock(git, 'RunGit')
Chris Sosadad0d322011-01-31 16:37:33 -080042
Matt Tennantcb522052013-11-25 14:23:43 -080043 # Run the flow.
Chris Sosadad0d322011-01-31 16:37:33 -080044 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040045 self._branch, self._target_manifest_branch, '.').AndReturn(True)
David James97d95872012-11-16 15:09:56 -080046 git.GetTrackingBranch('.', for_push=True).AndReturn(
Brian Harring609dc4e2012-05-07 02:17:44 -070047 ['gerrit', 'refs/remotes/gerrit/master'])
David James97d95872012-11-16 15:09:56 -080048 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070049 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070050 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Matt Tennantcb522052013-11-25 14:23:43 -080051
52 # Look for bad CLs.
53 cmd = ['log', '--format=short', '--perl-regexp', '--author',
54 '^(?!chrome-bot)', 'refs/remotes/gerrit/master..%s' % self._branch]
55
56 if bad_cls:
57 result = cros_build_lib.CommandResult(output='Found bad stuff')
58 git.RunGit('.', cmd).AndReturn(result)
59 else:
60 result = cros_build_lib.CommandResult(output='\n')
61 git.RunGit('.', cmd).AndReturn(result)
62 result = cros_build_lib.CommandResult(output=git_log)
63 cmd = ['log', '--format=format:%s%n%n%b',
64 'refs/remotes/gerrit/master..%s' % self._branch]
65 git.RunGit('.', cmd).AndReturn(result)
66 git.CreatePushBranch('merge_branch', '.')
67 git.RunGit('.', ['merge', '--squash', self._branch])
68 git.RunGit('.', ['commit', '-m', fake_description])
69 git.RunGit('.', ['config', 'push.default', 'tracking'])
70 git.PushWithRetry('merge_branch', '.', dryrun=False)
71
Chris Sosadad0d322011-01-31 16:37:33 -080072 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040073 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
74 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080075 self.mox.VerifyAll()
76
Matt Tennantcb522052013-11-25 14:23:43 -080077 def testPushChange(self):
78 self._TestPushChange(bad_cls=False)
79
80 def testPushChangeBadCls(self):
81 self.assertRaises(AssertionError, self._TestPushChange, bad_cls=True)
82
Chris Sosadad0d322011-01-31 16:37:33 -080083
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040084class CleanStalePackagesTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070085 """Tests for cros_mark_as_stable.CleanStalePackages."""
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040086
David James59a0a2b2013-03-22 14:04:44 -070087 def setUp(self):
88 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
89
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040090 def testNormalClean(self):
91 """Clean up boards/packages with normal success"""
92 cros_mark_as_stable.CleanStalePackages(('board1', 'board2'), ['cow', 'car'])
93
94 def testNothingToUnmerge(self):
95 """Clean up packages that don't exist (portage will exit 1)"""
96 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=1)
97 cros_mark_as_stable.CleanStalePackages((), ['no/pkg'])
98
99 def testUnmergeError(self):
100 """Make sure random exit errors are not ignored"""
101 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=123)
102 with parallel_unittest.ParallelMock():
103 self.assertRaises(cros_build_lib.RunCommandError,
David James59a0a2b2013-03-22 14:04:44 -0700104 cros_mark_as_stable.CleanStalePackages,
105 (), ['no/pkg'])
Mike Frysingerde5ab0e2013-03-21 20:48:36 -0400106
107
Brian Harringc92788f2012-09-21 18:07:15 -0700108class GitBranchTest(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700109 """Tests for cros_mark_as_stable.GitBranch."""
Chris Sosadad0d322011-01-31 16:37:33 -0800110
111 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -0800112 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400113 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -0800114 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
115 self._branch_name = 'test_branch'
116 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -0700117 self._target_manifest_branch = 'cros/test'
118 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400119 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -0800120
121 def testCheckoutCreate(self):
122 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400123 self._branch.Exists(self._branch_name).AndReturn(False)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800124 cros_build_lib.RunCommand(['repo', 'start', self._branch_name, '.'],
125 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800126 self.mox.ReplayAll()
127 cros_mark_as_stable.GitBranch.Checkout(self._branch)
128 self.mox.VerifyAll()
129
130 def testCheckoutNoCreate(self):
131 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400132 self._branch.Exists(self._branch_name).AndReturn(True)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800133 cros_build_lib.RunCommand(['git', 'checkout', '-f', self._branch_name],
134 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800135 self.mox.ReplayAll()
136 cros_mark_as_stable.GitBranch.Checkout(self._branch)
137 self.mox.VerifyAll()
138
Chris Sosadad0d322011-01-31 16:37:33 -0800139 def testExists(self):
140 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400141 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800142 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400143 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
David James67d73252013-09-19 17:33:12 -0700144 git.RunGit('.', ['branch']).AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800145 self.mox.ReplayAll()
146 self.assertTrue(branch.Exists())
147 self.mox.VerifyAll()
148
149
Chris Sosadad0d322011-01-31 16:37:33 -0800150if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700151 cros_test_lib.main()