def test_inequality(self): cc1 = cloud_region.CloudRegion("test1", "region-al", fake_config_dict) cc2 = cloud_region.CloudRegion("test2", "region-al", fake_config_dict) self.assertNotEqual(cc1, cc2) cc2 = cloud_region.CloudRegion("test1", "region-xx", fake_config_dict) self.assertNotEqual(cc1, cc2) cc2 = cloud_region.CloudRegion("test1", "region-al", {}) self.assertNotEqual(cc1, cc2)
def test_verify(self): config_dict = copy.deepcopy(fake_config_dict) config_dict['cacert'] = None config_dict['verify'] = False cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertFalse(verify) config_dict['verify'] = True cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertTrue(verify)
def _get_config_from_profile(profile, **kwargs): # Deal with clients still trying to use legacy profile objects region_name = None for service in profile.get_services(): if service.region: region_name = service.region service_type = service.service_type if service.interface: key = _make_key(service_type, 'interface') kwargs[key] = service.interface if service.version: version = service.version if version.startswith('v'): version = version[1:] key = _make_key(service_type, 'api_version') kwargs[key] = version if service.api_version: version = service.api_version key = _make_key(service_type, 'default_microversion') kwargs[key] = version config_kwargs = config_defaults.get_defaults() config_kwargs.update(kwargs) config = cloud_region.CloudRegion(region_name=region_name, config=config_kwargs) return config
def _get_config_from_profile(profile, authenticator, **kwargs): # TODO(shade) Remove this once we've shifted python-openstackclient # to not use the profile interface. # We don't have a cloud name. Make one up from the auth_url hostname # so that log messages work. name = urllib.parse.urlparse(authenticator.auth_url).hostname region_name = None for service in profile.get_services(): if service.region: region_name = service.region service_type = service.service_type if service.interface: key = cloud_region._make_key('interface', service_type) kwargs[key] = service.interface if service.version: version = service.version if version.startswith('v'): version = version[1:] key = cloud_region._make_key('api_version', service_type) kwargs[key] = service.version config_kwargs = config_defaults.get_defaults() config_kwargs.update(kwargs) config = cloud_region.CloudRegion( name=name, region_name=region_name, config=config_kwargs) config._auth = authenticator return config
def test_get_session_no_auth(self): config_dict = defaults.get_defaults() config_dict.update(fake_services_dict) cc = cloud_region.CloudRegion("test1", "region-al", config_dict) self.assertRaises( exceptions.ConfigException, cc.get_session)
def test_getters(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_services_dict) self.assertEqual(['compute', 'identity', 'image', 'volume'], sorted(cc.get_services())) self.assertEqual({ 'password': '******', 'username': '******' }, cc.get_auth_args()) self.assertEqual('public', cc.get_interface()) self.assertEqual('public', cc.get_interface('compute')) self.assertEqual('admin', cc.get_interface('identity')) self.assertEqual('region-al', cc.region_name) self.assertIsNone(cc.get_api_version('image')) self.assertEqual('2', cc.get_api_version('compute')) self.assertEqual('mage', cc.get_service_type('image')) self.assertEqual('compute', cc.get_service_type('compute')) self.assertEqual('1', cc.get_api_version('volume')) self.assertEqual('block-storage', cc.get_service_type('volume')) self.assertEqual('http://compute.example.com', cc.get_endpoint('compute')) self.assertIsNone(cc.get_endpoint('image')) self.assertIsNone(cc.get_service_name('compute')) self.assertEqual('locks', cc.get_service_name('identity')) self.assertIsNone(cc.get_status_code_retries('compute')) self.assertEqual(5, cc.get_status_code_retries('baremetal')) self.assertEqual(1, cc.get_connect_retries('compute')) self.assertEqual(3, cc.get_connect_retries('baremetal'))
def test_session_endpoint_not_found(self, mock_get_session): exc_to_raise = ksa_exceptions.catalog.EndpointNotFound mock_get_session.return_value.get_endpoint.side_effect = exc_to_raise cc = cloud_region.CloudRegion("test1", "region-al", {}, auth_plugin=mock.Mock()) self.assertIsNone(cc.get_session_endpoint('notfound'))
def _get_config_from_profile(profile, authenticator, **kwargs): # TODO(shade) Remove this once we've shifted python-openstackclient # to not use the profile interface. region_name = None for service in profile.get_services(): if service.region: region_name = service.region service_type = service.service_type if service.interface: key = cloud_region._make_key('interface', service_type) kwargs[key] = service.interface if service.version: version = service.version if version.startswith('v'): version = version[1:] key = cloud_region._make_key('api_version', service_type) kwargs[key] = version config_kwargs = config_defaults.get_defaults() config_kwargs.update(kwargs) config = cloud_region.CloudRegion(region_name=region_name, config=config_kwargs) config._auth = authenticator return config
def test_get_session_with_app_name(self, mock_session): config_dict = defaults.get_defaults() config_dict.update(fake_services_dict) fake_session = mock.Mock() fake_session.additional_user_agent = [] fake_session.app_name = None fake_session.app_version = None mock_session.return_value = fake_session cc = cloud_region.CloudRegion("test1", "region-al", config_dict, auth_plugin=mock.Mock(), app_name="test_app", app_version="test_version") cc.get_session() mock_session.assert_called_with(auth=mock.ANY, verify=True, cert=None, timeout=None, collect_timing=None, discovery_cache=None) self.assertEqual(fake_session.app_name, "test_app") self.assertEqual(fake_session.app_version, "test_version") self.assertEqual(fake_session.additional_user_agent, [('openstacksdk', openstack_version.__version__)])
def test_override_session_endpoint(self, mock_session): config_dict = defaults.get_defaults() config_dict.update(fake_services_dict) cc = cloud_region.CloudRegion( "test1", "region-al", config_dict, auth_plugin=mock.Mock()) self.assertEqual( cc.get_session_endpoint('telemetry'), fake_services_dict['telemetry_endpoint'])
def test_verify_cacert(self): config_dict = copy.deepcopy(fake_config_dict) config_dict['cacert'] = "certfile" config_dict['verify'] = False cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertFalse(verify) config_dict['verify'] = True cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertEqual("certfile", verify) config_dict['insecure'] = True cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertEqual(False, verify)
def test_aliases(self): services_dict = fake_services_dict.copy() services_dict['volume_api_version'] = 12 services_dict['alarming_service_name'] = 'aodh' cc = cloud_region.CloudRegion("test1", "region-al", services_dict) self.assertEqual('12', cc.get_api_version('volume')) self.assertEqual('12', cc.get_api_version('block-storage')) self.assertEqual('aodh', cc.get_service_name('alarm')) self.assertEqual('aodh', cc.get_service_name('alarming'))
def _make_clientmanager( self, auth_args=None, config_args=None, identity_api_version=None, auth_plugin_name=None, auth_required=None, ): if identity_api_version is None: identity_api_version = '2.0' if auth_plugin_name is None: auth_plugin_name = 'password' if auth_plugin_name.endswith('password'): auth_dict = copy.deepcopy(self.default_password_auth) elif auth_plugin_name.endswith('token'): auth_dict = copy.deepcopy(self.default_token_auth) else: auth_dict = {} if auth_args is not None: auth_dict = auth_args cli_options = defaults.get_defaults() cli_options.update({ 'auth_type': auth_plugin_name, 'auth': auth_dict, 'interface': fakes.INTERFACE, 'region_name': fakes.REGION_NAME, # 'workflow_api_version': '2', }) if config_args is not None: cli_options.update(config_args) loader = loading.get_plugin_loader(auth_plugin_name) auth_plugin = loader.load_from_options(**auth_dict) client_manager = self._clientmanager_class()( cli_options=cloud_region.CloudRegion( name='t1', region_name='1', config=cli_options, auth_plugin=auth_plugin, ), api_version={ 'identity': identity_api_version, }, ) client_manager._auth_required = auth_required is True client_manager.setup_auth() client_manager.auth_ref self.assertEqual( auth_plugin_name, client_manager.auth_plugin_name, ) return client_manager
def test_rackspace_workaround_only_rax(self): cc = cloud_region.CloudRegion("test1", "DFW", { 'region_name': 'DFW', 'auth': {'project_id': '123456'}, 'block_storage_endpoint_override': 'https://example.com/v2/', }) self.assertEqual( 'https://example.com/v2/', cc.get_endpoint('block-storage') )
def test_get_config(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_services_dict) self.assertIsNone(cc._get_config('nothing', None)) # This is what is happening behind the scenes in get_default_interface. self.assertEqual(fake_services_dict['interface'], cc._get_config('interface', None)) # The same call as above, but from one step up the stack self.assertEqual(fake_services_dict['interface'], cc.get_interface()) # Which finally is what is called to populate the below self.assertEqual('public', self.cloud.default_interface)
def test_cert_with_key(self): config_dict = copy.deepcopy(fake_config_dict) config_dict['cacert'] = None config_dict['verify'] = False config_dict['cert'] = 'cert' config_dict['key'] = 'key' cc = cloud_region.CloudRegion("test1", "region-xx", config_dict) (verify, cert) = cc.get_requests_verify_args() self.assertEqual(("cert", "key"), cert)
def test_session_endpoint(self, mock_get_session): mock_session = mock.Mock() mock_get_session.return_value = mock_session config_dict = defaults.get_defaults() config_dict.update(fake_services_dict) cc = cloud_region.CloudRegion( "test1", "region-al", config_dict, auth_plugin=mock.Mock()) cc.get_session_endpoint('orchestration') mock_session.get_endpoint.assert_called_with( interface='public', service_name=None, region_name='region-al', service_type='orchestration')
def test_rackspace_workaround(self): # We're skipping loader here, so we have to expand relevant # parts from the rackspace profile. The thing we're testing # is that the project_id logic works. cc = cloud_region.CloudRegion("test1", "DFW", { 'profile': 'rackspace', 'region_name': 'DFW', 'auth': {'project_id': '123456'}, 'block_storage_endpoint_override': 'https://example.com/v2/', }) self.assertEqual( 'https://example.com/v2/123456', cc.get_endpoint('block-storage') )
def test_get_region_name(self): def assert_region_name(default, compute): self.assertEqual(default, cc.region_name) self.assertEqual(default, cc.get_region_name()) self.assertEqual(default, cc.get_region_name(service_type=None)) self.assertEqual( compute, cc.get_region_name(service_type='compute')) self.assertEqual( default, cc.get_region_name(service_type='placement')) # No region_name kwarg, no regions specified in services dict # (including the default). cc = cloud_region.CloudRegion(config=fake_services_dict) assert_region_name(None, None) # Only region_name kwarg; it's returned for everything cc = cloud_region.CloudRegion( region_name='foo', config=fake_services_dict) assert_region_name('foo', 'foo') # No region_name kwarg; values (including default) show through from # config dict services_dict = dict( fake_services_dict, region_name='the-default', compute_region_name='compute-region') cc = cloud_region.CloudRegion(config=services_dict) assert_region_name('the-default', 'compute-region') # region_name kwarg overrides config dict default (for backward # compatibility), but service-specific region_name takes precedence. services_dict = dict( fake_services_dict, region_name='dict', compute_region_name='compute-region') cc = cloud_region.CloudRegion( region_name='kwarg', config=services_dict) assert_region_name('kwarg', 'compute-region')
def test_get_session(self, mock_session): config_dict = defaults.get_defaults() config_dict.update(fake_services_dict) fake_session = mock.Mock() fake_session.additional_user_agent = [] mock_session.return_value = fake_session cc = cloud_region.CloudRegion( "test1", "region-al", config_dict, auth_plugin=mock.Mock()) cc.get_session() mock_session.assert_called_with( auth=mock.ANY, verify=True, cert=None, timeout=None) self.assertEqual( fake_session.additional_user_agent, [('openstacksdk', openstack_version.__version__)])
def test_arbitrary_attributes(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_config_dict) self.assertEqual("test1", cc.name) self.assertEqual("region-al", cc.region_name) # Look up straight value self.assertEqual("1", cc.a) # Look up prefixed attribute, fail - returns None self.assertIsNone(cc.os_b) # Look up straight value, then prefixed value self.assertEqual("3", cc.c) self.assertEqual("3", cc.os_c) # Lookup mystery attribute self.assertIsNone(cc.x) # Test default ipv6 self.assertFalse(cc.force_ipv4)
def test_iteration(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_config_dict) self.assertTrue('a' in cc) self.assertFalse('x' in cc)
def test_no_override(self): """Test no override happens when defaults are not configured""" cc = cloud_region.CloudRegion("test1", "region-al", fake_services_dict) self.assertEqual('block-storage', cc.get_service_type('volume')) self.assertEqual('workflow', cc.get_service_type('workflow')) self.assertEqual('not-exist', cc.get_service_type('not-exist'))
def test_ipv6(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_config_dict, force_ipv4=True) self.assertTrue(cc.force_ipv4)
def test_volume_override_v3(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_services_dict) cc.config['volume_api_version'] = '3' self.assertEqual('volumev3', cc.get_service_type('volume'))
def test_workflow_override_v2(self): cc = cloud_region.CloudRegion("test1", "region-al", fake_services_dict) cc.config['workflow_api_version'] = '2' self.assertEqual('workflowv2', cc.get_service_type('workflow'))
def get_one_cloud_osc( self, cloud=None, validate=True, argparse=None, **kwargs ): """Retrieve a single CloudRegion and merge additional options :param string cloud: The name of the configuration to load from clouds.yaml :param boolean validate: Validate the config. Setting this to False causes no auth plugin to be created. It's really only useful for testing. :param Namespace argparse: An argparse Namespace object; allows direct passing in of argparse options to be added to the cloud config. Values of None and '' will be removed. :param region_name: Name of the region of the cloud. :param kwargs: Additional configuration options :raises: keystoneauth1.exceptions.MissingRequiredOptions on missing required auth parameters """ args = self._fix_args(kwargs, argparse=argparse) if cloud is None: if 'cloud' in args: cloud = args['cloud'] else: cloud = self.default_cloud config = self._get_base_cloud_config(cloud) # Get region specific settings if 'region_name' not in args: args['region_name'] = '' region = self._get_region(cloud=cloud, region_name=args['region_name']) args['region_name'] = region['name'] region_args = copy.deepcopy(region['values']) # Regions is a list that we can use to create a list of cloud/region # objects. It does not belong in the single-cloud dict config.pop('regions', None) # Can't just do update, because None values take over for arg_list in region_args, args: for (key, val) in iter(arg_list.items()): if val is not None: if key == 'auth' and config[key] is not None: config[key] = _auth_update(config[key], val) else: config[key] = val config = self.magic_fixes(config) # NOTE(dtroyer): OSC needs a hook into the auth args before the # plugin is loaded in order to maintain backward- # compatible behaviour config = self.auth_config_hook(config) if validate: loader = self._get_auth_loader(config) config = self._validate_auth_correctly(config, loader) auth_plugin = loader.load_from_options(**config['auth']) else: auth_plugin = None # If any of the defaults reference other values, we need to expand for (key, value) in config.items(): if hasattr(value, 'format') and key not in FORMAT_EXCLUSIONS: config[key] = value.format(**config) force_ipv4 = config.pop('force_ipv4', self.force_ipv4) prefer_ipv6 = config.pop('prefer_ipv6', True) if not prefer_ipv6: force_ipv4 = True if cloud is None: cloud_name = '' else: cloud_name = str(cloud) return cloud_region.CloudRegion( name=cloud_name, region_name=config['region_name'], config=self._normalize_keys(config), force_ipv4=force_ipv4, auth_plugin=auth_plugin, openstack_config=self, )