blob: f96d562668e3063395ee8b4f1c2eee6f936e4ec5 [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
Chris Sosa62ad8522011-03-08 17:46:17 -080024
David Jamesfba499d2011-10-19 10:47:21 -070025# pylint: disable=W0212,R0904
Brian Harringc92788f2012-09-21 18:07:15 -070026class NonClassTests(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070027 """Test the flow for pushing a change."""
Chris Sosadad0d322011-01-31 16:37:33 -080028 def setUp(self):
David James3a373092011-11-18 15:56:31 -080029 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080030 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070031 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080032
Matt Tennantcb522052013-11-25 14:23:43 -080033 def _TestPushChange(self, bad_cls):
Chris Sosadad0d322011-01-31 16:37:33 -080034 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')
David James97d95872012-11-16 15:09:56 -080039 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')
Chris Sosadad0d322011-01-31 16:37:33 -080044
Matt Tennantcb522052013-11-25 14:23:43 -080045 # Run the flow.
Chris Sosadad0d322011-01-31 16:37:33 -080046 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040047 self._branch, self._target_manifest_branch, '.').AndReturn(True)
David James97d95872012-11-16 15:09:56 -080048 git.GetTrackingBranch('.', for_push=True).AndReturn(
Brian Harring609dc4e2012-05-07 02:17:44 -070049 ['gerrit', 'refs/remotes/gerrit/master'])
David James97d95872012-11-16 15:09:56 -080050 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070051 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070052 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Matt Tennantcb522052013-11-25 14:23:43 -080053
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
Chris Sosadad0d322011-01-31 16:37:33 -080074 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040075 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
76 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080077 self.mox.VerifyAll()
78
Matt Tennantcb522052013-11-25 14:23:43 -080079 def testPushChange(self):
80 self._TestPushChange(bad_cls=False)
81
82 def testPushChangeBadCls(self):
83 self.assertRaises(AssertionError, self._TestPushChange, bad_cls=True)
84
Chris Sosadad0d322011-01-31 16:37:33 -080085
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040086class CleanStalePackagesTest(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -070087 """Tests for cros_mark_as_stable.CleanStalePackages."""
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040088
David James59a0a2b2013-03-22 14:04:44 -070089 def setUp(self):
90 self.PatchObject(osutils, 'FindMissingBinaries', return_value=[])
91
Mike Frysingerde5ab0e2013-03-21 20:48:36 -040092 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,
David James59a0a2b2013-03-22 14:04:44 -0700106 cros_mark_as_stable.CleanStalePackages,
107 (), ['no/pkg'])
Mike Frysingerde5ab0e2013-03-21 20:48:36 -0400108
109
Brian Harringc92788f2012-09-21 18:07:15 -0700110class GitBranchTest(cros_test_lib.MoxTestCase):
Don Garrett25f309a2014-03-19 14:02:12 -0700111 """Tests for cros_mark_as_stable.GitBranch."""
Chris Sosadad0d322011-01-31 16:37:33 -0800112
113 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -0800114 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400115 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Chris Sosadad0d322011-01-31 16:37:33 -0800116 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
117 self._branch_name = 'test_branch'
118 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -0700119 self._target_manifest_branch = 'cros/test'
120 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400121 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -0800122
123 def testCheckoutCreate(self):
124 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400125 self._branch.Exists(self._branch_name).AndReturn(False)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800126 cros_build_lib.RunCommand(['repo', 'start', self._branch_name, '.'],
127 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800128 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.
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400134 self._branch.Exists(self._branch_name).AndReturn(True)
Yu-Ju Hong3add4432014-01-30 11:46:15 -0800135 cros_build_lib.RunCommand(['git', 'checkout', '-f', self._branch_name],
136 print_cmd=False, cwd='.', capture_output=True)
Chris Sosadad0d322011-01-31 16:37:33 -0800137 self.mox.ReplayAll()
138 cros_mark_as_stable.GitBranch.Checkout(self._branch)
139 self.mox.VerifyAll()
140
Chris Sosadad0d322011-01-31 16:37:33 -0800141 def testExists(self):
142 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400143 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800144 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400145 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
David James67d73252013-09-19 17:33:12 -0700146 git.RunGit('.', ['branch']).AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800147 self.mox.ReplayAll()
148 self.assertTrue(branch.Exists())
149 self.mox.VerifyAll()
150
151
Chris Sosadad0d322011-01-31 16:37:33 -0800152if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700153 cros_test_lib.main()