blob: 751aeba2ff23d231262a2fa33f9f1a03509e7956 [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 '..', '..'))
J. Richard Barnetted422f622011-11-17 09:39:46 -080016from chromite.lib import cros_build_lib
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
Chris Sosadad0d322011-01-31 16:37:33 -080021class NonClassTests(mox.MoxTestBase):
22 def setUp(self):
23 mox.MoxTestBase.setUp(self)
David James3a373092011-11-18 15:56:31 -080024 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040025 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080026 self._branch = 'test_branch'
Brian Harringeb237932012-05-07 02:08:06 -070027 self._target_manifest_branch = 'cros/master'
Chris Sosadad0d322011-01-31 16:37:33 -080028
29 def testPushChange(self):
30 git_log = 'Marking test_one as stable\nMarking test_two as stable\n'
31 fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log
32 self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits')
33 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch')
34 self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists')
Chris Sosac13bba52011-05-24 15:14:09 -070035 self.mox.StubOutWithMock(cros_build_lib, 'GitPushWithRetry')
Brian Harring609dc4e2012-05-07 02:17:44 -070036 self.mox.StubOutWithMock(cros_build_lib, 'GetTrackingBranch')
David James66009462012-03-25 10:08:38 -070037 self.mox.StubOutWithMock(cros_build_lib, 'SyncPushBranch')
38 self.mox.StubOutWithMock(cros_build_lib, 'CreatePushBranch')
Brian Harring609dc4e2012-05-07 02:17:44 -070039 self.mox.StubOutWithMock(cros_build_lib, 'RunGitCommand')
Chris Sosadad0d322011-01-31 16:37:33 -080040
41 cros_mark_as_stable._DoWeHaveLocalCommits(
Mike Frysinger2ebe3732012-05-08 17:04:12 -040042 self._branch, self._target_manifest_branch, '.').AndReturn(True)
Brian Harring609dc4e2012-05-07 02:17:44 -070043 cros_build_lib.GetTrackingBranch('.', for_push=True).AndReturn(
44 ['gerrit', 'refs/remotes/gerrit/master'])
45 cros_build_lib.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master')
David James66009462012-03-25 10:08:38 -070046 cros_mark_as_stable._DoWeHaveLocalCommits(
Brian Harring609dc4e2012-05-07 02:17:44 -070047 self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040048 result = cros_build_lib.CommandResult(output=git_log)
49 cros_build_lib.RunCommandCaptureOutput(
50 ['git', 'log', '--format=format:%s%n%n%b',
Brian Harring609dc4e2012-05-07 02:17:44 -070051 'refs/remotes/gerrit/master..%s' % self._branch],
52 cwd='.').AndReturn(result)
David James66009462012-03-25 10:08:38 -070053 cros_build_lib.CreatePushBranch('merge_branch', '.')
Brian Harring609dc4e2012-05-07 02:17:44 -070054 cros_build_lib.RunGitCommand('.', ['merge', '--squash', self._branch])
55 cros_build_lib.RunGitCommand('.', ['commit', '-m', fake_description])
56 cros_build_lib.RunGitCommand('.', ['config', 'push.default', 'tracking'])
57 cros_build_lib.GitPushWithRetry('merge_branch', '.', dryrun=False)
Chris Sosadad0d322011-01-31 16:37:33 -080058 self.mox.ReplayAll()
Mike Frysinger2ebe3732012-05-08 17:04:12 -040059 cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch,
60 False, '.')
Chris Sosadad0d322011-01-31 16:37:33 -080061 self.mox.VerifyAll()
62
63
64class GitBranchTest(mox.MoxTestBase):
65
66 def setUp(self):
67 mox.MoxTestBase.setUp(self)
68 # Always stub RunCommmand out as we use it in every method.
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040069 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
70 self.mox.StubOutWithMock(cros_build_lib, 'RunCommandCaptureOutput')
Chris Sosadad0d322011-01-31 16:37:33 -080071 self._branch = self.mox.CreateMock(cros_mark_as_stable.GitBranch)
72 self._branch_name = 'test_branch'
73 self._branch.branch_name = self._branch_name
Brian Harringeb237932012-05-07 02:08:06 -070074 self._target_manifest_branch = 'cros/test'
75 self._branch.tracking_branch = self._target_manifest_branch
Mike Frysinger2ebe3732012-05-08 17:04:12 -040076 self._branch.cwd = '.'
Chris Sosadad0d322011-01-31 16:37:33 -080077
78 def testCheckoutCreate(self):
79 # Test init with no previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040080 self._branch.Exists(self._branch_name).AndReturn(False)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040081 cros_build_lib.RunCommandCaptureOutput(['repo', 'start', self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040082 '.'], print_cmd=False, cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080083 self.mox.ReplayAll()
84 cros_mark_as_stable.GitBranch.Checkout(self._branch)
85 self.mox.VerifyAll()
86
87 def testCheckoutNoCreate(self):
88 # Test init with previous branch existing.
Mike Frysinger2ebe3732012-05-08 17:04:12 -040089 self._branch.Exists(self._branch_name).AndReturn(True)
Mike Frysinger7dafd0e2012-05-08 15:47:16 -040090 cros_build_lib.RunCommandCaptureOutput(['git', 'checkout', '-f',
Mike Frysinger2ebe3732012-05-08 17:04:12 -040091 self._branch_name], print_cmd=False,
92 cwd='.')
Chris Sosadad0d322011-01-31 16:37:33 -080093 self.mox.ReplayAll()
94 cros_mark_as_stable.GitBranch.Checkout(self._branch)
95 self.mox.VerifyAll()
96
Chris Sosadad0d322011-01-31 16:37:33 -080097 def testExists(self):
98 branch = cros_mark_as_stable.GitBranch(self._branch_name,
Mike Frysinger2ebe3732012-05-08 17:04:12 -040099 self._target_manifest_branch, '.')
Chris Sosadad0d322011-01-31 16:37:33 -0800100 # Test if branch exists that is created
Mike Frysinger7dafd0e2012-05-08 15:47:16 -0400101 result = cros_build_lib.CommandResult(output=self._branch_name + '\n')
Mike Frysinger2ebe3732012-05-08 17:04:12 -0400102 cros_build_lib.RunCommandCaptureOutput(['git', 'branch'], print_cmd=False,
103 cwd='.').AndReturn(result)
Chris Sosadad0d322011-01-31 16:37:33 -0800104 self.mox.ReplayAll()
105 self.assertTrue(branch.Exists())
106 self.mox.VerifyAll()
107
108
Chris Sosadad0d322011-01-31 16:37:33 -0800109if __name__ == '__main__':
David Jamesbb20ac82012-07-18 10:59:16 -0700110 cros_build_lib.SetupBasicLogging()
Chris Sosadad0d322011-01-31 16:37:33 -0800111 unittest.main()