def test_is_unprocessable_entity(self): con = mock.Mock() c = clients.Clients(con) # only 'nova' client plugin need to check this exception if self.plugin == 'nova': client_plugin = c.client_plugin(self.plugin) try: raise self.exception() except Exception as e: iue = self.is_unprocessable_entity if iue != client_plugin.is_unprocessable_entity(e): raise
def test_clients_get_heat_cfn_url(self): con = mock.Mock() c = clients.Clients(con) con.clients = c obj = c.client_plugin('heat') obj._get_client_option = mock.Mock() obj._get_client_option.return_value = None obj.url_for = mock.Mock(name="url_for") heat_cfn_url = "http://0.0.0.0:8000/v1" obj.url_for.return_value = heat_cfn_url self.assertEqual(heat_cfn_url, obj.get_heat_cfn_url())
def test_invalidate_all_clients(self, mock_invalidate): plugin_types = clients._mgr.names() con = mock.Mock() c = clients.Clients(con) con.clients = c for plugin_name in plugin_types: plugin = c.client_plugin(plugin_name) self.assertIsNotNone(plugin) c.invalidate_plugins() # while client plugin is initialized and while client is invoked # its being invalidated, so the count will be doubled self.assertEqual(len(plugin_types) * 2, mock_invalidate.call_count)
def test_ignore_not_found(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) try: exp = self.exception() exp_class = exp.__class__ raise exp except Exception as e: if self.is_not_found: client_plugin.ignore_not_found(e) else: self.assertRaises(exp_class, client_plugin.ignore_not_found, e)
def test_clients_heat_no_auth_token(self, mock_call): con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = None con.auth_plugin = fakes.FakeAuth(auth_token='anewtoken') c = clients.Clients(con) con.clients = c obj = c.client_plugin('heat') obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" self.assertEqual('url_from_keystone', obj.get_heat_url())
def test_auth_token(self): con = mock.Mock() con.auth_token = "1234" con.trust_id = None c = clients.Clients(con) con.clients = c plugin = FooClientsPlugin(con) # assert token is from plugin rather than context # even though both are set self.assertEqual('5678', plugin.auth_token)
def test_clients_auth_token_update(self): fkc = self.stub_keystoneclient(auth_token='token1') con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.trust_id = "b363706f891f48019483f8bd6503c54b" con.username = '******' con.password = '******' con.auth_token = None obj = clients.Clients(con) self.assertIsNotNone(obj.client('heat')) self.assertEqual('token1', obj.auth_token) fkc.auth_token = 'token2' self.assertEqual('token2', obj.auth_token)
def test_clients_heat_no_auth_token(self, mock_call): self.stub_keystoneclient(auth_token='anewtoken') con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = None obj = clients.Clients(con) obj._get_heat_url = mock.Mock(name="_get_heat_url") obj._get_heat_url.return_value = None obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" self.assertIsNotNone(obj.client('heat')) self.assertEqual('anewtoken', obj.client('keystone').auth_token)
def test_create_all_client_plugins(self): plugin_types = clients._mgr.names() self.assertIsNotNone(plugin_types) con = mock.Mock() c = clients.Clients(con) for plugin_name in plugin_types: plugin = c.client_plugin(plugin_name) self.assertIsNotNone(plugin) self.assertEqual(c, plugin.clients) self.assertEqual(con, plugin.context) self.assertIsNone(plugin._client)
def test_clients_heat(self, mock_call): self.stub_keystoneclient() con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155" c = clients.Clients(con) con.clients = c obj = c.client_plugin('heat') obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" obj.client() self.assertEqual('url_from_keystone', obj.get_heat_url())
def test_clients_get_heat_url(self): con = mock.Mock() con.tenant_id = "b363706f891f48019483f8bd6503c54b" obj = clients.Clients(con) obj._get_client_option = mock.Mock() obj._get_client_option.return_value = None self.assertIsNone(obj._get_heat_url()) heat_url = "http://0.0.0.0:8004/v1/%(tenant_id)s" obj._get_client_option.return_value = heat_url tenant_id = "b363706f891f48019483f8bd6503c54b" result = heat_url % {"tenant_id": tenant_id} self.assertEqual(result, obj._get_heat_url()) obj._get_client_option.return_value = result self.assertEqual(result, obj._get_heat_url())
def test_clients_heat_cached(self, mock_call): con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155" obj = clients.Clients(con) obj._get_heat_url = mock.Mock(name="_get_heat_url") obj._get_heat_url.return_value = None obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" obj._heat = None heat = obj.heat() heat_cached = obj.heat() self.assertEqual(heat, heat_cached)
def test_url_for(self): con = mock.Mock() con.auth_token = "1234" c = clients.Clients(con) c.client = mock.Mock(name="client") mock_keystone = mock.Mock() c.client.return_value = mock_keystone mock_keystone.url_for.return_value = 'http://192.0.2.1/foo' plugin = FooClientsPlugin(c) self.assertEqual('http://192.0.2.1/foo', plugin.url_for(service_type='foo')) c.client.assert_called_with('keystone')
def test_auth_token(self): con = mock.Mock() con.auth_token = "1234" c = clients.Clients(con) c.client = mock.Mock(name="client") mock_keystone = mock.Mock() c.client.return_value = mock_keystone mock_keystone.auth_token = '5678' plugin = FooClientsPlugin(c) # assert token is from keystone rather than context # even though both are set self.assertEqual('5678', plugin.auth_token) c.client.assert_called_with('keystone')
def test_url_for(self): con = mock.Mock() con.auth_token = "1234" con.trust_id = None c = clients.Clients(con) con.clients = c con.auth_plugin = mock.Mock(name="auth_plugin") con.auth_plugin.get_endpoint = mock.Mock(name="get_endpoint") con.auth_plugin.get_endpoint.return_value = 'http://192.0.2.1/foo' plugin = FooClientsPlugin(con) self.assertEqual('http://192.0.2.1/foo', plugin.url_for(service_type='foo')) self.assertTrue(con.auth_plugin.get_endpoint.called)
def test_ignore_conflict_and_not_found_context_manager(self): con = mock.Mock() c = clients.Clients(con) client_plugin = c.client_plugin(self.plugin) exp = self.exception() exp_class = exp.__class__ def try_raise(): with client_plugin.ignore_conflict_and_not_found: raise exp if self.is_conflict or self.is_not_found: try_raise() else: self.assertRaises(exp_class, try_raise)
def test_auth_token(self): con = mock.Mock() con.auth_token = "1234" con.trust_id = None c = clients.Clients(con) con.clients = c con.auth_plugin = mock.Mock(name="auth_plugin") con.auth_plugin.get_token = mock.Mock(name="get_token") con.auth_plugin.get_token.return_value = '5678' plugin = FooClientsPlugin(con) # assert token is from plugin rather than context # even though both are set self.assertEqual('5678', plugin.auth_token) con.auth_plugin.get_token.assert_called()
def test_clients_heat(self, mock_call): self.stub_keystoneclient() con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155" obj = clients.Clients(con) obj._get_heat_url = mock.Mock(name="_get_heat_url") obj._get_heat_url.return_value = None obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" obj.client('heat') self.assertEqual('url_from_keystone', mock_call.call_args[0][1]) obj._get_heat_url.return_value = "url_from_config" del(obj._clients['heat']) obj.client('heat') self.assertEqual('url_from_config', mock_call.call_args[0][1])
def test_create_all_client_plugins(self): plugin_types = clients._mgr.names() self.assertIsNotNone(plugin_types) con = mock.Mock() c = clients.Clients(con) con.clients = c for plugin_name in plugin_types: plugin = c.client_plugin(plugin_name) self.assertIsNotNone(plugin) self.assertEqual(c, plugin.clients) self.assertEqual(con, plugin.context) self.assertEqual({}, plugin._client_instances) self.assertTrue(clients.has_client(plugin_name)) self.assertIsInstance(plugin.service_types, list) self.assertGreaterEqual(len(plugin.service_types), 1, 'service_types is not defined for plugin')
def test_clients_get_heat_url(self): con = mock.Mock() con.tenant_id = "b363706f891f48019483f8bd6503c54b" c = clients.Clients(con) con.clients = c obj = c.client_plugin('heat') obj._get_client_option = mock.Mock() obj._get_client_option.return_value = None obj.url_for = mock.Mock(name="url_for") obj.url_for.return_value = "url_from_keystone" self.assertEqual("url_from_keystone", obj.get_heat_url()) heat_url = "http://0.0.0.0:8004/v1/%(tenant_id)s" obj._get_client_option.return_value = heat_url tenant_id = "b363706f891f48019483f8bd6503c54b" result = heat_url % {"tenant_id": tenant_id} self.assertEqual(result, obj.get_heat_url()) obj._get_client_option.return_value = result self.assertEqual(result, obj.get_heat_url())
def _to_ceilometer(self, data): from heat.engine import clients clients = clients.Clients(self.context) sample = {} sample['meter_type'] = 'gauge' for k, d in iter(data.items()): if k == 'Namespace': continue sample['meter_name'] = k sample['sample_volume'] = d['Value'] sample['meter_unit'] = d['Unit'] dims = d.get('Dimensions', {}) if isinstance(dims, list): dims = dims[0] sample['resource_metadata'] = dims sample['resource_id'] = dims.get('InstanceId') logger.debug('new sample:%s data:%s' % (k, sample)) clients.ceilometer().samples.create(**sample)
def _client_cfn_url(self, use_uwsgi=False, use_ipv6=False): con = mock.Mock() c = clients.Clients(con) con.clients = c obj = c.client_plugin('heat') obj._get_client_option = mock.Mock() obj._get_client_option.return_value = None obj.url_for = mock.Mock(name="url_for") if use_ipv6: if use_uwsgi: obj.url_for.return_value = "http://[::1]/heat-api-cfn/v1/" else: obj.url_for.return_value = "http://[::1]:8000/v1/" else: if use_uwsgi: obj.url_for.return_value = "http://0.0.0.0/heat-api-cfn/v1/" else: obj.url_for.return_value = "http://0.0.0.0:8000/v1/" return obj
def test_get_client_option(self): con = mock.Mock() con.auth_url = "http://auth.example.com:5000/v2.0" con.tenant_id = "b363706f891f48019483f8bd6503c54b" con.auth_token = "3bcc3d3a03f44e3d8377f9247b0ad155" c = clients.Clients(con) con.clients = c plugin = FooClientsPlugin(con) cfg.CONF.set_override('ca_file', '/tmp/bar', group='clients_heat') cfg.CONF.set_override('ca_file', '/tmp/foo', group='clients') # check heat group self.assertEqual('/tmp/bar', plugin._get_client_option('heat', 'ca_file')) # check fallback clients group for unknown client foo self.assertEqual('/tmp/foo', plugin._get_client_option('foo', 'ca_file'))
def test_cinder_api_v2_only(self): self.stub_auth(only_services=['volumev2']) ctx = utils.dummy_context() client = clients.Clients(ctx).client('cinder') self.assertEqual(2, client.volume_api_version)
def test_cinder_api_v1_and_v2(self): self.stub_auth() ctx = utils.dummy_context() client = clients.Clients(ctx).client('cinder') self.assertEqual(2, client.volume_api_version)
def test_abstract_create(self): con = mock.Mock() c = clients.Clients(con) con.clients = c self.assertRaises(TypeError, client_plugin.ClientPlugin, c)
def clients(self): if self._clients is None: self._clients = clients.Clients(self) return self._clients
def test_cinder_api_v1_only(self): self.stub_keystoneclient(only_services=['volume']) ctx = utils.dummy_context() client = clients.Clients(ctx).client('cinder') self.assertEqual(1, client.volume_api_version)
def test_clients_chosen_at_module_initilization(self): self.assertFalse(hasattr(clients.Clients, 'nova')) self.assertTrue(hasattr(clients.Clients('fakecontext'), 'nova'))
def authenticated_to_backend(self, cnxt): """ Verify that the credentials in the RPC context are valid for the current cloud backend. """ return clients.Clients(cnxt).authenticated()