3 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 """Unit tests for the cros_best_revision program."""
12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(
13 os.path.abspath(__file__)))))
15 from chromite.buildbot import cbuildbot_config
16 from chromite.buildbot import constants
17 from chromite.buildbot import manifest_version
18 from chromite.lib import cros_build_lib_unittest
19 from chromite.lib import cros_test_lib
20 from chromite.lib import gclient
21 from chromite.lib import gs_unittest
22 from chromite.lib import osutils
23 from chromite.lib import partial_mock
24 from chromite.lib import timeout_util
26 from chromite.scripts import cros_best_revision
29 class BaseChromeCommitterTest(cros_test_lib.MockTempDirTestCase):
30 """Base class for tests using cros_best_revision.ChromeCommitter."""
33 """Common set up method for all tests."""
34 self.committer = cros_best_revision.ChromeCommitter(self.tempdir, False)
35 self.lkgm_file = os.path.join(self.tempdir, constants.CHROME_LKGM_FILE)
36 self.pass_status = manifest_version.BuilderStatus(
37 manifest_version.BuilderStatus.STATUS_PASSED, None)
38 self.fail_status = manifest_version.BuilderStatus(
39 manifest_version.BuilderStatus.STATUS_FAILED, None)
42 # pylint: disable=W0212
43 class ChromeGSTest(BaseChromeCommitterTest):
44 """Test cros_best_revision.ChromeCommitter version filtering."""
46 def testGetLatestCanaryVersions(self):
47 """Test that we correctly filter out non-canary and older versions."""
48 output = '\n'.join(['2910.0.1', '2900.0.0', '2908.0.0', '2909.0.0',
50 # Only return 2 -- the 2 newest canary results.
51 cros_best_revision.ChromeCommitter._CANDIDATES_TO_CONSIDER = 2
52 expected_output = ['2910.0.0', '2909.0.0']
54 self.committer._old_lkgm = '2905.0.0'
55 with gs_unittest.GSContextMock() as gs_mock:
56 gs_mock.AddCmdResult(partial_mock.In('ls'), output=output)
57 versions = self.committer._GetLatestCanaryVersions()
58 self.assertEqual(versions, expected_output)
61 class ChromeCommitterTester(cros_build_lib_unittest.RunCommandTestCase,
62 BaseChromeCommitterTest):
63 """Test cros_best_revision.Committer."""
65 canaries = ['a-release', 'b-release', 'c-release']
66 versions = ['4.0.0', '3.0.0']
68 def testCheckoutChromeLKGM(self):
69 "Tests that we can read/obtain the old LKGM from mocked out SVN."
70 # Write out an old lkgm file as if we got it from svn update.
72 osutils.WriteFile(self.lkgm_file, old_lkgm)
73 self.committer.CheckoutChromeLKGM()
74 self.assertTrue(self.committer._old_lkgm, old_lkgm)
75 self.assertCommandContains([
76 '%s/%s' % (gclient.CHROME_COMMITTER_URL,
77 os.path.dirname(constants.SVN_CHROME_LKGM))])
78 self.assertCommandContains([constants.CHROME_LKGM_FILE])
80 def _TestFindNewLKGM(self, all_results, lkgm):
81 """Stubs out methods used by FindNewLKGM."""
83 for canary, results in zip(self.canaries, all_results):
84 for version, status in zip(self.versions, results):
85 expected[(canary, version)] = status
86 def _GetBuildStatus(canary, version, **_):
87 return expected[(canary, version)]
88 self.PatchObject(self.committer, '_GetLatestCanaryVersions',
89 return_value=self.versions)
90 self.PatchObject(cbuildbot_config, 'GetCanariesForChromeLKGM',
91 return_value=self.canaries)
92 self.PatchObject(manifest_version.BuildSpecsManager, 'GetBuildStatus',
93 side_effect=_GetBuildStatus)
94 self.committer.FindNewLKGM()
95 self.assertTrue(self.committer._lkgm, lkgm)
97 def testFindNewLKGMBasic(self):
98 """Tests that we return the highest version if all versions are good."""
99 self._TestFindNewLKGM([[self.pass_status] * 2] * 3, '4.0.0')
101 def testFindNewLKGMAdvanced(self):
102 """Tests that we return the only version with passing canaries."""
103 self._TestFindNewLKGM([[self.fail_status, self.pass_status]] * 3, '3.0.0')
105 def testFindNewLKGMWithFailures(self):
106 """Ensure we reject versions with failed builds.
108 This test case is a bit more complex than the two above and tests the logic
109 where we want to reject versions with failed builds.
111 In this example both versions have 2 passing builds. The older version
112 is missing a score from one builder where the newer version reports
113 a failure. In this instance, our scoring mechanism should choose the older
116 all_results = [[self.pass_status] * 2] * 2 + [[self.fail_status, None]]
117 self._TestFindNewLKGM(all_results, '3.0.0')
119 def testCommitNewLKGM(self):
120 """Tests that we can commit a new LKGM file."""
121 self.committer._lkgm = '4.0.0'
122 self.PatchObject(timeout_util, 'IsTreeOpen', return_value=True)
123 self.committer.CommitNewLKGM()
125 # Check the file was actually written out correctly.
126 self.assertEqual(osutils.ReadFile(self.lkgm_file), self.committer._lkgm)
127 self.assertCommandContains([constants.CHROME_LKGM_FILE])
128 self.assertCommandContains(['commit'])
131 if __name__ == '__main__':