def deploy(self, mockCfg = 'build.ini'): from _sadm.env.settings import Settings cfgfn = path.join('tdata', 'plugin', self._p.name.replace('.', path.sep), 'config', mockCfg) cfg = Settings() with open(cfgfn, 'r') as fh: cfg.read_file(fh) with mock.utils(cfg, 'deploy'): self._envAction.run(self._env, 'deploy', sumode = self._p.sumode)
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 _testProfile(listen_wapp, profile, cfgfn): print(profile, cfgfn) profdir = path.dirname(cfgfn) for datfn in sorted(glob(path.join(profdir, '*.json'))): datname = path.basename(datfn).replace('.json', '').strip() print(' ', datname, datfn) with open(datfn, 'r') as fh: dat = json.load(fh) h = newHandler(dat) resp = newResponse(dat) with mock.utils(_mockConfig(cfgfn), tag=datname): with listen_wapp(profile=profile) as wapp: hfunc = _getHandler(wapp, h.name) if resp.error: with raises(bottle.HTTPError) as exc: if h.method == 'POST': _wappPOST(wapp, datname, hfunc, h.args) wapp.checkException(exc, resp.status, resp.content) else: if h.method == 'POST': _wappPOST(wapp, datname, hfunc, h.args) assert wapp.response == resp.content
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