blob: e35a3cb0b65c8a47924264febe1afa942978550e [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
Shuqian Zhao673519b2017-05-05 15:13:25 -070021 GIT_LOG_FOR_COMMITS = '''123 foo
22456 bar'''
23
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080024 PUSH_LOG = '''Total 0 (delta 0), reused 0 (delta 0)
25remote: Processing changes: done
26To https:TEST_URL
27123..456 prod -> prod'''
28
29 def setUp(self):
30 infra.chdir = mock.MagicMock()
31
32
33 @mock.patch.object(infra, 'local_runner')
Shuqian Zhao673519b2017-05-05 15:13:25 -070034 def testUpdateProdBranchWithNoNewChanges(self, run_cmd):
35 """Test update_prod_branch when there exist no new changes.
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080036
37 @param run_cmd: Mock of infra.local_runner call used.
38 """
Shuqian Zhao673519b2017-05-05 15:13:25 -070039 run_cmd.return_value = None
40 self.assertEqual(ad.update_prod_branch('test', 'test_dir', '123'), None)
41 expect_cmds = [
42 mock.call('git log prod..123 --oneline', stream_output=True)]
43 run_cmd.assert_has_calls(expect_cmds)
44
45
46 @mock.patch.object(infra, 'local_runner')
47 def testUpdateProdBranchRebaseToCorrectHash(self, run_cmd):
48 """Test whether update_prod_branch can rebase to the correct hash.
49
50 @param run_cmd: Mock of infra.local_runner call used.
51 """
52 run_cmd.side_effect = [self.GIT_LOG_FOR_COMMITS, None, self.PUSH_LOG]
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080053 ad.update_prod_branch('test', 'test_dir', '123')
Shuqian Zhao673519b2017-05-05 15:13:25 -070054 expect_cmds = [
55 mock.call('git log prod..123 --oneline', stream_output=True),
56 mock.call('git rebase 123 prod', stream_output=True),
57 mock.call('git push origin prod', stream_output=True)]
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080058 run_cmd.assert_has_calls(expect_cmds)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080059
Shuqian Zhao673519b2017-05-05 15:13:25 -070060
61 @mock.patch.object(infra, 'local_runner')
62 def testUpdateProdBranchRebaseToProdNext(self, run_cmd):
63 """Test whether rebase to prod-next branch when the hash is not given.
64
65 @param run_cmd: Mock of infra.local_runner call used.
66 """
67 run_cmd.side_effect = [self.GIT_LOG_FOR_COMMITS, None, self.PUSH_LOG]
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080068 ad.update_prod_branch('test', 'test_dir', None)
Shuqian Zhao673519b2017-05-05 15:13:25 -070069 expect_cmds = [
70 mock.call('git log prod..origin/prod-next --oneline',
71 stream_output=True),
72 mock.call('git rebase origin/prod-next prod',
73 stream_output=True),
74 mock.call('git push origin prod', stream_output=True)]
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -080075 run_cmd.assert_has_calls(expect_cmds)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080076
Shuqian Zhao673519b2017-05-05 15:13:25 -070077
78 @mock.patch.object(infra, 'local_runner')
79 def testUpdateProdBranchParseCommitRange(self, run_cmd):
80 """Test to grep the pushed commit range from the normal push log.
81
82 @param run_cmd: Mock of infra.local_runner call used.
83 """
84 run_cmd.side_effect = [self.GIT_LOG_FOR_COMMITS, None, self.PUSH_LOG]
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080085 self.assertEqual(ad.update_prod_branch('test', 'test_dir', None),
86 '123..456')
87
Shuqian Zhao673519b2017-05-05 15:13:25 -070088
89 @mock.patch.object(infra, 'local_runner')
90 def testUpdateProdBranchFailToParseCommitRange(self, run_cmd):
91 """Test to grep the pushed commit range from the failed push log.
92
93 @param run_cmd: Mock of infra.local_runner call used.
94 """
95 run_cmd.side_effect = [self.GIT_LOG_FOR_COMMITS, None,
96 'Fail to push prod branch']
Shuqian Zhaoae2d0782016-11-15 16:58:47 -080097 with self.assertRaises(ad.AutoDeployException):
98 ad.update_prod_branch('test', 'test_dir', None)
99
100
101 @mock.patch.object(infra, 'local_runner')
102 def testGetPushedCommits(self, run_cmd):
103 """Test automated_deploy.get_pushed_commits.
104
105 @param run_cmd: Mock of infra.local_runner call used.
106 """
xixuan6d782dc2017-06-21 18:08:48 -0700107 autotest_commits_logs = '123: autotest: cl_1\n456: autotest: cl_2\n'
108 chromite_commits_logs = '789: test_cl_1\n'
109 fake_commits_logs = autotest_commits_logs + chromite_commits_logs
Shuqian Zhaoae2d0782016-11-15 16:58:47 -0800110 run_cmd.return_value = fake_commits_logs
111
112 #Test to get pushed commits for autotest repo.
Shuqian Zhao4ce1e422016-11-28 13:09:58 -0800113 repo = 'autotest'
xixuan6d782dc2017-06-21 18:08:48 -0700114 expect_git_log_cmd = 'git log --oneline 123..789'
Aviv Keshet6dd1c3d2017-09-26 17:46:49 -0700115 expect_display_cmd = expect_git_log_cmd + ' | grep autotest'
Shuqian Zhao4ce1e422016-11-28 13:09:58 -0800116 expect_return = ('\n%s:\n%s\n%s\n' %
Aviv Keshet6dd1c3d2017-09-26 17:46:49 -0700117 (repo, expect_display_cmd, autotest_commits_logs))
xixuan6d782dc2017-06-21 18:08:48 -0700118 actual_return = ad.get_pushed_commits(repo, 'test', '123..789')
Shuqian Zhaoae2d0782016-11-15 16:58:47 -0800119
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -0800120 run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -0800121 self.assertEqual(expect_return, actual_return)
122
123 #Test to get pushed commits for chromite repo.
Shuqian Zhao4ce1e422016-11-28 13:09:58 -0800124 repo = 'chromite'
xixuan6d782dc2017-06-21 18:08:48 -0700125 expect_git_log_cmd = 'git log --oneline 123..789'
Shuqian Zhao4ce1e422016-11-28 13:09:58 -0800126 expect_return = ('\n%s:\n%s\n%s\n' %
127 (repo, expect_git_log_cmd, fake_commits_logs))
xixuan6d782dc2017-06-21 18:08:48 -0700128 actual_return = ad.get_pushed_commits(repo, 'test', '123..789')
Shuqian Zhaoae2d0782016-11-15 16:58:47 -0800129
Shuqian Zhaoa482c4a2016-11-21 18:49:41 -0800130 run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
Shuqian Zhaoae2d0782016-11-15 16:58:47 -0800131 self.assertEqual(expect_return, actual_return)
132
133
134if __name__ == '__main__':
135 unittest.main()