def test_logger(): with mock.log(): pass l = syslog._logger syslog.init() assert isinstance(l, syslog._webLogger), 'wrong instance' l.error('test.error') l.warn('test.warn') l.info('test.info') l.msg('test.msg') assert syslog._getMsgId(0) is None msg = syslog._getMsgId(1) assert msg[0] == 1 assert msg[2] == syslog._LVLMAP['error'] assert msg[3] == 'test.error' msg = syslog._getMsgId(2) assert msg[0] == 2 assert msg[2] == syslog._LVLMAP['warn'] assert msg[3] == 'test.warn' msg = syslog._getMsgId(3) assert msg[0] == 3 assert msg[2] == syslog._LVLMAP['info'] assert msg[3] == 'test.info' msg = syslog._getMsgId(4) assert msg[0] == 4 assert msg[2] == syslog._LVLMAP['msg'] assert msg[3] == 'test.msg' syslog.close() _cleanup()
def test_lastMsgs(): with mock.log(): pass l = syslog._logger syslog.init() assert isinstance(l, syslog._webLogger), 'wrong instance' l.error('test.error') l.warn('test.warn') l.info('test.info') l.msg('test.msg') assert syslog._getMsgId(0) is None msgs = syslog.last(10) assert isinstance(msgs, list) assert isinstance(msgs[0], Row) assert len(msgs) == 4 idx = 0 for m in msgs: idx += 1 assert m['id'] == idx assert msgs[0]['msg'] == 'test.error' assert msgs[1]['msg'] == 'test.warn' assert msgs[2]['msg'] == 'test.info' assert msgs[3]['msg'] == 'test.msg' msgs = syslog.last(2) assert len(msgs) == 2 assert msgs[0]['msg'] == 'test.info' assert msgs[1]['msg'] == 'test.msg'
def test_get_url(listen_wapp): with mock.log(): with listen_wapp(profile='exec') as wapp: req = wapp.request() assert req.url == 'http://127.0.0.1/' url = _exec._getURL(req) assert url == 'http://127.0.0.1:3666'
def test_initError(): with mock.log(): pass assert not path.isfile(_dbfile), 'db exists' syslog._dbfile = '/i/hope/this/is/invalid/enough/syslog.db' syslog.init() syslog._dbfile = _dbfile assert not path.isfile(_dbfile), 'db was created, so no error happen'
def test_dbInit(): with mock.log(): pass db = _dbInit() assert isinstance(db, Connection), 'wrong instance' assert db.row_factory == Row, 'wrong db row_factory' db.close() assert path.isfile(_dbfile), 'db not created' _cleanup()
def mock(self, tag='cmd'): mockcfg = cfg.new(self.cfgfile) program = flags.program with mock.log(), mock.utils(mockcfg, tag=tag) as ctx: try: flags.program = 'sadm' yield ctx finally: flags.program = program
def mock(self, tag='webapp'): mockcfg = None if path.isfile(self.cfgfn): mockcfg = cfg.new(self.cfgfn) with mock.log(), mock.utils(mockcfg, tag=tag): ctx = Mock() ctx.orig = _orig ctx.view = _mock.view ctx.wapp = _mock.wapp ctx.tpl = _mock.tpl try: yield ctx finally: pass
def test_all(listen_wapp): patterns = [ path.join('tdata', 'listen', '*', 'listen.cfg'), path.join('tdata', 'listen', '*', '*', 'listen.cfg'), ] cfgfiles = {} for patt in patterns: for fn in glob(patt): cfgfiles[fn] = True with mock.log(): with mock.utils(None): for fn in sorted(cfgfiles.keys()): profile = fn.replace(path.join('tdata', 'listen'), '', 1) profile = '/'.join(profile.split(path.sep)[:-1]) if profile.startswith('/'): profile = profile[1:] _testProfile(listen_wapp, profile, fn)
def test_initClose(): with mock.log(): pass assert isinstance(syslog._logger, syslog._webLogger), \ 'wrong instance' assert syslog._logger.db is None, \ '_logger.db should be None' assert isinstance(log._logger, log._dummyLogger), \ 'main logger wrong instance' assert log._logger._child is None, \ 'main logger._child should be None' syslog.init() assert isinstance(syslog._logger.db, Connection), \ 'wrong db instance' assert isinstance(log._logger, log._dummyLogger), \ 'main logger wrong instance' assert isinstance(log._logger._child, syslog._webLogger), \ 'wrong instance' syslog.close() assert isinstance(log._logger._child, log._dummyLogger), \ 'wrong instance' _cleanup()
def mock(self, tag='devops'): mockcfg = None if path.isfile(self.cfgfn): mockcfg = cfg.new(self.cfgfn) bup = Mock() # bup bottle bup.bottle = bup.mock.bottle bup.bottle.template = bottle.template # bup wapp bup.wapp = _sadm.devops.wapp.wapp.wapp # bup tpl bup.tpl = bup.mock.tpl bup.tpl.parse = _sadm.devops.wapp.tpl.tpl.parse bup.tpl.version = _sadm.devops.wapp.tpl.tpl.version # bup session bup.session = bup.mock.session bup.session._secret = _sadm.devops.wapp.session.session._secret with mock.log(), mock.utils(mockcfg, tag=tag): ctx = Mock() ctx.orig = bup try: # mock bottle ctx.bottle = ctx.mock.bottle ctx.bottle.template = ctx.mock.bottle.template bottle.template = ctx.bottle.template # test session db dir sessdbdir = path.join('tdata', 'tmp', 'devops', 'wapp', 'session') if path.isdir(sessdbdir): print('mock.devops.wapp rmtree', sessdbdir) rmtree(sessdbdir) # mock wapp print('mock.devops.wapp init') _sadm.devops.wapp.wapp.init(cfgfn=self.cfgfn) ctx.wapp = ctx.mock.wapp _sadm.devops.wapp.wapp.wapp = ctx.wapp # mock config ctx.config = _sadm.devops.wapp.cfg.config # mock tpl ctx.tpl = ctx.mock.tpl ctx.tpl.parse = ctx.mock.tpl.parse _sadm.devops.wapp.tpl.tpl.parse = ctx.tpl.parse ctx.tpl.version = ctx.mock.tpl.version ctx.tpl.version.string = ctx.mock.tpl.version.string ctx.tpl.version.string.return_value = 'testing' _sadm.devops.wapp.tpl.tpl.version = ctx.tpl.version yield ctx finally: print('mock.devops.wapp.restore') # restore bottle del bottle.template bottle.template = bup.bottle.template # restore wapp del _sadm.devops.wapp.wapp.wapp _sadm.devops.wapp.wapp.wapp = bup.wapp # restore config del _sadm.devops.wapp.cfg.config _sadm.devops.wapp.cfg.config = None # restore tpl del _sadm.devops.wapp.tpl.tpl.parse _sadm.devops.wapp.tpl.tpl.parse = bup.tpl.parse del _sadm.devops.wapp.tpl.tpl.version _sadm.devops.wapp.tpl.tpl.version = bup.tpl.version # restore session del _sadm.devops.wapp.session.session._secret _sadm.devops.wapp.session.session._secret = bup.session._secret
def test_main_exec(listen_wapp): with mock.log(): with mock.utils(cfg, tag='exec'): with listen_wapp(profile='exec'): rc = _exec.main(['tdata/listen/exec/exec.task']) assert rc == 0
def test_main_no_action_args(listen_wapp): with mock.log(): with mock.utils(cfg, tag='no_action_args'): with listen_wapp(profile='exec'): rc = _exec.main(['tdata/listen/exec/no_action_args.task']) assert rc == 5
def test_main_no_task_file(listen_wapp): with mock.log(): with mock.utils(cfg, tag='no_task_file'): with listen_wapp(profile='exec'): rc = _exec.main(['no.task.file']) assert rc == 2
def test_main_no_args(listen_wapp): with mock.log(): with mock.utils(None): with listen_wapp(profile='exec') as wapp: rc = _exec.main([]) assert rc == 1