class StdnetPlugin(TestPlugin): name = "server" flags = ["-s", "--server"] nargs = '*' desc = 'Back-end data server where to run tests.' default = [settings.DEFAULT_BACKEND] validator = pulsar.validate_list py_redis_parser = pulsar.Setting( flags=['--py-redis-parser'], desc=('Run tests using the python redis parser rather ' 'the C implementation.'), action="store_true", default=False) sync = pulsar.Setting(flags=['--sync'], desc='Switch off asynchronous bindings', action="store_true", default=False) def configure(self, cfg): if cfg.sync: settings.ASYNC_BINDINGS = False if cfg.py_redis_parser: settings.REDIS_PY_PARSER = True def on_start(self): servers = [] names = set() for s in self.config.server: try: s = getdb(s) s.ping() except Exception: LOGGER.error('Could not obtain server %s' % s, exc_info=True) else: if s.name not in names: names.add(s.name) servers.append(s.connection_string) if not servers: raise pulsar.HaltServer('No server available. BAILING OUT') settings.servers = servers
def __new__(cls, name, bases, attrs): settings = {} for base in bases: if isinstance(base, TestPluginMeta): settings.update(base.config.settings) for key, setting in list(iteritems(attrs)): if isinstance(setting, pulsar.Setting): attrs.pop(key) setting.name = setting.name or key.lower() settings[setting.name] = as_test_setting(setting) if not attrs.pop('virtual', False): setting_name = attrs.pop('name', name).lower() if setting_name: def_flag = '--%s' % setting_name.replace(' ', '-').replace( '_', '-') action = attrs.pop('action', None) type = attrs.pop('type', None) default = attrs.pop('default', None) validator = attrs.pop('validator', None) nargs = attrs.pop('nargs', None) if (validator is None and default is None and type is None and nargs is None): if action is None or action == 'store_true': action = 'store_true' default = False validator = pulsar.validate_bool elif action == 'store_false': default = True validator = pulsar.validate_bool setting = pulsar.Setting(name=setting_name, desc=attrs.pop('desc', name), type=type, flags=attrs.pop('flags', [def_flag]), action=action, default=default, validator=validator, nargs=nargs) settings[setting.name] = as_test_setting(setting) attrs['config'] = pulsar.Config(settings=settings) return super(TestPluginMeta, cls).__new__(cls, name, bases, attrs)
class BenchMark(TestPlugin): '''Benchmarking addon for pulsar test suite.''' desc = '''Run benchmarks function flagged with __benchmark__ attribute''' repeat = pulsar.Setting(flags=['--repeat'], type=int, default=10, validator=pulsar.validate_pos_int, desc=('Default number of repetition ' 'when benchmarking.')) def loadTestsFromTestCase(self, test_cls): bench = getattr(test_cls, '__benchmark__', False) if self.config.benchmark != bench: # skip the loading return TestSuite() def before_test_function_run(self, test, local): if self.config.benchmark: method_name = getattr(test, '_testMethodName', None) if method_name: method = getattr(test, method_name, None) bench = getattr(test, '__benchmark__', False) if not bench and method: bench = getattr(method, '__benchmark__', False) if bench: number = getattr(test, '__number__', 1) return BenchTest(test, number, self.config.repeat) def addSuccess(self, test): if self.config.benchmark and self.stream: result = getattr(test, 'bench_info', None) # if result and self.stream.showAll: if result: stream = self.stream.handler('benchmark') template = getattr(test, 'benchmark_template', BENCHMARK_TEMPLATE) stream.writeln(template.format(result)) stream.flush() self.result.addSuccess(test) return True def addError(self, test, err): msg = self._msg(test, 'ERROR') if msg: self.result.addError(test, err) return msg def addFailure(self, test, err): msg = self._msg(test, 'FAILURE') if msg: self.result.addFailure(test, err) return msg def addSkip(self, test, reason): msg = self._msg(test, 'SKIPPED') if msg: self.result.addSkip(test, reason) return msg def _msg(self, test, msg): if self.config.benchmark and self.stream: stream = self.stream.handler('benchmark') stream.writeln('%s: %s' % (test, msg)) stream.flush() return True
class Profile(test.TestPlugin): ''':class:`pulsar.apps.test.TestPlugin` for profiling test cases.''' desc = '''Profile tests using the cProfile module''' profile_stats_path = pulsar.Setting(flags=['--profile-stats-path'], default='htmlprof', desc='location of profile directory.', validator=absolute_file) def configure(self, cfg): self.config = cfg self.profile_stats_path = cfg.profile_stats_path dir, name = os.path.split(self.profile_stats_path) fname = '.' + name self.profile_temp_path = os.path.join(dir, fname) def before_test_function_run(self, test, local): # If active return a TestProfile instance wrapping the real test case. if self.config.profile: local.prof = profiler.Profile() local.tmp = tempfile.mktemp(dir=self.profile_temp_path) local.prof.enable() def after_test_function_run(self, test, local): if self.config.profile: local.prof.disable() local.prof.dump_stats(local.tmp) def on_start(self): if self.config.profile: self.remove_dir(self.profile_temp_path, build=True) def remove_dir(self, dir, build=False): sleep = 0 if os.path.exists(dir): shutil.rmtree(dir) sleep = 0.2 if build: time.sleep(sleep) os.mkdir(dir) def on_end(self): if self.config.profile: files = [ os.path.join(self.profile_temp_path, file) for file in os.listdir(self.profile_temp_path) ] if not files: return stats = pstats.Stats(*files, **{'stream': Stream()}) stats.sort_stats('time', 'calls') stats.print_stats() stats_str = stats.stream.getvalue() self.remove_dir(self.profile_temp_path) stats_str = stats_str.split('\n') run_info = 'Executed %s.' % datetime.now().isoformat() for n, line in enumerate(stats_str): b = 0 while b < len(line) and line[b] == ' ': b += 1 line = line[b:] if line: if line.startswith('ncalls'): break bits = line.split(' ') try: int(bits[0]) except Exception: continue else: run_info += ' ' + line data = ''.join(make_stat_table(data_stream(stats_str[n + 1:], 100))) self.remove_dir(self.profile_stats_path, build=True) for file in os.listdir(template_path): if file == 'index.html': copy_file( file, self.profile_stats_path, { 'table': data, 'run_info': run_info, 'version': pulsar.__version__ }) else: copy_file(file, self.profile_stats_path)