Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 1 | #!/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 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import mock |
| 11 | import unittest |
| 12 | |
| 13 | import common |
| 14 | from autotest_lib.site_utils import automated_deploy as ad |
| 15 | from autotest_lib.site_utils.lib import infra |
| 16 | |
| 17 | |
| 18 | class AutomatedDeployTest(unittest.TestCase): |
| 19 | """Test automated_deploy with commands mocked out.""" |
| 20 | |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 21 | GIT_LOG_FOR_COMMITS = '''123 foo |
| 22 | 456 bar''' |
| 23 | |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 24 | PUSH_LOG = '''Total 0 (delta 0), reused 0 (delta 0) |
| 25 | remote: Processing changes: done |
| 26 | To https:TEST_URL |
| 27 | 123..456 prod -> prod''' |
| 28 | |
| 29 | def setUp(self): |
| 30 | infra.chdir = mock.MagicMock() |
| 31 | |
| 32 | |
| 33 | @mock.patch.object(infra, 'local_runner') |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 34 | def testUpdateProdBranchWithNoNewChanges(self, run_cmd): |
| 35 | """Test update_prod_branch when there exist no new changes. |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 36 | |
| 37 | @param run_cmd: Mock of infra.local_runner call used. |
| 38 | """ |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 39 | 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 Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 53 | ad.update_prod_branch('test', 'test_dir', '123') |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 54 | 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 Zhao | a482c4a | 2016-11-21 18:49:41 -0800 | [diff] [blame] | 58 | run_cmd.assert_has_calls(expect_cmds) |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 59 | |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 60 | |
| 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 Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 68 | ad.update_prod_branch('test', 'test_dir', None) |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 69 | 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 Zhao | a482c4a | 2016-11-21 18:49:41 -0800 | [diff] [blame] | 75 | run_cmd.assert_has_calls(expect_cmds) |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 76 | |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 77 | |
| 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 Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 85 | self.assertEqual(ad.update_prod_branch('test', 'test_dir', None), |
| 86 | '123..456') |
| 87 | |
Shuqian Zhao | 673519b | 2017-05-05 15:13:25 -0700 | [diff] [blame] | 88 | |
| 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 Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 97 | 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 | """ |
xixuan | 6d782dc | 2017-06-21 18:08:48 -0700 | [diff] [blame] | 107 | 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 Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 110 | run_cmd.return_value = fake_commits_logs |
| 111 | |
| 112 | #Test to get pushed commits for autotest repo. |
Shuqian Zhao | 4ce1e42 | 2016-11-28 13:09:58 -0800 | [diff] [blame] | 113 | repo = 'autotest' |
xixuan | 6d782dc | 2017-06-21 18:08:48 -0700 | [diff] [blame] | 114 | expect_git_log_cmd = 'git log --oneline 123..789' |
Aviv Keshet | 6dd1c3d | 2017-09-26 17:46:49 -0700 | [diff] [blame] | 115 | expect_display_cmd = expect_git_log_cmd + ' | grep autotest' |
Shuqian Zhao | 4ce1e42 | 2016-11-28 13:09:58 -0800 | [diff] [blame] | 116 | expect_return = ('\n%s:\n%s\n%s\n' % |
Aviv Keshet | 6dd1c3d | 2017-09-26 17:46:49 -0700 | [diff] [blame] | 117 | (repo, expect_display_cmd, autotest_commits_logs)) |
xixuan | 6d782dc | 2017-06-21 18:08:48 -0700 | [diff] [blame] | 118 | actual_return = ad.get_pushed_commits(repo, 'test', '123..789') |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 119 | |
Shuqian Zhao | a482c4a | 2016-11-21 18:49:41 -0800 | [diff] [blame] | 120 | run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True) |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 121 | self.assertEqual(expect_return, actual_return) |
| 122 | |
| 123 | #Test to get pushed commits for chromite repo. |
Shuqian Zhao | 4ce1e42 | 2016-11-28 13:09:58 -0800 | [diff] [blame] | 124 | repo = 'chromite' |
xixuan | 6d782dc | 2017-06-21 18:08:48 -0700 | [diff] [blame] | 125 | expect_git_log_cmd = 'git log --oneline 123..789' |
Shuqian Zhao | 4ce1e42 | 2016-11-28 13:09:58 -0800 | [diff] [blame] | 126 | expect_return = ('\n%s:\n%s\n%s\n' % |
| 127 | (repo, expect_git_log_cmd, fake_commits_logs)) |
xixuan | 6d782dc | 2017-06-21 18:08:48 -0700 | [diff] [blame] | 128 | actual_return = ad.get_pushed_commits(repo, 'test', '123..789') |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 129 | |
Shuqian Zhao | a482c4a | 2016-11-21 18:49:41 -0800 | [diff] [blame] | 130 | run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True) |
Shuqian Zhao | ae2d078 | 2016-11-15 16:58:47 -0800 | [diff] [blame] | 131 | self.assertEqual(expect_return, actual_return) |
| 132 | |
| 133 | |
| 134 | if __name__ == '__main__': |
| 135 | unittest.main() |