def build_uep(self, options, proxy_only=False): conf = config.Config(rhsm.config.initConfig()) # Some commands/services only allow manipulation of the proxy information for a connection cp_provider = inj.require(inj.CP_PROVIDER) if proxy_only: self.validate_only_proxy_options(options) connection_info = {} server_sec = conf['server'] connection_info['host'] = options.get('host', server_sec['hostname']) connection_info['ssl_port'] = options.get('port', server_sec.get_int('port')) connection_info['handler'] = options.get('handler', server_sec['prefix']) connection_info['proxy_hostname_arg'] = options.get('proxy_hostname', server_sec['proxy_hostname']) connection_info['proxy_port_arg'] = options.get('proxy_port', server_sec.get_int('proxy_port')) connection_info['proxy_user_arg'] = options.get('proxy_user', server_sec['proxy_user']) connection_info['proxy_password_arg'] = options.get('proxy_password', server_sec['proxy_password']) connection_info['no_proxy_arg'] = options.get('no_proxy', server_sec['no_proxy']) cp_provider.set_connection_info(**connection_info) cp_provider.set_correlation_id(utils.generate_correlation_id()) if self.is_registered(): return cp_provider.get_consumer_auth_cp() elif 'username' in options and 'password' in options: cp_provider.set_user_pass(options['username'], options['password']) return cp_provider.get_basic_auth_cp() else: return cp_provider.get_no_auth_cp()
def __init__(self, options): self.rhncfg = initUp2dateConfig() self.rhsmcfg = config.Config(initConfig()) # Sometimes we need to send up the entire contents of the system id file # which is referred to in Satellite 5 nomenclature as a "certificate" # although it is not an X509 certificate. try: self.system_id_contents = open(self.rhncfg["systemIdPath"], 'r').read() except IOError: system_exit( os.EX_IOERR, _("Could not read legacy system id at %s") % self.rhncfg["systemIdPath"]) self.system_id = self.get_system_id(self.system_id_contents) self.proxy_host = None self.proxy_port = None self.proxy_user = None self.proxy_pass = None self.cp = None self.db = ProductDatabase() self.consumer_id = None self.options = options self.is_hosted = is_hosted()
def test_hostname_prefix_config(self): self.stubConfig.set("server", "prefix", "/test-prefix") local_url = "myhost.example.com:500" (hostname, port, prefix) = parse_server_info(local_url, config.Config(self.stubConfig)) self.assertEqual("myhost.example.com", hostname) self.assertEqual("500", port) self.assertEqual("/test-prefix", prefix)
def test_hostname_port_config(self): self.stubConfig.set("server", "port", "600") local_url = "myhost.example.com/myapp" (hostname, port, prefix) = parse_server_info(local_url, config.Config(self.stubConfig)) self.assertEqual("myhost.example.com", hostname) self.assertEqual("600", port) self.assertEqual("/myapp", prefix)
def build_uep(self, options, proxy_only=False, basic_auth_method=False): conf = config.Config(rhsm.config.get_config_parser()) # Some commands/services only allow manipulation of the proxy information for a connection cp_provider = inj.require(inj.CP_PROVIDER) if proxy_only: self.validate_only_proxy_options(options) connection_info = {} server_sec = conf["server"] connection_info["host"] = options.get("host", server_sec["hostname"]) connection_info["ssl_port"] = options.get("port", server_sec.get_int("port")) connection_info["handler"] = options.get("handler", server_sec["prefix"]) connection_info["proxy_hostname_arg"] = options.get( "proxy_hostname", server_sec["proxy_hostname"]) connection_info["proxy_port_arg"] = options.get( "proxy_port", server_sec.get_int("proxy_port")) connection_info["proxy_user_arg"] = options.get( "proxy_user", server_sec["proxy_user"]) connection_info["proxy_password_arg"] = options.get( "proxy_password", server_sec["proxy_password"]) connection_info["no_proxy_arg"] = options.get("no_proxy", server_sec["no_proxy"]) cp_provider.set_connection_info(**connection_info) cp_provider.set_correlation_id(utils.generate_correlation_id()) if self.is_registered() and basic_auth_method is False: return cp_provider.get_consumer_auth_cp() elif "username" in options and "password" in options: cp_provider.set_user_pass(options["username"], options["password"]) return cp_provider.get_basic_auth_cp() else: return cp_provider.get_no_auth_cp()
import rhsm.connection as connection from rhsm.profile import get_profile import subscription_manager.injection as inj from subscription_manager.jsonwrapper import PoolWrapper from rhsm import ourjson as json from subscription_manager.isodate import parse_date from rhsmlib.services import config, syspurpose from subscription_manager.i18n import ugettext as _ log = logging.getLogger(__name__) PACKAGES_RESOURCE = "packages" conf = config.Config(initConfig()) class CacheManager(object): """ Parent class used for common logic in a number of collections where we need to push some consumer JSON up to the server, maintain a local cache of that data, and check if anything has changed on subsequent runs. """ # Fields the subclass must override: CACHE_FILE = None def to_dict(self): """
def setUp(self): # No matter what, stop all patching (even if we have a failure in setUp itself) self.addCleanup(patch.stopall) # Never attempt to use the actual managercli.cfg which points to a # real file in etc. self.mock_cfg_parser = stubs.StubConfig() original_conf = cli.conf def unstub_conf(): cli.conf = original_conf # Mock makes it damn near impossible to mock a module attribute (which we shouldn't be using # in the first place because it's terrible) so we monkey-patch it ourselves. # TODO Fix this idiocy by not reading the damn config on module import cli.conf = config.Config(self.mock_cfg_parser) self.addCleanup(unstub_conf) facts_host_patcher = patch('rhsmlib.dbus.facts.FactsClient', auto_spec=True) self.mock_facts_host = facts_host_patcher.start() self.mock_facts_host.return_value.GetFacts.return_value = self.set_facts( ) # By default mock that we are registered. Individual test cases # can override if they are testing disconnected scenario. id_mock = NonCallableMock(name='FixtureIdentityMock') id_mock.exists_and_valid = Mock(return_value=True) id_mock.uuid = 'fixture_identity_mock_uuid' id_mock.name = 'fixture_identity_mock_name' id_mock.cert_dir_path = "/not/a/real/path/to/pki/consumer/" id_mock.keypath.return_value = "/not/a/real/key/path" id_mock.certpath.return_value = "/not/a/real/cert/path" # Don't really care about date ranges here: self.mock_calc = NonCallableMock() self.mock_calc.calculate.return_value = None # Avoid trying to read real /etc/yum.repos.d/redhat.repo self.mock_repofile_path_exists_patcher = patch( 'subscription_manager.repolib.YumRepoFile.path_exists') mock_repofile_path_exists = self.mock_repofile_path_exists_patcher.start( ) mock_repofile_path_exists.return_value = True inj.provide(inj.IDENTITY, id_mock) inj.provide(inj.PRODUCT_DATE_RANGE_CALCULATOR, self.mock_calc) inj.provide(inj.ENTITLEMENT_STATUS_CACHE, stubs.StubEntitlementStatusCache()) inj.provide(inj.POOL_STATUS_CACHE, stubs.StubPoolStatusCache()) inj.provide(inj.PROD_STATUS_CACHE, stubs.StubProductStatusCache()) inj.provide(inj.SUPPORTED_RESOURCES_CACHE, stubs.StubSupportedResourcesCache()) inj.provide(inj.SYSPURPOSE_VALID_FIELDS_CACHE, stubs.StubSyspurposeValidFieldsCache()) inj.provide(inj.CURRENT_OWNER_CACHE, stubs.StubCurrentOwnerCache) inj.provide(inj.CONTENT_ACCESS_MODE_CACHE, stubs.StubContentAccessModeCache()) inj.provide(inj.OVERRIDE_STATUS_CACHE, stubs.StubOverrideStatusCache()) inj.provide(inj.RELEASE_STATUS_CACHE, stubs.StubReleaseStatusCache()) inj.provide(inj.AVAILABLE_ENTITLEMENT_CACHE, stubs.StubAvailableEntitlementsCache()) inj.provide(inj.PROFILE_MANAGER, stubs.StubProfileManager()) # By default set up an empty stub entitlement and product dir. # Tests need to modify or create their own but nothing should hit # the system. self.ent_dir = stubs.StubEntitlementDirectory() inj.provide(inj.ENT_DIR, self.ent_dir) self.prod_dir = stubs.StubProductDirectory() inj.provide(inj.PROD_DIR, self.prod_dir) # Installed products manager needs PROD_DIR injected first inj.provide(inj.INSTALLED_PRODUCTS_MANAGER, stubs.StubInstalledProductsManager()) self.stub_cp_provider = stubs.StubCPProvider() self._release_versions = [] self.stub_cp_provider.content_connection.get_versions = self._get_release_versions inj.provide(inj.CP_PROVIDER, self.stub_cp_provider) inj.provide(inj.CERT_SORTER, stubs.StubCertSorter()) # setup and mock the plugin_manager plugin_manager_mock = MagicMock(name='FixturePluginManagerMock') plugin_manager_mock.runiter.return_value = iter([]) inj.provide(inj.PLUGIN_MANAGER, plugin_manager_mock) inj.provide(inj.DBUS_IFACE, Mock(name='FixtureDbusIfaceMock')) pooltype_cache = Mock() inj.provide(inj.POOLTYPE_CACHE, pooltype_cache) # don't use file based locks for tests inj.provide(inj.ACTION_LOCK, RLock) self.stub_facts = stubs.StubFacts() inj.provide(inj.FACTS, self.stub_facts) content_access_cache_mock = MagicMock(name='ContentAccessCacheMock') inj.provide(inj.CONTENT_ACCESS_CACHE, content_access_cache_mock) self.dbus_patcher = patch( 'subscription_manager.cli_command.cli.CliCommand._request_validity_check' ) self.dbus_patcher.start() # No tests should be trying to connect to any configure or test server # so really, everything needs this mock. May need to be in __init__, or # better, all test classes need to use SubManFixture self.is_valid_server_patcher = patch( "subscription_manager.cli_command.cli.is_valid_server_info") is_valid_server_mock = self.is_valid_server_patcher.start() is_valid_server_mock.return_value = True # No tests should be trying to test the proxy connection # so really, everything needs this mock. May need to be in __init__, or # better, all test classes need to use SubManFixture self.test_proxy_connection_patcher = patch( "subscription_manager.cli_command.cli.CliCommand.test_proxy_connection" ) test_proxy_connection_mock = self.test_proxy_connection_patcher.start() test_proxy_connection_mock.return_value = True self.syncedstore_patcher = patch( 'subscription_manager.syspurposelib.SyncedStore') syncedstore_mock = self.syncedstore_patcher.start() set_up_mock_sp_store(syncedstore_mock) self.files_to_cleanup = []
import subscription_manager.injection as inj from subscription_manager.jsonwrapper import PoolWrapper from rhsm import ourjson as json from subscription_manager.isodate import parse_date from subscription_manager.utils import get_supported_resources from subscription_manager.syspurposelib import post_process_received_data from rhsmlib.services import config, syspurpose from subscription_manager.i18n import ugettext as _ log = logging.getLogger(__name__) PACKAGES_RESOURCE = "packages" conf = config.Config(get_config_parser()) class CacheManager(object): """ Parent class used for common logic in a number of collections where we need to push some consumer JSON up to the server, maintain a local cache of that data, and check if anything has changed on subsequent runs. """ # Fields the subclass must override: CACHE_FILE = None def to_dict(self): """
def is_hosted(): rhsmcfg = config.Config(initConfig()) hostname = rhsmcfg['server']['hostname'] return bool( re.search('subscription\.rhn\.(.*\.)*redhat\.com', hostname) or re.search('subscription\.rhsm\.(.*\.)*redhat\.com', hostname))
# granted to use or replicate Red Hat trademarks that are incorporated # in this software or its documentation. # import logging import six import dbus.service from rhsmlib.dbus import constants, exceptions, util from subscription_manager import utils from subscription_manager import injection as inj from subscription_manager.injectioninit import init_dep_injection from rhsmlib.services import config import rhsm.config conf = config.Config(rhsm.config.initConfig()) init_dep_injection() log = logging.getLogger(__name__) class BaseObject(dbus.service.Object): # Name of the DBus interface provided by this object interface_name = constants.INTERFACE_BASE default_dbus_path = constants.ROOT_DBUS_PATH def __init__(self, conn=None, object_path=None, bus_name=None): if object_path is None: object_path = self.default_dbus_path super(BaseObject, self).__init__(conn=conn, object_path=object_path, bus_name=bus_name)
def setUp(self): super(TestConfigCommand, self).setUp() self.original_conf = config_command.conf config_command.conf = config.Config(self.mock_cfg_parser)
from subscription_manager import ga_loader ga_loader.init_ga() from subscription_manager.ga import GLib from functools import partial from rhsmlib.services import config from rhsm.config import get_config_parser from rhsmlib.file_monitor import create_filesystem_watcher, DirectoryWatch from rhsmlib.file_monitor import CONSUMER_WATCHER, ENTITLEMENT_WATCHER, CONFIG_WATCHER, PRODUCT_WATCHER, \ SYSPURPOSE_WATCHER from subscription_manager import injection as inj from rhsm.logutil import init_logger log = logging.getLogger(__name__) parser = get_config_parser() conf = config.Config(parser) init_logger(parser) class Server(object): """ Class used for rhsm.service providing D-Bus API """ INSTANCE = None def __new__(cls, *args, **kwargs): """ Function called, when new instance of Server is requested """