def test_install(self, alembic_command): # TODO cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True pass
def test_module_is_installed(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) self.assertFalse(mm.is_installed('mod', None)) mm.loaded['core'] = True sess = mock.MagicMock() mod = mock.MagicMock() mod.name = 'mod' mod.parsed_version = _ver('2.0') sess.query.return_value = [mod] self.assertTrue(mm.is_installed('mod', sess)) self.assertFalse(mm.is_installed('notfound', sess)) mod = mock.MagicMock() mod.version.return_value = _ver('2.0') mm.loaded['mod'] = mod self.assertTrue(mm.is_installed('mod == 2.0', None)) self.assertFalse(mm.is_installed('mod == 1.0', None)) mm.installed = None sess.query.side_effect = _prog_error self.assertFalse(mm.is_installed('errormod', sess))
def test_get_module_browser(self): from netprofile.ext.data import ExtBrowser cfg = mock.MagicMock() mm = ModuleManager(cfg) mb = mm.get_module_browser() self.assertIsInstance(mb, ExtBrowser)
def test_module_rescan(self, fplug, iep): cfg = mock.MagicMock() mm = ModuleManager(cfg) old_mod = ('exists1', 'exists2') new_mod = (('netprofile', 'netprofile'), ('test1', 'netprofile-test1'), ('test2', 'test2'), ('test3', 'netprofile-whatever'), ('exists1', 'netprofile-exists1'), ('exists2', 'wrong-exists2'), ('wrong1', 'np-wrong1'), ('wrong2', 'np-whatever')) for mod in old_mod: ep = mock.MagicMock() type(ep).name = mock.PropertyMock(return_value=mod) ep.dist.project_name = 'netprofile-' + mod mm.modules[mod] = ep new_dists = [] new_eps = [] def _new_mod(mod_name, dist_name): ep = mock.MagicMock() dist = mock.MagicMock() type(ep).name = mock.PropertyMock(return_value=mod_name) dist.project_name = dist_name ep.dist = dist new_eps.append(ep) new_dists.append(dist) for mod_name, dist_name in new_mod: _new_mod(mod_name, dist_name) fplug.return_value = [new_dists, []] iep.return_value = new_eps ret = mm.rescan() fplug.assert_called_once() iep.assert_called_once_with('netprofile.modules') self.assertEqual(ret, ['test1']) fplug.reset_mock() iep.reset_mock() new_dists = [] new_eps = [] new_mod = (('test1', 'netprofile-test1'), ('wrong', 'np-wrong')) for mod_name, dist_name in new_mod: _new_mod(mod_name, dist_name) fplug.return_value = [new_dists, []] iep.return_value = new_eps ret = mm.rescan() fplug.assert_called_once() iep.assert_not_called() self.assertEqual(ret, [])
def module_list(args): """ List available/installed modules """ cfg = setup_app(args.ini_file, args.application) loc = get_loc(cfg) has_core = True flt = args.filter try: from netprofile_core.models import NPModule except ImportError: has_core = False tr_name = loc.translate(_('Name')) tr_avail = loc.translate(_('Available')) tr_inst = loc.translate(_('Installed')) tr_enab = loc.translate(_('Enabled')) mm = ModuleManager(cfg) tbl = PrettyTable(( tr_name, tr_avail, tr_inst, tr_enab )) tbl.align = 'l' tbl.align[tr_enab] = 'c' tbl.sortby = tr_name tbl.padding_width = 2 installed = {} if has_core: sess = get_session() try: for mod in sess.query(NPModule): installed[mod.name] = (mod.current_version, mod.enabled) except ProgrammingError: has_core = False for moddef, data in mm.prepare().items(): curversion = _('- N/A -') enabled = _('- N/A -') if moddef in installed: if installed[moddef][1] and (flt == 'disabled'): continue if (not installed[moddef][1]) and (flt == 'enabled'): continue curversion = installed[moddef][0] enabled = _('YES') if installed[moddef][1] else _('NO') elif flt in ('installed', 'enabled', 'disabled'): continue tbl.add_row(( moddef, data[1], loc.translate(curversion), loc.translate(enabled) )) return tbl
def setUp(self): from netprofile.common import cache cache.cache = cache.configure_cache( {'netprofile.cache.backend': 'dogpile.cache.memory'}) self.req = testing.DummyRequest() self.cfg = testing.setUp(request=self.req) self.mm = ModuleManager(self.cfg) self.mod_names = ('test1', 'test2', 'empty') self.dict = False
def test_module_manager(self): cfg = mock.MagicMock() cfg.get_settings.return_value = {'netprofile.vhost': 'test1'} mm = ModuleManager(cfg) self.assertIs(mm.cfg, cfg) self.assertEqual(mm.vhost, 'test1') mm = ModuleManager(cfg, vhost='test2') self.assertEqual(mm.vhost, 'test2')
def test_cls_version(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) cls = mock.MagicMock(spec=[]) self.assertEqual(mm._cls_version(cls), packaging.version.parse('0')) cls = mock.MagicMock() cls.version.return_value = packaging.version.parse('1') self.assertEqual(mm._cls_version(cls), packaging.version.parse('1'))
def module_uninstall(args): """ Uninstall module from database """ cfg = setup_app(args.ini_file, args.application) setup_mako_sql(cfg) mm = ModuleManager(cfg) sess = get_session() mm.scan() raise Exception('Unimplemented')
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) ModuleManager.prepare() Base.metadata.drop_all(engine)
def test_module_load_import_no_methods(self, import_model, mock_version, get_models, get_sqlfunc, get_sqlview, get_sqlevt): mock_version.return_value = _ver('1.0') cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.version.return_value = mm.installed['mod1'] = _ver('1.0') ep1.load.return_value = ModuleBase mm.modules['mod1'] = ep1 self.assertTrue(mm.load('mod1'))
def test_import_model(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) hm = mock.MagicMock() mb = mock.MagicMock() mb.__getitem__.return_value.__getitem__.return_value = 'TEST' model1 = mock.MagicMock() model1.__name__ = 'testmodel1' mm.models['mod1'] = {} mm._import_model('mod1', model1, mb, hm, False) self.assertEqual(model1.__moddef__, 'mod1') model2 = mock.MagicMock() model2.__name__ = 'testmodel2' mm.models['mod2'] = {} mm._import_model('mod2', model2, mb, hm, True) self.assertEqual(model2.__moddef__, 'mod2') self.assertEqual(mm.models['mod2'], {'testmodel2': model2}) mb.__getitem__.assert_called_once_with('mod2') mb.__getitem__.return_value.__getitem__.assert_called_once_with( 'testmodel2') hm.run_hook.assert_called_once_with('np.model.load', mm, 'TEST')
def module_disable(args): """ Disable installed module """ cfg = setup_app(args.ini_file, args.application) setup_mako_sql(cfg) mm = ModuleManager(cfg) sess = get_session() mm.scan() if not mm.load('core'): return 'Error: Unable to proceed without core module.' if args.name.lower() == 'all': for mod in mm.modules: if mm.is_installed(mod, sess) and (mod != 'core'): mm.disable(mod) return 'All done.' ret = mm.disable(args.name) if isinstance(ret, bool): if ret: return 'Disabled module \'%s\'.' % (args.name,) else: return 'Error: Module \'%s\' wasn\'t found or is not installed.' % (args.name,) return 'Error: Unknown result.'
def test_get_export_formats(self, iep): cfg = mock.MagicMock() mm = ModuleManager(cfg) ep1 = mock.MagicMock() type(ep1).name = mock.PropertyMock(return_value='EP1') ep1.load.return_value.return_value = 'DATA1' ep2 = mock.MagicMock() type(ep2).name = mock.PropertyMock(return_value='EP2') ep2.load.side_effect = ImportError iep.return_value = [ep1, ep2] ret = mm.get_export_formats() iep.assert_called_once_with('netprofile.export.formats') self.assertEqual(ret, { 'EP1' : 'DATA1' })
def test_get_export_formats(self, iep): cfg = mock.MagicMock() mm = ModuleManager(cfg) ep1 = mock.MagicMock() type(ep1).name = mock.PropertyMock(return_value='EP1') ep1.load.return_value.return_value = 'DATA1' ep2 = mock.MagicMock() type(ep2).name = mock.PropertyMock(return_value='EP2') ep2.load.side_effect = ImportError iep.return_value = [ep1, ep2] ret = mm.get_export_formats() iep.assert_called_once_with('netprofile.export.formats') self.assertEqual(ret, {'EP1': 'DATA1'})
def test_module_base_defaults(self): self.assertEqual(ModuleBase.get_deps(), ()) self.assertIsNone(ModuleBase.install(None)) self.assertIsNone(ModuleBase.uninstall(None)) self.assertIsNone(ModuleBase.upgrade(None, None)) self.assertEqual(ModuleBase.get_models(), ()) self.assertEqual(ModuleBase.get_sql_functions(), ()) self.assertEqual(ModuleBase.get_sql_views(), ()) self.assertEqual(ModuleBase.get_sql_events(), ()) self.assertIsNone(ModuleBase.get_sql_data(None, None, None)) cfg = mock.MagicMock() mm = ModuleManager(cfg) mod = ModuleBase(mm) self.assertIsNone(mod.add_routes(None)) self.assertEqual(mod.get_menus(None), ()) self.assertEqual(mod.get_js(None), ()) self.assertEqual(mod.get_local_js(None, 'en'), ()) self.assertEqual(mod.get_css(None), ()) self.assertEqual(mod.get_autoload_js(None), ()) self.assertEqual(mod.get_controllers(None), ()) self.assertEqual(mod.get_rt_handlers(), {}) self.assertEqual(mod.get_rt_routes(), ()) self.assertEqual(mod.get_dav_plugins(None), {}) self.assertEqual(mod.get_task_imports(), ()) self.assertIsNone(mod.load()) self.assertIsNone(mod.unload()) self.assertEqual(mod.get_settings(), ()) self.assertEqual(mod.name, 'netprofile.common.modules')
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) config = setup_config(settings) reg = threadlocal.get_current_registry() factory = pyramid_mako.MakoRendererFactory() name_resolver = DottedNameResolver() lookup_opts = pyramid_mako.parse_options_from_settings(settings, 'mako.', name_resolver.maybe_resolve) lookup_opts.update({ 'default_filters' : ['context[\'self\'].ddl.ddl_fmt'] }) factory.lookup = pyramid_mako.PkgResourceTemplateLookup(**lookup_opts) reg.registerUtility(factory, IRendererFactory, name='.mak') sess = DBSession() mm = ModuleManager(config) sess.execute(SetVariable('accessuid', 0)) sess.execute(SetVariable('accessgid', 0)) sess.execute(SetVariable('accesslogin', '[CREATEDB]')) mm.scan() mm.install('core', sess) for moddef in mm.modules: if moddef != 'core': mm.install(moddef, sess)
def test_load_enabled(self, query, load): mod_names = ['core', 'mod1', 'mod2'] mods = [] for modname in mod_names: mod = mock.MagicMock() name = mock.PropertyMock(return_value=modname) type(mod).name = name mods.append(mod) query.return_value.filter.return_value = mods cfg = mock.MagicMock() mm = ModuleManager(cfg) self.assertTrue(mm.load_enabled()) load.assert_has_calls((mock.call('mod1'), mock.call('mod2'))) query.side_effect = _prog_error self.assertFalse(mm.load_enabled())
def test_assert_loaded(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True self.assertIsNone(mm.assert_loaded('core')) self.assertIsNone(mm.assert_loaded(['core'])) with self.assertRaises(ModuleError): mm.assert_loaded('missing')
def setUp(self): from netprofile.common import cache cache.cache = cache.configure_cache({ 'netprofile.cache.backend' : 'dogpile.cache.memory' }) self.req = testing.DummyRequest() self.cfg = testing.setUp(request=self.req) self.mm = ModuleManager(self.cfg) self.mod_names = ('test1', 'test2', 'empty') self.dict = False
def test_module_scan(self, iep): ep1 = mock.MagicMock() type(ep1).name = mock.PropertyMock(return_value='netprofile') ep1.dist.project_name = 'netprofile' ep2 = mock.MagicMock() type(ep2).name = mock.PropertyMock(return_value='core') ep2.dist.project_name = 'netprofile-core' ep3 = mock.MagicMock() type(ep3).name = mock.PropertyMock(return_value='test') ep3.dist.project_name = 'netprofile-nottest' iep.return_value = [ep1, ep1, ep2, ep2, ep3] cfg = mock.MagicMock() mm = ModuleManager(cfg) ret = mm.scan() iep.assert_called_once_with('netprofile.modules') self.assertEqual(ret, ['core'])
def test_load_enabled(self, query, load): mod_names = ['core', 'mod1', 'mod2'] mods = [] for modname in mod_names: mod = mock.MagicMock() name = mock.PropertyMock(return_value=modname) type(mod).name = name mods.append(mod) query.return_value.filter.return_value = mods cfg = mock.MagicMock() mm = ModuleManager(cfg) self.assertTrue(mm.load_enabled()) load.assert_has_calls(( mock.call('mod1'), mock.call('mod2') )) query.side_effect = _prog_error self.assertFalse(mm.load_enabled())
def test_module_disable(self, mock_version, query): mock_version.return_value = _ver('1.0') cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True self.assertFalse(mm.disable('missing')) query.side_effect = _no_result ep1 = mock.MagicMock() mm.modules['mod1'] = ep1 self.assertFalse(mm.disable('mod1')) query.assert_called_once() modobj = mock.MagicMock() enabled = mock.PropertyMock(return_value=True) type(modobj).enabled = enabled query.reset_mock() query.side_effect = None query.return_value.filter.return_value.one.return_value = modobj ep2 = mock.MagicMock() ep2.version.return_value = mm.installed['mod2'] = _ver('1.0') ep2.load.return_value = ModuleBase mm.modules['mod2'] = ep2 self.assertTrue(mm.disable('mod2')) enabled.assert_called_with(False) enabled.return_value = False enabled.reset_mock() ep3 = mock.MagicMock() ep3.version.return_value = mm.installed['mod3'] = _ver('1.0') ep3.load.return_value = ModuleBase mm.modules['mod3'] = ep3 self.assertTrue(mm.disable('mod3')) enabled.assert_called_once_with()
def test_module_load_import(self, import_model, mock_version, get_models, get_sqlfunc, get_sqlview, get_sqlevt): mock_version.return_value = _ver('1.0') get_models.return_value = (mock.MagicMock(),) get_sqlfunc.return_value = (mock.MagicMock(),) get_sqlview.return_value = (mock.MagicMock(),) get_sqlevt.return_value = (mock.MagicMock(),) cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.version.return_value = mm.installed['mod1'] = _ver('1.0') ep1.load.return_value = ModuleBase mm.modules['mod1'] = ep1 self.assertTrue(mm.load('mod1')) get_models.assert_called_once_with() import_model.assert_called_once() get_sqlfunc.assert_called_once_with() get_sqlview.assert_called_once_with() get_sqlevt.assert_called_once_with()
def test_module_preload(self, mock_version): mock_version.return_value = _ver('1.0') cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.version.return_value = mm.installed['mod1'] = _ver('1.0') ep1.load.return_value = ModuleBase mm.modules['mod1'] = ep1 self.assertTrue(mm.preload('mod1')) ep1.load.assert_called_once_with() ep2 = mock.MagicMock() ep2.version.return_value = mm.installed['mod2'] = _ver('1.0') ep2.load.return_value = ModuleBase mm.modules['mod2'] = ep2 self.assertTrue( mm.preload(packaging.requirements.Requirement('mod2 < 2.0'))) ep2.load.assert_called_once_with()
def test_module_preload(self, mock_version): mock_version.return_value = _ver('1.0') cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.version.return_value = mm.installed['mod1'] = _ver('1.0') ep1.load.return_value = ModuleBase mm.modules['mod1'] = ep1 self.assertTrue(mm.preload('mod1')) ep1.load.assert_called_once_with() ep2 = mock.MagicMock() ep2.version.return_value = mm.installed['mod2'] = _ver('1.0') ep2.load.return_value = ModuleBase mm.modules['mod2'] = ep2 self.assertTrue(mm.preload(packaging.requirements.Requirement('mod2 < 2.0'))) ep2.load.assert_called_once_with()
def test_import_model(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) hm = mock.MagicMock() mb = mock.MagicMock() mb.__getitem__.return_value.__getitem__.return_value = 'TEST' model1 = mock.MagicMock() model1.__name__ = 'testmodel1' mm.models['mod1'] = {} mm._import_model('mod1', model1, mb, hm, False) self.assertEqual(model1.__moddef__, 'mod1') model2 = mock.MagicMock() model2.__name__ = 'testmodel2' mm.models['mod2'] = {} mm._import_model('mod2', model2, mb, hm, True) self.assertEqual(model2.__moddef__, 'mod2') self.assertEqual(mm.models['mod2'], { 'testmodel2' : model2 }) mb.__getitem__.assert_called_once_with('mod2') mb.__getitem__.return_value.__getitem__.assert_called_once_with('testmodel2') hm.run_hook.assert_called_once_with('np.model.load', mm, 'TEST')
def test_module_load_import(self, import_model, mock_version, get_models, get_sqlfunc, get_sqlview, get_sqlevt): mock_version.return_value = _ver('1.0') get_models.return_value = (mock.MagicMock(), ) get_sqlfunc.return_value = (mock.MagicMock(), ) get_sqlview.return_value = (mock.MagicMock(), ) get_sqlevt.return_value = (mock.MagicMock(), ) cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.version.return_value = mm.installed['mod1'] = _ver('1.0') ep1.load.return_value = ModuleBase mm.modules['mod1'] = ep1 self.assertTrue(mm.load('mod1')) get_models.assert_called_once_with() import_model.assert_called_once() get_sqlfunc.assert_called_once_with() get_sqlview.assert_called_once_with() get_sqlevt.assert_called_once_with()
def module_install(args): """ Install available module to database """ cfg = setup_app(args.ini_file, args.application) setup_mako_sql(cfg) mm = ModuleManager(cfg) sess = get_session() mm.scan() if args.name != 'core': mm.load('core') mm.load_all() if args.name.lower() == 'all': mm.install('core', sess) for mod in mm.modules: if mod != 'core': mm.install(mod, sess) return 'All done.' ret = mm.install(args.name, sess) if isinstance(ret, bool): if ret: return 'Module \'%s\' successfully installed.' % (args.name,) else: return 'Error: Module \'%s\' is already installed.' % (args.name,) return 'Error: Unknown result.'
def test_module_load(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) found = mock.MagicMock() mm.modules['found'] = found self.assertFalse(mm.load('notfound')) self.assertFalse(mm.load('found')) version_loaded = mock.MagicMock() version_loaded.version.return_value = _ver('1.0') mm.loaded['vmod'] = version_loaded self.assertFalse(mm.load('vmod == 2.0')) self.assertTrue(mm.load('vmod == 1.0')) mm.installed = {} mm.modules['core'] = mock.MagicMock() self.assertFalse(mm.load('core')) mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.load.side_effect = ImportError mm.installed['mod1'] = _ver('1.0') mm.modules['mod1'] = ep1 self.assertFalse(mm.load('mod1')) with mock.patch('netprofile.common.modules.ModuleBase.version' ) as mock_version: mock_version.return_value = _ver('1.0') ep2 = mock.MagicMock() ep2.version.return_value = _ver('1.0') mm.installed['mod2'] = _ver('1.0') mm.modules['mod2'] = ep2 ep2.load.return_value = ModuleBase self.assertTrue(mm.load('mod2')) ep2.load.assert_called_once_with() ep3 = mock.MagicMock() ep3.version.return_value = _ver('1.0') mm.installed['mod3'] = _ver('1.0') mm.modules['mod3'] = ep3 ep3.load.return_value = mock.MagicMock self.assertFalse(mm.load('mod3')) ep3.load.assert_called_once_with() ep4 = mock.MagicMock() ep4.version.return_value = _ver('2.0') mm.installed['mod4'] = _ver('2.0') mm.modules['mod4'] = ep4 ep4.load.return_value = ModuleBase self.assertFalse(mm.load('mod4 == 2.0')) ep4.load.assert_called_once_with() with mock.patch('netprofile.common.modules.ModuleBase.get_deps' ) as mock_deps: mock_deps.return_value = ('mod5', ) ep5 = mock.MagicMock() ep5.version.return_value = _ver('1.0') mm.installed['mod5'] = _ver('1.0') mm.modules['mod5'] = ep5 ep5.load.return_value = ModuleBase self.assertTrue(mm.load('mod5')) ep5.load.assert_called_once_with() mock_deps.assert_called_once_with() mock_deps.reset_mock() mock_deps.return_value = ('missing', ) ep6 = mock.MagicMock() ep6.version.return_value = _ver('1.0') mm.installed['mod6'] = _ver('1.0') mm.modules['mod6'] = ep6 ep6.load.return_value = ModuleBase self.assertFalse(mm.load('mod6')) ep6.load.assert_called_once_with() mock_deps.assert_called_once_with() ep7 = mock.MagicMock() ep7.version.return_value = _ver('1.0') mm.installed['mod7'] = _ver('1.0') mm.modules['mod7'] = ep7 ep7.load.return_value = ModuleBase self.assertTrue( mm.load(packaging.requirements.Requirement('mod7 > 0.9'))) ep7.load.assert_called_once_with()
def test_module_rescan(self, fplug, iep): cfg = mock.MagicMock() mm = ModuleManager(cfg) old_mod = ('exists1', 'exists2') new_mod = ( ('netprofile', 'netprofile'), ('test1', 'netprofile-test1'), ('test2', 'test2'), ('test3', 'netprofile-whatever'), ('exists1', 'netprofile-exists1'), ('exists2', 'wrong-exists2'), ('wrong1', 'np-wrong1'), ('wrong2', 'np-whatever') ) for mod in old_mod: ep = mock.MagicMock() type(ep).name = mock.PropertyMock(return_value=mod) ep.dist.project_name = 'netprofile-' + mod mm.modules[mod] = ep new_dists = [] new_eps = [] def _new_mod(mod_name, dist_name): ep = mock.MagicMock() dist = mock.MagicMock() type(ep).name = mock.PropertyMock(return_value=mod_name) dist.project_name = dist_name ep.dist = dist new_eps.append(ep) new_dists.append(dist) for mod_name, dist_name in new_mod: _new_mod(mod_name, dist_name) fplug.return_value = [new_dists, []] iep.return_value = new_eps ret = mm.rescan() fplug.assert_called_once() iep.assert_called_once_with('netprofile.modules') self.assertEqual(ret, ['test1']) fplug.reset_mock() iep.reset_mock() new_dists = [] new_eps = [] new_mod = ( ('test1', 'netprofile-test1'), ('wrong', 'np-wrong') ) for mod_name, dist_name in new_mod: _new_mod(mod_name, dist_name) fplug.return_value = [new_dists, []] iep.return_value = new_eps ret = mm.rescan() fplug.assert_called_once() iep.assert_not_called() self.assertEqual(ret, [])
class TestModuleGettersAPI(unittest.TestCase): def setUp(self): from netprofile.common import cache cache.cache = cache.configure_cache({ 'netprofile.cache.backend' : 'dogpile.cache.memory' }) self.req = testing.DummyRequest() self.cfg = testing.setUp(request=self.req) self.mm = ModuleManager(self.cfg) self.mod_names = ('test1', 'test2', 'empty') self.dict = False def setup_mocks(self, cb_value): for mod_name in self.mod_names: mod = mock.MagicMock() fn = getattr(mod, self.fn) if mod_name.startswith('empty'): fn.return_value = {} if self.dict else [] else: fn.return_value = cb_value(mod_name) self.mm.loaded[mod_name] = mod def assert_getter(self, ret, assert_ret, *args, **kwargs): for mod_name in self.mod_names: fn = getattr(self.mm.loaded[mod_name], self.fn) fn.assert_called_once_with(*args, **kwargs) self.assertEqual(ret, assert_ret) def tearDown(self): testing.tearDown() def test_get_js(self): self.fn = 'get_js' self.setup_mocks(lambda mod: [mod + '.js']) ret = self.mm.get_js(self.req) self.assert_getter(set(ret), set(['test1.js', 'test2.js']), self.req) def test_get_local_js(self): self.fn = 'get_local_js' self.setup_mocks(lambda mod: [mod + '-loc.js']) ret = self.mm.get_local_js(self.req, 'en') self.assert_getter(set(ret), set(['test1-loc.js', 'test2-loc.js']), self.req, 'en') def test_get_css(self): self.fn = 'get_css' self.setup_mocks(lambda mod: [mod + '.css']) ret = self.mm.get_css(self.req) self.assert_getter(set(ret), set(['test1.css', 'test2.css']), self.req) def test_get_autoload_js(self): self.fn = 'get_autoload_js' self.setup_mocks(lambda mod: [mod.capitalize()]) ret = self.mm.get_autoload_js(self.req) self.assert_getter(set(ret), set(['Test1', 'Test2']), self.req) def test_get_contollers(self): self.fn = 'get_controllers' self.setup_mocks(lambda mod: [mod.capitalize()]) ret = self.mm.get_controllers(self.req) self.assert_getter(set(ret), set(['Test1', 'Test2']), self.req) def test_get_rt_handlers(self): self.fn = 'get_rt_handlers' self.dict = True self.setup_mocks(lambda mod: { mod : mod + '_rt' }) ret = self.mm.get_rt_handlers(self.req) self.assert_getter(ret, { 'test1' : 'test1_rt', 'test2' : 'test2_rt' }, self.req) def test_get_rt_routes(self): self.fn = 'get_rt_routes' self.setup_mocks(lambda mod: [mod + '_rt']) ret = self.mm.get_rt_routes() self.assert_getter(set(ret), set(['test1_rt', 'test2_rt'])) def test_get_dav_plugins(self): self.fn = 'get_dav_plugins' self.dict = True self.setup_mocks(lambda mod: { mod : 'DAV' + mod.capitalize() }) ret = self.mm.get_dav_plugins(self.req) self.assert_getter(ret, { 'test1' : 'DAVTest1', 'test2' : 'DAVTest2' }, self.req) def test_get_task_imports(self): self.fn = 'get_task_imports' self.setup_mocks(lambda mod: [mod]) ret = self.mm.get_task_imports() self.assert_getter(set(ret), set(['test1', 'test2'])) def test_get_settings_main_global(self): # TODO pass def test_get_settings_vhost_user(self): # TODO pass def test_menu_generator(self): menus = {} for mod_name in self.mod_names: if mod_name.startswith('empty'): continue menu = mock.MagicMock() menus[mod_name] = menu self.fn = 'get_menus' self.setup_mocks(lambda mod: [menus[mod]]) ret = list(self.mm.menu_generator(self.req)) self.assert_getter(set(ret), set(menus.values()), self.req) for mod_name, menu in menus.items(): self.assertEqual(menu.__moddef__, mod_name)
def test_module_load(self): cfg = mock.MagicMock() mm = ModuleManager(cfg) found = mock.MagicMock() mm.modules['found'] = found self.assertFalse(mm.load('notfound')) self.assertFalse(mm.load('found')) version_loaded = mock.MagicMock() version_loaded.version.return_value = _ver('1.0') mm.loaded['vmod'] = version_loaded self.assertFalse(mm.load('vmod == 2.0')) self.assertTrue(mm.load('vmod == 1.0')) mm.installed = {} mm.modules['core'] = mock.MagicMock() self.assertFalse(mm.load('core')) mm.loaded['core'] = True ep1 = mock.MagicMock() ep1.load.side_effect = ImportError mm.installed['mod1'] = _ver('1.0') mm.modules['mod1'] = ep1 self.assertFalse(mm.load('mod1')) with mock.patch('netprofile.common.modules.ModuleBase.version') as mock_version: mock_version.return_value = _ver('1.0') ep2 = mock.MagicMock() ep2.version.return_value = _ver('1.0') mm.installed['mod2'] = _ver('1.0') mm.modules['mod2'] = ep2 ep2.load.return_value = ModuleBase self.assertTrue(mm.load('mod2')) ep2.load.assert_called_once_with() ep3 = mock.MagicMock() ep3.version.return_value = _ver('1.0') mm.installed['mod3'] = _ver('1.0') mm.modules['mod3'] = ep3 ep3.load.return_value = mock.MagicMock self.assertFalse(mm.load('mod3')) ep3.load.assert_called_once_with() ep4 = mock.MagicMock() ep4.version.return_value = _ver('2.0') mm.installed['mod4'] = _ver('2.0') mm.modules['mod4'] = ep4 ep4.load.return_value = ModuleBase self.assertFalse(mm.load('mod4 == 2.0')) ep4.load.assert_called_once_with() with mock.patch('netprofile.common.modules.ModuleBase.get_deps') as mock_deps: mock_deps.return_value = ('mod5',) ep5 = mock.MagicMock() ep5.version.return_value = _ver('1.0') mm.installed['mod5'] = _ver('1.0') mm.modules['mod5'] = ep5 ep5.load.return_value = ModuleBase self.assertTrue(mm.load('mod5')) ep5.load.assert_called_once_with() mock_deps.assert_called_once_with() mock_deps.reset_mock() mock_deps.return_value = ('missing',) ep6 = mock.MagicMock() ep6.version.return_value = _ver('1.0') mm.installed['mod6'] = _ver('1.0') mm.modules['mod6'] = ep6 ep6.load.return_value = ModuleBase self.assertFalse(mm.load('mod6')) ep6.load.assert_called_once_with() mock_deps.assert_called_once_with() ep7 = mock.MagicMock() ep7.version.return_value = _ver('1.0') mm.installed['mod7'] = _ver('1.0') mm.modules['mod7'] = ep7 ep7.load.return_value = ModuleBase self.assertTrue(mm.load(packaging.requirements.Requirement('mod7 > 0.9'))) ep7.load.assert_called_once_with()
def test_find_ep(self, iter_ep): cfg = mock.MagicMock() mm = ModuleManager(cfg) mm.installed = {} mm.loaded['core'] = True ep1 = mock.MagicMock() mm.modules['mod1'] = ep1 self.assertEqual(mm._find_ep('mod1'), ep1) iter_ep.return_value = [] with self.assertRaises(ModuleError): mm._find_ep('missing') iter_ep.return_value = ['too', 'many'] with self.assertRaises(ModuleError): mm._find_ep('too') ep2 = mock.MagicMock() ep2_name = mock.PropertyMock(return_value='wrong') type(ep2).name = ep2_name iter_ep.return_value = [ep2] with self.assertRaises(ModuleError): mm._find_ep('right') ep3 = mock.MagicMock() ep3_name = mock.PropertyMock(return_value='mod3') type(ep3).name = ep3_name iter_ep.return_value = [ep3] self.assertEqual(mm._find_ep('mod3'), ep3) self.assertIn('mod3', mm.modules) self.assertEqual(mm.modules['mod3'], ep3)
class TestModuleGettersAPI(unittest.TestCase): def setUp(self): from netprofile.common import cache cache.cache = cache.configure_cache( {'netprofile.cache.backend': 'dogpile.cache.memory'}) self.req = testing.DummyRequest() self.cfg = testing.setUp(request=self.req) self.mm = ModuleManager(self.cfg) self.mod_names = ('test1', 'test2', 'empty') self.dict = False def setup_mocks(self, cb_value): for mod_name in self.mod_names: mod = mock.MagicMock() fn = getattr(mod, self.fn) if mod_name.startswith('empty'): fn.return_value = {} if self.dict else [] else: fn.return_value = cb_value(mod_name) self.mm.loaded[mod_name] = mod def assert_getter(self, ret, assert_ret, *args, **kwargs): for mod_name in self.mod_names: fn = getattr(self.mm.loaded[mod_name], self.fn) fn.assert_called_once_with(*args, **kwargs) self.assertEqual(ret, assert_ret) def tearDown(self): testing.tearDown() def test_get_js(self): self.fn = 'get_js' self.setup_mocks(lambda mod: [mod + '.js']) ret = self.mm.get_js(self.req) self.assert_getter(set(ret), set(['test1.js', 'test2.js']), self.req) def test_get_local_js(self): self.fn = 'get_local_js' self.setup_mocks(lambda mod: [mod + '-loc.js']) ret = self.mm.get_local_js(self.req, 'en') self.assert_getter(set(ret), set(['test1-loc.js', 'test2-loc.js']), self.req, 'en') def test_get_css(self): self.fn = 'get_css' self.setup_mocks(lambda mod: [mod + '.css']) ret = self.mm.get_css(self.req) self.assert_getter(set(ret), set(['test1.css', 'test2.css']), self.req) def test_get_autoload_js(self): self.fn = 'get_autoload_js' self.setup_mocks(lambda mod: [mod.capitalize()]) ret = self.mm.get_autoload_js(self.req) self.assert_getter(set(ret), set(['Test1', 'Test2']), self.req) def test_get_contollers(self): self.fn = 'get_controllers' self.setup_mocks(lambda mod: [mod.capitalize()]) ret = self.mm.get_controllers(self.req) self.assert_getter(set(ret), set(['Test1', 'Test2']), self.req) def test_get_rt_handlers(self): self.fn = 'get_rt_handlers' self.dict = True self.setup_mocks(lambda mod: {mod: mod + '_rt'}) ret = self.mm.get_rt_handlers(self.req) self.assert_getter(ret, { 'test1': 'test1_rt', 'test2': 'test2_rt' }, self.req) def test_get_rt_routes(self): self.fn = 'get_rt_routes' self.setup_mocks(lambda mod: [mod + '_rt']) ret = self.mm.get_rt_routes() self.assert_getter(set(ret), set(['test1_rt', 'test2_rt'])) def test_get_dav_plugins(self): self.fn = 'get_dav_plugins' self.dict = True self.setup_mocks(lambda mod: {mod: 'DAV' + mod.capitalize()}) ret = self.mm.get_dav_plugins(self.req) self.assert_getter(ret, { 'test1': 'DAVTest1', 'test2': 'DAVTest2' }, self.req) def test_get_task_imports(self): self.fn = 'get_task_imports' self.setup_mocks(lambda mod: [mod]) ret = self.mm.get_task_imports() self.assert_getter(set(ret), set(['test1', 'test2'])) def test_get_settings_main_global(self): # TODO pass def test_get_settings_vhost_user(self): # TODO pass def test_menu_generator(self): menus = {} for mod_name in self.mod_names: if mod_name.startswith('empty'): continue menu = mock.MagicMock() menus[mod_name] = menu self.fn = 'get_menus' self.setup_mocks(lambda mod: [menus[mod]]) ret = list(self.mm.menu_generator(self.req)) self.assert_getter(set(ret), set(menus.values()), self.req) for mod_name, menu in menus.items(): self.assertEqual(menu.__moddef__, mod_name)
def mm(self): return ModuleManager(self.app_config, stdout=self.stdout)