OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding=utf-8 | 2 # coding=utf-8 |
3 # Copyright 2013 The LUCI Authors. All rights reserved. | 3 # Copyright 2013 The LUCI Authors. All rights reserved. |
4 # Use of this source code is governed under the Apache License, Version 2.0 | 4 # Use of this source code is governed under the Apache License, Version 2.0 |
5 # that can be found in the LICENSE file. | 5 # that can be found in the LICENSE file. |
6 | 6 |
7 import base64 | 7 import base64 |
8 import contextlib | |
8 import json | 9 import json |
9 import logging | 10 import logging |
10 import os | 11 import os |
11 import re | 12 import re |
12 import signal | 13 import signal |
13 import sys | 14 import sys |
14 import tempfile | 15 import tempfile |
15 import time | 16 import time |
16 import unittest | 17 import unittest |
17 | 18 |
18 import test_env_bot_code | 19 import test_env_bot_code |
19 test_env_bot_code.setup_test_env() | 20 test_env_bot_code.setup_test_env() |
20 | 21 |
21 # Creates a server mock for functions in net.py. | 22 # Creates a server mock for functions in net.py. |
22 import net_utils | 23 import net_utils |
23 | 24 |
24 from depot_tools import fix_encoding | 25 from depot_tools import fix_encoding |
25 from utils import file_path | 26 from utils import file_path |
26 from utils import large | 27 from utils import large |
27 from utils import logging_utils | 28 from utils import logging_utils |
28 from utils import subprocess42 | 29 from utils import subprocess42 |
29 from libs import luci_context | 30 from libs import luci_context |
30 import bot_auth | 31 import bot_auth |
32 import fake_swarming | |
33 import named_cache | |
31 import remote_client | 34 import remote_client |
32 import fake_swarming | |
33 import task_runner | 35 import task_runner |
34 | 36 |
35 CLIENT_DIR = os.path.normpath( | 37 CLIENT_DIR = os.path.normpath( |
36 os.path.join(test_env_bot_code.BOT_DIR, '..', '..', '..', 'client')) | 38 os.path.join(test_env_bot_code.BOT_DIR, '..', '..', '..', 'client')) |
37 | 39 |
38 sys.path.insert(0, os.path.join(CLIENT_DIR, 'tests')) | 40 sys.path.insert(0, os.path.join(CLIENT_DIR, 'tests')) |
39 import isolateserver_mock | 41 import isolateserver_mock |
40 | 42 |
41 | 43 |
42 def get_manifest(script=None, isolated=None, **kwargs): | 44 def get_manifest(script=None, isolated=None, **kwargs): |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 expected = { | 582 expected = { |
581 u'exit_code': -1, | 583 u'exit_code': -1, |
582 u'hard_timeout': False, | 584 u'hard_timeout': False, |
583 u'io_timeout': False, | 585 u'io_timeout': False, |
584 u'must_signal_internal_failure': None, | 586 u'must_signal_internal_failure': None, |
585 u'version': 3, | 587 u'version': 3, |
586 } | 588 } |
587 self.assertEqual(expected, self._run_command(task_details)) | 589 self.assertEqual(expected, self._run_command(task_details)) |
588 | 590 |
589 def test_run_command_caches(self): | 591 def test_run_command_caches(self): |
592 # Put a file into a named cache. | |
593 cache_manager = named_cache.CacheManager(os.path.join(self.root_dir, u'c')) | |
594 install_dir = os.path.join(self.root_dir, u'install') | |
595 | |
596 @contextlib.contextmanager | |
597 def tmpinstall(): | |
598 with cache_manager.open(): | |
599 cache_manager.install(install_dir, 'foo') | |
600 yield | |
601 cache_manager.uninstall(install_dir, 'foo') | |
602 | |
603 with tmpinstall(), open(os.path.join(install_dir, 'bar'), 'wb') as f: | |
M-A Ruel
2017/05/11 19:55:13
Why not:
with cache_manager.open():
cache_manage
| |
604 f.write('thecache') | |
605 | |
590 # This runs the command for real. | 606 # This runs the command for real. |
591 self.requests(cost_usd=1, exit_code=0); | 607 self.requests(cost_usd=1, exit_code=0) |
592 script = ( | 608 script = ( |
593 'import os\n' | 609 'import os\n' |
594 'print "hi"\n' | 610 'print "hi"\n' |
595 'with open("../../result", "w") as f:\n' | 611 'with open("cache_foo/bar", "rb") as f:\n' |
596 ' print >> f, os.path.abspath(os.readlink("git_cache"))' | 612 ' cached = f.read()\n' |
613 'with open("../../result", "wb") as f:\n' | |
614 ' f.write(cached)\n' | |
615 'with open("cache_foo/bar", "wb") as f:\n' | |
616 ' f.write("updated_cache")\n' | |
597 ) | 617 ) |
598 task_details = self.get_task_details( | 618 task_details = self.get_task_details( |
599 script, | 619 script, |
600 caches=[{'name': 'git_chromium', 'path': 'git_cache'}]) | 620 caches=[{'name': 'foo', 'path': 'cache_foo'}]) |
601 expected = { | 621 expected = { |
602 u'exit_code': 0, | 622 u'exit_code': 0, |
603 u'hard_timeout': False, | 623 u'hard_timeout': False, |
604 u'io_timeout': False, | 624 u'io_timeout': False, |
605 u'must_signal_internal_failure': None, | 625 u'must_signal_internal_failure': None, |
606 u'version': task_runner.OUT_VERSION, | 626 u'version': task_runner.OUT_VERSION, |
607 } | 627 } |
608 self.assertEqual(expected, self._run_command(task_details)) | 628 self.assertEqual(expected, self._run_command(task_details)) |
609 | 629 |
610 with open(os.path.join(self.root_dir, 'result')) as f: | 630 with open(os.path.join(self.root_dir, 'result'), 'rb') as f: |
611 rundir_cache_path = f.read().strip() | 631 self.assertEqual('thecache', f.read()) |
612 named_caches = os.path.join(self.root_dir, 'c', 'named') | 632 |
613 self.assertTrue(os.path.isdir(named_caches)) | 633 with tmpinstall(), open(os.path.join(install_dir, 'bar'), 'rb') as f: |
614 named_cache_path = os.path.abspath( | 634 self.assertEqual(f.read(), 'updated_cache') |
M-A Ruel
2017/05/11 19:55:13
self.assertEqual('updated_cache', f.read())
| |
615 os.readlink(os.path.join(named_caches, 'git_chromium'))) | |
616 self.assertEqual(rundir_cache_path, named_cache_path) | |
617 | 635 |
618 def test_main(self): | 636 def test_main(self): |
619 def load_and_run( | 637 def load_and_run( |
620 manifest, swarming_server, is_grpc, cost_usd_hour, start, | 638 manifest, swarming_server, is_grpc, cost_usd_hour, start, |
621 json_file, run_isolated_flags, bot_file, auth_params_file): | 639 json_file, run_isolated_flags, bot_file, auth_params_file): |
622 self.assertEqual('foo', manifest) | 640 self.assertEqual('foo', manifest) |
623 self.assertEqual('http://localhost', swarming_server) | 641 self.assertEqual('http://localhost', swarming_server) |
624 self.assertFalse(is_grpc) | 642 self.assertFalse(is_grpc) |
625 self.assertEqual(3600., cost_usd_hour) | 643 self.assertEqual(3600., cost_usd_hour) |
626 self.assertEqual(time.time(), start) | 644 self.assertEqual(time.time(), start) |
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1273 fix_encoding.fix_encoding() | 1291 fix_encoding.fix_encoding() |
1274 if '-v' in sys.argv: | 1292 if '-v' in sys.argv: |
1275 unittest.TestCase.maxDiff = None | 1293 unittest.TestCase.maxDiff = None |
1276 logging_utils.prepare_logging(None) | 1294 logging_utils.prepare_logging(None) |
1277 logging_utils.set_console_level( | 1295 logging_utils.set_console_level( |
1278 logging.DEBUG if '-v' in sys.argv else logging.CRITICAL+1) | 1296 logging.DEBUG if '-v' in sys.argv else logging.CRITICAL+1) |
1279 # Fix litteral text expectation. | 1297 # Fix litteral text expectation. |
1280 os.environ['LANG'] = 'en_US.UTF-8' | 1298 os.environ['LANG'] = 'en_US.UTF-8' |
1281 os.environ['LANGUAGE'] = 'en_US.UTF-8' | 1299 os.environ['LANGUAGE'] = 'en_US.UTF-8' |
1282 unittest.main() | 1300 unittest.main() |
OLD | NEW |