blob: 70e77d429ccead428c8906d7506532fcb409e5f6 [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
Mike Frysinger6cb624a2012-05-24 18:17:38 -040016from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080017
Chris Sosa62ad8522011-03-08 17:46:17 -080018
David Jamesfba499d2011-10-19 10:47:21 -070019# pylint: disable=W0212,R0904
Brian Harringc92788f2012-09-21 18:07:15 -070020class NonClassTests(cros_test_lib.MoxTestCase):
Chris Sosadad0d322011-01-31 16:37:33 -080021 def setUp(self):
David James3a373092011-11-18 15:56:31 -080022 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040023 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080024 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070025 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080026
27 def testPushChange(self):
28 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
29 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
30 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
31 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
32 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
Chris Sosac13bba52011-05-24 15:14:09 -070033 self.mox.StubOutWithMock(cros_build_lib, 'GitPushWithRetry')
Brian Harring609dc4e2012-05-07 02:17:44 -070034 self.mox.StubOutWithMock(cros_build_lib, 'GetTrackingBranch')
David James66009462012-03-25 10:08:38 -070035 self.mox.StubOutWithMock(cros_build_lib, 'SyncPushBranch')
36 self.mox.StubOutWithMock(cros_build_lib, 'CreatePushBranch')
Brian Harring609dc4e2012-05-07 02:17:44 -070037 self.mox.StubOutWithMock(cros_build_lib, 'RunGitCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080038
39 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040040 self._branch, self._target_manifest_branch, '.').AndReturn(True)
Brian Harring609dc4e2012-05-07 02:17:44 -070041 cros_build_lib.GetTrackingBranch('.', for_push=True).AndReturn(
42 ['gerrit', 'refs/remotes/gerrit/master'])
43 cros_build_lib.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070044 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070045 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040046 result = cros_build_lib.CommandResult(output=git_log)
47 cros_build_lib.RunCommandCaptureOutput(
48 ['git', 'log', '--format=format:%s%n%n%b',
Brian Harring609dc4e2012-05-07 02:17:44 -070049 'refs/remotes/gerrit/master..%s' % self._branch],
50 cwd='.').AndReturn(result)
David James66009462012-03-25 10:08:38 -070051 cros_build_lib.CreatePushBranch('merge_branch', '.')
Brian Harring609dc4e2012-05-07 02:17:44 -070052 cros_build_lib.RunGitCommand('.', ['merge', '--squash', self._branch])
53 cros_build_lib.RunGitCommand('.', ['commit', '-m', fake_description])
54 cros_build_lib.RunGitCommand('.', ['config', 'push.default', 'tracking'])
55 cros_build_lib.GitPushWithRetry('merge_branch', '.', dryrun=False)
Chris Sosadad0d322011-01-31 16:37:33 -080056 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040057 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
58 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080059 self.mox.VerifyAll()
60
61
Brian Harringc92788f2012-09-21 18:07:15 -070062class GitBranchTest(cros_test_lib.MoxTestCase):
Chris Sosadad0d322011-01-31 16:37:33 -080063
64 def setUp(self):
Chris Sosadad0d322011-01-31 16:37:33 -080065 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040066 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
67 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080068 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
69 self._branch_name = 'test_branch'
70 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -070071 self._target_manifest_branch = 'cros/test'
72 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -040073 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -080074
75 def testCheckoutCreate(self):
76 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040077 self._branch.Exists(self._branch_name).AndReturn(False)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040078 cros_build_lib.RunCommandCaptureOutput(['repo', 'start', self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040079 '.'], print_cmd=False, cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080080 self.mox.ReplayAll()
81 cros_mark_as_stable.GitBranch.Checkout(self._branch)
82 self.mox.VerifyAll()
83
84 def testCheckoutNoCreate(self):
85 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040086 self._branch.Exists(self._branch_name).AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040087 cros_build_lib.RunCommandCaptureOutput(['git', 'checkout', '-f',
Mike Frysinger2ebe3732012-05-08 17:04:12 -040088 self._branch_name], print_cmd=False,
89 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080090 self.mox.ReplayAll()
91 cros_mark_as_stable.GitBranch.Checkout(self._branch)
92 self.mox.VerifyAll()
93
Chris Sosadad0d322011-01-31 16:37:33 -080094 def testExists(self):
95 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040096 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080097 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040098 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysinger2ebe3732012-05-08 17:04:12 -040099 cros_build_lib.RunCommandCaptureOutput(['git', 'branch'], print_cmd=False,
100 cwd='.').AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800101 self.mox.ReplayAll()
102 self.assertTrue(branch.Exists())
103 self.mox.VerifyAll()
104
105
Chris Sosadad0d322011-01-31 16:37:33 -0800106if __name__ == '__main__':
Brian Harringc92788f2012-09-21 18:07:15 -0700107 cros_test_lib.main()