def test_entry_point_priority(self, mock_iter): # Make sure we load extensions in the correct order based on priority. class MockLoad(object): """ Used to determine in what order the load() methods were called. """ def __init__(self, i, order_of_calling): self.i = i self.order_of_calling = order_of_calling def __call__(self, *args, **kwargs): self.order_of_calling.append(self) return mock.MagicMock() order_of_calling = [] context = mock.MagicMock() entry_point1 = decorator.priority()(mock.MagicMock()) entry_point1.load = MockLoad(1, order_of_calling) entry_point2 = decorator.priority(loader.DEFAULT_PRIORITY - 10)(mock.MagicMock()) entry_point2.load = MockLoad(2, order_of_calling) entry_point3 = decorator.priority(loader.DEFAULT_PRIORITY + 10)(mock.MagicMock()) entry_point3.load = MockLoad(3, order_of_calling) mock_iter.return_value = [entry_point1, entry_point2, entry_point3] loader.load_extensions(EMPTY_SET, context, 'admin') self.assertEqual(len(order_of_calling), 3) self.assertEqual([load.i for load in order_of_calling], [2, 1, 3])
def test_load_partial_fail_set_cli(self, mock_entry): # Test try: loader.load_extensions(PARTIAL_FAIL_SET, self.context, 'admin') self.fail('Exception expected') except loader.LoadFailed, e: self.assertEqual(1, len(e.failed_packs)) self.assertTrue('init_exception' in e.failed_packs)
def test_load_extensions_bad_dir(self): """ Tests loading extensions on a directory that doesn't exist. """ try: loader.load_extensions('fake_dir', self.context, 'admin') except loader.InvalidExtensionsDirectory, e: self.assertEqual(e.dir, 'fake_dir') self.assertEqual(str(e), 'Inaccessible or missing extensions directory [fake_dir]')
def test_load_extensions_bad_dir(self): """ Tests loading extensions on a directory that doesn't exist. """ try: loader.load_extensions('fake_dir', self.context, 'admin') except loader.InvalidExtensionsDirectory, e: self.assertEqual(e.dir, 'fake_dir') print(e) # for coverage
def test_load_partial_fail_set_2_cli(self): # Test try: loader.load_extensions(PARTIAL_FAIL_SET_2, self.context, 'admin') self.fail('Exception expected') except loader.LoadFailed, e: self.assertEqual(1, len(e.failed_packs)) self.assertTrue('not_python_module' in e.failed_packs) print(e) # for coverage
def test_load_partial_fail_set_2_cli(self): # Test try: loader.load_extensions(PARTIAL_FAIL_SET_2, self.context, 'admin') self.fail('Exception expected') except loader.LoadFailed, e: self.assertEqual(1, len(e.failed_packs)) self.assertTrue('not_python_module' in e.failed_packs) self.assertEqual(str(e), 'The following extension packs failed to load: [not_python_module]')
def test_load_entry_points(self, mock_iter): # Make sure we try to load extensions through entry points. context = mock.MagicMock() entry_point = mock.MagicMock() mock_iter.return_value = [entry_point] loader.load_extensions(EMPTY_SET, context, 'admin') mock_iter.assert_called_once_with('pulp.extensions.admin') entry_point.load.assert_called_once_with() entry_point.load.return_value.assert_called_once_with(context)
def test_load_valid_set_cli(self, mock_entry): """ Tests loading the set of CLI extensions in the valid_set directory. These extensions have the following properties: * Three extensions, all of which are set up correctly to be loaded * Only two of them (ext1 and ext2) contain a CLI loading module * Each of those will add a single section to the CLI named section-X, where X is the number in the directory name """ # Test loader.load_extensions(VALID_SET, self.context, 'admin') # Verify self.assertTrue(self.cli.root_section.find_subsection('section-1') is not None) self.assertTrue(self.cli.root_section.find_subsection('section-2') is not None)
def main(config_filenames, exception_handler_class=ExceptionHandler): """ Entry point into the launcher. Any extra necessary values will be pulled from the given configuration files. @param config_filenames: ordered list of files to load configuration from @type config_filenames: list @return: exit code suitable to return to the shell launching the client """ # Command line argument handling parser = OptionParser() parser.disable_interspersed_args() parser.add_option('-u', '--username', dest='username', action='store', default=None, help=_('credentials for the Pulp server; if specified will bypass the stored certificate')) parser.add_option('-p', '--password', dest='password', action='store', default=None, help=_('credentials for the Pulp server; must be specified with --username')) parser.add_option('--debug', dest='debug', action='store_true', default=False, help=_('enables debug logging')) parser.add_option('--config', dest='config', default=None, help=_('absolute path to the configuration file')) parser.add_option('--map', dest='print_map', action='store_true', default=False, help=_('prints a map of the CLI sections and commands')) options, args = parser.parse_args() # Configuration and Logging if options.config is not None: config_filenames = [options.config] config = _load_configuration(config_filenames) logger = _initialize_logging(config, debug=options.debug) # General UI pieces prompt = _create_prompt(config) exception_handler = exception_handler_class(prompt, config) # REST Bindings username = options.username password = options.password if username and not password: prompt_msg = 'Enter password: '******'Login cancelled')) sys.exit(os.EX_NOUSER) server = _create_bindings(config, logger, username, password) # Client context context = ClientContext(server, config, logger, prompt, exception_handler) cli = PulpCli(context) context.cli = cli # Load extensions into the UI in the context extensions_dir = config['filesystem']['extensions_dir'] extensions_dir = os.path.expanduser(extensions_dir) role = config['client']['role'] try: extensions_loader.load_extensions(extensions_dir, context, role) except extensions_loader.LoadFailed, e: prompt.write(_('The following extensions failed to load: %(f)s' % {'f' : ', '.join(e.failed_packs)})) prompt.write(_('More information on the failures can be found in %(l)s' % {'l' : config['logging']['filename']})) return os.EX_OSFILE
def main(config_filenames): """ Entry point into the launcher. Any extra necessary values will be pulled from the given configuration files. @param config_filenames: ordered list of files to load configuration from @type config_filenames: list @return: exit code suitable to return to the shell launching the client """ # Command line argument handling parser = OptionParser() parser.disable_interspersed_args() parser.add_option( "-u", "--username", dest="username", action="store", default=None, help=_("credentials for the Pulp server; if specified will bypass the stored certificate"), ) parser.add_option( "-p", "--password", dest="password", action="store", default=None, help=_("credentials for the Pulp server; must be specified with --username"), ) parser.add_option("--debug", dest="debug", action="store_true", default=False, help=_("enables debug logging")) parser.add_option("--config", dest="config", default=None, help=_("absolute path to the configuration file")) parser.add_option( "--map", dest="print_map", action="store_true", default=False, help=_("prints a map of the CLI sections and commands"), ) options, args = parser.parse_args() # Configuration and Logging if options.config is not None: config_filenames = [options.config] config = _load_configuration(config_filenames) logger = _initialize_logging(config, debug=options.debug) # General UI pieces prompt = _create_prompt(config) exception_handler = ExceptionHandler(prompt, config) # REST Bindings username = options.username password = options.password if username and not password: prompt_msg = "Enter password: "******"Login cancelled")) sys.exit(os.EX_NOUSER) server = _create_bindings(config, logger, username, password) # Client context context = ClientContext(server, config, logger, prompt, exception_handler) cli = PulpCli(context) context.cli = cli # Load extensions into the UI in the context extensions_dir = config["filesystem"]["extensions_dir"] extensions_dir = os.path.expanduser(extensions_dir) role = config["client"]["role"] try: extensions_loader.load_extensions(extensions_dir, context, role) except extensions_loader.LoadFailed, e: prompt.write(_("The following extensions failed to load: %(f)s" % {"f": ", ".join(e.failed_packs)})) prompt.write(_("More information on the failures can be found in %(l)s" % {"l": config["logging"]["filename"]})) return os.EX_OSFILE
def main(config_filenames, exception_handler_class=ExceptionHandler): """ Entry point into the launcher. Any extra necessary values will be pulled from the given configuration files. @param config_filenames: ordered list of files to load configuration from @type config_filenames: list @return: exit code suitable to return to the shell launching the client """ # Command line argument handling parser = OptionParser() parser.disable_interspersed_args() parser.add_option( '-u', '--username', dest='username', action='store', default=None, help= _('credentials for the Pulp server; if specified will bypass the stored certificate' )) parser.add_option( '-p', '--password', dest='password', action='store', default=None, help=_( 'credentials for the Pulp server; must be specified with --username' )) parser.add_option('--debug', dest='debug', action='store_true', default=False, help=_('enables debug logging')) parser.add_option('--config', dest='config', default=None, help=_('absolute path to the configuration file')) parser.add_option('--map', dest='print_map', action='store_true', default=False, help=_('prints a map of the CLI sections and commands')) options, args = parser.parse_args() # Configuration and Logging if options.config is not None: config_filenames = [options.config] config = _load_configuration(config_filenames) logger = _initialize_logging(config, debug=options.debug) # General UI pieces prompt = _create_prompt(config) exception_handler = exception_handler_class(prompt, config) # REST Bindings username = options.username password = options.password if username and not password: prompt_msg = 'Enter password: '******'Login cancelled')) sys.exit(os.EX_NOUSER) server = _create_bindings(config, logger, username, password) # Client context context = ClientContext(server, config, logger, prompt, exception_handler) cli = PulpCli(context) context.cli = cli # Load extensions into the UI in the context extensions_dir = config['filesystem']['extensions_dir'] extensions_dir = os.path.expanduser(extensions_dir) role = config['client']['role'] try: extensions_loader.load_extensions(extensions_dir, context, role) except extensions_loader.LoadFailed, e: prompt.write( _('The following extensions failed to load: %(f)s' % {'f': ', '.join(e.failed_packs)})) prompt.write( _('More information on the failures can be found in %(l)s' % {'l': config['logging']['filename']})) return os.EX_OSFILE
def main(config, exception_handler_class=ExceptionHandler): """ Entry point into the launcher. Any extra necessary values will be pulled from the given configuration files. @param config: The CLI configuration. @type config: Config @return: exit code suitable to return to the shell launching the client """ ensure_user_pulp_dir() # Command line argument handling parser = OptionParser() parser.disable_interspersed_args() parser.add_option('-u', '--username', dest='username', action='store', default=None, help=_('username for the Pulp server; if used will bypass the stored ' 'certificate and override a username specified in ~/.pulp/admin.conf')) parser.add_option('-p', '--password', dest='password', action='store', default=None, help=_('password for the Pulp server; must be used with --username. ' 'if used will bypass the stored certificate and override a password ' 'specified in ~/.pulp/admin.conf')) parser.add_option('--config', dest='config', default=None, help=_('absolute path to the configuration file')) parser.add_option('--map', dest='print_map', action='store_true', default=False, help=_('prints a map of the CLI sections and commands')) parser.add_option( '-v', dest='verbose', action='count', help=_('enables verbose output; use twice for increased verbosity with debug information')) options, args = parser.parse_args() # Configuration and Logging if options.config is not None: config.update(Config(options.config)) logger = _initialize_logging(verbose=options.verbose) # General UI pieces prompt = _create_prompt(config) exception_handler = exception_handler_class(prompt, config) # REST Bindings username = options.username password = options.password if not username and not password: # Try to get username/password from config if not explicitly set. username and password are # not included by default so we need to catch KeyError Exceptions. try: username = config['auth']['username'] password = config['auth']['password'] except KeyError: pass if username and not password: prompt_msg = 'Enter password: '******'Login canceled')) sys.exit(os.EX_NOUSER) server = _create_bindings(config, logger, username, password, verbose=options.verbose) # Client context context = ClientContext(server, config, logger, prompt, exception_handler) cli = PulpCli(context) context.cli = cli # Load extensions into the UI in the context extensions_dir = config['filesystem']['extensions_dir'] extensions_dir = os.path.expanduser(extensions_dir) role = config['client']['role'] try: extensions_loader.load_extensions(extensions_dir, context, role) except extensions_loader.LoadFailed, e: prompt.write( _('The following extensions failed to load: %(f)s' % {'f': ', '.join(e.failed_packs)})) prompt.write(_('More information on the failures may be found by using -v option one or ' 'more times')) return os.EX_OSFILE