blob: b209cc6025f0fd32cc738cd407f589fc9f41d5c0 [file] [log] [blame]
Chris Sosadad0d322011-01-31 16:37:33 -08001#!/usr/bin/python
2
Mike Frysinger6cb624a2012-05-24 18:17:38 -04003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosadad0d322011-01-31 16:37:33 -08004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for cros_mark_as_stable.py."""
8
Mike Frysinger6cb624a2012-05-24 18:17:38 -04009import os
Chris Sosadad0d322011-01-31 16:37:33 -080010import sys
Chris Sosadad0d322011-01-31 16:37:33 -080011
Mike Frysinger6cb624a2012-05-24 18:17:38 -040012sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
13 '..', '..'))
J. Richard Barnetted422f622011-11-17 09:39:46 -080014from chromite.lib import cros_build_lib
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040015from chromite.lib import cros_build_lib_unittest
Brian Harringc92788f2012-09-21 18:07:15 -070016from chromite.lib import cros_test_lib
David James97d95872012-11-16 15:09:56 -080017from chromite.lib import git
David James59a0a2b2013-03-22 14:04:44 -070018from chromite.lib import osutils
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040019from chromite.lib import parallel_unittest
20from chromite.lib import partial_mock
Mike Frysinger6cb624a2012-05-24 18:17:38 -040021from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080022
Chris Sosa62ad8522011-03-08 17:46:17 -080023
David Jamesfba499d2011-10-19 10:47:21 -070024# pylint: disable=W0212,R0904
Brian Harringc92788f2012-09-21 18:07:15 -070025class NonClassTests(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070026 """Test the flow for pushing a change."""
Chris Sosadad0d322011-01-31 16:37:33 -080027 def setUp(self):
David James3a373092011-11-18 15:56:31 -080028 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080029 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070030 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080031
Matt Tennantcb522052013-11-25 14:23:43 -080032 def _TestPushChange(self, bad_cls):
Chris Sosadad0d322011-01-31 16:37:33 -080033 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')
David James97d95872012-11-16 15:09:56 -080038 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')
Chris Sosadad0d322011-01-31 16:37:33 -080043
Matt Tennantcb522052013-11-25 14:23:43 -080044 # Run the flow.
Chris Sosadad0d322011-01-31 16:37:33 -080045 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040046 self._branch, self._target_manifest_branch, '.').AndReturn(True)
David James97d95872012-11-16 15:09:56 -080047 git.GetTrackingBranch('.', for_push=True).AndReturn(
Brian Harring609dc4e2012-05-07 02:17:44 -070048 ['gerrit', 'refs/remotes/gerrit/master'])
David James97d95872012-11-16 15:09:56 -080049 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070050 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070051 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Matt Tennantcb522052013-11-25 14:23:43 -080052
53 # Look for bad CLs.
54 cmd = ['log', '--format=short', '--perl-regexp', '--author',
55 '^(?!chrome-bot)', 'refs/remotes/gerrit/master..%s' % self._branch]
56
57 if bad_cls:
58 result = cros_build_lib.CommandResult(output='Found bad stuff')
59 git.RunGit('.', cmd).AndReturn(result)
60 else:
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)
72
Chris Sosadad0d322011-01-31 16:37:33 -080073 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040074 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
75 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080076 self.mox.VerifyAll()
77
Matt Tennantcb522052013-11-25 14:23:43 -080078 def testPushChange(self):
79 self._TestPushChange(bad_cls=False)
80
81 def testPushChangeBadCls(self):
82 self.assertRaises(AssertionError, self._TestPushChange, bad_cls=True)
83
Chris Sosadad0d322011-01-31 16:37:33 -080084
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040085class CleanStalePackagesTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070086 """Tests for cros_mark_as_stable.CleanStalePackages."""
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040087
David James59a0a2b2013-03-22 14:04:44 -070088 def setUp(self):
89 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
90
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040091 def testNormalClean(self):
92 """Clean up boards/packages with normal success"""
93 cros_mark_as_stable.CleanStalePackages(('board1', 'board2'), ['cow', 'car'])
94
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'])
99
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,
David James59a0a2b2013-03-22 14:04:44 -0700105 cros_mark_as_stable.CleanStalePackages,
106 (), ['no/pkg'])
Mike Frysingerde5ab0e2013-03-21 20:48:36 -0400107
108
Brian Harringc92788f2012-09-21 18:07:15 -0700109class GitBranchTest(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700110 """Tests for cros_mark_as_stable.GitBranch."""
Chris Sosadad0d322011-01-31 16:37:33 -0800111
112 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -0800113 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400114 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -0800115 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
116 self._branch_name = 'test_branch'
117 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -0700118 self._target_manifest_branch = 'cros/test'
119 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400120 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -0800121
122 def testCheckoutCreate(self):
123 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400124 self._branch.Exists(self._branch_name).AndReturn(False)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800125 cros_build_lib.RunCommand(['repo', 'start', self._branch_name, '.'],
126 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800127 self.mox.ReplayAll()
128 cros_mark_as_stable.GitBranch.Checkout(self._branch)
129 self.mox.VerifyAll()
130
131 def testCheckoutNoCreate(self):
132 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400133 self._branch.Exists(self._branch_name).AndReturn(True)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800134 cros_build_lib.RunCommand(['git', 'checkout', '-f', self._branch_name],
135 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800136 self.mox.ReplayAll()
137 cros_mark_as_stable.GitBranch.Checkout(self._branch)
138 self.mox.VerifyAll()
139
Chris Sosadad0d322011-01-31 16:37:33 -0800140 def testExists(self):
141 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400142 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800143 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400144 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
David James67d73252013-09-19 17:33:12 -0700145 git.RunGit('.', ['branch']).AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800146 self.mox.ReplayAll()
147 self.assertTrue(branch.Exists())
148 self.mox.VerifyAll()
149
150
Chris Sosadad0d322011-01-31 16:37:33 -0800151if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700152 cros_test_lib.main()