blob: 144c63ce63428e3057790d7c35ddd0105ba3e1fb [file] [log] [blame]
Shuqian Zhaoae2d0782016-11-15 16:58:47 -08001#!/usr/bin/python
2# Copyright (c) 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unittests for automated_deploy.py."""
7
8from __future__ import print_function
9
10import mock
11import unittest
12
13import common
14from autotest_lib.site_utils import automated_deploy as ad
15from autotest_lib.site_utils.lib import infra
16
17
18class AutomatedDeployTest(unittest.TestCase):
19 """Test automated_deploy with commands mocked out."""
20
21 PUSH_LOG = '''Total 0 (delta 0), reused 0 (delta 0)
22remote: Processing changes: done
23To https:TEST_URL
24123..456 prod -> prod'''
25
26 def setUp(self):
27 infra.chdir = mock.MagicMock()
28
29
30 @mock.patch.object(infra, 'local_runner')
31 def testUpdateProdBranch(self, run_cmd):
32 """Test automated_deploy.update_prod_branch.
33
34 @param run_cmd: Mock of infra.local_runner call used.
35 """
36 # Test whether rebase to the given hash when the hash is given.
37 run_cmd.return_value = self.PUSH_LOG
38 ad.update_prod_branch('test', 'test_dir', '123')
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080039 expect_cmds = [mock.call('git rebase 123 prod', stream_output=True),
40 mock.call('git push origin prod', stream_output=True)]
41 run_cmd.assert_has_calls(expect_cmds)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080042
43 # Test whether rebase to prod-next branch when the hash is not given.
44 run_cmd.return_value = self.PUSH_LOG
45 ad.update_prod_branch('test', 'test_dir', None)
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080046 expect_cmds = [mock.call('git rebase origin/prod-next prod',
47 stream_output=True),
48 mock.call('git push origin prod', stream_output=True)]
49 run_cmd.assert_has_calls(expect_cmds)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080050
51 # Test to grep the pushed commit range from the normal push log.
52 run_cmd.return_value = self.PUSH_LOG
53 self.assertEqual(ad.update_prod_branch('test', 'test_dir', None),
54 '123..456')
55
56 # Test to grep the pushed commit range from the failed push log.
57 run_cmd.return_value = 'Fail to push prod branch'
58 with self.assertRaises(ad.AutoDeployException):
59 ad.update_prod_branch('test', 'test_dir', None)
60
61
62 @mock.patch.object(infra, 'local_runner')
63 def testGetPushedCommits(self, run_cmd):
64 """Test automated_deploy.get_pushed_commits.
65
66 @param run_cmd: Mock of infra.local_runner call used.
67 """
68 fake_commits_logs = '123 Test_change_1\n456 Test_change_2'
69 run_cmd.return_value = fake_commits_logs
70
71 #Test to get pushed commits for autotest repo.
Shuqian Zhao4ce1e422016-11-28 13:09:58 -080072 repo = 'autotest'
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080073 expect_git_log_cmd = 'git log --oneline 123..456|grep autotest'
Shuqian Zhao4ce1e422016-11-28 13:09:58 -080074 expect_return = ('\n%s:\n%s\n%s\n' %
75 (repo, expect_git_log_cmd, fake_commits_logs))
76 actual_return = ad.get_pushed_commits(repo, 'test', '123..456')
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080077
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080078 run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080079 self.assertEqual(expect_return, actual_return)
80
81 #Test to get pushed commits for chromite repo.
Shuqian Zhao4ce1e422016-11-28 13:09:58 -080082 repo = 'chromite'
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080083 expect_git_log_cmd = 'git log --oneline 123..456'
Shuqian Zhao4ce1e422016-11-28 13:09:58 -080084 expect_return = ('\n%s:\n%s\n%s\n' %
85 (repo, expect_git_log_cmd, fake_commits_logs))
86 actual_return = ad.get_pushed_commits(repo, 'test', '123..456')
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080087
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080088 run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080089 self.assertEqual(expect_return, actual_return)
90
91
92if __name__ == '__main__':
93 unittest.main()