blob: 635de287c5dced93f315ae4b97a5ba8e7820cbd5 [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
Chris Sosadad0d322011-01-31 16:37:33 -08009import mox
Mike Frysinger6cb624a2012-05-24 18:17:38 -040010import os
Chris Sosadad0d322011-01-31 16:37:33 -080011import sys
12import unittest
13
Mike Frysinger6cb624a2012-05-24 18:17:38 -040014sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
15 '..', '..'))
16from chromite.buildbot import constants
17from chromite.buildbot import constants
J. Richard Barnetted422f622011-11-17 09:39:46 -080018from chromite.lib import cros_build_lib
Mike Frysinger6cb624a2012-05-24 18:17:38 -040019from chromite.scripts import cros_mark_as_stable
Chris Sosadad0d322011-01-31 16:37:33 -080020
Chris Sosa62ad8522011-03-08 17:46:17 -080021
David Jamesfba499d2011-10-19 10:47:21 -070022# pylint: disable=W0212,R0904
Chris Sosadad0d322011-01-31 16:37:33 -080023class NonClassTests(mox.MoxTestBase):
24 def setUp(self):
25 mox.MoxTestBase.setUp(self)
David James3a373092011-11-18 15:56:31 -080026 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040027 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
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
31 def testPushChange(self):
32 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')
Chris Sosac13bba52011-05-24 15:14:09 -070037 self.mox.StubOutWithMock(cros_build_lib, 'GitPushWithRetry')
Brian Harring609dc4e2012-05-07 02:17:44 -070038 self.mox.StubOutWithMock(cros_build_lib, 'GetTrackingBranch')
David James66009462012-03-25 10:08:38 -070039 self.mox.StubOutWithMock(cros_build_lib, 'SyncPushBranch')
40 self.mox.StubOutWithMock(cros_build_lib, 'CreatePushBranch')
Brian Harring609dc4e2012-05-07 02:17:44 -070041 self.mox.StubOutWithMock(cros_build_lib, 'RunGitCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080042
43 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040044 self._branch, self._target_manifest_branch, '.').AndReturn(True)
Brian Harring609dc4e2012-05-07 02:17:44 -070045 cros_build_lib.GetTrackingBranch('.', for_push=True).AndReturn(
46 ['gerrit', 'refs/remotes/gerrit/master'])
47 cros_build_lib.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070048 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070049 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040050 result = cros_build_lib.CommandResult(output=git_log)
51 cros_build_lib.RunCommandCaptureOutput(
52 ['git', 'log', '--format=format:%s%n%n%b',
Brian Harring609dc4e2012-05-07 02:17:44 -070053 'refs/remotes/gerrit/master..%s' % self._branch],
54 cwd='.').AndReturn(result)
David James66009462012-03-25 10:08:38 -070055 cros_build_lib.CreatePushBranch('merge_branch', '.')
Brian Harring609dc4e2012-05-07 02:17:44 -070056 cros_build_lib.RunGitCommand('.', ['merge', '--squash', self._branch])
57 cros_build_lib.RunGitCommand('.', ['commit', '-m', fake_description])
58 cros_build_lib.RunGitCommand('.', ['config', 'push.default', 'tracking'])
59 cros_build_lib.GitPushWithRetry('merge_branch', '.', dryrun=False)
Chris Sosadad0d322011-01-31 16:37:33 -080060 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040061 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
62 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080063 self.mox.VerifyAll()
64
65
66class GitBranchTest(mox.MoxTestBase):
67
68 def setUp(self):
69 mox.MoxTestBase.setUp(self)
70 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040071 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
72 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080073 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
74 self._branch_name = 'test_branch'
75 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -070076 self._target_manifest_branch = 'cros/test'
77 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -040078 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -080079
80 def testCheckoutCreate(self):
81 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040082 self._branch.Exists(self._branch_name).AndReturn(False)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040083 cros_build_lib.RunCommandCaptureOutput(['repo', 'start', self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040084 '.'], print_cmd=False, cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080085 self.mox.ReplayAll()
86 cros_mark_as_stable.GitBranch.Checkout(self._branch)
87 self.mox.VerifyAll()
88
89 def testCheckoutNoCreate(self):
90 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040091 self._branch.Exists(self._branch_name).AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040092 cros_build_lib.RunCommandCaptureOutput(['git', 'checkout', '-f',
Mike Frysinger2ebe3732012-05-08 17:04:12 -040093 self._branch_name], print_cmd=False,
94 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080095 self.mox.ReplayAll()
96 cros_mark_as_stable.GitBranch.Checkout(self._branch)
97 self.mox.VerifyAll()
98
Chris Sosadad0d322011-01-31 16:37:33 -080099 def testExists(self):
100 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400101 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800102 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400103 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400104 cros_build_lib.RunCommandCaptureOutput(['git', 'branch'], print_cmd=False,
105 cwd='.').AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800106 self.mox.ReplayAll()
107 self.assertTrue(branch.Exists())
108 self.mox.VerifyAll()
109
110
Chris Sosadad0d322011-01-31 16:37:33 -0800111if __name__ == '__main__':
112 unittest.main()