1
2
3
4
5
6
7
8
9
10
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
30 """
31 A modified TestSuite that provides hooks for startUp and tearDown methods.
32 """
33 - def run(self, result):
37
40
43
44
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
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
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
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
103
105 self.login()
106 self.globals['sync']()
107 testObject.globs.update(self.globals)
108
110 self.logout()
111 testObject.globs['abort']()
112 self.globals['sync']()
113
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
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
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