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