blob: feb0d3ea6a9a82c9d97af5659aa1d7772bdd11cc [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
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
Mike Frysinger6cb624a2012-05-24 18:17:38 -040017from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080018
Chris Sosa62ad8522011-03-08 17:46:17 -080019
David Jamesfba499d2011-10-19 10:47:21 -070020# pylint: disable=W0212,R0904
Brian Harringc92788f2012-09-21 18:07:15 -070021class NonClassTests(cros_test_lib.MoxTestCase):
Chris Sosadad0d322011-01-31 16:37:33 -080022 def setUp(self):
David James3a373092011-11-18 15:56:31 -080023 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040024 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080025 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070026 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080027
28 def testPushChange(self):
29 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
30 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
31 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
32 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
33 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
David James97d95872012-11-16 15:09:56 -080034 self.mox.StubOutWithMock(git, 'PushWithRetry')
35 self.mox.StubOutWithMock(git, 'GetTrackingBranch')
36 self.mox.StubOutWithMock(git, 'SyncPushBranch')
37 self.mox.StubOutWithMock(git, 'CreatePushBranch')
38 self.mox.StubOutWithMock(git, 'RunGit')
Chris Sosadad0d322011-01-31 16:37:33 -080039
40 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040041 self._branch, self._target_manifest_branch, '.').AndReturn(True)
David James97d95872012-11-16 15:09:56 -080042 git.GetTrackingBranch('.', for_push=True).AndReturn(
Brian Harring609dc4e2012-05-07 02:17:44 -070043 ['gerrit', 'refs/remotes/gerrit/master'])
David James97d95872012-11-16 15:09:56 -080044 git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070045 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070046 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040047 result = cros_build_lib.CommandResult(output=git_log)
48 cros_build_lib.RunCommandCaptureOutput(
49 ['git', 'log', '--format=format:%s%n%n%b',
Brian Harring609dc4e2012-05-07 02:17:44 -070050 'refs/remotes/gerrit/master..%s' % self._branch],
51 cwd='.').AndReturn(result)
David James97d95872012-11-16 15:09:56 -080052 git.CreatePushBranch('merge_branch', '.')
53 git.RunGit('.', ['merge', '--squash', self._branch])
54 git.RunGit('.', ['commit', '-m', fake_description])
55 git.RunGit('.', ['config', 'push.default', 'tracking'])
56 git.PushWithRetry('merge_branch', '.', dryrun=False)
Chris Sosadad0d322011-01-31 16:37:33 -080057 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040058 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
59 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080060 self.mox.VerifyAll()
61
62
Brian Harringc92788f2012-09-21 18:07:15 -070063class GitBranchTest(cros_test_lib.MoxTestCase):
Chris Sosadad0d322011-01-31 16:37:33 -080064
65 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -080066 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040067 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
68 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080069 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
70 self._branch_name = 'test_branch'
71 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -070072 self._target_manifest_branch = 'cros/test'
73 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -040074 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -080075
76 def testCheckoutCreate(self):
77 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040078 self._branch.Exists(self._branch_name).AndReturn(False)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040079 cros_build_lib.RunCommandCaptureOutput(['repo', 'start', self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040080 '.'], print_cmd=False, cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080081 self.mox.ReplayAll()
82 cros_mark_as_stable.GitBranch.Checkout(self._branch)
83 self.mox.VerifyAll()
84
85 def testCheckoutNoCreate(self):
86 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040087 self._branch.Exists(self._branch_name).AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040088 cros_build_lib.RunCommandCaptureOutput(['git', 'checkout', '-f',
Mike Frysinger2ebe3732012-05-08 17:04:12 -040089 self._branch_name], print_cmd=False,
90 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080091 self.mox.ReplayAll()
92 cros_mark_as_stable.GitBranch.Checkout(self._branch)
93 self.mox.VerifyAll()
94
Chris Sosadad0d322011-01-31 16:37:33 -080095 def testExists(self):
96 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040097 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080098 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040099 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400100 cros_build_lib.RunCommandCaptureOutput(['git', 'branch'], print_cmd=False,
101 cwd='.').AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800102 self.mox.ReplayAll()
103 self.assertTrue(branch.Exists())
104 self.mox.VerifyAll()
105
106
Chris Sosadad0d322011-01-31 16:37:33 -0800107if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700108 cros_test_lib.main()