Home | History | Annotate | Download | only in server2
      1 # Copyright 2013 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 from instance_servlet import InstanceServlet, OfflineRenderServletDelegate
      6 from link_error_detector import LinkErrorDetector, StringifyBrokenLinks
      7 from servlet import Request, Response, Servlet
      8 import svn_constants
      9 
     10 class BrokenLinkTester(object):
     11   '''Run link error detector tests.
     12   '''
     13   def __init__(self, server_instance, renderer):
     14     self.link_error_detector = LinkErrorDetector(
     15       server_instance.host_file_system,
     16       renderer,
     17       svn_constants.PUBLIC_TEMPLATE_PATH,
     18       root_pages=('extensions/index.html', 'apps/about_apps.html'))
     19 
     20   def TestBrokenLinks(self):
     21     broken_links = self.link_error_detector.GetBrokenLinks()
     22     return (
     23         len(broken_links),
     24         'Warning: Found %d broken links:\n%s' % (
     25             len(broken_links), StringifyBrokenLinks(broken_links)))
     26 
     27   def TestOrphanedPages(self):
     28     orphaned_pages = self.link_error_detector.GetOrphanedPages()
     29     return (
     30         len(orphaned_pages),
     31         'Warning: Found %d orphaned pages:\n%s' % (
     32             len(orphaned_pages), '\n'.join(orphaned_pages)))
     33 
     34 class TestServlet(Servlet):
     35   '''Runs tests against the live server. Supports running all broken link
     36   detection tests, in parts or all at once.
     37   '''
     38   def __init__(self, request, delegate_for_test=None):
     39     Servlet.__init__(self, request)
     40     self._delegate = delegate_for_test or InstanceServlet.Delegate()
     41 
     42   def Get(self):
     43     link_error_tests = ('broken_links', 'orphaned_pages', 'link_errors')
     44 
     45     if not self._request.path in link_error_tests:
     46       return Response.NotFound('Test %s not found. Available tests are: %s' % (
     47           self._request.path, ','.join(link_error_tests)))
     48 
     49     constructor = InstanceServlet.GetConstructor(self._delegate)
     50     def renderer(path):
     51       return constructor(Request(path, '', self._request.headers)).Get()
     52 
     53     link_tester = BrokenLinkTester(
     54         OfflineRenderServletDelegate(self._delegate).CreateServerInstance(),
     55         renderer)
     56     if self._request.path == 'broken_links':
     57       errors, content = link_tester.TestBrokenLinks()
     58     elif self._request.path == 'orphaned_pages':
     59       errors, content = link_tester.TestOrphanedPages()
     60     else:
     61       link_errors, link_content = link_tester.TestBrokenLinks()
     62       orphaned_errors, orphaned_content = link_tester.TestOrphanedPages()
     63       errors = link_errors + orphaned_errors
     64       content = "%s\n%s" % (link_content, orphaned_content)
     65 
     66     if errors:
     67       return Response.InternalError(content=content)
     68 
     69     return Response.Ok(content="%s test passed." % self._request.path)
     70