Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: cros_mark_as_stable_unittest.py

Issue 3798003: Robustify and speed up cros_mark_all_as_stable. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git
Patch Set: Check no changes Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cros_mark_as_stable.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 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 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Unit tests for cros_mark_as_stable.py.""" 7 """Unit tests for cros_mark_as_stable.py."""
8 8
9 9
10 import mox 10 import mox
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 '%s %s' % (self._branch, 'cros/master')) 82 '%s %s' % (self._branch, 'cros/master'))
83 self.mox.ReplayAll() 83 self.mox.ReplayAll()
84 self.assertTrue(branch.Exists()) 84 self.assertTrue(branch.Exists())
85 self.mox.VerifyAll() 85 self.mox.VerifyAll()
86 86
87 87
88 class EBuildTest(mox.MoxTestBase): 88 class EBuildTest(mox.MoxTestBase):
89 89
90 def setUp(self): 90 def setUp(self):
91 mox.MoxTestBase.setUp(self) 91 mox.MoxTestBase.setUp(self)
92 self.package = 'test_package'
93 self.ebuild_path = '/path/test_package-0.0.1-r1.ebuild'
94 self.ebuild_path_no_rev = '/path/test_package-0.0.1.ebuild'
95 92
96 def testInit(self): 93 def testInit(self):
97 self.mox.StubOutWithMock(cros_mark_as_stable._EBuild, '_FindEBuildPath')
98 self.mox.StubOutWithMock(cros_mark_as_stable._EBuild, '_ParseEBuildPath') 94 self.mox.StubOutWithMock(cros_mark_as_stable._EBuild, '_ParseEBuildPath')
99 95
100 cros_mark_as_stable._EBuild._FindEBuildPath( 96 ebuild_path = '/overlay/cat/test_package/test_package-0.0.1-r1.ebuild'
101 self.package).AndReturn(self.ebuild_path)
102 cros_mark_as_stable._EBuild._ParseEBuildPath( 97 cros_mark_as_stable._EBuild._ParseEBuildPath(
103 self.ebuild_path).AndReturn(['/path/test_package-0.0.1', 98 ebuild_path).AndReturn(['/overlay/cat/test_package-0.0.1',
104 '/path/test_package', 99 '/overlay/cat/test_package',
105 1]) 100 1])
101 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input')
102 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id',
103 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}']
104 cros_mark_as_stable.fileinput.input(ebuild_path).AndReturn(mock_file)
105
106 self.mox.ReplayAll() 106 self.mox.ReplayAll()
107 ebuild = cros_mark_as_stable._EBuild(self.package, 'my_id') 107 ebuild = cros_mark_as_stable._EBuild(ebuild_path)
108 self.mox.VerifyAll() 108 self.mox.VerifyAll()
109 self.assertEquals(ebuild.package, self.package) 109 self.assertEquals(ebuild.package, 'cat/test_package')
110 self.assertEquals(ebuild.ebuild_path, self.ebuild_path) 110 self.assertEquals(ebuild.ebuild_path, ebuild_path)
111 self.assertEquals(ebuild.ebuild_path_no_revision, 111 self.assertEquals(ebuild.ebuild_path_no_revision,
112 '/path/test_package-0.0.1') 112 '/overlay/cat/test_package-0.0.1')
113 self.assertEquals(ebuild.ebuild_path_no_version, '/path/test_package') 113 self.assertEquals(ebuild.ebuild_path_no_version,
114 '/overlay/cat/test_package')
114 self.assertEquals(ebuild.current_revision, 1) 115 self.assertEquals(ebuild.current_revision, 1)
115 self.assertEquals(ebuild.commit_id, 'my_id')
116
117 def testFindEBuildPath(self):
118 self.mox.StubOutWithMock(cros_mark_as_stable, '_SimpleRunCommand')
119 cmd = ('ACCEPT_KEYWORDS="x86 arm amd64" '
120 'equery-x86-generic which %s 2> /dev/null')
121 cros_mark_as_stable._SimpleRunCommand(cmd % self.package).AndReturn(
122 self.ebuild_path)
123 self.mox.ReplayAll()
124 path = cros_mark_as_stable._EBuild._FindEBuildPath(self.package)
125 self.mox.VerifyAll()
126 self.assertEquals(path, self.ebuild_path)
127 116
128 def testParseEBuildPath(self): 117 def testParseEBuildPath(self):
129 # Test with ebuild with revision number. 118 # Test with ebuild with revision number.
130 no_rev, no_version, revision = cros_mark_as_stable._EBuild._ParseEBuildPath( 119 no_rev, no_version, revision = cros_mark_as_stable._EBuild._ParseEBuildPath(
131 self.ebuild_path) 120 '/path/test_package-0.0.1-r1.ebuild')
132 self.assertEquals(no_rev, '/path/test_package-0.0.1') 121 self.assertEquals(no_rev, '/path/test_package-0.0.1')
133 self.assertEquals(no_version, '/path/test_package') 122 self.assertEquals(no_version, '/path/test_package')
134 self.assertEquals(revision, 1) 123 self.assertEquals(revision, 1)
135 124
136 def testParseEBuildPathNoRevisionNumber(self): 125 def testParseEBuildPathNoRevisionNumber(self):
137 # Test with ebuild without revision number. 126 # Test with ebuild without revision number.
138 no_rev, no_version, revision = cros_mark_as_stable._EBuild._ParseEBuildPath( 127 no_rev, no_version, revision = cros_mark_as_stable._EBuild._ParseEBuildPath(
139 self.ebuild_path_no_rev) 128 '/path/test_package-0.0.1.ebuild')
140 self.assertEquals(no_rev, '/path/test_package-0.0.1') 129 self.assertEquals(no_rev, '/path/test_package-0.0.1')
141 self.assertEquals(no_version, '/path/test_package') 130 self.assertEquals(no_version, '/path/test_package')
142 self.assertEquals(revision, 0) 131 self.assertEquals(revision, 0)
143 132
144 133
145 class EBuildStableMarkerTest(mox.MoxTestBase): 134 class EBuildStableMarkerTest(mox.MoxTestBase):
146 135
147 def setUp(self): 136 def setUp(self):
148 mox.MoxTestBase.setUp(self) 137 mox.MoxTestBase.setUp(self)
149 self.mox.StubOutWithMock(cros_mark_as_stable, '_SimpleRunCommand') 138 self.mox.StubOutWithMock(cros_mark_as_stable, '_SimpleRunCommand')
139 self.mox.StubOutWithMock(cros_mark_as_stable, 'RunCommand')
140 self.mox.StubOutWithMock(os, 'unlink')
150 self.m_ebuild = self.mox.CreateMock(cros_mark_as_stable._EBuild) 141 self.m_ebuild = self.mox.CreateMock(cros_mark_as_stable._EBuild)
151 self.m_ebuild.package = 'test_package' 142 self.m_ebuild.package = 'test_package'
152 self.m_ebuild.current_revision = 1 143 self.m_ebuild.current_revision = 1
153 self.m_ebuild.ebuild_path_no_revision = '/path/test_package-0.0.1' 144 self.m_ebuild.ebuild_path_no_revision = '/path/test_package-0.0.1'
154 self.m_ebuild.ebuild_path_no_version = '/path/test_package' 145 self.m_ebuild.ebuild_path_no_version = '/path/test_package'
155 self.m_ebuild.ebuild_path = '/path/test_package-0.0.1-r1.ebuild' 146 self.m_ebuild.ebuild_path = '/path/test_package-0.0.1-r1.ebuild'
156 self.revved_ebuild_path = '/path/test_package-0.0.1-r2.ebuild' 147 self.revved_ebuild_path = '/path/test_package-0.0.1-r2.ebuild'
157 148
158 def testRevEBuild(self): 149 def testRevEBuild(self):
159 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input') 150 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input')
160 self.mox.StubOutWithMock(cros_mark_as_stable.os.path, 'exists') 151 self.mox.StubOutWithMock(cros_mark_as_stable.os.path, 'exists')
161 self.mox.StubOutWithMock(cros_mark_as_stable.shutil, 'copyfile') 152 self.mox.StubOutWithMock(cros_mark_as_stable.shutil, 'copyfile')
162 m_file = self.mox.CreateMock(file) 153 m_file = self.mox.CreateMock(file)
163 154
164 # Prepare mock fileinput. This tests to make sure both the commit id 155 # Prepare mock fileinput. This tests to make sure both the commit id
165 # and keywords are changed correctly. 156 # and keywords are changed correctly.
166 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id', 157 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id',
167 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}'] 158 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}']
168 159
169 ebuild_9999 = self.m_ebuild.ebuild_path_no_version + '-9999.ebuild' 160 ebuild_9999 = self.m_ebuild.ebuild_path_no_version + '-9999.ebuild'
170 cros_mark_as_stable.os.path.exists(ebuild_9999).AndReturn(True) 161 cros_mark_as_stable.os.path.exists(ebuild_9999).AndReturn(True)
171 cros_mark_as_stable.shutil.copyfile(ebuild_9999, self.revved_ebuild_path) 162 cros_mark_as_stable.shutil.copyfile(ebuild_9999, self.revved_ebuild_path)
172 cros_mark_as_stable.fileinput.input(self.revved_ebuild_path, 163 cros_mark_as_stable.fileinput.input(self.revved_ebuild_path,
173 inplace=1).AndReturn(mock_file) 164 inplace=1).AndReturn(mock_file)
174 m_file.write('EAPI=2') 165 m_file.write('EAPI=2')
175 m_file.write('CROS_WORKON_COMMIT="my_id"\n') 166 m_file.write('CROS_WORKON_COMMIT="my_id"\n')
176 m_file.write('KEYWORDS="x86 arm"') 167 m_file.write('KEYWORDS="x86 arm"')
177 m_file.write('src_unpack(){}') 168 m_file.write('src_unpack(){}')
169 diff_cmd = ['diff', '-Bu', self.m_ebuild.ebuild_path,
170 self.revved_ebuild_path]
171 cros_mark_as_stable.RunCommand(diff_cmd, exit_code=True,
172 print_cmd=False).AndReturn(1)
178 cros_mark_as_stable._SimpleRunCommand('git add ' + self.revved_ebuild_path) 173 cros_mark_as_stable._SimpleRunCommand('git add ' + self.revved_ebuild_path)
179 cros_mark_as_stable._SimpleRunCommand('git rm ' + self.m_ebuild.ebuild_path) 174 cros_mark_as_stable._SimpleRunCommand('git rm ' + self.m_ebuild.ebuild_path)
180 175
181 self.mox.ReplayAll() 176 self.mox.ReplayAll()
182 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild) 177 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild)
183 marker.RevEBuild('my_id', redirect_file=m_file) 178 marker.RevEBuild('my_id', redirect_file=m_file)
184 self.mox.VerifyAll() 179 self.mox.VerifyAll()
185 180
181 def testRevUnchangedEBuild(self):
182 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input')
183 self.mox.StubOutWithMock(cros_mark_as_stable.os.path, 'exists')
184 self.mox.StubOutWithMock(cros_mark_as_stable.shutil, 'copyfile')
185 m_file = self.mox.CreateMock(file)
186
187 # Prepare mock fileinput. This tests to make sure both the commit id
188 # and keywords are changed correctly.
189 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id',
190 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}']
191
192 ebuild_9999 = self.m_ebuild.ebuild_path_no_version + '-9999.ebuild'
193 cros_mark_as_stable.os.path.exists(ebuild_9999).AndReturn(True)
194 cros_mark_as_stable.shutil.copyfile(ebuild_9999, self.revved_ebuild_path)
195 cros_mark_as_stable.fileinput.input(self.revved_ebuild_path,
196 inplace=1).AndReturn(mock_file)
197 m_file.write('EAPI=2')
198 m_file.write('CROS_WORKON_COMMIT="my_id"\n')
199 m_file.write('KEYWORDS="x86 arm"')
200 m_file.write('src_unpack(){}')
201 diff_cmd = ['diff', '-Bu', self.m_ebuild.ebuild_path,
202 self.revved_ebuild_path]
203 cros_mark_as_stable.RunCommand(diff_cmd, exit_code=True,
204 print_cmd=False).AndReturn(0)
205 cros_mark_as_stable.os.unlink(self.revved_ebuild_path)
206
207 self.mox.ReplayAll()
208 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild)
209 marker.RevEBuild('my_id', redirect_file=m_file)
210 self.mox.VerifyAll()
211
186 def testRevMissingEBuild(self): 212 def testRevMissingEBuild(self):
187 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input') 213 self.mox.StubOutWithMock(cros_mark_as_stable.fileinput, 'input')
188 self.mox.StubOutWithMock(cros_mark_as_stable.os.path, 'exists') 214 self.mox.StubOutWithMock(cros_mark_as_stable.os.path, 'exists')
189 self.mox.StubOutWithMock(cros_mark_as_stable.shutil, 'copyfile') 215 self.mox.StubOutWithMock(cros_mark_as_stable.shutil, 'copyfile')
190 self.mox.StubOutWithMock(cros_mark_as_stable, 'Die') 216 self.mox.StubOutWithMock(cros_mark_as_stable, 'Die')
191 m_file = self.mox.CreateMock(file) 217 m_file = self.mox.CreateMock(file)
192 218
193 # Prepare mock fileinput. This tests to make sure both the commit id 219 # Prepare mock fileinput. This tests to make sure both the commit id
194 # and keywords are changed correctly. 220 # and keywords are changed correctly.
195 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id', 221 mock_file = ['EAPI=2', 'CROS_WORKON_COMMIT=old_id',
196 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}'] 222 'KEYWORDS=\"~x86 ~arm\"', 'src_unpack(){}']
197 223
198 ebuild_9999 = self.m_ebuild.ebuild_path_no_version + '-9999.ebuild' 224 ebuild_9999 = self.m_ebuild.ebuild_path_no_version + '-9999.ebuild'
199 cros_mark_as_stable.os.path.exists(ebuild_9999).AndReturn(False) 225 cros_mark_as_stable.os.path.exists(ebuild_9999).AndReturn(False)
200 cros_mark_as_stable.Die("Missing 9999 ebuild: %s" % ebuild_9999) 226 cros_mark_as_stable.Die("Missing 9999 ebuild: %s" % ebuild_9999)
201 cros_mark_as_stable.shutil.copyfile(ebuild_9999, self.revved_ebuild_path) 227 cros_mark_as_stable.shutil.copyfile(ebuild_9999, self.revved_ebuild_path)
202 cros_mark_as_stable.fileinput.input(self.revved_ebuild_path, 228 cros_mark_as_stable.fileinput.input(self.revved_ebuild_path,
203 inplace=1).AndReturn(mock_file) 229 inplace=1).AndReturn(mock_file)
204 m_file.write('EAPI=2') 230 m_file.write('EAPI=2')
205 m_file.write('CROS_WORKON_COMMIT="my_id"\n') 231 m_file.write('CROS_WORKON_COMMIT="my_id"\n')
206 m_file.write('KEYWORDS="x86 arm"') 232 m_file.write('KEYWORDS="x86 arm"')
207 m_file.write('src_unpack(){}') 233 m_file.write('src_unpack(){}')
234 diff_cmd = ['diff', '-Bu', self.m_ebuild.ebuild_path,
235 self.revved_ebuild_path]
236 cros_mark_as_stable.RunCommand(diff_cmd, exit_code=True,
237 print_cmd=False).AndReturn(1)
208 cros_mark_as_stable._SimpleRunCommand('git add ' + self.revved_ebuild_path) 238 cros_mark_as_stable._SimpleRunCommand('git add ' + self.revved_ebuild_path)
209 cros_mark_as_stable._SimpleRunCommand('git rm ' + self.m_ebuild.ebuild_path) 239 cros_mark_as_stable._SimpleRunCommand('git rm ' + self.m_ebuild.ebuild_path)
210 240
211 self.mox.ReplayAll() 241 self.mox.ReplayAll()
212 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild) 242 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild)
213 marker.RevEBuild('my_id', redirect_file=m_file) 243 marker.RevEBuild('my_id', redirect_file=m_file)
214 self.mox.VerifyAll() 244 self.mox.VerifyAll()
215 245
216 246
217 def testCommitChange(self): 247 def testCommitChange(self):
218 mock_message = 'Commit me' 248 mock_message = 'Commit me'
219 cros_mark_as_stable._SimpleRunCommand( 249 cros_mark_as_stable._SimpleRunCommand(
220 'git commit -am "%s"' % mock_message) 250 'git commit -am "%s"' % mock_message)
221 self.mox.ReplayAll() 251 self.mox.ReplayAll()
222 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild) 252 marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild)
223 marker.CommitChange(mock_message) 253 marker.CommitChange(mock_message)
224 self.mox.VerifyAll() 254 self.mox.VerifyAll()
225 255
226 def testPushChange(self): 256 def testPushChange(self):
227 #cros_mark_as_stable._SimpleRunCommand('git push') 257 #cros_mark_as_stable._SimpleRunCommand('git push')
228 #self.mox.ReplayAll() 258 #self.mox.ReplayAll()
229 #marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild) 259 #marker = cros_mark_as_stable.EBuildStableMarker(self.m_ebuild)
230 #marker.PushChange() 260 #marker.PushChange()
231 #self.mox.VerifyAll() 261 #self.mox.VerifyAll()
232 pass 262 pass
233 263
264
265 class _Package(object):
266 def __init__(self, package):
267 self.package = package
268
269
234 class BuildEBuildDictionaryTest(mox.MoxTestBase): 270 class BuildEBuildDictionaryTest(mox.MoxTestBase):
235 271
236 def setUp(self): 272 def setUp(self):
237 mox.MoxTestBase.setUp(self) 273 mox.MoxTestBase.setUp(self)
238 self.mox.StubOutWithMock(cros_mark_as_stable, '_SimpleRunCommand') 274 self.mox.StubOutWithMock(cros_mark_as_stable.os, 'walk')
239 self.ebuild_path = '/path/test_package-0.0.1-r1.ebuild' 275 self.mox.StubOutWithMock(cros_mark_as_stable, 'RunCommand')
240 self.package = "test_package" 276 self.package = 'chromeos-base/test_package'
277 self.root = '/overlay/chromeos-base/test_package'
278 self.package_path = self.root + '/test_package-0.0.1.ebuild'
279 paths = [[self.root, [], []]]
280 cros_mark_as_stable.os.walk("/overlay").AndReturn(paths)
281 self.mox.StubOutWithMock(cros_mark_as_stable, '_FindStableEBuilds')
241 282
242 def testValidPackage(self): 283
243 overlays = {"/path": []} 284 def testWantedPackage(self):
244 cmd = ('ACCEPT_KEYWORDS="x86 arm amd64" ' 285 overlays = {"/overlay": []}
245 'equery-x86-generic which %s 2> /dev/null' % self.package) 286 package = _Package(self.package)
246 cros_mark_as_stable._SimpleRunCommand(cmd).AndReturn(self.ebuild_path) 287 cros_mark_as_stable._FindStableEBuilds([]).AndReturn(package)
247 self.mox.ReplayAll() 288 self.mox.ReplayAll()
248 cros_mark_as_stable._BuildEBuildDictionary(overlays, [self.package], []) 289 cros_mark_as_stable._BuildEBuildDictionary(overlays, False, [self.package])
290 self.mox.VerifyAll()
249 self.assertEquals(len(overlays), 1) 291 self.assertEquals(len(overlays), 1)
250 self.assertEquals(overlays["/path"][0].package, self.package) 292 self.assertEquals(overlays["/overlay"], [package])
251 self.mox.VerifyAll()
252 293
253 def testPackageInDifferentOverlay(self): 294 def testUnwantedPackage(self):
254 self.mox.StubOutWithMock(cros_mark_as_stable, 'Die') 295 overlays = {"/overlay": []}
255 cros_mark_as_stable.Die("No overlay found for %s" % self.ebuild_path) 296 package = _Package(self.package)
256 cmd = ('ACCEPT_KEYWORDS="x86 arm amd64" ' 297 cros_mark_as_stable._FindStableEBuilds([]).AndReturn(package)
257 'equery-x86-generic which %s 2> /dev/null' % self.package)
258 cros_mark_as_stable._SimpleRunCommand(cmd).AndReturn(self.ebuild_path)
259 overlays = {"/newpath": []}
260 self.mox.ReplayAll() 298 self.mox.ReplayAll()
261 cros_mark_as_stable._BuildEBuildDictionary(overlays, [self.package], []) 299 cros_mark_as_stable._BuildEBuildDictionary(overlays, False, [])
262 self.assertEquals(len(overlays), 1) 300 self.assertEquals(len(overlays), 1)
263 self.assertEquals(overlays["/newpath"], []) 301 self.assertEquals(overlays["/overlay"], [])
264 self.mox.VerifyAll()
265
266 def testMissingPackage(self):
267 self.mox.StubOutWithMock(cros_mark_as_stable, 'Die')
268 cros_mark_as_stable.Die("No ebuild found for %s" % self.package)
269 cmd = ('ACCEPT_KEYWORDS="x86 arm amd64" '
270 'equery-x86-generic which %s 2> /dev/null' % self.package)
271 cros_mark_as_stable._SimpleRunCommand(cmd).AndReturn("")
272 self.mox.ReplayAll()
273 overlays = {"/path": []}
274 cros_mark_as_stable._BuildEBuildDictionary(overlays, [self.package], [])
275 self.assertEquals(len(overlays), 1)
276 self.assertEquals(overlays["/path"], [])
277 self.mox.VerifyAll() 302 self.mox.VerifyAll()
278 303
279 304
280 if __name__ == '__main__': 305 if __name__ == '__main__':
281 unittest.main() 306 unittest.main()
OLDNEW
« no previous file with comments | « cros_mark_as_stable.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698