Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / scripts / cros_mark_chrome_as_stable_unittest.py
1 #!/usr/bin/python
2
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.
6
7 """Unit tests for cros_mark_chrome_as_stable.py."""
8
9 # run with:
10 #    cros_sdk ../../chromite.cbuildbot/cros_mark_chrome_as_stable_unittest.py
11
12 import mox
13 import os
14 import sys
15
16 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
17                                 '..', '..'))
18 from chromite.cbuildbot import constants
19 from chromite.cbuildbot 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
25
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'
31
32 def _TouchAndWrite(path, data=None):
33   """Writes data (if it exists) to the file specified by the path."""
34   fh = open(path, 'w')
35   if data:
36     fh.write(data)
37
38   fh.close()
39
40
41 class _StubCommandResult(object):
42   """Helper for mocking RunCommand results."""
43   def __init__(self, msg):
44     self.output = msg
45
46
47 class CrosMarkChromeAsStable(cros_test_lib.MoxTempDirTestCase):
48   """Tests for cros_mark_chrome_as_stable."""
49
50   def setUp(self):
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)
55
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')
68
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')
75
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,
81                    '\n'.join(
82                        (stable_data,
83                         '%s=%s' % (cros_mark_chrome_as_stable._CHROME_SVN_TAG,
84                                    fake_svn_rev))))
85
86   def testFindChromeCandidates(self):
87     """Test creation of stable ebuilds from mock dir."""
88     unstable, stable_ebuilds = cros_mark_chrome_as_stable.FindChromeCandidates(
89         self.mock_chrome_dir)
90
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)
98
99   def _GetStableEBuilds(self):
100     """Common helper to create a list of stable ebuilds."""
101     return [
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),
106     ]
107
108   def testTOTFindChromeUprevCandidate(self):
109     """Tests if we can find tot uprev candidate from our mock dir data."""
110     stable_ebuilds = self._GetStableEBuilds()
111
112     candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
113         stable_ebuilds, constants.CHROME_REV_TOT,
114         self.sticky_branch)
115
116     self.assertEqual(candidate.ebuild_path, self.tot_stable)
117
118   def testLatestFindChromeUprevCandidate(self):
119     """Tests if we can find latest uprev candidate from our mock dir data."""
120     stable_ebuilds = self._GetStableEBuilds()
121
122     candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
123         stable_ebuilds, constants.CHROME_REV_LATEST,
124         self.sticky_branch)
125
126     self.assertEqual(candidate.ebuild_path, self.latest_stable)
127
128   def testStickyFindChromeUprevCandidate(self):
129     """Tests if we can find sticky uprev candidate from our mock dir data."""
130     stable_ebuilds = self._GetStableEBuilds()
131
132     candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate(
133         stable_ebuilds, constants.CHROME_REV_STICKY,
134         self.sticky_branch)
135
136     self.assertEqual(candidate.ebuild_path, self.sticky_rc)
137
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(
145           _StubCommandResult(
146             'Some Junk 2134\nRevision: %s\nOtherInfo: test_data' %
147             fake_svn_rev))
148     self.mox.ReplayAll()
149     revision = gclient.GetTipOfTrunkSvnRevision(A_URL)
150     self.mox.VerifyAll()
151     self.assertEquals(revision, fake_svn_rev)
152
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(
162           _StubCommandResult(
163             'Some Junk 2134\nRevision: %s\nOtherInfo: test_data' %
164             fake_svn_rev))
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'))
169
170     self.mox.ReplayAll()
171     version = cros_mark_chrome_as_stable._GetSpecificVersionUrl(ARBITRARY_URL,
172                                                                 fake_svn_rev)
173     self.mox.VerifyAll()
174     self.assertEquals(version, '8.0.256.0')
175
176   def testCheckIfChromeRightForOS(self):
177     """Tests if we can find the chromeos build from our mock DEPS."""
178     ARBITRARY_URL = 'phthp://sores.chromium.org/tqs/7.0.224.1/DEPS'
179     test_data1 = "buildspec_platforms:\n    'chromeos,',\n"
180     test_data2 = "buildspec_platforms:\n    'android,',\n"
181     self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
182     cros_build_lib.RunCommand(
183         ['svn', 'cat', ARBITRARY_URL],
184         redirect_stdout=True).AndReturn(_StubCommandResult(test_data1))
185     cros_build_lib.RunCommand(
186         ['svn', 'cat', ARBITRARY_URL],
187         redirect_stdout=True).AndReturn(_StubCommandResult(test_data2))
188     self.mox.ReplayAll()
189     expected_deps = cros_mark_chrome_as_stable.CheckIfChromeRightForOS(
190         ARBITRARY_URL)
191     unexpected_deps = cros_mark_chrome_as_stable.CheckIfChromeRightForOS(
192         ARBITRARY_URL)
193     self.mox.VerifyAll()
194     self.assertTrue(expected_deps)
195     self.assertFalse(unexpected_deps)
196
197   def testGetLatestRelease(self):
198     """Tests if we can find the latest release from our mock url data."""
199     ARBITRARY_URL = 'phthp://sores.chromium.org/tqs'
200     input_data = ['7.0.224.1/', '7.0.224.2/', '8.0.365.5/', 'LATEST.txt']
201     test_data = '\n'.join(input_data)
202     sorted_data = '\n'.join(reversed(input_data))
203     self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
204     cros_build_lib.RunCommand(
205         ['svn', 'ls', ARBITRARY_URL + '/releases'],
206         redirect_stdout=True).AndReturn(_StubCommandResult(test_data))
207     cros_build_lib.RunCommand(
208         ['sort', '--version-sort', '-r'], input=test_data,
209         redirect_stdout=True).AndReturn(_StubCommandResult(sorted_data))
210     # pretend this one is missing to test the skipping logic.
211     cros_build_lib.RunCommand(
212         ['svn', 'ls', ARBITRARY_URL + '/releases/8.0.365.5/DEPS'],
213         error_code_ok=True, redirect_stdout=True).AndReturn(
214           _StubCommandResult('BAH BAH BAH'))
215     cros_build_lib.RunCommand(
216         ['svn', 'ls', ARBITRARY_URL + '/releases/7.0.224.2/DEPS'],
217         error_code_ok=True, redirect_stdout=True).AndReturn(
218           _StubCommandResult('DEPS\n'))
219     self.mox.ReplayAll()
220     self.mox.StubOutWithMock(cros_mark_chrome_as_stable,
221                              'CheckIfChromeRightForOS')
222     cros_mark_chrome_as_stable.CheckIfChromeRightForOS(
223         ARBITRARY_URL + '/releases/7.0.224.2/DEPS').AndReturn(
224             _StubCommandResult('True'))
225     self.mox.ReplayAll()
226     release = cros_mark_chrome_as_stable.GetLatestRelease(ARBITRARY_URL)
227     self.mox.VerifyAll()
228     self.assertEqual('7.0.224.2', release)
229
230   def testGetLatestStickyRelease(self):
231     """Tests if we can find the latest sticky release from our mock url data."""
232     ARBITRARY_URL = 'http://src.chromium.org/svn'
233     test_data = '\n'.join(['7.0.222.1/',
234                            '8.0.224.2/',
235                            '8.0.365.5/',
236                            'LATEST.txt'])
237     self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
238     cros_build_lib.RunCommand(
239         ['svn', 'ls', ARBITRARY_URL + '/releases'],
240         redirect_stdout=True).AndReturn(_StubCommandResult('some_data'))
241     cros_build_lib.RunCommand(
242         ['sort', '--version-sort', '-r'], input='some_data',
243         redirect_stdout=True).AndReturn(_StubCommandResult(test_data))
244     cros_build_lib.RunCommand(
245         ['svn', 'ls', ARBITRARY_URL + '/releases/8.0.224.2/DEPS'],
246         error_code_ok=True, redirect_stdout=True).AndReturn(
247           _StubCommandResult('DEPS\n'))
248     self.mox.ReplayAll()
249     self.mox.StubOutWithMock(cros_mark_chrome_as_stable,
250                              'CheckIfChromeRightForOS')
251     cros_mark_chrome_as_stable.CheckIfChromeRightForOS(
252         ARBITRARY_URL + '/releases/8.0.224.2/DEPS').AndReturn(
253             _StubCommandResult(True))
254     self.mox.ReplayAll()
255     release = cros_mark_chrome_as_stable.GetLatestRelease(ARBITRARY_URL,
256                                                           '8.0.224')
257     self.mox.VerifyAll()
258     self.assertEqual('8.0.224.2', release)
259
260   def testLatestChromeRevisionListLink(self):
261     """Tests link generation to rev lists.
262
263     Verifies that we can generate a link to the revision list between the
264     latest Chromium release and the last one we successfully built.
265     """
266     _TouchAndWrite(self.latest_new, stable_data)
267     expected = cros_mark_chrome_as_stable.GetChromeRevisionLinkFromVersions(
268         self.latest_stable_version, self.latest_new_version)
269     made = cros_mark_chrome_as_stable.GetChromeRevisionListLink(
270         cros_mark_chrome_as_stable.ChromeEBuild(self.latest_stable),
271         cros_mark_chrome_as_stable.ChromeEBuild(self.latest_new),
272         constants.CHROME_REV_LATEST)
273     self.assertEqual(expected, made)
274
275   def testStickyEBuild(self):
276     """Tests if we can find the sticky ebuild from our mock directories."""
277     stable_ebuilds = self._GetStableEBuilds()
278     sticky_ebuild = cros_mark_chrome_as_stable._GetStickyEBuild(
279         stable_ebuilds)
280     self.assertEqual(sticky_ebuild.chrome_version, self.sticky_version)
281
282   def testChromeEBuildInit(self):
283     """Tests if the chrome_version is set correctly in a ChromeEBuild."""
284     ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.sticky)
285     self.assertEqual(ebuild.chrome_version, self.sticky_version)
286
287   def _CommonMarkAsStableTest(self, chrome_rev, new_version, old_ebuild_path,
288                               new_ebuild_path, commit_string_indicator):
289     """Common function used for test functions for MarkChromeEBuildAsStable.
290
291     This function stubs out others calls, and runs MarkChromeEBuildAsStable
292     with the specified args.
293
294     Args:
295       chrome_rev: standard chrome_rev argument
296       new_version: version we are revving up to
297       old_ebuild_path: path to the stable ebuild
298       new_ebuild_path: path to the to be created path
299       commit_string_indicator: a string that the commit message must contain
300     """
301     self.mox.StubOutWithMock(cros_build_lib, 'RunCommand')
302     self.mox.StubOutWithMock(git, 'RunGit')
303     self.mox.StubOutWithMock(portage_utilities.EBuild, 'CommitChange')
304     stable_candidate = cros_mark_chrome_as_stable.ChromeEBuild(old_ebuild_path)
305     unstable_ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.unstable)
306     chrome_version = new_version
307     commit = None
308     overlay_dir = self.mock_chrome_dir
309
310     git.RunGit(overlay_dir, ['add', new_ebuild_path])
311     git.RunGit(overlay_dir, ['rm', old_ebuild_path])
312     portage_utilities.EBuild.CommitChange(
313         mox.StrContains(commit_string_indicator), overlay_dir)
314
315     self.mox.ReplayAll()
316     cros_mark_chrome_as_stable.MarkChromeEBuildAsStable(
317         stable_candidate, unstable_ebuild, chrome_rev, chrome_version, commit,
318         overlay_dir)
319     self.mox.VerifyAll()
320
321   def testStickyMarkAsStable(self):
322     """Tests to see if we can mark chrome as stable for a new sticky release."""
323     self._CommonMarkAsStableTest(
324         constants.CHROME_REV_STICKY,
325         self.sticky_new_rc_version, self.sticky_rc,
326         self.sticky_new_rc, 'stable_release')
327
328   def testLatestMarkAsStable(self):
329     """Tests to see if we can mark chrome for a latest release."""
330     self._CommonMarkAsStableTest(
331         constants.CHROME_REV_LATEST,
332         self.latest_new_version, self.latest_stable,
333         self.latest_new, 'latest_release')
334
335   def testTotMarkAsStable(self):
336     """Tests to see if we can mark chrome for tot."""
337     self._CommonMarkAsStableTest(
338         constants.CHROME_REV_TOT,
339         self.tot_new_version, self.tot_stable,
340         self.tot_new, 'tot')
341
342
343 if __name__ == '__main__':
344   cros_test_lib.main()