Home | History | Annotate | Download | only in site_utils
      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 
     21     PUSH_LOG = '''Total 0 (delta 0), reused 0 (delta 0)
     22 remote: Processing changes: done
     23 To https:TEST_URL
     24 123..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')
     39         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)
     42 
     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)
     46         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)
     50 
     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.
     72         repo = 'autotest'
     73         expect_git_log_cmd = 'git log --oneline 123..456|grep autotest'
     74         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')
     77 
     78         run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
     79         self.assertEqual(expect_return, actual_return)
     80 
     81         #Test to get pushed commits for chromite repo.
     82         repo = 'chromite'
     83         expect_git_log_cmd = 'git log --oneline 123..456'
     84         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')
     87 
     88         run_cmd.assert_called_with(expect_git_log_cmd, stream_output=True)
     89         self.assertEqual(expect_return, actual_return)
     90 
     91 
     92 if __name__ == '__main__':
     93     unittest.main()
     94