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 cros_mark_chrome_as_stable.py."""
10 # cros_sdk ../../chromite/buildbot/cros_mark_chrome_as_stable_unittest.py
16 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
18 from chromite.buildbot import constants
19 from chromite.buildbot import portage_utilities
20 from chromite.lib import cros_build_lib
21 from chromite.lib import cros_test_lib
22 from chromite.lib import gclient
23 from chromite.lib import git
24 from chromite.scripts import cros_mark_chrome_as_stable
26 # pylint: disable=W0212,R0904
27 unstable_data = 'KEYWORDS=~x86 ~arm'
28 stable_data = 'KEYWORDS=x86 arm'
29 fake_svn_rev = '12345'
30 new_fake_svn_rev = '23456'
32 def _TouchAndWrite(path, data=None):
33 """Writes data (if it exists) to the file specified by the path."""
41 class _StubCommandResult(object):
42 """Helper for mocking RunCommand results."""
43 def __init__(self, msg):
47 class CrosMarkChromeAsStable(cros_test_lib.MoxTempDirTestCase):
48 """Tests for cros_mark_chrome_as_stable."""
51 """Setup vars and create mock dir."""
52 self.tmp_overlay = os.path.join(self.tempdir, 'chromiumos-overlay')
53 self.mock_chrome_dir = os.path.join(self.tmp_overlay, constants.CHROME_CP)
54 os.makedirs(self.mock_chrome_dir)
56 ebuild = os.path.join(self.mock_chrome_dir,
57 constants.CHROME_PN + '-%s.ebuild')
58 self.unstable = ebuild % '9999'
59 self.sticky_branch = '8.0.224'
60 self.sticky_version = '%s.503' % self.sticky_branch
61 self.sticky = ebuild % self.sticky_version
62 self.sticky_rc_version = '%s.504' % self.sticky_branch
63 self.sticky_rc = ebuild % (self.sticky_rc_version + '_rc-r1')
64 self.latest_stable_version = '8.0.300.1'
65 self.latest_stable = ebuild % (self.latest_stable_version + '_rc-r2')
66 self.tot_stable_version = '9.0.305.0'
67 self.tot_stable = ebuild % (self.tot_stable_version + '_alpha-r1')
69 self.sticky_new_rc_version = '%s.520' % self.sticky_branch
70 self.sticky_new_rc = ebuild % (self.sticky_new_rc_version + '_rc-r1')
71 self.latest_new_version = '9.0.305.1'
72 self.latest_new = ebuild % (self.latest_new_version + '_rc-r1')
73 self.tot_new_version = '9.0.306.0'
74 self.tot_new = ebuild % (self.tot_new_version + '_alpha-r1')
76 _TouchAndWrite(self.unstable, unstable_data)
77 _TouchAndWrite(self.sticky, stable_data)
78 _TouchAndWrite(self.sticky_rc, stable_data)
79 _TouchAndWrite(self.latest_stable, stable_data)
80 _TouchAndWrite(self.tot_stable,
83 '%s=%s' % (cros_mark_chrome_as_stable._CHROME_SVN_TAG,
86 def testFindChromeCandidates(self):
87 """Test creation of stable ebuilds from mock dir."""
88 unstable, stable_ebuilds = cros_mark_chrome_as_stable.FindChromeCandidates(
91 stable_ebuild_paths = map(lambda eb: eb.ebuild_path, stable_ebuilds)
92 self.assertEqual(unstable.ebuild_path, self.unstable)
93 self.assertEqual(len(stable_ebuilds), 4)
94 self.assertTrue(self.sticky in stable_ebuild_paths)
95 self.assertTrue(self.sticky_rc in stable_ebuild_paths)
96 self.assertTrue(self.latest_stable in stable_ebuild_paths)
97 self.assertTrue(self.tot_stable in stable_ebuild_paths)
99 def _GetStableEBuilds(self):
100 """Common helper to create a list of stable ebuilds."""
102 cros_mark_chrome_as_stable.ChromeEBuild(self.sticky),
103 cros_mark_chrome_as_stable.ChromeEBuild(self.sticky_rc),
104 cros_mark_chrome_as_stable.ChromeEBuild(self.latest_stable),
105 cros_mark_chrome_as_stable.ChromeEBuild(self.tot_stable),
108 def testTOTFindChromeUprevCandidate(self):
109 """Tests if we can find tot uprev candidate from our mock dir data."""
110 stable_ebuilds = self._GetStableEBuilds()
112 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
113 stable_ebuilds, constants.CHROME_REV_TOT,
116 self.assertEqual(candidate.ebuild_path, self.tot_stable)
118 def testLatestFindChromeUprevCandidate(self):
119 """Tests if we can find latest uprev candidate from our mock dir data."""
120 stable_ebuilds = self._GetStableEBuilds()
122 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
123 stable_ebuilds, constants.CHROME_REV_LATEST,
126 self.assertEqual(candidate.ebuild_path, self.latest_stable)
128 def testStickyFindChromeUprevCandidate(self):
129 """Tests if we can find sticky uprev candidate from our mock dir data."""
130 stable_ebuilds = self._GetStableEBuilds()
132 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
133 stable_ebuilds, constants.CHROME_REV_STICKY,
136 self.assertEqual(candidate.ebuild_path, self.sticky_rc)
138 def testGetTipOfTrunkSvnRevision(self):
139 """Tests if we can get the latest svn revision from TOT."""
140 A_URL = 'dorf://mink/delaane/forkat/sertiunu.ortg./desk'
141 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
142 cros_build_lib.RunCommand(
143 ['svn', 'info', A_URL],
144 redirect_stdout=True).AndReturn(
146 'Some Junk 2134\nRevision: %s\nOtherInfo: test_data' %
149 revision = gclient.GetTipOfTrunkSvnRevision(A_URL)
151 self.assertEquals(revision, fake_svn_rev)
153 def testGetTipOfTrunkVersion(self):
154 """Tests if we get the latest version from TOT."""
155 ARBITRARY_URL = 'Pratooey'
156 path = os.path.join(cros_mark_chrome_as_stable._GetSvnUrl(ARBITRARY_URL),
157 'src', 'chrome', 'VERSION')
158 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
159 cros_build_lib.RunCommand(
160 ['svn', 'info', ARBITRARY_URL],
161 redirect_stdout=True).AndReturn(
163 'Some Junk 2134\nRevision: %s\nOtherInfo: test_data' %
165 cros_build_lib.RunCommand(
166 ['svn', 'cat', '-r', fake_svn_rev, path], redirect_stdout=True,
167 error_message=mox.IsA(str)).AndReturn(
168 _StubCommandResult('A=8\nB=0\nC=256\nD=0'))
171 version = cros_mark_chrome_as_stable._GetSpecificVersionUrl(ARBITRARY_URL,
174 self.assertEquals(version, '8.0.256.0')
176 def testGetLatestRelease(self):
177 """Tests if we can find the latest release from our mock url data."""
178 ARBITRARY_URL = 'phthp://sores.chromium.org/tqs'
179 input_data = ['7.0.224.1/', '7.0.224.2/', '8.0.365.5/', 'LATEST.txt']
180 test_data = '\n'.join(input_data)
181 sorted_data = '\n'.join(reversed(input_data))
182 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
183 cros_build_lib.RunCommand(
184 ['svn', 'ls', ARBITRARY_URL + '/releases'],
185 redirect_stdout=True).AndReturn(_StubCommandResult(test_data))
186 cros_build_lib.RunCommand(
187 ['sort', '--version-sort', '-r'], input=test_data,
188 redirect_stdout=True).AndReturn(_StubCommandResult(sorted_data))
189 # pretend this one is missing to test the skipping logic.
190 cros_build_lib.RunCommand(
191 ['svn', 'ls', ARBITRARY_URL + '/releases/8.0.365.5/DEPS'],
192 error_code_ok=True, redirect_stdout=True).AndReturn(
193 _StubCommandResult('BAH BAH BAH'))
194 cros_build_lib.RunCommand(
195 ['svn', 'ls', ARBITRARY_URL + '/releases/7.0.224.2/DEPS'],
196 error_code_ok=True, redirect_stdout=True).AndReturn(
197 _StubCommandResult('DEPS\n'))
199 release = cros_mark_chrome_as_stable._GetLatestRelease(ARBITRARY_URL)
201 self.assertEqual('7.0.224.2', release)
203 def testGetLatestStickyRelease(self):
204 """Tests if we can find the latest sticky release from our mock url data."""
205 ARBITRARY_URL = 'http://src.chromium.org/svn'
206 test_data = '\n'.join(['7.0.222.1/',
210 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
211 cros_build_lib.RunCommand(
212 ['svn', 'ls', ARBITRARY_URL + '/releases'],
213 redirect_stdout=True).AndReturn(_StubCommandResult('some_data'))
214 cros_build_lib.RunCommand(
215 ['sort', '--version-sort', '-r'], input='some_data',
216 redirect_stdout=True).AndReturn(_StubCommandResult(test_data))
217 cros_build_lib.RunCommand(
218 ['svn', 'ls', ARBITRARY_URL + '/releases/8.0.224.2/DEPS'],
219 error_code_ok=True, redirect_stdout=True).AndReturn(
220 _StubCommandResult('DEPS\n'))
222 release = cros_mark_chrome_as_stable._GetLatestRelease(ARBITRARY_URL,
225 self.assertEqual('8.0.224.2', release)
227 def testLatestChromeRevisionListLink(self):
228 """Tests link generation to rev lists.
230 Verifies that we can generate a link to the revision list between the
231 latest Chromium release and the last one we successfully built.
233 _TouchAndWrite(self.latest_new, stable_data)
234 expected = cros_mark_chrome_as_stable.GetChromeRevisionLinkFromVersions(
235 self.latest_stable_version, self.latest_new_version)
236 made = cros_mark_chrome_as_stable.GetChromeRevisionListLink(
237 cros_mark_chrome_as_stable.ChromeEBuild(self.latest_stable),
238 cros_mark_chrome_as_stable.ChromeEBuild(self.latest_new),
239 constants.CHROME_REV_LATEST)
240 self.assertEqual(expected, made)
242 def testStickyEBuild(self):
243 """Tests if we can find the sticky ebuild from our mock directories."""
244 stable_ebuilds = self._GetStableEBuilds()
245 sticky_ebuild = cros_mark_chrome_as_stable._GetStickyEBuild(
247 self.assertEqual(sticky_ebuild.chrome_version, self.sticky_version)
249 def testChromeEBuildInit(self):
250 """Tests if the chrome_version is set correctly in a ChromeEBuild."""
251 ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.sticky)
252 self.assertEqual(ebuild.chrome_version, self.sticky_version)
254 def _CommonMarkAsStableTest(self, chrome_rev, new_version, old_ebuild_path,
255 new_ebuild_path, commit_string_indicator):
256 """Common function used for test functions for MarkChromeEBuildAsStable.
258 This function stubs out others calls, and runs MarkChromeEBuildAsStable
259 with the specified args.
262 chrome_rev: standard chrome_rev argument
263 new_version: version we are revving up to
264 old_ebuild_path: path to the stable ebuild
265 new_ebuild_path: path to the to be created path
266 commit_string_indicator: a string that the commit message must contain
268 self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
269 self.mox.StubOutWithMock(git, 'RunGit')
270 self.mox.StubOutWithMock(portage_utilities.EBuild, 'CommitChange')
271 stable_candidate = cros_mark_chrome_as_stable.ChromeEBuild(old_ebuild_path)
272 unstable_ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.unstable)
273 chrome_version = new_version
275 overlay_dir = self.mock_chrome_dir
277 git.RunGit(overlay_dir, ['add', new_ebuild_path])
278 git.RunGit(overlay_dir, ['rm', old_ebuild_path])
279 portage_utilities.EBuild.CommitChange(
280 mox.StrContains(commit_string_indicator), overlay_dir)
283 cros_mark_chrome_as_stable.MarkChromeEBuildAsStable(
284 stable_candidate, unstable_ebuild, chrome_rev, chrome_version, commit,
288 def testStickyMarkAsStable(self):
289 """Tests to see if we can mark chrome as stable for a new sticky release."""
290 self._CommonMarkAsStableTest(
291 constants.CHROME_REV_STICKY,
292 self.sticky_new_rc_version, self.sticky_rc,
293 self.sticky_new_rc, 'stable_release')
295 def testLatestMarkAsStable(self):
296 """Tests to see if we can mark chrome for a latest release."""
297 self._CommonMarkAsStableTest(
298 constants.CHROME_REV_LATEST,
299 self.latest_new_version, self.latest_stable,
300 self.latest_new, 'latest_release')
302 def testTotMarkAsStable(self):
303 """Tests to see if we can mark chrome for tot."""
304 self._CommonMarkAsStableTest(
305 constants.CHROME_REV_TOT,
306 self.tot_new_version, self.tot_stable,
310 if __name__ == '__main__':