OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The LUCI Authors. All rights reserved. | 2 # Copyright 2013 The LUCI Authors. All rights reserved. |
3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
5 | 5 |
6 # pylint: disable=R0201 | 6 # pylint: disable=R0201 |
7 | 7 |
8 import StringIO | 8 import StringIO |
9 import base64 | 9 import base64 |
10 import functools | 10 import functools |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 return temp_dir | 107 return temp_dir |
108 | 108 |
109 def temp_join(self, *args): | 109 def temp_join(self, *args): |
110 """Shortcut for joining path with self.run_test_temp_dir.""" | 110 """Shortcut for joining path with self.run_test_temp_dir.""" |
111 return os.path.join(self.run_test_temp_dir, *args) | 111 return os.path.join(self.run_test_temp_dir, *args) |
112 | 112 |
113 | 113 |
114 class RunIsolatedTest(RunIsolatedTestBase): | 114 class RunIsolatedTest(RunIsolatedTestBase): |
115 def setUp(self): | 115 def setUp(self): |
116 super(RunIsolatedTest, self).setUp() | 116 super(RunIsolatedTest, self).setUp() |
| 117 # list of func(args, **kwargs) -> retcode |
| 118 # if the func returns None, then it's skipped. The first function to return |
| 119 # non-None is taken as the retcode for the mocked Popen call. |
| 120 self.popen_mocks = [] |
117 self.popen_calls = [] | 121 self.popen_calls = [] |
118 # pylint: disable=no-self-argument | 122 # pylint: disable=no-self-argument |
119 class Popen(object): | 123 class Popen(object): |
120 def __init__(self2, args, **kwargs): | 124 def __init__(self2, args, **kwargs): |
121 kwargs.pop('cwd', None) | 125 kwargs.pop('cwd', None) |
122 kwargs.pop('env', None) | 126 kwargs.pop('env', None) |
| 127 self2.returncode = None |
| 128 self2.args = args |
| 129 self2.kwargs = kwargs |
123 self.popen_calls.append((args, kwargs)) | 130 self.popen_calls.append((args, kwargs)) |
124 self2.returncode = None | |
125 | 131 |
126 def yield_any_line(self, timeout=None): # pylint: disable=unused-argument | 132 def yield_any_line(self, timeout=None): # pylint: disable=unused-argument |
127 return () | 133 return () |
128 | 134 |
129 def wait(self, timeout=None): # pylint: disable=unused-argument | 135 def wait(self2, timeout=None): # pylint: disable=unused-argument |
130 self.returncode = 0 | 136 self2.returncode = 0 |
131 return self.returncode | 137 for mock_fn in self.popen_mocks: |
| 138 ret = mock_fn(self2.args, **self2.kwargs) |
| 139 if ret is not None: |
| 140 self2.returncode = ret |
| 141 break |
| 142 return self2.returncode |
132 | 143 |
133 def kill(self): | 144 def kill(self): |
134 pass | 145 pass |
135 | 146 |
136 self.mock(subprocess42, 'Popen', Popen) | 147 self.mock(subprocess42, 'Popen', Popen) |
137 | 148 |
138 def test_main(self): | 149 def test_main(self): |
139 self.mock(tools, 'disable_buffering', lambda: None) | 150 self.mock(tools, 'disable_buffering', lambda: None) |
140 isolated = json_dumps( | 151 isolated = json_dumps( |
141 { | 152 { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 'world', | 338 'world', |
328 ] | 339 ] |
329 ret = run_isolated.main(cmd) | 340 ret = run_isolated.main(cmd) |
330 self.assertEqual(1, ret) | 341 self.assertEqual(1, ret) |
331 self.assertEqual(1, len(self.popen_calls)) | 342 self.assertEqual(1, len(self.popen_calls)) |
332 self.assertEqual( | 343 self.assertEqual( |
333 [([u'/bin/echo', u'hello', u'world'], {'detached': True})], | 344 [([u'/bin/echo', u'hello', u'world'], {'detached': True})], |
334 self.popen_calls) | 345 self.popen_calls) |
335 | 346 |
336 def test_main_naked_with_packages(self): | 347 def test_main_naked_with_packages(self): |
| 348 pin_idx_ref = [0] |
| 349 pins = [ |
| 350 [ |
| 351 ('infra/data/x', 'badc0fee'*5), |
| 352 ('infra/data/y', 'cafebabe'*5), |
| 353 ], |
| 354 [ |
| 355 ('infra/tools/echo/linux-amd64', 'deadbeef'*5), |
| 356 ], |
| 357 ] |
| 358 |
| 359 def fake_ensure(args, **_kwargs): |
| 360 if (args[0].endswith('/cipd') and |
| 361 args[1] == 'ensure' |
| 362 and '-json-output' in args): |
| 363 idx = args.index('-json-output') |
| 364 with open(args[idx+1], 'w') as json_out: |
| 365 json.dump({ |
| 366 'result': [ |
| 367 {'package': pkg, 'instance_id': ver} |
| 368 for pkg, ver in pins[pin_idx_ref[0]] |
| 369 ], |
| 370 }, json_out) |
| 371 pin_idx_ref[0] += 1 |
| 372 return 0 |
| 373 |
| 374 self.popen_mocks.append(fake_ensure) |
337 cipd_cache = os.path.join(self.tempdir, 'cipd_cache') | 375 cipd_cache = os.path.join(self.tempdir, 'cipd_cache') |
338 cmd = [ | 376 cmd = [ |
339 '--no-log', | 377 '--no-log', |
340 '--cache', os.path.join(self.tempdir, 'cache'), | 378 '--cache', os.path.join(self.tempdir, 'cache'), |
341 '--cipd-package', 'bin:infra/tools/echo/${platform}:latest', | 379 '--cipd-package', 'bin:infra/tools/echo/${platform}:latest', |
342 '--cipd-package', '.:infra/data/x:latest', | 380 '--cipd-package', '.:infra/data/x:latest', |
343 '--cipd-package', '.:infra/data/y:canary', | 381 '--cipd-package', '.:infra/data/y:canary', |
344 '--cipd-server', self.cipd_server.url, | 382 '--cipd-server', self.cipd_server.url, |
345 '--cipd-cache', cipd_cache, | 383 '--cipd-cache', cipd_cache, |
346 'bin/echo${EXECUTABLE_SUFFIX}', | 384 'bin/echo${EXECUTABLE_SUFFIX}', |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 self.assertEqual(expected, actual) | 647 self.assertEqual(expected, actual) |
610 | 648 |
611 | 649 |
612 if __name__ == '__main__': | 650 if __name__ == '__main__': |
613 fix_encoding.fix_encoding() | 651 fix_encoding.fix_encoding() |
614 if '-v' in sys.argv: | 652 if '-v' in sys.argv: |
615 unittest.TestCase.maxDiff = None | 653 unittest.TestCase.maxDiff = None |
616 logging.basicConfig( | 654 logging.basicConfig( |
617 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 655 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
618 unittest.main() | 656 unittest.main() |
OLD | NEW |