Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unit tests for the chrome_chromeos_lkgm program.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import os |
| 10 | import time |
| 11 | |
| 12 | from chromite.lib import builder_status_lib |
| 13 | from chromite.lib import constants |
| 14 | from chromite.lib import cros_build_lib_unittest |
| 15 | from chromite.lib import cros_test_lib |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.lib import partial_mock |
| 18 | from chromite.lib import tree_status |
| 19 | from chromite.scripts import chrome_chromeos_lkgm |
| 20 | |
| 21 | # pylint: disable=protected-access |
| 22 | |
| 23 | class BaseChromeLGTMCommitterTest(cros_test_lib.MockTempDirTestCase): |
| 24 | """Base class for tests using cros_chromeos_lkgm.ChromeLGTMCommitter.""" |
| 25 | |
| 26 | def setUp(self): |
| 27 | """Common set up method for all tests.""" |
| 28 | self.lkgm = '5678' |
| 29 | self.committer = chrome_chromeos_lkgm.ChromeLGTMCommitter( |
| 30 | self.tempdir, self.lkgm, False) |
| 31 | self.lkgm_file = os.path.join(self.tempdir, constants.PATH_TO_CHROME_LKGM) |
| 32 | self.pass_status = builder_status_lib.BuilderStatus( |
| 33 | constants.BUILDER_STATUS_PASSED, None) |
| 34 | self.fail_status = builder_status_lib.BuilderStatus( |
| 35 | constants.BUILDER_STATUS_FAILED, None) |
| 36 | # No need to make tests sleep. |
| 37 | self.PatchObject(time, 'sleep') |
| 38 | |
| 39 | |
| 40 | class ChromeLGTMCommitterTester(cros_build_lib_unittest.RunCommandTestCase, |
| 41 | BaseChromeLGTMCommitterTest): |
| 42 | """Test cros_chromeos_lkgm.Committer.""" |
| 43 | |
| 44 | def testCheckoutChromeLKGM(self): |
| 45 | "Tests that we can read/obtain the old LKGM from mocked out git." |
| 46 | # Write out an old lkgm file as if we got it from a git fetch. |
| 47 | old_lkgm = '1234.0.0' |
| 48 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 49 | osutils.WriteFile(self.lkgm_file, old_lkgm) |
| 50 | self.committer.CheckoutChromeLKGM() |
| 51 | self.assertTrue(self.committer._old_lkgm, old_lkgm) |
| 52 | |
| 53 | def testCommitNewLKGM(self): |
| 54 | """Tests that we can commit a new LKGM file.""" |
| 55 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 56 | self.committer._lkgm = '5678.0.0' |
| 57 | |
| 58 | self.PatchObject(tree_status, 'IsTreeOpen', return_value=True) |
| 59 | self.committer.CommitNewLKGM() |
| 60 | |
| 61 | # Check the file was actually written out correctly. |
| 62 | self.assertEqual(osutils.ReadFile(self.lkgm_file), self.committer._lkgm) |
| 63 | self.assertCommandContains(['git', 'commit']) |
| 64 | |
| 65 | def testLandNewLKGM(self): |
| 66 | """Tests that we try to execute git cl land if the tree is open.""" |
| 67 | self.PatchObject(tree_status, 'IsTreeOpen', return_value=True) |
| 68 | |
| 69 | self.committer.LandNewLKGM() |
| 70 | |
| 71 | self.assertCommandContains(['git', 'cl', 'land']) |
| 72 | |
| 73 | def testLandNewLKGMWithRetry(self): |
| 74 | """Tests that we try to rebase if landing fails.""" |
| 75 | self.PatchObject(tree_status, 'IsTreeOpen', return_value=True) |
| 76 | |
| 77 | self.rc.AddCmdResult(partial_mock.In('land'), returncode=1) |
| 78 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotCommitted, |
| 79 | self.committer.LandNewLKGM) |
| 80 | |
| 81 | self.assertCommandContains(['git', 'cl', 'land']) |
| 82 | self.assertCommandContains(['git', 'fetch', 'origin', 'master']) |
| 83 | self.assertCommandContains(['git', 'rebase']) |