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 | |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 23 | class BaseChromeLKGMCommitterTest(cros_test_lib.MockTempDirTestCase): |
| 24 | """Base class for tests using cros_chromeos_lkgm.ChromeLKGMCommitter.""" |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 25 | |
| 26 | def setUp(self): |
| 27 | """Common set up method for all tests.""" |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 28 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 29 | self.tempdir, '1001.0.0', False, 'user@test.org') |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 30 | self.lkgm_file = os.path.join(self.tempdir, constants.PATH_TO_CHROME_LKGM) |
| 31 | self.pass_status = builder_status_lib.BuilderStatus( |
| 32 | constants.BUILDER_STATUS_PASSED, None) |
| 33 | self.fail_status = builder_status_lib.BuilderStatus( |
| 34 | constants.BUILDER_STATUS_FAILED, None) |
| 35 | # No need to make tests sleep. |
| 36 | self.PatchObject(time, 'sleep') |
| 37 | |
| 38 | |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 39 | class ChromeLKGMCommitterTester(cros_build_lib_unittest.RunCommandTestCase, |
| 40 | BaseChromeLKGMCommitterTest): |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 41 | """Test cros_chromeos_lkgm.Committer.""" |
| 42 | |
| 43 | def testCheckoutChromeLKGM(self): |
| 44 | "Tests that we can read/obtain the old LKGM from mocked out git." |
| 45 | # Write out an old lkgm file as if we got it from a git fetch. |
| 46 | old_lkgm = '1234.0.0' |
| 47 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 48 | osutils.WriteFile(self.lkgm_file, old_lkgm) |
| 49 | self.committer.CheckoutChromeLKGM() |
| 50 | self.assertTrue(self.committer._old_lkgm, old_lkgm) |
| 51 | |
| 52 | def testCommitNewLKGM(self): |
| 53 | """Tests that we can commit a new LKGM file.""" |
| 54 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 55 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 56 | self.tempdir, '1002.0.0', False, 'user@test.org') |
Steven Bennetts | ddf9bcd | 2017-06-14 14:07:43 -0700 | [diff] [blame] | 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']) |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 84 | |
| 85 | def testOlderLKGMFails(self): |
| 86 | """Tests that trying to update to an older lkgm version fails.""" |
| 87 | old_lkgm = '1002.0.0' |
| 88 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 89 | osutils.WriteFile(self.lkgm_file, old_lkgm) |
| 90 | |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 91 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 92 | self.tempdir, '1001.0.0', False, 'user@test.org') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 93 | self.committer.CheckoutChromeLKGM() |
| 94 | self.assertTrue(self.committer._old_lkgm, old_lkgm) |
| 95 | |
| 96 | self.PatchObject(tree_status, 'IsTreeOpen', return_value=True) |
| 97 | self.assertRaises(chrome_chromeos_lkgm.LKGMNotValid, |
| 98 | self.committer.CommitNewLKGM) |
| 99 | |
| 100 | def testVersionWithChromeBranch(self): |
| 101 | """Tests passing a version with a chrome branch strips the branch.""" |
| 102 | old_lkgm = '1002.0.0' |
| 103 | osutils.SafeMakedirs(os.path.dirname(self.lkgm_file)) |
| 104 | osutils.WriteFile(self.lkgm_file, old_lkgm) |
| 105 | self.committer.CheckoutChromeLKGM() |
| 106 | self.assertTrue(self.committer._old_lkgm, old_lkgm) |
| 107 | |
Steven Bennetts | 415d062 | 2017-09-06 07:50:07 -0700 | [diff] [blame] | 108 | self.committer = chrome_chromeos_lkgm.ChromeLKGMCommitter( |
| 109 | self.tempdir, '1003.0.0-rc2', False, 'user@test.org') |
Steven Bennetts | 4bc322a | 2017-08-28 09:37:39 -0700 | [diff] [blame] | 110 | |
| 111 | self.PatchObject(tree_status, 'IsTreeOpen', return_value=True) |
| 112 | self.committer.CommitNewLKGM() |
| 113 | |
| 114 | # Check the file was actually written out correctly. |
| 115 | stripped_lkgm = '1003.0.0' |
| 116 | self.assertEqual(osutils.ReadFile(self.lkgm_file), stripped_lkgm) |