blob: 5fb99ad6227b5617e0b11157d07d39843183ff9d [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):
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):
85
David James59a0a2b2013-03-22 14:04:44 -070086 def setUp(self):
87 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
88
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040089 def testNormalClean(self):
90 """Clean up boards/packages with normal success"""
91 cros_mark_as_stable.CleanStalePackages(('board1', 'board2'), ['cow', 'car'])
92
93 def testNothingToUnmerge(self):
94 """Clean up packages that don't exist (portage will exit 1)"""
95 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=1)
96 cros_mark_as_stable.CleanStalePackages((), ['no/pkg'])
97
98 def testUnmergeError(self):
99 """Make sure random exit errors are not ignored"""
100 self.rc.AddCmdResult(partial_mock.In('emerge'), returncode=123)
101 with parallel_unittest.ParallelMock():
102 self.assertRaises(cros_build_lib.RunCommandError,
David James59a0a2b2013-03-22 14:04:44 -0700103 cros_mark_as_stable.CleanStalePackages,
104 (), ['no/pkg'])
Mike Frysingerde5ab0e2013-03-21 20:48:36 -0400105
106
Brian Harringc92788f2012-09-21 18:07:15 -0700107class GitBranchTest(cros_test_lib.MoxTestCase):
Chris Sosadad0d322011-01-31 16:37:33 -0800108
109 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -0800110 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400111 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -0800112 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
113 self._branch_name = 'test_branch'
114 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -0700115 self._target_manifest_branch = 'cros/test'
116 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400117 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -0800118
119 def testCheckoutCreate(self):
120 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400121 self._branch.Exists(self._branch_name).AndReturn(False)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800122 cros_build_lib.RunCommand(['repo', 'start', self._branch_name, '.'],
123 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800124 self.mox.ReplayAll()
125 cros_mark_as_stable.GitBranch.Checkout(self._branch)
126 self.mox.VerifyAll()
127
128 def testCheckoutNoCreate(self):
129 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400130 self._branch.Exists(self._branch_name).AndReturn(True)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800131 cros_build_lib.RunCommand(['git', 'checkout', '-f', self._branch_name],
132 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800133 self.mox.ReplayAll()
134 cros_mark_as_stable.GitBranch.Checkout(self._branch)
135 self.mox.VerifyAll()
136
Chris Sosadad0d322011-01-31 16:37:33 -0800137 def testExists(self):
138 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400139 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800140 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400141 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
David James67d73252013-09-19 17:33:12 -0700142 git.RunGit('.', ['branch']).AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800143 self.mox.ReplayAll()
144 self.assertTrue(branch.Exists())
145 self.mox.VerifyAll()
146
147
Chris Sosadad0d322011-01-31 16:37:33 -0800148if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700149 cros_test_lib.main()