Package Products :: Package ZenUtils :: Module ZenDocTest |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.ZenDocTest

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 or (at your
      8  # option) any later version as published by the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  import unittest
     15  import doctest
     16  import transaction
     17  import socket
     18  import Globals
     19
     20  import zope.component
     21  from zope.traversing.adapters import DefaultTraversable
     22
     23  from AccessControl.SecurityManagement import newSecurityManager
     24  from AccessControl.SecurityManagement import noSecurityManager
     25
     26  from Products.ZenUtils.ZeoConn import ZeoConn
     27
     28
     29 -class TestSuiteWithHooks(unittest.TestSuite):
     30      """
     31      A modified TestSuite that provides hooks for startUp and tearDown methods.
     32      """
     33 -    def run(self, result):
     34          self.startUp()
     35          unittest.TestSuite.run(self, result)
     36          self.tearDown()
     37
     38 -    def startUp(self):
     39          pass
     40
     41 -    def tearDown(self):
     42          pass
     43
     44
     45 -class ZenDocTestRunner(object):
     46      """
     47      Extracts doctests from the docstrings of a Zenoss module
     48      and runs them in an environment similar to that of zendmd.
     49
     50      Example usage:
     51          zdtr = ZenDocTestRunner()
     52          zdtr.add_modules("Products.ZenModel.ZenModelBase")
     53          zdtr.run()
     54      """
     55
     56      modules = []
     57      conn = None
     58
     59 -    def _find_relstorage_adapter_config(self):
     60          from App.config import getConfiguration
     61          zope_config = getConfiguration()
     62          for db in zope_config.databases:
     63              if db.name == 'main':
     64                  return db.config.storage.config.adapter.config
     65
     66 -    def setUp(self):
     67          adapter_config = self._find_relstorage_adapter_config()
     68          unix_socket = adapter_config.unix_socket if adapter_config else None
     69          zope.component.provideAdapter(DefaultTraversable, (None,))
     70          if not self.conn: self.conn = ZeoConn(unix_socket=unix_socket)
     71          self.app = self.conn.app
     72          self.login()
     73          self.dmd = self.app.zport.dmd
     74          find = self.dmd.Devices.findDevice
     75          self.globals = dict(
     76              app = self.app,
     77              zport = self.app.zport,
     78              dmd = self.dmd,
     79              find = find,
     80              devices = self.dmd.Devices,
     81              sync = self.dmd._p_jar.sync,
     82              commit = transaction.commit,
     83              abort = transaction.abort,
     84              me = find(socket.getfqdn())
     85          )
     86
     87 -    def tearDown(self):
     88          self.logout()
     89          self.conn.closedb()
     90
     91 -    def login(self, name='admin', userfolder=None):
     92          '''Logs in.'''
     93          if userfolder is None:
     94              userfolder = self.app.acl_users
     95          user = userfolder.getUserById(name)
     96          if user is None: return
     97          if not hasattr(user, 'aq_base'):
     98              user = user.__of__(userfolder)
     99          newSecurityManager(None, user)
    100
    101 -    def logout(self):
    102          noSecurityManager()
    103
    104 -    def doctest_setUp(self, testObject):
    105          self.login()
    106          self.globals['sync']()
    107          testObject.globs.update(self.globals)
    108
    109 -    def doctest_tearDown(self, testObject):
    110          self.logout()
    111          testObject.globs['abort']()
    112          self.globals['sync']()
    113
    114 -    def add_modules(self, mods):
    115          """
    116          Add Zenoss modules to be tested.
    117
    118          @param mods: One or more module objects or dotted names.
    119          @type mods: module or list
    120          """
    121          if not isinstance(mods, list): mods = [mods]
    122          self.modules.extend(mods)
    123
    124 -    def get_suites(self):
    125          """
    126          Returns a doctest.DocTestSuite for each module
    127          in self.modules.
    128
    129          Provided for integration with existing unittest framework.
    130          """
    131          self.setUp()
    132          doctest.DocTestFinder(exclude_empty=True)
    133          suites = []
    134          for mod in self.modules:
    135              try:
    136                  dtsuite = doctest.DocTestSuite(
    137                      mod,
    138                      optionflags=doctest.NORMALIZE_WHITESPACE,
    139                      setUp = self.doctest_setUp,
    140                      tearDown = self.doctest_tearDown
    141                  )
    142              except ValueError:
    143                  pass
    144              else:
    145                  suites.append(dtsuite)
    146          return suites
    147
    148 -    def run(self):
    149          """
    150          Run the doctests found in the modules added to this instance.
    151
    152          This method sets up the zendmd global variables, creates a
    153          test suite for each module that has been added, and runs
    154          all suites.
    155          """
    156          suite = unittest.TestSuite()
    157          for dtsuite in self.get_suites():
    158              suite.addTest(dtsuite)
    159          runner = unittest.TextTestRunner()
    160          runner.run(suite)
    161          self.tearDown()
    162