/usr/share/pyshared/gozerbot/tests.py is in gozerbot 0.99.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | # gozerbot/tests.py
#
#
""" gozerbot tests framework. """
## IMPORT SECTION
# gozerbot imports
from config import config
from threads.thr import start_new_thread
from utils.locking import lockdec
from utils.log import rlog
from utils.trace import calledfrom, whichmodule
from utils.exception import exceptionmsg
from utils.dol import Dol
from eventbase import EventBase
# basic imports
import sys, re, thread, copy, time, threading, random
## END IMPORT
# used to copy attributes
cpy = copy.deepcopy
## LOCK SECTION
# locks
testlock = threading.RLock()
locked = lockdec(testlock)
## END LOCK
class Test(object):
""" a test object. """
def __init__(self, execstring="", expect="", descr="", where="", fakein=""):
self.plugin = calledfrom(sys._getframe(1))
if not self.plugin:
self.plugin = calledfrom(sys._getframe(2))
self.descr = cpy(descr)
self.execstring = cpy(execstring)
self.expect = cpy(expect)
self.response = ""
self.groups = []
self.error = ""
self.where = cpy(where)
self.fakein = cpy(fakein)
self.start = None
self.end = None
self.prev = None
self.activate = False
def __str__(self):
return "test %s (%s) %s ==> %s (%s)" % (self.descr, self.where, self.execstring, self.response, self.error)
def begin(self):
if self.start:
self.start()
return self
def run(self, bot, event):
""" run the test on bot with event. """
if not self.activate:
return
if config['loadlist'] and self.plugin not in config['loadlist']:
return
mevent = copy.deepcopy(event)
mevent.onlyqueues = False
mevent.channel = '#dunkbots'
bot.userhosts['bottest'] = 'bottest@test'
bot.userhosts['mtest'] = 'mekker@test'
bot.userhosts['exec'] = 'exec@gozerbot'
if 'exec@gozerbot' not in bot.state['joinedchannels']:
bot.state['joinedchannels'].append('exec@gozerbot')
bot.channels['exec@gozerbot'] = {}
if '#dunkbots' not in bot.state['joinedchannels']:
bot.state['joinedchannels'].append('#dunkbots')
bot.channels['#dunkbots'] = {'cc': '!'}
self.error = ""
self.response = ""
self.groups = []
origexec = self.execstring
origexpect = self.expect
if self.start:
self.start()
return self
if self.end:
self.end()
return self
if self.fakein:
bot.fakein(self.fakein)
return self
if self.prev and self.prev.groups:
try:
execstring = self.execstring % self.prev.groups
self.execstring = execstring
except TypeError:
pass
try:
expect = self.expect % self.prev.groups
self.expect = expect
except TypeError:
pass
self.execstring = self.execstring.replace('{{ me }}', mevent.nick)
mevent.txt = mevent.origtxt = str(self.execstring)
rlog(100, 'tests', 'launching %s' % mevent.txt)
from gozerbot.plugins import plugins
self.response = plugins.cmnd(bot, mevent)
if self.response and self.expect:
self.expect = self.expect.replace('{{ me }}', mevent.nick)
expects = self.expect.split('|')
got = False
for expect in expects:
regex = re.compile(expect)
result = regex.search(str(self.response))
if result:
got = True
break
if not got:
self.error = 'invalid response'
else:
self.groups = result.groups()
self.execstring = origexec
self.expect = origexpect
return self
class Tests(object):
""" collection of all tests. """
def __init__(self):
self.tests = []
self.err = Dol()
self.toolate = Dol()
self.teller = 0
#@locked
def add(self, execstr, expect=None, descr="", fakein=""):
""" add a test. """
where = whichmodule(1)
if not where:
where = whichmodule(2)
if not where:
where = whichmodule(3)
test = Test(execstr, expect, descr, where, fakein)
self.tests.append(test)
return self
#@locked
def fakein(self, execstr, expect=None, descr=""):
""" call bot.fakein(). """
where = whichmodule(1)
if not where:
where = whichmodule(2)
if not where:
where = whichmodule(3)
test = Test(execstr, expect, descr, where, execstr)
test.where = where
self.tests.append(test)
return self
def start(self, func):
""" optional start function. """
where = whichmodule(1)
if not where:
where = whichmodule(2)
if not where:
where = whichmodule(3)
test = Test()
test.start = func
test.where = where
test.execstring = 'start'
self.tests.append(test)
return self
def end(self, func):
""" optional end function. """
where = whichmodule(1)
if not where:
where = whichmodule(2)
if not where:
where = whichmodule(3)
test = Test()
test.end = func
test.where = where
test.execstring = 'end'
self.tests.append(test)
return self
def unload(self, plugname):
""" unload tests. """
for i in range(len(self.tests)-1, -1, -1):
if self.tests[i].plugin == plugname:
del self.tests[i]
return self
def activate(self, plugname):
""" activate tests. """
for i in range(len(self.tests)-1, -1, -1):
if self.tests[i].plugin == plugname:
self.tests[i].activate = True
return self
def disable(self, plugname):
""" unload tests. """
for i in range(len(self.tests)-1, -1, -1):
if self.tests[i].plugin == plugname:
self.tests[i].activate = False
return self
def dorun(self, bot, event, tests, where, plug=None):
teller = 0
err = {}
toolate = []
prev = None
for test in tests:
if event.rest and event.rest not in test.plugin:
continue
if prev:
test.prev = prev
prev = test
if test.expect:
teller += 1
try:
starttime = time.time()
self.teller += 1
e = copy.deepcopy(event)
result = test.run(bot, e)
finished = time.time()
if finished - starttime > 10:
self.toolate[test.execstring] = finished - starttime
if not result:
continue
if not result.error:
event.reply("OK %s (%s) ==> %s" % (test.execstring, test.where, result.response))
else:
self.err[test.execstring] = test
event.reply('ERROR %s (%s): %s ==> %s (%s)' % (test.error, test.where, test.execstring, test.response, test.expect))
except Exception, ex:
test.error = exceptionmsg()
self.err[test.execstring] = test
event.reply(test.error)
return self
def dotests(self, bot, event, threaded=False, plug=None):
""" fire all tests. """
#event = EventBase(eventin)
groups = Dol()
for test in self.tests:
groups.add(test.where, test)
threads = []
testlist = []
for where, tests in groups.iteritems():
testlist.append((where, tests))
random.shuffle(testlist)
for t in testlist:
where, tests = t
if plug and plug not in where:
continue
event.reply("running tests on %s" % where)
if threaded:
thread = start_new_thread(self.dorun, (bot, event, tests, where, plug))
threads.append(thread)
else:
self.dorun(bot, event, tests, where, plug)
for thread in threads:
thread.join()
event.done()
def sleep(self, seconds):
""" sleep nr of seconds. """
time.sleep(seconds)
return self
## INIT SECTION
# expect is for examples
expect = {}
# the tests
tests = Tests()
## END INIT
|