blob: 191cdb048665a3e3d526cec8718a8196c80dae2b [file] [log] [blame]
Chris Sosadad0d322011-01-31 16:37:33 -08001#!/usr/bin/python
2
Chris Sosac13bba52011-05-24 15:14:09 -07003# Copyright (c) 2011 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
Chris Sosadad0d322011-01-31 16:37:33 -08009import mox
Chris Sosadad0d322011-01-31 16:37:33 -080010import sys
11import unittest
12
Chris Sosa471532a2011-02-01 15:10:06 -080013import constants
J. Richard Barnetted422f622011-11-17 09:39:46 -080014if __name__ == '__main__':
David James710b7dc2012-02-07 16:49:59 -080015 sys.path.insert(0, constants.SOURCE_ROOT)
Chris Sosac13bba52011-05-24 15:14:09 -070016
J. Richard Barnetted422f622011-11-17 09:39:46 -080017from chromite.lib import cros_build_lib
18from chromite.buildbot import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080019
Chris Sosa62ad8522011-03-08 17:46:17 -080020
David Jamesfba499d2011-10-19 10:47:21 -070021# pylint: disable=W0212,R0904
Chris Sosadad0d322011-01-31 16:37:33 -080022class NonClassTests(mox.MoxTestBase):
23 def setUp(self):
24 mox.MoxTestBase.setUp(self)
David James3a373092011-11-18 15:56:31 -080025 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040026 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080027 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070028 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080029
30 def testPushChange(self):
31 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
32 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
33 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
34 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
35 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
Chris Sosac13bba52011-05-24 15:14:09 -070036 self.mox.StubOutWithMock(cros_build_lib, 'GitPushWithRetry')
Brian Harring609dc4e2012-05-07 02:17:44 -070037 self.mox.StubOutWithMock(cros_build_lib, 'GetTrackingBranch')
David James66009462012-03-25 10:08:38 -070038 self.mox.StubOutWithMock(cros_build_lib, 'SyncPushBranch')
39 self.mox.StubOutWithMock(cros_build_lib, 'CreatePushBranch')
Brian Harring609dc4e2012-05-07 02:17:44 -070040 self.mox.StubOutWithMock(cros_build_lib, 'RunGitCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080041
42 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040043 self._branch, self._target_manifest_branch, '.').AndReturn(True)
Brian Harring609dc4e2012-05-07 02:17:44 -070044 cros_build_lib.GetTrackingBranch('.', for_push=True).AndReturn(
45 ['gerrit', 'refs/remotes/gerrit/master'])
46 cros_build_lib.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070047 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070048 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040049 result = cros_build_lib.CommandResult(output=git_log)
50 cros_build_lib.RunCommandCaptureOutput(
51 ['git', 'log', '--format=format:%s%n%n%b',
Brian Harring609dc4e2012-05-07 02:17:44 -070052 'refs/remotes/gerrit/master..%s' % self._branch],
53 cwd='.').AndReturn(result)
David James66009462012-03-25 10:08:38 -070054 cros_build_lib.CreatePushBranch('merge_branch', '.')
Brian Harring609dc4e2012-05-07 02:17:44 -070055 cros_build_lib.RunGitCommand('.', ['merge', '--squash', self._branch])
56 cros_build_lib.RunGitCommand('.', ['commit', '-m', fake_description])
57 cros_build_lib.RunGitCommand('.', ['config', 'push.default', 'tracking'])
58 cros_build_lib.GitPushWithRetry('merge_branch', '.', dryrun=False)
Chris Sosadad0d322011-01-31 16:37:33 -080059 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040060 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
61 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080062 self.mox.VerifyAll()
63
64
65class GitBranchTest(mox.MoxTestBase):
66
67 def setUp(self):
68 mox.MoxTestBase.setUp(self)
69 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040070 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
71 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080072 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
73 self._branch_name = 'test_branch'
74 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -070075 self._target_manifest_branch = 'cros/test'
76 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -040077 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -080078
79 def testCheckoutCreate(self):
80 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040081 self._branch.Exists(self._branch_name).AndReturn(False)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040082 cros_build_lib.RunCommandCaptureOutput(['repo', 'start', self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040083 '.'], print_cmd=False, cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080084 self.mox.ReplayAll()
85 cros_mark_as_stable.GitBranch.Checkout(self._branch)
86 self.mox.VerifyAll()
87
88 def testCheckoutNoCreate(self):
89 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040090 self._branch.Exists(self._branch_name).AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040091 cros_build_lib.RunCommandCaptureOutput(['git', 'checkout', '-f',
Mike Frysinger2ebe3732012-05-08 17:04:12 -040092 self._branch_name], print_cmd=False,
93 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080094 self.mox.ReplayAll()
95 cros_mark_as_stable.GitBranch.Checkout(self._branch)
96 self.mox.VerifyAll()
97
Chris Sosadad0d322011-01-31 16:37:33 -080098 def testExists(self):
99 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400100 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800101 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400102 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400103 cros_build_lib.RunCommandCaptureOutput(['git', 'branch'], print_cmd=False,
104 cwd='.').AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800105 self.mox.ReplayAll()
106 self.assertTrue(branch.Exists())
107 self.mox.VerifyAll()
108
109
Chris Sosadad0d322011-01-31 16:37:33 -0800110if __name__ == '__main__':
111 unittest.main()