def setUp(self): core.initialize() core.ModelBase.metadata.create_all(core.get_engine()) # enforce foreign key constraint for sqlite core.get_engine().execute('pragma foreign_keys=on') self.context = context.Context() pod_dict = { 'pod_id': FAKE_SITE_ID, 'pod_name': FAKE_SITE_NAME, 'az_name': FAKE_AZ } config_dict = { 'service_id': FAKE_SERVICE_ID, 'pod_id': FAKE_SITE_ID, 'service_type': FAKE_TYPE, 'service_url': FAKE_URL } api.create_pod(self.context, pod_dict) api.create_pod_service_configuration(self.context, config_dict) global FAKE_RESOURCES FAKE_RESOURCES = [{'name': 'res1'}, {'name': 'res2'}] cfg.CONF.set_override(name='top_pod_name', override=FAKE_SITE_NAME, group='client') self.client = client.Client() self.client.resource_service_map[FAKE_RESOURCE] = FAKE_TYPE self.client.operation_resources_map['list'].add(FAKE_RESOURCE) self.client.operation_resources_map['create'].add(FAKE_RESOURCE) self.client.operation_resources_map['delete'].add(FAKE_RESOURCE) self.client.operation_resources_map['action'].add(FAKE_RESOURCE) self.client.service_handle_map[FAKE_TYPE] = FakeResHandle(None)
def test_get_pod_and_endpoint_by_name(self): pod_dict = { 'pod_id': 'fake_pod_id', 'pod_name': 'fake_pod_name', 'az_name': 'fake_az' } api.create_pod(self.context, pod_dict) pod = api.get_pod_by_name(self.context, pod_dict['pod_name'] + '1') self.assertIsNone(pod) pod = api.get_pod_by_name(self.context, pod_dict['pod_name']) self.assertEqual(pod['pod_id'], pod_dict['pod_id']) self.assertEqual(pod['pod_name'], pod_dict['pod_name']) self.assertEqual(pod['az_name'], pod_dict['az_name']) config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_CINDER, 'service_url': 'http://127.0.0.1:8774/v2.1/$(tenant_id)s' } api.create_pod_service_configuration(self.context, config_dict) endpoint = hclient.get_pod_service_endpoint( self.context, pod_dict['pod_name'], config_dict['service_type']) self.assertEqual(endpoint, config_dict['service_url'])
def _update_endpoint_from_keystone(self, cxt, is_internal): """Update the database by querying service endpoint url from Keystone :param cxt: context object :param is_internal: if True, this method utilizes pre-configured admin username and password to apply an new admin token, this happens only when auto_refresh_endpoint is set to True. if False, token in cxt is directly used, users should prepare admin token themselves :return: None """ if is_internal: admin_context = tricircle_context.Context() admin_context.auth_token = self._get_admin_token() endpoint_map = self._get_endpoint_from_keystone(admin_context) else: endpoint_map = self._get_endpoint_from_keystone(cxt) for region in endpoint_map: # use region name to query pod pod_filters = [{ 'key': 'pod_name', 'comparator': 'eq', 'value': region }] pod_list = api.list_pods(cxt, pod_filters) # skip region/pod not registered in cascade service if len(pod_list) != 1: continue for service in endpoint_map[region]: pod_id = pod_list[0]['pod_id'] config_filters = [{ 'key': 'pod_id', 'comparator': 'eq', 'value': pod_id }, { 'key': 'service_type', 'comparator': 'eq', 'value': service }] config_list = api.list_pod_service_configurations( cxt, config_filters) if len(config_list) > 1: raise exceptions.EndpointNotUnique(pod_id, service) if len(config_list) == 1: config_id = config_list[0]['service_id'] update_dict = { 'service_url': endpoint_map[region][service] } api.update_pod_service_configuration( cxt, config_id, update_dict) else: config_dict = { 'service_id': str(uuid.uuid4()), 'pod_id': pod_id, 'service_type': service, 'service_url': endpoint_map[region][service] } api.create_pod_service_configuration(cxt, config_dict)
def _prepare_pod_service(self, pod_id, service): config_dict = { 'service_id': uuidutils.generate_uuid(), 'pod_id': pod_id, 'service_type': service, 'service_url': 'fake_pod_service' } api.create_pod_service_configuration(self.context, config_dict) pass
def test_list_endpoint_not_unique(self): # add a new configuration with same pod and service type config_dict = { 'service_id': FAKE_SERVICE_ID + '_new', 'pod_id': FAKE_SITE_ID, 'service_type': FAKE_TYPE, 'service_url': FAKE_URL } api.create_pod_service_configuration(self.context, config_dict) self.assertRaises(exceptions.EndpointNotUnique, self.client.list_resources, FAKE_RESOURCE, self.context, [])
def test_get_pod_service_ctx(self): pod_dict = { 'pod_id': 'fake_pod_id', 'pod_name': 'fake_pod_name', 'az_name': 'fake_az' } config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_CINDER, 'service_url': 'http://127.0.0.1:8774/v2.1/$(tenant_id)s' } t_url = 'http://127.0.0.1:8774/v2/my_tenant_id/volumes' api.create_pod(self.context, pod_dict) api.create_pod_service_configuration(self.context, config_dict) b_url = 'http://127.0.0.1:8774/v2.1/my_tenant_id/volumes' b_endpoint = hclient.get_pod_service_endpoint(self.context, pod_dict['pod_name'], cons.ST_CINDER) self.assertEqual(b_endpoint, config_dict['service_url']) b_ctx = hclient.get_pod_service_ctx(self.context, t_url, pod_dict['pod_name'], cons.ST_CINDER) self.assertEqual(b_ctx['t_ver'], 'v2') self.assertEqual(b_ctx['t_url'], t_url) self.assertEqual(b_ctx['b_ver'], 'v2.1') self.assertEqual(b_ctx['b_url'], b_url) # wrong pod name b_ctx = hclient.get_pod_service_ctx(self.context, t_url, pod_dict['pod_name'] + '1', cons.ST_CINDER) self.assertEqual(b_ctx['t_ver'], 'v2') self.assertEqual(b_ctx['t_url'], t_url) self.assertEqual(b_ctx['b_ver'], '') self.assertEqual(b_ctx['b_url'], '') # wrong service_type b_ctx = hclient.get_pod_service_ctx(self.context, t_url, pod_dict['pod_name'], cons.ST_CINDER + '1') self.assertEqual(b_ctx['t_ver'], 'v2') self.assertEqual(b_ctx['t_url'], t_url) self.assertEqual(b_ctx['b_ver'], '') self.assertEqual(b_ctx['b_url'], '')
def _update_endpoint_from_keystone(self, cxt, is_internal): """Update the database by querying service endpoint url from Keystone :param cxt: context object :param is_internal: if True, this method utilizes pre-configured admin username and password to apply an new admin token, this happens only when auto_refresh_endpoint is set to True. if False, token in cxt is directly used, users should prepare admin token themselves :return: None """ if is_internal: admin_context = tricircle_context.Context() admin_context.auth_token = self._get_admin_token() endpoint_map = self._get_endpoint_from_keystone(admin_context) else: endpoint_map = self._get_endpoint_from_keystone(cxt) for region in endpoint_map: # use region name to query pod pod_filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': region}] pod_list = api.list_pods(cxt, pod_filters) # skip region/pod not registered in cascade service if len(pod_list) != 1: continue for service in endpoint_map[region]: pod_id = pod_list[0]['pod_id'] config_filters = [{'key': 'pod_id', 'comparator': 'eq', 'value': pod_id}, {'key': 'service_type', 'comparator': 'eq', 'value': service}] config_list = api.list_pod_service_configurations( cxt, config_filters) if len(config_list) > 1: continue if len(config_list) == 1: config_id = config_list[0]['service_id'] update_dict = { 'service_url': endpoint_map[region][service]} api.update_pod_service_configuration( cxt, config_id, update_dict) else: config_dict = { 'service_id': str(uuid.uuid4()), 'pod_id': pod_id, 'service_type': service, 'service_url': endpoint_map[region][service] } api.create_pod_service_configuration( cxt, config_dict)
def _init_db(self): core.initialize() core.ModelBase.metadata.create_all(core.get_engine()) # enforce foreign key constraint for sqlite core.get_engine().execute('pragma foreign_keys=on') self.context = context.Context() pod_dict = { 'pod_id': 'fake_pod_id', 'pod_name': 'fake_pod_name', 'az_name': FAKE_AZ } config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_NOVA, 'service_url': 'http://127.0.0.1:8774/v2/$(tenant_id)s' } pod_dict2 = { 'pod_id': 'fake_pod_id' + '2', 'pod_name': 'fake_pod_name' + '2', 'az_name': FAKE_AZ + '2' } config_dict2 = { 'service_id': 'fake_service_id' + '2', 'pod_id': 'fake_pod_id' + '2', 'service_type': cons.ST_CINDER, 'service_url': 'http://10.0.0.2:8774/v2/$(tenant_id)s' } top_pod = { 'pod_id': 'fake_top_pod_id', 'pod_name': 'RegionOne', 'az_name': '' } top_config = { 'service_id': 'fake_top_service_id', 'pod_id': 'fake_top_pod_id', 'service_type': cons.ST_CINDER, 'service_url': 'http://127.0.0.1:19998/v2/$(tenant_id)s' } db_api.create_pod(self.context, pod_dict) db_api.create_pod(self.context, pod_dict2) db_api.create_pod(self.context, top_pod) db_api.create_pod_service_configuration(self.context, config_dict) db_api.create_pod_service_configuration(self.context, config_dict2) db_api.create_pod_service_configuration(self.context, top_config)
def test_create(self): pod = {'pod_id': 'test_pod_uuid', 'pod_name': 'test_pod', 'pod_az_name': 'test_pod_az_name', 'dc_name': 'test_dc_name', 'az_name': 'test_az_uuid'} pod_ret = api.create_pod(self.context, pod) self.assertEqual(pod_ret, pod) configuration = { 'service_id': 'test_config_uuid', 'pod_id': 'test_pod_uuid', 'service_type': 'nova', 'service_url': 'http://test_url' } config_ret = api.create_pod_service_configuration(self.context, configuration) self.assertEqual(config_ret, configuration)
def setUp(self): core.initialize() core.ModelBase.metadata.create_all(core.get_engine()) # enforce foreign key constraint for sqlite core.get_engine().execute('pragma foreign_keys=on') self.context = context.Context() top_pod = { 'pod_id': FAKE_TOP_ID, 'pod_name': FAKE_TOP_NAME, 'az_name': '' } config_dict_top = { 'service_id': FAKE_TOP_SERVICE_ID, 'pod_id': FAKE_TOP_ID, 'service_type': FAKE_SERVICE_TYPE, 'service_url': FAKE_TOP_ENDPOINT } pod_dict = { 'pod_id': FAKE_SITE_ID, 'pod_name': FAKE_SITE_NAME, 'az_name': FAKE_AZ } pod_dict2 = { 'pod_id': FAKE_SITE_ID_2, 'pod_name': FAKE_SITE_NAME_2, 'az_name': FAKE_AZ } config_dict = { 'service_id': FAKE_SERVICE_ID, 'pod_id': FAKE_SITE_ID, 'service_type': FAKE_SERVICE_TYPE, 'service_url': FAKE_SERVICE_ENDPOINT } config_dict2 = { 'service_id': FAKE_SERVICE_ID_2, 'pod_id': FAKE_SITE_ID_2, 'service_type': FAKE_SERVICE_TYPE, 'service_url': FAKE_SERVICE_ENDPOINT_2 } api.create_pod(self.context, pod_dict) api.create_pod(self.context, pod_dict2) api.create_pod(self.context, top_pod) api.create_pod_service_configuration(self.context, config_dict) api.create_pod_service_configuration(self.context, config_dict2) api.create_pod_service_configuration(self.context, config_dict_top)
def _init_db(self): core.initialize() core.ModelBase.metadata.create_all(core.get_engine()) # enforce foreign key constraint for sqlite core.get_engine().execute('pragma foreign_keys=on') self.context = context.Context() pod_dict = { 'pod_id': 'fake_pod_id', 'pod_name': 'fake_pod_name', 'az_name': FAKE_AZ } config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_CINDER, 'service_url': 'http://127.0.0.1:8774/v2/$(tenant_id)s' } pod_dict2 = { 'pod_id': 'fake_pod_id' + '2', 'pod_name': 'fake_pod_name' + '2', 'az_name': FAKE_AZ + '2' } config_dict2 = { 'service_id': 'fake_service_id' + '2', 'pod_id': 'fake_pod_id' + '2', 'service_type': cons.ST_CINDER, 'service_url': 'http://10.0.0.2:8774/v2/$(tenant_id)s' } top_pod = { 'pod_id': 'fake_top_pod_id', 'pod_name': 'RegionOne', 'az_name': '' } top_config = { 'service_id': 'fake_top_service_id', 'pod_id': 'fake_top_pod_id', 'service_type': cons.ST_CINDER, 'service_url': 'http://127.0.0.1:19998/v2/$(tenant_id)s' } db_api.create_pod(self.context, pod_dict) db_api.create_pod(self.context, pod_dict2) db_api.create_pod(self.context, top_pod) db_api.create_pod_service_configuration(self.context, config_dict) db_api.create_pod_service_configuration(self.context, config_dict2) db_api.create_pod_service_configuration(self.context, top_config)