OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # Copyright (c) 2010 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 import cros_mark_chrome_as_stable |
| 10 import mox |
| 11 import os |
| 12 import shutil |
| 13 import sys |
| 14 import tempfile |
| 15 import unittest |
| 16 import urllib |
| 17 |
| 18 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
| 19 import cros_mark_as_stable |
| 20 |
| 21 unstable_data = 'KEYWORDS=~x86 ~arm' |
| 22 stable_data = 'KEYWORDS=x86 arm' |
| 23 |
| 24 def _TouchAndWrite(path, data=None): |
| 25 """Writes data (if it exists) to the file specified by the path.""" |
| 26 fh = open(path, 'w') |
| 27 if data: |
| 28 fh.write(data) |
| 29 |
| 30 fh.close() |
| 31 |
| 32 |
| 33 class CrosMarkChromeAsStable(mox.MoxTestBase): |
| 34 |
| 35 def setUp(self): |
| 36 """Setup vars and create mock dir.""" |
| 37 mox.MoxTestBase.setUp(self) |
| 38 self.tmp_overlay = tempfile.mkdtemp(prefix='chromiumos-overlay') |
| 39 self.mock_chrome_dir = os.path.join(self.tmp_overlay, 'chromeos-base', |
| 40 'chromeos-chrome') |
| 41 os.makedirs(self.mock_chrome_dir) |
| 42 |
| 43 self.unstable = os.path.join(self.mock_chrome_dir, |
| 44 'chromeos-chrome-9999.ebuild') |
| 45 self.sticky_branch = '8.0.224' |
| 46 self.sticky_version = '%s.503' % self.sticky_branch |
| 47 self.sticky = os.path.join(self.mock_chrome_dir, |
| 48 'chromeos-chrome-%s.ebuild' % |
| 49 self.sticky_version) |
| 50 self.sticky_rc_version = '%s.504' % self.sticky_branch |
| 51 self.sticky_rc = os.path.join(self.mock_chrome_dir, |
| 52 'chromeos-chrome-%s_rc-r1.ebuild' % |
| 53 self.sticky_rc_version) |
| 54 self.latest_stable_version = '8.0.300.1' |
| 55 self.latest_stable = os.path.join(self.mock_chrome_dir, |
| 56 'chromeos-chrome-%s_rc-r2.ebuild' % |
| 57 self.latest_stable_version) |
| 58 self.tot_stable_version = '9.0.305.0' |
| 59 self.tot_stable = os.path.join(self.mock_chrome_dir, |
| 60 'chromeos-chrome-%s_alpha-r1.ebuild' % |
| 61 self.tot_stable_version) |
| 62 |
| 63 self.sticky_new_rc_version = '%s.520' % self.sticky_branch |
| 64 self.sticky_new_rc = os.path.join(self.mock_chrome_dir, |
| 65 'chromeos-chrome-%s_rc-r1.ebuild' % |
| 66 self.sticky_new_rc_version) |
| 67 self.latest_new_version = '9.0.305.1' |
| 68 self.latest_new = os.path.join(self.mock_chrome_dir, |
| 69 'chromeos-chrome-%s_rc-r1.ebuild' % |
| 70 self.latest_new_version) |
| 71 self.tot_new_version = '9.0.306.0' |
| 72 self.tot_new = os.path.join(self.mock_chrome_dir, |
| 73 'chromeos-chrome-%s_alpha-r1.ebuild' % |
| 74 self.tot_new_version) |
| 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, stable_data) |
| 81 |
| 82 def tearDown(self): |
| 83 """Cleans up mock dir.""" |
| 84 shutil.rmtree(self.tmp_overlay) |
| 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 self.assertEqual(unstable.ebuild_path, self.unstable) |
| 92 self.assertEqual(len(stable_ebuilds), 4) |
| 93 self.assertTrue(cros_mark_chrome_as_stable.ChromeEBuild(self.sticky) in |
| 94 stable_ebuilds) |
| 95 self.assertTrue(cros_mark_chrome_as_stable.ChromeEBuild(self.sticky_rc) in |
| 96 stable_ebuilds) |
| 97 self.assertTrue(cros_mark_chrome_as_stable.ChromeEBuild(self.latest_stable) |
| 98 in stable_ebuilds) |
| 99 self.assertTrue(cros_mark_chrome_as_stable.ChromeEBuild(self.tot_stable) in |
| 100 stable_ebuilds) |
| 101 |
| 102 def _GetStableEBuilds(self): |
| 103 """Common helper to create a list of stable ebuilds.""" |
| 104 return [ |
| 105 cros_mark_chrome_as_stable.ChromeEBuild(self.sticky), |
| 106 cros_mark_chrome_as_stable.ChromeEBuild(self.sticky_rc), |
| 107 cros_mark_chrome_as_stable.ChromeEBuild(self.latest_stable), |
| 108 cros_mark_chrome_as_stable.ChromeEBuild(self.tot_stable), |
| 109 ] |
| 110 |
| 111 def testTOTFindChromeUprevCandidate(self): |
| 112 """Tests if we can find tot uprev candidate from our mock dir data.""" |
| 113 stable_ebuilds = self._GetStableEBuilds() |
| 114 |
| 115 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate( |
| 116 stable_ebuilds, cros_mark_chrome_as_stable.TIP_OF_TRUNK, |
| 117 self.sticky_branch) |
| 118 |
| 119 self.assertEqual(candidate.ebuild_path, self.tot_stable) |
| 120 |
| 121 def testLatestFindChromeUprevCandidate(self): |
| 122 """Tests if we can find latest uprev candidate from our mock dir data.""" |
| 123 stable_ebuilds = self._GetStableEBuilds() |
| 124 |
| 125 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate( |
| 126 stable_ebuilds, cros_mark_chrome_as_stable.LATEST_RELEASE, |
| 127 self.sticky_branch) |
| 128 |
| 129 self.assertEqual(candidate.ebuild_path, self.latest_stable) |
| 130 |
| 131 def testStickyFindChromeUprevCandidate(self): |
| 132 """Tests if we can find sticky uprev candidate from our mock dir data.""" |
| 133 stable_ebuilds = self._GetStableEBuilds() |
| 134 |
| 135 candidate = cros_mark_chrome_as_stable.FindChromeUprevCandidate( |
| 136 stable_ebuilds, cros_mark_chrome_as_stable.STICKY, |
| 137 self.sticky_branch) |
| 138 |
| 139 self.assertEqual(candidate.ebuild_path, self.sticky_rc) |
| 140 |
| 141 def testGetTipOfTrunkSvnRevision(self): |
| 142 """Tests if we can get the latest svn revision from TOT.""" |
| 143 self.mox.StubOutWithMock(cros_mark_chrome_as_stable, 'RunCommand') |
| 144 cros_mark_chrome_as_stable.RunCommand( |
| 145 ['svn', 'info', cros_mark_chrome_as_stable._GetSvnUrl()], |
| 146 redirect_stdout=True).AndReturn( |
| 147 'Some Junk 2134\nRevision: 12345\nOtherInfo: test_data') |
| 148 self.mox.ReplayAll() |
| 149 revision = cros_mark_chrome_as_stable._GetTipOfTrunkSvnRevision() |
| 150 self.mox.VerifyAll() |
| 151 self.assertEquals(revision, '12345') |
| 152 |
| 153 def testGetTipOfTrunkVersion(self): |
| 154 """Tests if we get the latest version from TOT.""" |
| 155 self.mox.StubOutWithMock(urllib, 'urlopen') |
| 156 mock_file = self.mox.CreateMock(file) |
| 157 urllib.urlopen(os.path.join(cros_mark_chrome_as_stable._GetSvnUrl(), 'src', |
| 158 'chrome', 'VERSION')).AndReturn(mock_file) |
| 159 mock_file.read().AndReturn('A=8\nB=0\nC=256\nD=0') |
| 160 mock_file.close() |
| 161 |
| 162 self.mox.ReplayAll() |
| 163 version = cros_mark_chrome_as_stable._GetTipOfTrunkVersion() |
| 164 self.mox.VerifyAll() |
| 165 self.assertEquals(version, '8.0.256.0') |
| 166 |
| 167 def testGetLatestRelease(self): |
| 168 """Tests if we can find the latest release from our mock url data.""" |
| 169 test_data = '\n'.join(['7.0.224.1/', |
| 170 '7.0.224.2/', |
| 171 '8.0.365.5/', |
| 172 'LATEST.txt']) |
| 173 self.mox.StubOutWithMock(cros_mark_chrome_as_stable, 'RunCommand') |
| 174 cros_mark_chrome_as_stable.RunCommand( |
| 175 ['svn', 'ls', 'http://src.chromium.org/svn/releases'], |
| 176 redirect_stdout=True).AndReturn('some_data') |
| 177 cros_mark_chrome_as_stable.RunCommand( |
| 178 ['sort', '--version-sort'], input='some_data', |
| 179 redirect_stdout=True).AndReturn(test_data) |
| 180 self.mox.ReplayAll() |
| 181 release = cros_mark_chrome_as_stable._GetLatestRelease() |
| 182 self.mox.VerifyAll() |
| 183 self.assertEqual('8.0.365.5', release) |
| 184 |
| 185 def testGetLatestStickyRelease(self): |
| 186 """Tests if we can find the latest sticky release from our mock url data.""" |
| 187 test_data = '\n'.join(['7.0.222.1/', |
| 188 '8.0.224.2/', |
| 189 '8.0.365.5/', |
| 190 'LATEST.txt']) |
| 191 self.mox.StubOutWithMock(cros_mark_chrome_as_stable, 'RunCommand') |
| 192 cros_mark_chrome_as_stable.RunCommand( |
| 193 ['svn', 'ls', 'http://src.chromium.org/svn/releases'], |
| 194 redirect_stdout=True).AndReturn('some_data') |
| 195 cros_mark_chrome_as_stable.RunCommand( |
| 196 ['sort', '--version-sort'], input='some_data', |
| 197 redirect_stdout=True).AndReturn(test_data) |
| 198 self.mox.ReplayAll() |
| 199 release = cros_mark_chrome_as_stable._GetLatestRelease(self.sticky_branch) |
| 200 self.mox.VerifyAll() |
| 201 self.assertEqual('8.0.224.2', release) |
| 202 |
| 203 def testStickyVersion(self): |
| 204 """Tests if we can find the sticky version from our mock directories.""" |
| 205 stable_ebuilds = self._GetStableEBuilds() |
| 206 sticky_version = cros_mark_chrome_as_stable._GetStickyVersion( |
| 207 stable_ebuilds) |
| 208 self.assertEqual(sticky_version, self.sticky_version) |
| 209 |
| 210 def testChromeEBuildInit(self): |
| 211 """Tests if the chrome_version is set correctly in a ChromeEBuild.""" |
| 212 ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.sticky) |
| 213 self.assertEqual(ebuild.chrome_version, self.sticky_version) |
| 214 |
| 215 def _CommonMarkAsStableTest(self, chrome_rev, new_version, old_ebuild_path, |
| 216 new_ebuild_path, commit_string_indicator): |
| 217 """Common function used for test functions for MarkChromeEBuildAsStable. |
| 218 |
| 219 This function stubs out others calls, and runs MarkChromeEBuildAsStable |
| 220 with the specified args. |
| 221 |
| 222 Args: |
| 223 chrome_rev: standard chrome_rev argument |
| 224 new_version: version we are revving up to |
| 225 old_ebuild_path: path to the stable ebuild |
| 226 new_ebuild_path: path to the to be created path |
| 227 commit_string_indicator: a string that the commit message must contain |
| 228 """ |
| 229 self.mox.StubOutWithMock(cros_mark_chrome_as_stable, 'RunCommand') |
| 230 self.mox.StubOutWithMock(cros_mark_as_stable.EBuildStableMarker, |
| 231 'CommitChange') |
| 232 stable_candidate = cros_mark_chrome_as_stable.ChromeEBuild(old_ebuild_path) |
| 233 unstable_ebuild = cros_mark_chrome_as_stable.ChromeEBuild(self.unstable) |
| 234 chrome_version = new_version |
| 235 commit = None |
| 236 overlay_dir = self.mock_chrome_dir |
| 237 |
| 238 cros_mark_chrome_as_stable.RunCommand(['git', 'add', new_ebuild_path]) |
| 239 cros_mark_chrome_as_stable.RunCommand(['git', 'rm', old_ebuild_path]) |
| 240 cros_mark_as_stable.EBuildStableMarker.CommitChange( |
| 241 mox.StrContains(commit_string_indicator)) |
| 242 |
| 243 self.mox.ReplayAll() |
| 244 cros_mark_chrome_as_stable.MarkChromeEBuildAsStable( |
| 245 stable_candidate, unstable_ebuild, chrome_rev, chrome_version, commit, |
| 246 overlay_dir) |
| 247 self.mox.VerifyAll() |
| 248 |
| 249 def testStickyMarkAsStable(self): |
| 250 """Tests to see if we can mark chrome as stable for a new sticky release.""" |
| 251 self._CommonMarkAsStableTest(cros_mark_chrome_as_stable.STICKY, |
| 252 self.sticky_new_rc_version, self.sticky_rc, |
| 253 self.sticky_new_rc, 'sticky_release') |
| 254 |
| 255 def testLatestMarkAsStable(self): |
| 256 """Tests to see if we can mark chrome for a latest release.""" |
| 257 self._CommonMarkAsStableTest(cros_mark_chrome_as_stable.LATEST_RELEASE, |
| 258 self.latest_new_version, self.latest_stable, |
| 259 self.latest_new, 'latest_release') |
| 260 |
| 261 def testTotMarkAsStable(self): |
| 262 """Tests to see if we can mark chrome for tot.""" |
| 263 self._CommonMarkAsStableTest(cros_mark_chrome_as_stable.TIP_OF_TRUNK, |
| 264 self.tot_new_version, self.tot_stable, |
| 265 self.tot_new, 'tot') |
| 266 |
| 267 |
| 268 if __name__ == '__main__': |
| 269 unittest.main() |
OLD | NEW |