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

Side by Side Diff: chromite/chromite_unittest.py

Issue 6005004: WIP Chromite supporting "LEGACY" mode, as requested by Anush. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Removed 'HOST' option when looking for chroot specs. Created 9 years, 11 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
OLDNEW
(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 chromite.py."""
8
9 import doctest
10 import os
11 import re
12 import unittest
13
14 import chromite
15 from lib import text_menu
16 import mox
17
18
19 class _DeathException(Exception):
20 """A bogus exception used by the mock out of Die."""
21 pass
22
23
24 class TestFindCommand(unittest.TestCase):
25 """Test chromite._FindCommand."""
26
27 def setUp(self):
28 """Test initialization."""
29 # Create our mox and stub out function calls used by _FindCommand()...
30 self.mox = mox.Mox()
31 self.mox.StubOutWithMock(chromite, 'Die')
32 self.mox.StubOutWithMock(chromite, 'Info')
33 self.mox.StubOutWithMock(text_menu, 'TextMenu')
34
35 def tearDown(self):
36 """Test cleanup."""
37 # Unset stubs...
38 self.mox.UnsetStubs()
39
40 def testInvalidCommand(self):
41 """Test that _FindCommand('implode') causes Die()."""
42 # Should be a call to Die. We'll have it fake a _DeathException...
43 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException)
44
45 # Run the command and verify proper mocks were called...
46 self.mox.ReplayAll()
47 self.assertRaises(_DeathException, chromite._FindCommand, 'implode')
48 self.mox.VerifyAll()
49
50 def testBlankCommandWithQuit(self):
51 """Test that _FindCommand('') shows menu, mocking quit.
52
53 This tests the case that the user quit out of the menu without choosing
54 anything.
55 """
56 # Should be one call to TextMenu, which will return None for quit.
57 text_menu.TextMenu(mox.IsA(list),
58 mox.IsA(basestring),
59 menu_width=0).AndReturn(None)
60
61 # Run the command and verify proper mocks were called...
62 self.mox.ReplayAll()
63 cmd_str = chromite._FindCommand('')
64 self.mox.VerifyAll()
65
66 self.assertEquals(cmd_str, None,
67 '_FindCommand should have returned None from TextMenu')
68
69 def testBlankCommandWithChoice0(self):
70 """Test that _FindCommand('') shows menu, mocking choice 0.
71
72 This tests the case that the user chose choice 0 for the menu.
73 """
74 # Should be one call to TextMenu, which will return item 0.
75 text_menu.TextMenu(mox.IsA(list),
76 mox.IsA(basestring),
77 menu_width=0).AndReturn(0)
78
79 # Run the command and verify proper mocks were called...
80 self.mox.ReplayAll()
81 cmd_str = chromite._FindCommand('')
82 self.mox.VerifyAll()
83
84 self.assertTrue(isinstance(cmd_str, basestring),
85 '_FindCommand should return a string')
86 self.assertTrue(bool(cmd_str),
87 '_FindCommand should not return a blank string')
88
89 def _TestExactCommand(self, cmd_to_find):
90 """Helper for tests that try to find an exact match.
91
92 Args:
93 cmd_to_find: The name of the command to find, like 'Build', ...
94 """
95 # Expect no mocks to be called...
96
97 # Run the command and verify proper mocks were called...
98 self.mox.ReplayAll()
99 cmd_str = chromite._FindCommand(cmd_to_find)
100 self.mox.VerifyAll()
101
102 self.assertEqual(cmd_str, cmd_to_find.lower(),
103 '_FindCommand("%s") should return "%s" back.' %
104 (cmd_to_find, cmd_to_find.lower()))
105
106 def testCaseSensitiveBuild(self):
107 """Test that _FindCommand('build') returns 'build'.
108
109 The command matching should be case sensitive.
110 """
111 self._TestExactCommand('build')
112
113 def testCaseInsensitiveBuild(self):
sosa 2011/01/11 04:44:22 Up to you but you may want to mock out the "known"
diandersAtChromium 2011/01/12 00:04:45 Added TODO for this. On 2011/01/11 04:44:22, sosa
114 """Test that _FindCommand('BUiLD') returns 'build'.
115
116 The command matching should be case insensitive.
117 """
118 self._TestExactCommand('BUiLD')
119
120 def testShCommand(self):
121 """Test that _FindCommand('sh') returns 'shell'.
122
123 This serves two purposes:
124 1. Test the 'prefix' feature of _FindCommand
125 2. Validate that nobody has introduced another command that starts with
126 'sh', since it's expected that many people will use this to invoke the
127 shell.
128 """
129 # _FindCommand should give us a message that it has interpreted sh as shell.
130 chromite.Info(mox.IsA(basestring))
131
132 # Run the command and verify proper mocks were called...
133 self.mox.ReplayAll()
134 cmd_str = chromite._FindCommand('sh')
135 self.mox.VerifyAll()
136
137 self.assertEqual(cmd_str, 'shell',
138 '_FindCommand("sh") should return "shell" back.')
139
140
141 class TestFindSpec(unittest.TestCase):
142 """Test chromite._FindSpec."""
143
144 def setUp(self):
145 """Test initialization."""
146 # Create our mox and stub out function calls used by _FindSpec()...
sosa 2011/01/11 04:44:22 It almost seems for the setup and teardown's you s
diandersAtChromium 2011/01/12 00:04:45 Not going to in this case, though I can see your p
147 self.mox = mox.Mox()
148 self.mox.StubOutWithMock(os, 'listdir')
149 self.mox.StubOutWithMock(os.path, 'isfile')
150 self.mox.StubOutWithMock(chromite, 'Die')
151 self.mox.StubOutWithMock(chromite, 'Info')
152 self.mox.StubOutWithMock(text_menu, 'TextMenu')
153
154 def tearDown(self):
155 """Test cleanup."""
156 # Unset stubs...
157 self.mox.UnsetStubs()
158
159 def testInvalidSpec(self):
160 """Test that _FindSpec('bogusSpec') causes Die()."""
161 # Pass this spec name...
162 spec_name = 'bogusSpec'
163
164 # We'll tell mox to say that these specs exist...
165 dir_list = ['x87-toadstool.spec', 'x87-luigi.SPeC', 'x88-princess.spec',
166 '_default']
167
168 # This spec doesn't represent any full path.
169 os.path.isfile(spec_name).AndReturn(False)
170
171 # This spec isn't found in our search path.
172 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
173 ).AndReturn(False)
174
175 # Give the fake directory listing...
176 os.listdir(mox.IsA(basestring)).MultipleTimes().AndReturn(dir_list)
177
178 # Should be a call to Die. We'll have it fake a _DeathException...
179 chromite.Die(mox.IsA(basestring)).AndRaise(_DeathException)
180
181 # Run the command and verify proper mocks were called...
182 self.mox.ReplayAll()
183 self.assertRaises(_DeathException, chromite._FindSpec, 'bogusSpec')
184 self.mox.VerifyAll()
185
186 def testFullPath(self):
187 """Test that _FindSpec(full_path) returns full_path.
188
189 _FindSpec is defined so that if you pass a full file path to it, it
190 should just return that. It doesn't need to have any special suffix or
191 live in a spec folder.
192 """
193 # Pass this spec name...
194 spec_name = __file__
195
196 # Just say that this is a full path...
197 os.path.isfile(spec_name).AndReturn(True)
198
199 # Run the command and verify proper mocks were called...
200 self.mox.ReplayAll()
201 path = chromite._FindSpec(spec_name)
202 self.mox.VerifyAll()
203
204 self.assertEqual(path, spec_name,
205 '_FindSpec() should just return param if full path.')
206
207 def testExactSpecName(self):
208 """Test that _FindSpec(exact_spec_name) returns the path for the spec."""
209 # We'll search for this bogus spec; we'll use mox to pretend it exists in
210 # the search path.
211 spec_name = 'y87-luigi'
212
213 # This spec doesn't represent any full path
214 os.path.isfile(spec_name).AndReturn(False)
215
216 # When we look through the search path for this spec (with .spec at
217 # the end), we will consider the spec to be found.
218 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).AndReturn(True)
219
220 # Run the command and verify proper mocks were called...
221 self.mox.ReplayAll()
222 spec_path = chromite._FindSpec(spec_name)
223 self.mox.VerifyAll()
224
225 self.assertTrue(re.search('^/.*%s.spec$' % spec_name, spec_path),
226 '_FindSpec() should have returned absolute path for spec.')
227
228 def testUniqueSpecName(self):
229 """Test that _FindSpec(unique_part_name) returns the path for the spec."""
230 # We'll search for this spec. Weird capitalization on purpose to test
231 # case sensitiveness.
232 spec_name = 'ToaDSTooL'
233
234 # We'll tell mox to say that these specs exist in the first directory...
235 dir_list = ['_default',
236 'x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
237
238 # We expect it to find this spec.
239 expected_result = 'x87-toadstool.SPeC'
240
241 # Self-checks for test code...
242 assert dir_list == sorted(dir_list)
243
244 # This spec doesn't represent any full path.
245 os.path.isfile(spec_name).AndReturn(False)
246
247 # This spec isn't found in our search path.
248 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
249 ).AndReturn(False)
250
251 # Return our directory listing.
252 # TODO(dianders): How to make first mocked call return dir_list and
253 # subsequent return []
254 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
255
256 # Run the command and verify proper mocks were called...
257 self.mox.ReplayAll()
258 spec_path = chromite._FindSpec(spec_name)
259 self.mox.VerifyAll()
260
261 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
262 '_FindSpec("%s") incorrectly returned "%s".' %
263 (spec_name, spec_path))
264
265 def _TestBlankSpecName(self, menu_return):
266 """Helper for tests passing a blank spec name.
267
268 Args:
269 menu_return: This value is returned by TextMenu. Could be none or an
270 integer index into the dir_list of this function.
271 """
272 # We'll search for this spec.
273 spec_name = ''
274
275 # We'll tell mox to say that these specs exist in the first directory...
276 # ...only valid specs to make it easier to compare things.
277 dir_list = ['x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
278 num_specs = len(dir_list)
279
280 # Self-checks for test code...
281 assert menu_return < len(dir_list)
282 assert dir_list == sorted(dir_list)
283
284 # This spec doesn't represent any full path.
285 os.path.isfile(spec_name).AndReturn(False)
286
287 # Return our directory listing.
288 # TODO(dianders): How to make first mocked call return dir_list and
289 # subsequent return []
290 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
291
292 # We expect there to be 1 more item than we passed in, since we account
293 # for 'HOST'.
294 check_num_items_fn = lambda items: len(items) == (num_specs + 1)
295
296 # Add 1 to menu_return, since 'HOST' is first...
297 if menu_return is None:
298 adjusted_menu_return = menu_return
299 else:
300 adjusted_menu_return = menu_return + 1
301
302 # Should be one call to TextMenu, which will return menu_return.
303 text_menu.TextMenu(mox.And(mox.IsA(list), mox.Func(check_num_items_fn)),
304 mox.IsA(basestring)).AndReturn(adjusted_menu_return)
305
306 # Run the command and verify proper mocks were called...
307 self.mox.ReplayAll()
308 spec_path = chromite._FindSpec(spec_name)
309 self.mox.VerifyAll()
310
311 if menu_return is None:
312 self.assertEquals(spec_path, None,
313 '_FindSpec should have returned None from TextMenu')
314 else:
315 expected_result = dir_list[menu_return]
316 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
317 '_FindSpec("%s") incorrectly returned "%s".' %
318 (spec_name, spec_path))
319
320 def testBlankSpecNameWithQuit(self):
321 """Test that _FindSpec('') shows menu, mocking quit."""
322 self._TestBlankSpecName(None)
323
324 def testBlankSpecNameWithChoice0(self):
325 """Test that _FindSpec('') shows menu, mocking choice 0."""
326 self._TestBlankSpecName(0)
327
328 def testPartialSpecNameWithChoice0(self):
329 """Test that _FindSpec(non_unique_str) shows menu, mocking choice 0."""
330 # We'll search for this spec.
331 spec_name = 'x87'
332
333 # We'll tell mox to say that these specs exist in the first directory...
334 dir_list = ['_default',
335 'x87-luigi.spec', 'x87-toadstool.SPeC', 'x88-princess.spec']
336
337 # We expect 2 matches and should get back luigi as choice 0.
338 num_match = 2
339 expected_result = 'x87-luigi.spec'
340
341 # Self-checks for test code...
342 assert dir_list == sorted(dir_list)
343
344 # This spec doesn't represent any full path.
345 os.path.isfile(spec_name).AndReturn(False)
346
347 # This spec isn't found in our search path.
348 os.path.isfile(mox.Regex('^/.*%s.spec$' % spec_name)).MultipleTimes(
349 ).AndReturn(False)
350
351 # Return our directory listing.
352 # TODO(dianders): How to make first mocked call return dir_list and
353 # subsequent return []
354 os.listdir(mox.IsA(basestring)).AndReturn(dir_list)
355
356 # Should be one call to TextMenu, which will return 0.
357 text_menu.TextMenu(mox.And(mox.IsA(list),
358 mox.Func(lambda items: len(items) == num_match)),
359 mox.IsA(basestring)).AndReturn(0)
360
361 # Run the command and verify proper mocks were called...
362 self.mox.ReplayAll()
363 spec_path = chromite._FindSpec(spec_name)
364 self.mox.VerifyAll()
365
366 self.assertTrue(re.search('^/.*%s$' % expected_result, spec_path),
367 '_FindSpec("%s") incorrectly returned "%s".' %
368 (spec_name, spec_path))
369
370
371 if __name__ == '__main__':
372 doctest.testmod(chromite)
373 unittest.main()
374
OLDNEW
« chromite/chromite.py ('K') | « chromite/chromite.sh ('k') | chromite/lib/cros_build_lib.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698