| 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 | |
| 8 # Run build_server so that files needed by tests are copied to the local | 6 # Run build_server so that files needed by tests are copied to the local |
| 9 # third_party directory. | 7 # third_party directory. |
| 10 import build_server | 8 import build_server |
| 11 build_server.main() | 9 build_server.main() |
| 12 | 10 |
| 13 import logging | 11 import logging |
| 14 import optparse | 12 import optparse |
| 15 import os | 13 import os |
| 16 import sys | 14 import sys |
| 17 import time | 15 import time |
| 18 import unittest | 16 import unittest |
| 19 | 17 |
| 20 from cron_servlet import CronServlet | 18 from cron_servlet import CronServlet |
| 21 from local_renderer import LocalRenderer | 19 from local_renderer import LocalRenderer |
| 22 from render_servlet import AlwaysOnline | 20 from test_util import EnableLogging, DisableLogging |
| 23 from test_util import DisableLogging | |
| 24 | 21 |
| 25 # Arguments set up if __main__ specifies them. | 22 # Arguments set up if __main__ specifies them. |
| 26 _BASE_PATH = os.path.join( | 23 _BASE_PATH = os.path.join( |
| 27 os.path.abspath(os.path.dirname(__file__)), os.pardir, os.pardir) | 24 os.path.abspath(os.path.dirname(__file__)), os.pardir, os.pardir) |
| 28 _EXPLICIT_TEST_FILES = None | 25 _EXPLICIT_TEST_FILES = None |
| 29 | 26 |
| 30 def _GetPublicFiles(): | 27 def _GetPublicFiles(): |
| 31 '''Gets all public files mapped to their contents. | 28 '''Gets all public files mapped to their contents. |
| 32 ''' | 29 ''' |
| 33 public_path = os.path.join(_BASE_PATH, 'docs', 'templates', 'public', '') | 30 public_path = os.path.join(_BASE_PATH, 'docs', 'templates', 'public', '') |
| 34 public_files = {} | 31 public_files = {} |
| 35 for path, dirs, files in os.walk(public_path, topdown=True): | 32 for path, dirs, files in os.walk(public_path, topdown=True): |
| 36 dirs[:] = [d for d in dirs if d != '.svn'] | 33 dirs[:] = [d for d in dirs if d != '.svn'] |
| 37 relative_path = path[len(public_path):] | 34 relative_path = path[len(public_path):] |
| 38 for filename in files: | 35 for filename in files: |
| 39 with open(os.path.join(path, filename), 'r') as f: | 36 with open(os.path.join(path, filename), 'r') as f: |
| 40 public_files[os.path.join(relative_path, filename)] = f.read() | 37 public_files[os.path.join(relative_path, filename)] = f.read() |
| 41 return public_files | 38 return public_files |
| 42 | 39 |
| 43 class IntegrationTest(unittest.TestCase): | 40 class IntegrationTest(unittest.TestCase): |
| 44 def setUp(self): | 41 def setUp(self): |
| 45 self._renderer = LocalRenderer(_BASE_PATH) | 42 self._renderer = LocalRenderer(_BASE_PATH) |
| 46 | 43 |
| 44 @EnableLogging('info') |
| 47 def testCronAndPublicFiles(self): | 45 def testCronAndPublicFiles(self): |
| 48 '''Runs cron then requests every public file. Cron needs to be run first | 46 '''Runs cron then requests every public file. Cron needs to be run first |
| 49 because the public file requests are offline. | 47 because the public file requests are offline. |
| 50 ''' | 48 ''' |
| 51 if _EXPLICIT_TEST_FILES is not None: | 49 if _EXPLICIT_TEST_FILES is not None: |
| 52 return | 50 return |
| 53 | 51 |
| 54 print('Running cron...') | 52 print('Running cron...') |
| 55 start_time = time.time() | 53 start_time = time.time() |
| 56 try: | 54 try: |
| 57 logging_info = logging.info | |
| 58 logging.info = print | |
| 59 response = self._renderer.Render('/stable', servlet=CronServlet) | 55 response = self._renderer.Render('/stable', servlet=CronServlet) |
| 60 self.assertEqual(200, response.status) | 56 self.assertEqual(200, response.status) |
| 61 self.assertEqual('Success', response.content.ToString()) | 57 self.assertEqual('Success', response.content.ToString()) |
| 62 finally: | 58 finally: |
| 63 logging.info = logging_info | |
| 64 print('Took %s seconds' % (time.time() - start_time)) | 59 print('Took %s seconds' % (time.time() - start_time)) |
| 65 | 60 |
| 66 public_files = _GetPublicFiles() | 61 public_files = _GetPublicFiles() |
| 67 | 62 |
| 68 print('Rendering %s public files...' % len(public_files.keys())) | 63 print('Rendering %s public files...' % len(public_files.keys())) |
| 69 start_time = time.time() | 64 start_time = time.time() |
| 70 try: | 65 try: |
| 71 for path, content in _GetPublicFiles().iteritems(): | 66 for path, content in _GetPublicFiles().iteritems(): |
| 72 def check_result(response): | 67 def check_result(response): |
| 73 self.assertEqual(200, response.status, | 68 self.assertEqual(200, response.status, |
| 74 'Got %s when rendering %s' % (response.status, path)) | 69 'Got %s when rendering %s' % (response.status, path)) |
| 75 # This is reaaaaally rough since usually these will be tiny templates | 70 # This is reaaaaally rough since usually these will be tiny templates |
| 76 # that render large files. At least it'll catch zero-length responses. | 71 # that render large files. At least it'll catch zero-length responses. |
| 77 self.assertTrue(len(response.content) >= len(content), | 72 self.assertTrue(len(response.content) >= len(content), |
| 78 'Content was "%s" when rendering %s' % (response.content, path)) | 73 'Content was "%s" when rendering %s' % (response.content, path)) |
| 79 check_result(self._renderer.Render(path)) | 74 check_result(self._renderer.Render(path, offline=True)) |
| 80 # Samples are internationalized, test some locales. | 75 # Samples are internationalized, test some locales. |
| 81 if path.endswith('/samples.html'): | 76 if path.endswith('/samples.html'): |
| 82 for lang in ['en-US', 'es', 'ar']: | 77 for lang in ['en-US', 'es', 'ar']: |
| 83 check_result(self._renderer.Render( | 78 check_result(self._renderer.Render( |
| 84 path, headers={'Accept-Language': '%s;q=0.8' % lang})) | 79 path, |
| 80 headers={'Accept-Language': '%s;q=0.8' % lang}, |
| 81 offline=True)) |
| 85 finally: | 82 finally: |
| 86 print('Took %s seconds' % (time.time() - start_time)) | 83 print('Took %s seconds' % (time.time() - start_time)) |
| 87 | 84 |
| 88 @AlwaysOnline | |
| 89 def testExplicitFiles(self): | 85 def testExplicitFiles(self): |
| 90 '''Tests just the files in _EXPLICIT_TEST_FILES. | 86 '''Tests just the files in _EXPLICIT_TEST_FILES. |
| 91 ''' | 87 ''' |
| 92 if _EXPLICIT_TEST_FILES is None: | 88 if _EXPLICIT_TEST_FILES is None: |
| 93 return | 89 return |
| 94 for filename in _EXPLICIT_TEST_FILES: | 90 for filename in _EXPLICIT_TEST_FILES: |
| 95 print('Rendering %s...' % filename) | 91 print('Rendering %s...' % filename) |
| 96 start_time = time.time() | 92 start_time = time.time() |
| 97 try: | 93 try: |
| 98 response = self._renderer.Render(filename) | 94 response = self._renderer.Render(filename) |
| 99 self.assertEqual(200, response.status) | 95 self.assertEqual(200, response.status) |
| 100 self.assertTrue(response.content != '') | 96 self.assertTrue(response.content != '') |
| 101 finally: | 97 finally: |
| 102 print('Took %s seconds' % (time.time() - start_time)) | 98 print('Took %s seconds' % (time.time() - start_time)) |
| 103 | 99 |
| 104 @DisableLogging('warning') | 100 @DisableLogging('warning') |
| 105 @AlwaysOnline | |
| 106 def testFileNotFound(self): | 101 def testFileNotFound(self): |
| 107 response = self._renderer.Render('/extensions/notfound.html') | 102 response = self._renderer.Render('/extensions/notfound.html') |
| 108 self.assertEqual(404, response.status) | 103 self.assertEqual(404, response.status) |
| 109 | 104 |
| 110 if __name__ == '__main__': | 105 if __name__ == '__main__': |
| 111 parser = optparse.OptionParser() | 106 parser = optparse.OptionParser() |
| 112 parser.add_option('-p', '--path', default=None) | 107 parser.add_option('-p', '--path', default=None) |
| 113 parser.add_option('-a', '--all', action='store_true', default=False) | 108 parser.add_option('-a', '--all', action='store_true', default=False) |
| 114 (opts, args) = parser.parse_args() | 109 (opts, args) = parser.parse_args() |
| 115 if not opts.all: | 110 if not opts.all: |
| 116 _EXPLICIT_TEST_FILES = args | 111 _EXPLICIT_TEST_FILES = args |
| 117 if opts.path is not None: | 112 if opts.path is not None: |
| 118 _BASE_PATH = opts.path | 113 _BASE_PATH = opts.path |
| 119 # Kill sys.argv because we have our own flags. | 114 # Kill sys.argv because we have our own flags. |
| 120 sys.argv = [sys.argv[0]] | 115 sys.argv = [sys.argv[0]] |
| 121 unittest.main() | 116 unittest.main() |
| OLD | NEW |