| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 from __future__ import print_function |
| 7 |
| 6 # Run build_server so that files needed by tests are copied to the local | 8 # Run build_server so that files needed by tests are copied to the local |
| 7 # third_party directory. | 9 # third_party directory. |
| 8 import build_server | 10 import build_server |
| 9 build_server.main() | 11 build_server.main() |
| 10 | 12 |
| 11 import logging | 13 import logging |
| 12 import optparse | 14 import optparse |
| 13 import os | 15 import os |
| 14 import sys | 16 import sys |
| 15 import time | 17 import time |
| 16 import unittest | 18 import unittest |
| 17 | 19 |
| 18 from handler import Handler | |
| 19 from local_renderer import LocalRenderer | 20 from local_renderer import LocalRenderer |
| 21 from render_servlet import AlwaysOnline |
| 20 from test_util import DisableLogging | 22 from test_util import DisableLogging |
| 21 | 23 |
| 22 # Arguments set up if __main__ specifies them. | 24 # Arguments set up if __main__ specifies them. |
| 23 _BASE_PATH = os.path.join( | 25 _BASE_PATH = os.path.join( |
| 24 os.path.abspath(os.path.dirname(__file__)), os.pardir, os.pardir) | 26 os.path.abspath(os.path.dirname(__file__)), os.pardir, os.pardir) |
| 25 _EXPLICIT_TEST_FILES = None | 27 _EXPLICIT_TEST_FILES = None |
| 26 | 28 |
| 27 def _GetPublicFiles(): | 29 def _GetPublicFiles(): |
| 28 '''Gets all public files mapped to their contents. | 30 '''Gets all public files mapped to their contents. |
| 29 ''' | 31 ''' |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 def testCronAndPublicFiles(self): | 46 def testCronAndPublicFiles(self): |
| 45 '''Runs cron then requests every public file. Cron needs to be run first | 47 '''Runs cron then requests every public file. Cron needs to be run first |
| 46 because the public file requests are offline. | 48 because the public file requests are offline. |
| 47 ''' | 49 ''' |
| 48 if _EXPLICIT_TEST_FILES is not None: | 50 if _EXPLICIT_TEST_FILES is not None: |
| 49 return | 51 return |
| 50 | 52 |
| 51 print('Running cron...') | 53 print('Running cron...') |
| 52 start_time = time.time() | 54 start_time = time.time() |
| 53 try: | 55 try: |
| 54 render_content, render_status, _ = self._renderer.Render('/cron/stable') | 56 logging_info = logging.info |
| 57 logging.info = print |
| 58 render_content, render_status, _ = self._renderer.Render('/_cron/stable') |
| 55 self.assertEqual(200, render_status) | 59 self.assertEqual(200, render_status) |
| 56 self.assertEqual('Success', render_content) | 60 self.assertEqual('Success', render_content) |
| 57 finally: | 61 finally: |
| 62 logging.info = logging_info |
| 58 print('Took %s seconds' % (time.time() - start_time)) | 63 print('Took %s seconds' % (time.time() - start_time)) |
| 59 | 64 |
| 60 public_files = _GetPublicFiles() | 65 public_files = _GetPublicFiles() |
| 61 | 66 |
| 62 print('Rendering %s public files...' % len(public_files.keys())) | 67 print('Rendering %s public files...' % len(public_files.keys())) |
| 63 start_time = time.time() | 68 start_time = time.time() |
| 64 try: | 69 try: |
| 65 for path, content in _GetPublicFiles().iteritems(): | 70 for path, content in _GetPublicFiles().iteritems(): |
| 66 def check_result(render_content, render_status, _): | 71 def check_result(render_content, render_status, _): |
| 67 self.assertEqual(200, render_status, | 72 self.assertEqual(200, render_status, |
| 68 'Got %s when rendering %s' % (render_status, path)) | 73 'Got %s when rendering %s' % (render_status, path)) |
| 69 # This is reaaaaally rough since usually these will be tiny templates | 74 # This is reaaaaally rough since usually these will be tiny templates |
| 70 # that render large files. At least it'll catch zero-length responses. | 75 # that render large files. At least it'll catch zero-length responses. |
| 71 self.assertTrue(len(render_content) >= len(content), | 76 self.assertTrue(len(render_content) >= len(content), |
| 72 'Content was "%s" when rendering %s' % (render_content, path)) | 77 'Content was "%s" when rendering %s' % (render_content, path)) |
| 73 check_result(*self._renderer.Render(path)) | 78 check_result(*self._renderer.Render(path)) |
| 74 # Samples are internationalized, test some locales. | 79 # Samples are internationalized, test some locales. |
| 75 if path.endswith('/samples.html'): | 80 if path.endswith('/samples.html'): |
| 76 for lang in ['en-US', 'es', 'ar']: | 81 for lang in ['en-US', 'es', 'ar']: |
| 77 check_result(*self._renderer.Render( | 82 check_result(*self._renderer.Render( |
| 78 path, headers={'Accept-Language': '%s;q=0.8' % lang})) | 83 path, headers={'Accept-Language': '%s;q=0.8' % lang})) |
| 79 finally: | 84 finally: |
| 80 print('Took %s seconds' % (time.time() - start_time)) | 85 print('Took %s seconds' % (time.time() - start_time)) |
| 81 | 86 |
| 87 @AlwaysOnline |
| 82 def testExplicitFiles(self): | 88 def testExplicitFiles(self): |
| 83 '''Tests just the files in _EXPLICIT_TEST_FILES. | 89 '''Tests just the files in _EXPLICIT_TEST_FILES. |
| 84 ''' | 90 ''' |
| 85 if _EXPLICIT_TEST_FILES is None: | 91 if _EXPLICIT_TEST_FILES is None: |
| 86 return | 92 return |
| 87 for filename in _EXPLICIT_TEST_FILES: | 93 for filename in _EXPLICIT_TEST_FILES: |
| 88 print('Rendering %s...' % filename) | 94 print('Rendering %s...' % filename) |
| 89 start_time = time.time() | 95 start_time = time.time() |
| 90 try: | 96 try: |
| 91 render_content, render_status, _ = self._renderer.Render( | 97 render_content, render_status, _ = self._renderer.Render(filename) |
| 92 filename, always_online=True) | |
| 93 self.assertEqual(200, render_status) | 98 self.assertEqual(200, render_status) |
| 94 self.assertTrue(render_content != '') | 99 self.assertTrue(render_content != '') |
| 95 finally: | 100 finally: |
| 96 print('Took %s seconds' % (time.time() - start_time)) | 101 print('Took %s seconds' % (time.time() - start_time)) |
| 97 | 102 |
| 98 @DisableLogging('warning') | 103 @DisableLogging('warning') |
| 104 @AlwaysOnline |
| 99 def testFileNotFound(self): | 105 def testFileNotFound(self): |
| 100 render_content, render_status, _ = self._renderer.Render( | 106 render_content, render_status, _ = self._renderer.Render( |
| 101 '/extensions/notfound.html', always_online=True) | 107 '/extensions/notfound.html') |
| 102 self.assertEqual(404, render_status) | 108 self.assertEqual(404, render_status) |
| 103 | 109 |
| 104 if __name__ == '__main__': | 110 if __name__ == '__main__': |
| 105 parser = optparse.OptionParser() | 111 parser = optparse.OptionParser() |
| 106 parser.add_option('-p', '--path', default=None) | 112 parser.add_option('-p', '--path', default=None) |
| 107 parser.add_option('-a', '--all', action='store_true', default=False) | 113 parser.add_option('-a', '--all', action='store_true', default=False) |
| 108 (opts, args) = parser.parse_args() | 114 (opts, args) = parser.parse_args() |
| 109 if not opts.all: | 115 if not opts.all: |
| 110 _EXPLICIT_TEST_FILES = args | 116 _EXPLICIT_TEST_FILES = args |
| 111 if opts.path is not None: | 117 if opts.path is not None: |
| 112 _BASE_PATH = opts.path | 118 _BASE_PATH = opts.path |
| 113 # Kill sys.argv because we have our own flags. | 119 # Kill sys.argv because we have our own flags. |
| 114 sys.argv = [sys.argv[0]] | 120 sys.argv = [sys.argv[0]] |
| 115 unittest.main() | 121 unittest.main() |
| OLD | NEW |