• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
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    GIT_LOG_FOR_COMMITS = '''123 foo
22456 bar'''
23
24    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')
34    def testUpdateProdBranchWithNoNewChanges(self, run_cmd):
35        """Test update_prod_branch when there exist no new changes.
36
37        @param run_cmd: Mock of infra.local_runner call used.
38        """
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]
53        ad.update_prod_branch('test', 'test_dir', '123')
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)]
58        run_cmd.assert_has_calls(expect_cmds)
59
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]
68        ad.update_prod_branch('test', 'test_dir', None)
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)]
75        run_cmd.assert_has_calls(expect_cmds)
76
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]
85        self.assertEqual(ad.update_prod_branch('test', 'test_dir', None),
86                         '123..456')
87
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']
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        """
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
110        run_cmd.return_value = fake_commits_logs
111
112        #Test to get pushed commits for autotest repo.
113        repo = 'autotest'
114        expect_git_log_cmd = 'git log --oneline 123..789'
115        expect_display_cmd = expect_git_log_cmd + ' | grep autotest'
116        expect_return = ('\n%s:\n%s\n%s\n' %
117                         (repo, expect_display_cmd, autotest_commits_logs))
118        actual_return = ad.get_pushed_commits(repo, 'test', '123..789')
119
120        run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
121        self.assertEqual(expect_return, actual_return)
122
123        #Test to get pushed commits for chromite repo.
124        repo = 'chromite'
125        expect_git_log_cmd = 'git log --oneline 123..789'
126        expect_return = ('\n%s:\n%s\n%s\n' %
127                         (repo, expect_git_log_cmd, fake_commits_logs))
128        actual_return = ad.get_pushed_commits(repo, 'test', '123..789')
129
130        run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
131        self.assertEqual(expect_return, actual_return)
132
133
134if __name__ == '__main__':
135    unittest.main()
136