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 test_get_pod_and_endpoint_by_name(self): pod_dict = { 'pod_id': 'fake_pod_id', 'region_name': 'fake_region_name', 'az_name': 'fake_az' } api.create_pod(self.context, pod_dict) pod = api.get_pod_by_name(self.context, pod_dict['region_name'] + '1') self.assertIsNone(pod) pod = api.get_pod_by_name(self.context, pod_dict['region_name']) self.assertEqual(pod['pod_id'], pod_dict['pod_id']) self.assertEqual(pod['region_name'], pod_dict['region_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_NEUTRON, 'service_url': 'http://127.0.0.1:9696/v2.0/networks' } api.create_cached_endpoints(self.context, config_dict) endpoint = hclient.get_pod_service_endpoint( self.context, pod_dict['region_name'], config_dict['service_type']) self.assertEqual(endpoint, config_dict['service_url']) endpoint = hclient.get_pod_service_endpoint( self.context, 'x_region_name', config_dict['service_type']) self.assertEqual(endpoint, '')
def _basic_pod_route_setup(self): pod1 = {'pod_id': 'pod_id_1', 'region_name': 'pod_1', 'az_name': 'az_name_1'} pod2 = {'pod_id': 'pod_id_2', 'region_name': 'pod_2', 'az_name': 'az_name_2'} pod3 = {'pod_id': 'pod_id_0', 'region_name': 'top_pod', 'az_name': ''} for pod in (pod1, pod2, pod3): db_api.create_pod(self.context, pod) route1 = { 'top_id': 'top_id_1', 'pod_id': 'pod_id_1', 'bottom_id': 'bottom_id_1', 'resource_type': 'port'} route2 = { 'top_id': 'top_id_2', 'pod_id': 'pod_id_2', 'bottom_id': 'bottom_id_2', 'resource_type': 'port'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route1) core.create_resource(self.context, models.ResourceRouting, route2)
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, 'region_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_cached_endpoints(self.context, config_dict) global FAKE_RESOURCES FAKE_RESOURCES = [{'name': 'res1'}, {'name': 'res2'}] cfg.CONF.set_override(name='top_region_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.assertEqual(pod, None) 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 test_get_bottom_mappings_by_top_id(self): for i in xrange(3): pod = {'pod_id': 'test_pod_uuid_%d' % i, 'pod_name': 'test_pod_%d' % i, 'az_name': 'test_az_uuid_%d' % i} api.create_pod(self.context, pod) route1 = { 'top_id': 'top_uuid', 'pod_id': 'test_pod_uuid_0', 'resource_type': 'port'} route2 = { 'top_id': 'top_uuid', 'pod_id': 'test_pod_uuid_1', 'bottom_id': 'bottom_uuid_1', 'resource_type': 'port'} route3 = { 'top_id': 'top_uuid', 'pod_id': 'test_pod_uuid_2', 'bottom_id': 'bottom_uuid_2', 'resource_type': 'neutron'} routes = [route1, route2, route3] with self.context.session.begin(): for route in routes: core.create_resource( self.context, models.ResourceRouting, route) mappings = api.get_bottom_mappings_by_top_id(self.context, 'top_uuid', 'port') self.assertEqual('test_pod_uuid_1', mappings[0][0]['pod_id']) self.assertEqual('bottom_uuid_1', mappings[0][1])
def _create_pod(self, index, test_az_uuid): pod_body = {'pod_id': 'test_pod_uuid_%d' % index, 'region_name': 'test_pod_%d' % index, 'pod_az_name': 'test_pod_az_name_%d' % index, 'dc_name': 'test_dc_name_%d' % index, 'az_name': test_az_uuid, } api.create_pod(self.context, pod_body)
def test_delete(self): pod = {'pod_id': 'test_pod_uuid', 'pod_name': 'test_pod', 'az_name': 'test_az_uuid'} api.create_pod(self.context, pod) api.delete_pod(self.context, 'test_pod_uuid') self.assertRaises(exceptions.ResourceNotFound, api.get_pod, self.context, 'test_pod_uuid')
def _basic_pod_setup(self): pod1 = {'pod_id': 'pod_id_1', 'region_name': 'pod_1', 'az_name': 'az_name_1'} pod2 = {'pod_id': 'pod_id_2', 'region_name': 'pod_2', 'az_name': 'az_name_2'} pod3 = {'pod_id': 'pod_id_0', 'region_name': 'top_pod', 'az_name': ''} for pod in (pod1, pod2, pod3): db_api.create_pod(self.context, pod)
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 test_resource_routing_unique_key(self): pod = {'pod_id': 'test_pod1_uuid', 'pod_name': 'test_pod1', 'az_name': 'test_az1_uuid'} api.create_pod(self.context, pod) routing = {'top_id': 'top_uuid', 'pod_id': 'test_pod1_uuid', 'resource_type': 'port'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, routing) self.assertRaises(oslo_db.exception.DBDuplicateEntry, core.create_resource, self.context, models.ResourceRouting, routing)
def test_update(self): pod = {'pod_id': 'test_pod_uuid', 'pod_name': 'test_pod', 'az_name': 'test_az1_uuid'} api.create_pod(self.context, pod) update_dict = {'pod_id': 'fake_uuid', 'pod_name': 'test_pod2', 'az_name': 'test_az2_uuid'} ret = api.update_pod(self.context, 'test_pod_uuid', update_dict) # primary key value will not be updated self.assertEqual(ret['pod_id'], 'test_pod_uuid') self.assertEqual(ret['pod_name'], 'test_pod2') self.assertEqual(ret['az_name'], 'test_az2_uuid')
def test_get_res_routing_ref(self): t_url = 'http://127.0.0.1:9696/v2.0/networks' self.assertIsNone( hclient.get_res_routing_ref(self.context, 'fake_pod_id', t_url, s_type=cons.ST_NEUTRON)) pod_dict = { 'pod_id': 'fake_pod_id', 'region_name': 'fake_region_name', 'az_name': 'fake_az' } api.create_pod(self.context, pod_dict) routes = [ { 'top_id': 'top_id', 'bottom_id': 'bottom_id', 'pod_id': 'fake_pod_id', 'project_id': 'test_project_id', 'resource_type': 'network' }, ] with self.context.session.begin(): for route in routes: core.create_resource(self.context, models.ResourceRouting, route) config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_NEUTRON, 'service_url': 'http://127.0.0.1:9696/v2.0/networks' } api.create_cached_endpoints(self.context, config_dict) s_ctx = { 't_ver': 'v2.0', 'b_ver': 'v2.0', 't_url': t_url, 'b_url': t_url } self.assertEqual( s_ctx, hclient.get_res_routing_ref(self.context, 'top_id', t_url, s_type=cons.ST_NEUTRON))
def test_get_pod_service_ctx(self): pod_dict = { 'pod_id': 'fake_pod_id', 'region_name': 'fake_region_name', 'az_name': 'fake_az' } config_dict = { 'service_id': 'fake_service_id', 'pod_id': 'fake_pod_id', 'service_type': cons.ST_NEUTRON, 'service_url': 'http://127.0.0.1:9696/v2.0/networks' } t_url = 'http://127.0.0.1:9696/v2.0/networks' api.create_pod(self.context, pod_dict) api.create_cached_endpoints(self.context, config_dict) b_url = 'http://127.0.0.1:9696/v2.0/networks' b_endpoint = hclient.get_pod_service_endpoint(self.context, pod_dict['region_name'], cons.ST_NEUTRON) self.assertEqual(b_endpoint, config_dict['service_url']) b_ctx = hclient.get_pod_service_ctx(self.context, t_url, pod_dict['region_name'], cons.ST_NEUTRON) self.assertEqual(b_ctx['t_ver'], 'v2.0') self.assertEqual(b_ctx['t_url'], t_url) self.assertEqual(b_ctx['b_ver'], 'v2.0') self.assertEqual(b_ctx['b_url'], b_url) # wrong pod name b_ctx = hclient.get_pod_service_ctx(self.context, t_url, pod_dict['region_name'] + '1', cons.ST_NEUTRON) self.assertEqual(b_ctx['t_ver'], 'v2.0') 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['region_name'], cons.ST_NEUTRON + '1') self.assertEqual(b_ctx['t_ver'], 'v2.0') self.assertEqual(b_ctx['t_url'], t_url) self.assertEqual(b_ctx['b_ver'], '') self.assertEqual(b_ctx['b_url'], '')
def test_sort(self): pod1 = {'pod_id': 'test_pod1_uuid', 'pod_name': 'test_pod1', 'pod_az_name': 'test_pod_az_name1', 'dc_name': 'test_dc_name1', 'az_name': 'test_az1_uuid'} pod2 = {'pod_id': 'test_pod2_uuid', 'pod_name': 'test_pod2', 'pod_az_name': 'test_pod_az_name2', 'dc_name': 'test_dc_name1', 'az_name': 'test_az2_uuid'} pod3 = {'pod_id': 'test_pod3_uuid', 'pod_name': 'test_pod3', 'pod_az_name': 'test_pod_az_name3', 'dc_name': 'test_dc_name1', 'az_name': 'test_az3_uuid'} pods = [pod1, pod2, pod3] for pod in pods: api.create_pod(self.context, pod) pods = api.list_pods(self.context, sorts=[(models.Pod.pod_id, False)]) self.assertEqual(pods, [pod3, pod2, pod1])
def test_get_next_bottom_pod(self): next_pod = api.get_next_bottom_pod(self.context) self.assertIsNone(next_pod) pods = [] for i in xrange(5): pod = { "pod_id": "test_pod_uuid_%d" % i, "pod_name": "test_pod_%d" % i, "pod_az_name": "test_pod_az_name_%d" % i, "dc_name": "test_dc_name_%d" % i, "az_name": "test_az_uuid_%d" % i, } api.create_pod(self.context, pod) pods.append(pod) next_pod = api.get_next_bottom_pod(self.context) self.assertEqual(next_pod, pods[0]) next_pod = api.get_next_bottom_pod(self.context, current_pod_id="test_pod_uuid_2") self.assertEqual(next_pod, pods[3]) next_pod = api.get_next_bottom_pod(self.context, current_pod_id="test_pod_uuid_4") self.assertIsNone(next_pod)
def test_get_next_bottom_pod(self): next_pod = api.get_next_bottom_pod(self.context) self.assertIsNone(next_pod) pods = [] for i in xrange(5): pod = {'pod_id': 'test_pod_uuid_%d' % i, 'region_name': 'test_pod_%d' % i, 'pod_az_name': 'test_pod_az_name_%d' % i, 'dc_name': 'test_dc_name_%d' % i, 'az_name': 'test_az_uuid_%d' % i, } api.create_pod(self.context, pod) pods.append(pod) next_pod = api.get_next_bottom_pod(self.context) self.assertEqual(next_pod, pods[0]) next_pod = api.get_next_bottom_pod( self.context, current_pod_id='test_pod_uuid_2') self.assertEqual(next_pod, pods[3]) next_pod = api.get_next_bottom_pod( self.context, current_pod_id='test_pod_uuid_4') self.assertIsNone(next_pod)
def test_get_bottom_mappings_by_top_id(self): for i in xrange(3): pod = {"pod_id": "test_pod_uuid_%d" % i, "pod_name": "test_pod_%d" % i, "az_name": "test_az_uuid_%d" % i} api.create_pod(self.context, pod) route1 = {"top_id": "top_uuid", "pod_id": "test_pod_uuid_0", "resource_type": "port"} route2 = { "top_id": "top_uuid", "pod_id": "test_pod_uuid_1", "bottom_id": "bottom_uuid_1", "resource_type": "port", } route3 = { "top_id": "top_uuid", "pod_id": "test_pod_uuid_2", "bottom_id": "bottom_uuid_2", "resource_type": "neutron", } routes = [route1, route2, route3] with self.context.session.begin(): for route in routes: core.create_resource(self.context, models.ResourceRouting, route) mappings = api.get_bottom_mappings_by_top_id(self.context, "top_uuid", "port") self.assertEqual("test_pod_uuid_1", mappings[0][0]["pod_id"]) self.assertEqual("bottom_uuid_1", mappings[0][1])
def test_query(self): pod1 = {'pod_id': 'test_pod1_uuid', 'pod_name': 'test_pod1', 'pod_az_name': 'test_pod_az_name1', 'dc_name': 'test_dc_name1', 'az_name': 'test_az1_uuid'} pod2 = {'pod_id': 'test_pod2_uuid', 'pod_name': 'test_pod2', 'pod_az_name': 'test_pod_az_name2', 'dc_name': 'test_dc_name1', 'az_name': 'test_az2_uuid'} api.create_pod(self.context, pod1) api.create_pod(self.context, pod2) filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': 'test_pod2'}] pods = api.list_pods(self.context, filters) self.assertEqual(len(pods), 1) self.assertEqual(pods[0], pod2) filters = [{'key': 'pod_name', 'comparator': 'eq', 'value': 'test_pod3'}] pods = api.list_pods(self.context, filters) self.assertEqual(len(pods), 0)
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)
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_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 _prepare_pod(self, bottom_pod_num=1): t_pod = {'pod_id': 't_pod_uuid', 'pod_name': 't_region', 'az_name': ''} api.create_pod(self.context, t_pod) if bottom_pod_num == 1: b_pod = {'pod_id': 'b_pod_uuid', 'pod_name': 'b_region', 'az_name': 'b_az'} api.create_pod(self.context, b_pod) return t_pod, b_pod b_pods = [] for i in xrange(1, bottom_pod_num + 1): b_pod = {'pod_id': 'b_pod_%d_uuid' % i, 'pod_name': 'b_region_%d' % i, 'az_name': 'b_az_%d' % i} api.create_pod(self.context, b_pod) b_pods.append(b_pod) return t_pod, b_pods
def test_configure_extra_routes_ew_gw(self, router_update, subnet_update): for i in (1, 2): pod_dict = { 'pod_id': 'pod_id_%d' % i, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i } db_api.create_pod(self.context, pod_dict) for i in (1, 2, 3): router = {'id': 'top_router_%d_id' % i} TOP_ROUTER.append(router) # gateway in podX is attached to routerX gw_map = { 'net1_pod1_gw': '10.0.1.1', 'net2_pod2_gw': '10.0.2.1', 'net3_pod1_gw': '10.0.3.3', 'net3_pod2_gw': '10.0.3.4' } # interfaces are all attached to router3 inf_map = { 'net1_pod1_inf': '10.0.1.3', 'net2_pod2_inf': '10.0.2.3', 'net3_pod1_inf': '10.0.3.5', 'net3_pod2_inf': '10.0.3.6' } get_gw_map = lambda n_idx, p_idx: gw_map['net%d_pod%d_gw' % (n_idx, p_idx)] get_inf_map = lambda n_idx, p_idx: inf_map['net%d_pod%d_inf' % (n_idx, p_idx)] bridge_infos = [] for net_idx, router_idx, pod_idx in [(1, 1, 1), (3, 1, 1), (1, 3, 1), (3, 3, 1), (2, 2, 2), (3, 2, 2), (2, 3, 2), (3, 3, 2)]: region_name = 'pod_%d' % pod_idx pod_id = 'pod_id_%d' % pod_idx top_router_id = 'top_router_%d_id' % router_idx network = {'id': 'network_%d_id' % net_idx} router = {'id': 'router_%d_%d_id' % (pod_idx, router_idx)} subnet = { 'id': 'subnet_%d_id' % net_idx, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % net_idx, 'gateway_ip': get_gw_map(net_idx, pod_idx) } port = { 'network_id': network['id'], 'device_id': router['id'], 'device_owner': 'network:router_interface', 'fixed_ips': [{ 'subnet_id': subnet['id'] }] } if router_idx == 3: port['fixed_ips'][0]['ip_address'] = get_inf_map( net_idx, pod_idx) else: port['fixed_ips'][0]['ip_address'] = get_gw_map( net_idx, pod_idx) if net_idx == pod_idx and router_idx == 3: vm_idx = net_idx * 2 + pod_idx + 10 vm_ip = '10.0.%d.%d' % (net_idx, vm_idx) vm_port = { 'id': 'vm_port_%d_id' % vm_idx, 'network_id': network['id'], 'device_id': 'vm%d_id' % vm_idx, 'device_owner': 'compute:None', 'fixed_ips': [{ 'subnet_id': subnet['id'], 'ip_address': vm_ip }] } bridge_network = {'id': 'bridge_network_%d_id' % net_idx} bridge_subnet = { 'id': 'bridge_subnet_%d_id' % net_idx, 'network_id': bridge_network['id'], 'cidr': '100.0.1.0/24', 'gateway_ip': '100.0.1.1' } bridge_cidr = bridge_subnet['cidr'] bridge_port_ip = '%s.%d' % ( bridge_cidr[:bridge_cidr.rindex('.')], 2 + pod_idx) bridge_infos.append({ 'router_id': router['id'], 'bridge_ip': bridge_port_ip, 'vm_ip': vm_ip }) bridge_port = { 'network_id': bridge_network['id'], 'device_id': router['id'], 'device_owner': 'network:router_gateway', 'fixed_ips': [{ 'subnet_id': bridge_subnet['id'], 'ip_address': bridge_port_ip }] } RES_MAP[region_name]['port'].append(vm_port) RES_MAP[region_name]['network'].append(bridge_network) RES_MAP[region_name]['subnet'].append(bridge_subnet) RES_MAP[region_name]['port'].append(bridge_port) RES_MAP[region_name]['network'].append(network) RES_MAP[region_name]['subnet'].append(subnet) RES_MAP[region_name]['port'].append(port) RES_MAP[region_name]['router'].append(router) db_api.create_resource_mapping(self.context, top_router_id, router['id'], pod_id, 'project_id', constants.RT_ROUTER) # the above codes create this topology # pod1: net1 is attached to R1, default gateway is set on R1 # net1 is attached to R3 # net3 is attached to R1, default gateway is set on R1 # net3 is attached to R3 # pod2: net2 is attached to R2, default gateway is set on R2 # net2 is attached to R3 # net3 is attached to R2, default gateway is set on R2 # net3 is attached to R3 target_router_id = 'top_router_3_id' project_id = uuidutils.generate_uuid() db_api.new_job(self.context, project_id, constants.JT_CONFIGURE_ROUTE, target_router_id) self.xmanager.configure_route( self.context, payload={constants.JT_CONFIGURE_ROUTE: target_router_id}) # for the following paths, packets will go to R3 via the interface # which is attached to R3 # net1 in pod1 -> net2 in pod2 # net2 in pod2 -> net1 in pod1 # net3 in pod1 -> net2 in pod2 # net3 in pod2 -> net1 in pod1 expect_calls = [ mock.call( self.context, 'subnet_1_id', { 'subnet': { 'host_routes': [{ 'nexthop': get_inf_map(1, 1), 'destination': '10.0.2.0/24' }] } }), mock.call( self.context, 'subnet_2_id', { 'subnet': { 'host_routes': [{ 'nexthop': get_inf_map(2, 2), 'destination': '10.0.1.0/24' }] } }), mock.call( self.context, 'subnet_3_id', { 'subnet': { 'host_routes': [{ 'nexthop': get_inf_map(3, 1), 'destination': '10.0.2.0/24' }] } }), mock.call( self.context, 'subnet_3_id', { 'subnet': { 'host_routes': [{ 'nexthop': get_inf_map(3, 2), 'destination': '10.0.1.0/24' }] } }) ] subnet_update.assert_has_calls(expect_calls, any_order=True) expect_calls = [] for i in (0, 1): bridge_info = bridge_infos[i] expect_call = mock.call( self.context, bridge_infos[1 - i]['router_id'], { 'router': { 'routes': [{ 'nexthop': bridge_info['bridge_ip'], 'destination': bridge_info['vm_ip'] + '/32' }] } }) expect_calls.append(expect_call) router_update.assert_has_calls(expect_calls, any_order=True)
def test_setup_shadow_ports(self, mock_setup): project_id = uuidutils.generate_uuid() net1_id = uuidutils.generate_uuid() subnet1_id = uuidutils.generate_uuid() port1_id = uuidutils.generate_uuid() port2_id = uuidutils.generate_uuid() for i in (1, 2): pod_id = 'pod_id_%d' % i pod_dict = { 'pod_id': pod_id, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i } db_api.create_pod(self.context, pod_dict) db_api.create_resource_mapping(self.context, net1_id, net1_id, pod_id, project_id, constants.RT_NETWORK) TOP_NETWORK.append({'id': net1_id, 'tenant_id': project_id}) BOTTOM1_PORT.append({ 'id': port1_id, 'network_id': net1_id, 'device_owner': 'compute:None', 'binding:vif_type': 'ovs', 'binding:host_id': 'host1', 'mac_address': 'fa:16:3e:d4:01:03', 'fixed_ips': [{ 'subnet_id': subnet1_id, 'ip_address': '10.0.1.3' }] }) BOTTOM2_PORT.append({ 'id': port2_id, 'network_id': net1_id, 'device_owner': 'compute:None', 'binding:vif_type': 'ovs', 'binding:host_id': 'host2', 'mac_address': 'fa:16:3e:d4:01:03', 'fixed_ips': [{ 'subnet_id': subnet1_id, 'ip_address': '10.0.1.4' }] }) db_api.ensure_agent_exists(self.context, 'pod_id_1', 'host1', q_constants.AGENT_TYPE_OVS, '192.168.1.101') db_api.ensure_agent_exists(self.context, 'pod_id_2', 'host2', q_constants.AGENT_TYPE_OVS, '192.168.1.102') resource_id = 'pod_id_1#' + net1_id db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) # check shadow port in pod1 is created and updated client1 = FakeClient('pod_1') sd_ports = client1.list_ports( self.context, [{ 'key': 'device_owner', 'comparator': 'eq', 'value': constants.DEVICE_OWNER_SHADOW }]) self.assertEqual(sd_ports[0]['fixed_ips'][0]['ip_address'], '10.0.1.4') self.assertIn(constants.PROFILE_FORCE_UP, sd_ports[0]['binding:profile']) # check job to setup shadow ports for pod2 is registered mock_setup.assert_called_once_with(self.context, project_id, 'pod_id_2', net1_id) # update shadow port to down and test again, this is possible when we # succeed to create shadow port but fail to update it to active profile = sd_ports[0]['binding:profile'] profile.pop(constants.PROFILE_FORCE_UP) client1.update_ports( self.context, sd_ports[0]['id'], { 'port': { 'status': q_constants.PORT_STATUS_DOWN, 'binding:profile': profile } }) db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) # check shadow port is udpated to active again sd_port = client1.get_ports(self.context, sd_ports[0]['id']) self.assertIn(constants.PROFILE_FORCE_UP, sd_port['binding:profile']) # manually trigger shadow ports setup in pod2 resource_id = 'pod_id_2#' + net1_id db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) client2 = FakeClient('pod_2') sd_ports = client2.list_ports( self.context, [{ 'key': 'device_owner', 'comparator': 'eq', 'value': constants.DEVICE_OWNER_SHADOW }]) self.assertEqual(sd_ports[0]['fixed_ips'][0]['ip_address'], '10.0.1.3')
def test_get_bottom_mappings_by_tenant_pod(self): for i in xrange(3): pod = {'pod_id': 'test_pod_uuid_%d' % i, 'region_name': 'test_pod_%d' % i, 'az_name': 'test_az_uuid_%d' % i} api.create_pod(self.context, pod) routes = [ { 'route': { 'top_id': 'top_uuid', 'pod_id': 'test_pod_uuid_0', 'project_id': 'test_project_uuid_0', 'resource_type': 'port' }, }, { 'route': { 'top_id': 'top_uuid_0', 'bottom_id': 'top_uuid_0', 'pod_id': 'test_pod_uuid_0', 'project_id': 'test_project_uuid_0', 'resource_type': 'port' }, }, { 'route': { 'top_id': 'top_uuid_1', 'bottom_id': 'top_uuid_1', 'pod_id': 'test_pod_uuid_0', 'project_id': 'test_project_uuid_0', 'resource_type': 'port' }, }, { 'route': { 'top_id': 'top_uuid_2', 'bottom_id': 'top_uuid_2', 'pod_id': 'test_pod_uuid_0', 'project_id': 'test_project_uuid_1', 'resource_type': 'port' }, }, { 'route': { 'top_id': 'top_uuid_3', 'bottom_id': 'top_uuid_3', 'pod_id': 'test_pod_uuid_1', 'project_id': 'test_project_uuid_1', 'resource_type': 'port' }, } ] with self.context.session.begin(): for route in routes: core.create_resource( self.context, models.ResourceRouting, route['route']) routings = api.get_bottom_mappings_by_tenant_pod( self.context, 'test_project_uuid_0', 'test_pod_uuid_0', 'port' ) self.assertEqual(len(routings), 2) self.assertEqual(routings['top_uuid_0']['top_id'], 'top_uuid_0') self.assertEqual(routings['top_uuid_1']['top_id'], 'top_uuid_1') routings = api.get_bottom_mappings_by_tenant_pod( self.context, 'test_project_uuid_1', 'test_pod_uuid_0', 'port' ) self.assertEqual(len(routings), 1) self.assertEqual(routings['top_uuid_2']['top_id'], 'top_uuid_2') self.assertEqual(routings['top_uuid_2']['bottom_id'], 'top_uuid_2') routings = api.get_bottom_mappings_by_tenant_pod( self.context, 'test_project_uuid_1', 'test_pod_uuid_1', 'port' ) self.assertEqual(len(routings), 1) self.assertEqual(routings['top_uuid_3']['top_id'], 'top_uuid_3') self.assertEqual(routings['top_uuid_3']['bottom_id'], 'top_uuid_3')
def _prepare_pod(self): return api.create_pod(self.t_ctx, { 'pod_id': 'pod_id_1', 'region_name': 'pod_1', 'az_name': 'az_name_1' })
def _prepare_east_west_network_test(self, top_router_id): bridge_infos = [] router = {'id': top_router_id} TOP_ROUTER.append(router) for i in xrange(1, 3): pod_dict = {'pod_id': 'pod_id_%d' % i, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) network = {'id': 'network_%d_id' % i} bridge_network = {'id': 'bridge_network_%d_id' % i} router = {'id': 'router_%d_id' % i} subnet = { 'id': 'subnet_%d_id' % i, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % i, 'gateway_ip': '10.0.%d.1' % i, } bridge_subnet = { 'id': 'bridge_subnet_%d_id' % i, 'network_id': bridge_network['id'], 'cidr': '100.0.1.0/24', 'gateway_ip': '100.0.1.1', } port = { 'network_id': network['id'], 'device_id': router['id'], 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': subnet['gateway_ip']}] } vm_port = { 'id': 'vm_port_%d_id' % i, 'network_id': network['id'], 'device_id': 'vm%d_id' % i, 'device_owner': 'compute:None', 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': '10.0.%d.3' % i}] } bridge_cidr = bridge_subnet['cidr'] bridge_port_ip = '%s.%d' % (bridge_cidr[:bridge_cidr.rindex('.')], 2 + i) bridge_port = { 'network_id': bridge_network['id'], 'device_id': router['id'], 'device_owner': 'network:router_gateway', 'fixed_ips': [{'subnet_id': bridge_subnet['id'], 'ip_address': bridge_port_ip}] } region_name = 'pod_%d' % i RES_MAP[region_name]['network'].append(network) RES_MAP[region_name]['network'].append(bridge_network) RES_MAP[region_name]['subnet'].append(subnet) RES_MAP[region_name]['subnet'].append(bridge_subnet) RES_MAP[region_name]['port'].append(port) RES_MAP[region_name]['port'].append(vm_port) RES_MAP[region_name]['port'].append(bridge_port) RES_MAP[region_name]['router'].append(router) route = {'top_id': top_router_id, 'bottom_id': router['id'], 'pod_id': pod_dict['pod_id'], 'resource_type': 'router'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route) bridge_info = { 'router_id': router['id'], 'bridge_ip': bridge_port['fixed_ips'][0]['ip_address'], 'vm_ips': ['10.0.%d.3' % i]} bridge_infos.append(bridge_info) BOTTOM1_NETWORK.append({'id': 'network_3_id'}) BOTTOM1_SUBNET.append({'id': 'subnet_3_id', 'network_id': 'network_3_id', 'cidr': '10.0.3.0/24', 'gateway_ip': '10.0.3.1'}) BOTTOM1_PORT.append({'network_id': 'network_3_id', 'device_id': 'router_1_id', 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': 'subnet_3_id', 'ip_address': '10.0.3.1'}]}) BOTTOM1_PORT.append({'network_id': 'network_3_id', 'device_id': 'vm3_id', 'device_owner': 'compute:None', 'fixed_ips': [{'subnet_id': 'subnet_3_id', 'ip_address': '10.0.3.3'}]}) bridge_infos[0]['vm_ips'].append('10.0.3.3') return bridge_infos
def test_configure_security_group_rules(self, mock_create, mock_delete): project_id = uuidutils.generate_uuid() sg_id = uuidutils.generate_uuid() sg_rule_id_1 = uuidutils.generate_uuid() sg_rule_id_2 = uuidutils.generate_uuid() sg = {'id': sg_id, 'tenant_id': project_id, 'name': 'default', 'security_group_rules': [{ 'id': sg_rule_id_1, 'remote_group_id': sg_id, 'direction': 'ingress', 'remote_ip_prefix': None, 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}, {'id': sg_rule_id_2, 'remote_group_id': None, 'direction': 'engress', 'remote_ip_prefix': None, 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]} RES_MAP['top']['security_group'].append(sg) for i in xrange(1, 3): pod_dict = {'pod_id': 'pod_id_%d' % i, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) network = {'id': 'network_%d_id' % i, 'tenant_id': project_id} subnet = {'id': 'subnet_%d_id' % i, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % i, 'gateway_ip': '10.0.%d.1' % i, 'tenant_id': project_id} RES_MAP['top']['network'].append(network) RES_MAP['top']['subnet'].append(subnet) region_name = 'pod_%d' % i RES_MAP[region_name]['security_group'].append(sg) route = {'top_id': sg_id, 'bottom_id': sg_id, 'pod_id': pod_dict['pod_id'], 'resource_type': 'security_group'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route) db_api.new_job(self.context, constants.JT_SEG_RULE_SETUP, project_id) self.xmanager.configure_security_group_rules( self.context, payload={constants.JT_SEG_RULE_SETUP: project_id}) calls = [mock.call(self.context, sg_rule_id_1)] mock_delete.assert_has_calls(calls) call_rules_id = [ call_arg[0][1] for call_arg in mock_delete.call_args_list] # bottom security group already has sg_rule_id_2, so this rule will # not be deleted self.assertNotIn(sg_rule_id_2, call_rules_id) calls = [mock.call(self.context, {'security_group_rules': [ {'remote_group_id': None, 'direction': 'ingress', 'remote_ip_prefix': '10.0.1.0/24', 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}, {'remote_group_id': None, 'direction': 'ingress', 'remote_ip_prefix': '10.0.2.0/24', 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]})] mock_create.assert_has_calls(calls)
def test_configure_security_group_rules_duplicated_cidr(self, mock_create, mock_delete): project_id = uuidutils.generate_uuid() sg_id = uuidutils.generate_uuid() sg_rule_id_1 = uuidutils.generate_uuid() sg_rule_id_2 = uuidutils.generate_uuid() sg = {'id': sg_id, 'tenant_id': project_id, 'name': 'default', 'security_group_rules': [{ 'id': sg_rule_id_1, 'remote_group_id': sg_id, 'direction': 'ingress', 'remote_ip_prefix': None, 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}, {'id': sg_rule_id_2, 'remote_group_id': None, 'direction': 'engress', 'remote_ip_prefix': None, 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]} RES_MAP['top']['security_group'].append(sg) for i in xrange(1, 3): pod_dict = {'pod_id': 'pod_id_%d' % i, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) network = {'id': 'network_%d_id' % i, 'tenant_id': project_id} # we create two subnets with identical cidr but different # allocation pools subnet = {'id': 'subnet_%d_id' % i, 'network_id': network['id'], 'cidr': '10.0.1.0/24', 'gateway_ip': '10.0.1.%d' % i, 'tenant_id': project_id, 'allocation_pools': {'start': '10.0.1.%d' % 10 * i, 'end': '10.0.1.%d' % (10 * i + 9)}, 'ip_version': q_constants.IP_VERSION_4} RES_MAP['top']['network'].append(network) RES_MAP['top']['subnet'].append(subnet) region_name = 'pod_%d' % i RES_MAP[region_name]['security_group'].append(sg) route = {'top_id': sg_id, 'bottom_id': sg_id, 'pod_id': pod_dict['pod_id'], 'resource_type': 'security_group'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route) db_api.new_job(self.context, project_id, constants.JT_SEG_RULE_SETUP, project_id) self.xmanager.configure_security_group_rules( self.context, payload={constants.JT_SEG_RULE_SETUP: project_id}) calls = [mock.call(self.context, sg_rule_id_1)] mock_delete.assert_has_calls(calls) call_rules_id = [ call_arg[0][1] for call_arg in mock_delete.call_args_list] # bottom security group already has sg_rule_id_2, so this rule will # not be deleted self.assertNotIn(sg_rule_id_2, call_rules_id) calls = [mock.call(self.context, {'security_group_rules': [ {'remote_group_id': None, 'direction': 'ingress', 'remote_ip_prefix': '10.0.1.0/24', 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]})] mock_create.assert_has_calls(calls)
def test_configure_extra_routes(self, mock_update): top_router_id = 'router_id' for i in xrange(1, 3): pod_dict = {'pod_id': 'pod_id_%d' % i, 'pod_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) network = {'id': 'network_%d_id' % i} bridge_network = {'id': 'bridge_network_%d_id' % i} router = {'id': 'router_%d_id' % i} subnet = { 'id': 'subnet_%d_id' % i, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % i, 'gateway_ip': '10.0.%d.1' % i, } bridge_subnet = { 'id': 'bridge_subnet_%d_id' % i, 'network_id': bridge_network['id'], 'cidr': '100.0.1.0/24', 'gateway_ip': '100.0.1.%d' % i, } port = { 'network_id': network['id'], 'device_id': router['id'], 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': subnet['gateway_ip']}] } bridge_port = { 'network_id': bridge_network['id'], 'device_id': router['id'], 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': bridge_subnet['id'], 'ip_address': bridge_subnet['gateway_ip']}] } pod_name = 'pod_%d' % i RES_MAP[pod_name]['network'].append(network) RES_MAP[pod_name]['network'].append(bridge_network) RES_MAP[pod_name]['subnet'].append(subnet) RES_MAP[pod_name]['subnet'].append(bridge_subnet) RES_MAP[pod_name]['port'].append(port) RES_MAP[pod_name]['port'].append(bridge_port) RES_MAP[pod_name]['router'].append(router) route = {'top_id': top_router_id, 'bottom_id': router['id'], 'pod_id': pod_dict['pod_id'], 'resource_type': 'router'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route) BOTTOM1_NETWORK.append({'id': 'network_3_id'}) BOTTOM1_SUBNET.append({'id': 'subnet_3_id', 'network_id': 'network_3_id', 'cidr': '10.0.3.0/24', 'gateway_ip': '10.0.3.1'}) BOTTOM1_PORT.append({'network_id': 'network_3_id', 'device_id': 'router_1_id', 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': 'subnet_3_id', 'ip_address': '10.0.3.1'}]}) self.xmanager.configure_extra_routes(self.context, payload={'router': top_router_id}) calls = [mock.call(self.context, 'router_1_id', {'router': { 'routes': [{'nexthop': '100.0.1.2', 'destination': '10.0.2.0/24'}]}}), mock.call(self.context, 'router_2_id', {'router': { 'routes': [{'nexthop': '100.0.1.1', 'destination': '10.0.1.0/24'}, {'nexthop': '100.0.1.1', 'destination': '10.0.3.0/24'}]}})] mock_update.assert_has_calls(calls)
def test_get_bottom_mappings_by_tenant_pod(self): for i in xrange(3): pod = {"pod_id": "test_pod_uuid_%d" % i, "pod_name": "test_pod_%d" % i, "az_name": "test_az_uuid_%d" % i} api.create_pod(self.context, pod) routes = [ { "route": { "top_id": "top_uuid", "pod_id": "test_pod_uuid_0", "project_id": "test_project_uuid_0", "resource_type": "port", } }, { "route": { "top_id": "top_uuid_0", "bottom_id": "top_uuid_0", "pod_id": "test_pod_uuid_0", "project_id": "test_project_uuid_0", "resource_type": "port", } }, { "route": { "top_id": "top_uuid_1", "bottom_id": "top_uuid_1", "pod_id": "test_pod_uuid_0", "project_id": "test_project_uuid_0", "resource_type": "port", } }, { "route": { "top_id": "top_uuid_2", "bottom_id": "top_uuid_2", "pod_id": "test_pod_uuid_0", "project_id": "test_project_uuid_1", "resource_type": "port", } }, { "route": { "top_id": "top_uuid_3", "bottom_id": "top_uuid_3", "pod_id": "test_pod_uuid_1", "project_id": "test_project_uuid_1", "resource_type": "port", } }, ] with self.context.session.begin(): for route in routes: core.create_resource(self.context, models.ResourceRouting, route["route"]) routings = api.get_bottom_mappings_by_tenant_pod(self.context, "test_project_uuid_0", "test_pod_uuid_0", "port") self.assertEqual(len(routings), 2) self.assertEqual(routings["top_uuid_0"]["top_id"], "top_uuid_0") self.assertEqual(routings["top_uuid_1"]["top_id"], "top_uuid_1") routings = api.get_bottom_mappings_by_tenant_pod(self.context, "test_project_uuid_1", "test_pod_uuid_0", "port") self.assertEqual(len(routings), 1) self.assertEqual(routings["top_uuid_2"]["top_id"], "top_uuid_2") self.assertEqual(routings["top_uuid_2"]["bottom_id"], "top_uuid_2") routings = api.get_bottom_mappings_by_tenant_pod(self.context, "test_project_uuid_1", "test_pod_uuid_1", "port") self.assertEqual(len(routings), 1) self.assertEqual(routings["top_uuid_3"]["top_id"], "top_uuid_3") self.assertEqual(routings["top_uuid_3"]["bottom_id"], "top_uuid_3")
def test_configure_extra_routes_ew_gw(self, router_update, subnet_update): for i in (1, 2): pod_dict = {'pod_id': 'pod_id_%d' % i, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) for i in (1, 2, 3): router = {'id': 'top_router_%d_id' % i} TOP_ROUTER.append(router) # gateway in podX is attached to routerX gw_map = {'net1_pod1_gw': '10.0.1.1', 'net2_pod2_gw': '10.0.2.1', 'net3_pod1_gw': '10.0.3.3', 'net3_pod2_gw': '10.0.3.4'} # interfaces are all attached to router3 inf_map = {'net1_pod1_inf': '10.0.1.3', 'net2_pod2_inf': '10.0.2.3', 'net3_pod1_inf': '10.0.3.5', 'net3_pod2_inf': '10.0.3.6'} get_gw_map = lambda n_idx, p_idx: gw_map[ 'net%d_pod%d_gw' % (n_idx, p_idx)] get_inf_map = lambda n_idx, p_idx: inf_map[ 'net%d_pod%d_inf' % (n_idx, p_idx)] bridge_infos = [] for net_idx, router_idx, pod_idx in [(1, 1, 1), (3, 1, 1), (1, 3, 1), (3, 3, 1), (2, 2, 2), (3, 2, 2), (2, 3, 2), (3, 3, 2)]: region_name = 'pod_%d' % pod_idx pod_id = 'pod_id_%d' % pod_idx top_router_id = 'top_router_%d_id' % router_idx network = {'id': 'network_%d_id' % net_idx} router = {'id': 'router_%d_%d_id' % (pod_idx, router_idx)} subnet = {'id': 'subnet_%d_id' % net_idx, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % net_idx, 'gateway_ip': get_gw_map(net_idx, pod_idx)} port = {'network_id': network['id'], 'device_id': router['id'], 'device_owner': 'network:router_interface', 'fixed_ips': [{'subnet_id': subnet['id']}]} if router_idx == 3: port['fixed_ips'][0][ 'ip_address'] = get_inf_map(net_idx, pod_idx) else: port['fixed_ips'][0][ 'ip_address'] = get_gw_map(net_idx, pod_idx) if net_idx == pod_idx and router_idx == 3: vm_idx = net_idx * 2 + pod_idx + 10 vm_ip = '10.0.%d.%d' % (net_idx, vm_idx) vm_port = {'id': 'vm_port_%d_id' % vm_idx, 'network_id': network['id'], 'device_id': 'vm%d_id' % vm_idx, 'device_owner': 'compute:None', 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': vm_ip}]} bridge_network = {'id': 'bridge_network_%d_id' % net_idx} bridge_subnet = {'id': 'bridge_subnet_%d_id' % net_idx, 'network_id': bridge_network['id'], 'cidr': '100.0.1.0/24', 'gateway_ip': '100.0.1.1'} bridge_cidr = bridge_subnet['cidr'] bridge_port_ip = '%s.%d' % ( bridge_cidr[:bridge_cidr.rindex('.')], 2 + pod_idx) bridge_infos.append({'router_id': router['id'], 'bridge_ip': bridge_port_ip, 'vm_ip': vm_ip}) bridge_port = { 'network_id': bridge_network['id'], 'device_id': router['id'], 'device_owner': 'network:router_gateway', 'fixed_ips': [{'subnet_id': bridge_subnet['id'], 'ip_address': bridge_port_ip}] } RES_MAP[region_name]['port'].append(vm_port) RES_MAP[region_name]['network'].append(bridge_network) RES_MAP[region_name]['subnet'].append(bridge_subnet) RES_MAP[region_name]['port'].append(bridge_port) RES_MAP[region_name]['network'].append(network) RES_MAP[region_name]['subnet'].append(subnet) RES_MAP[region_name]['port'].append(port) RES_MAP[region_name]['router'].append(router) db_api.create_resource_mapping(self.context, top_router_id, router['id'], pod_id, 'project_id', constants.RT_ROUTER) # the above codes create this topology # pod1: net1 is attached to R1, default gateway is set on R1 # net1 is attached to R3 # net3 is attached to R1, default gateway is set on R1 # net3 is attached to R3 # pod2: net2 is attached to R2, default gateway is set on R2 # net2 is attached to R3 # net3 is attached to R2, default gateway is set on R2 # net3 is attached to R3 target_router_id = 'top_router_3_id' project_id = uuidutils.generate_uuid() db_api.new_job(self.context, project_id, constants.JT_CONFIGURE_ROUTE, target_router_id) self.xmanager.configure_route( self.context, payload={constants.JT_CONFIGURE_ROUTE: target_router_id}) # for the following paths, packets will go to R3 via the interface # which is attached to R3 # net1 in pod1 -> net2 in pod2 # net2 in pod2 -> net1 in pod1 # net3 in pod1 -> net2 in pod2 # net3 in pod2 -> net1 in pod1 expect_calls = [ mock.call(self.context, 'subnet_1_id', {'subnet': { 'host_routes': [{'nexthop': get_inf_map(1, 1), 'destination': '10.0.2.0/24'}]}}), mock.call(self.context, 'subnet_2_id', {'subnet': { 'host_routes': [{'nexthop': get_inf_map(2, 2), 'destination': '10.0.1.0/24'}]}}), mock.call(self.context, 'subnet_3_id', {'subnet': { 'host_routes': [{'nexthop': get_inf_map(3, 1), 'destination': '10.0.2.0/24'}]}}), mock.call(self.context, 'subnet_3_id', {'subnet': { 'host_routes': [{'nexthop': get_inf_map(3, 2), 'destination': '10.0.1.0/24'}]}})] subnet_update.assert_has_calls(expect_calls, any_order=True) expect_calls = [] for i in (0, 1): bridge_info = bridge_infos[i] expect_call = mock.call( self.context, bridge_infos[1 - i]['router_id'], {'router': {'routes': [ {'nexthop': bridge_info['bridge_ip'], 'destination': bridge_info['vm_ip'] + '/32'}]}}) expect_calls.append(expect_call) router_update.assert_has_calls(expect_calls, any_order=True)
def _prepare_pod(self): return api.create_pod(self.t_ctx, {'pod_id': 'pod_id_1', 'region_name': 'pod_1', 'az_name': 'az_name_1'})
def test_configure_security_group_rules(self, mock_create, mock_delete): project_id = uuidutils.generate_uuid() sg_id = uuidutils.generate_uuid() sg_rule_id = uuidutils.generate_uuid() sg = {'id': sg_id, 'tenant_id': project_id, 'name': 'default', 'security_group_rules': [{ 'id': sg_rule_id, 'remote_group_id': sg_id, 'direction': 'ingress', 'remote_ip_prefix': None, 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]} RES_MAP['top']['security_group'].append(sg) for i in xrange(1, 3): pod_dict = {'pod_id': 'pod_id_%d' % i, 'pod_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) network = {'id': 'network_%d_id' % i, 'tenant_id': project_id} subnet = {'id': 'subnet_%d_id' % i, 'network_id': network['id'], 'cidr': '10.0.%d.0/24' % i, 'gateway_ip': '10.0.%d.1' % i, 'tenant_id': project_id} RES_MAP['top']['network'].append(network) RES_MAP['top']['subnet'].append(subnet) pod_name = 'pod_%d' % i RES_MAP[pod_name]['security_group'].append(sg) route = {'top_id': sg_id, 'bottom_id': sg_id, 'pod_id': pod_dict['pod_id'], 'resource_type': 'security_group'} with self.context.session.begin(): core.create_resource(self.context, models.ResourceRouting, route) self.xmanager.configure_security_group_rules( self.context, payload={'seg_rule_setup': project_id}) calls = [mock.call(self.context, sg_rule_id)] mock_delete.assert_has_calls(calls) calls = [mock.call(self.context, {'security_group_rules': [ {'remote_group_id': None, 'direction': 'ingress', 'remote_ip_prefix': '10.0.1.0/24', 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}, {'remote_group_id': None, 'direction': 'ingress', 'remote_ip_prefix': '10.0.2.0/24', 'protocol': None, 'ethertype': 'IPv4', 'port_range_max': -1, 'port_range_min': -1, 'security_group_id': sg_id}]})] mock_create.assert_has_calls(calls)
def test_setup_shadow_ports(self, mock_setup): project_id = uuidutils.generate_uuid() net1_id = uuidutils.generate_uuid() subnet1_id = uuidutils.generate_uuid() port1_id = uuidutils.generate_uuid() port2_id = uuidutils.generate_uuid() for i in (1, 2): pod_id = 'pod_id_%d' % i pod_dict = {'pod_id': pod_id, 'region_name': 'pod_%d' % i, 'az_name': 'az_name_%d' % i} db_api.create_pod(self.context, pod_dict) db_api.create_resource_mapping( self.context, net1_id, net1_id, pod_id, project_id, constants.RT_NETWORK) TOP_NETWORK.append({'id': net1_id, 'tenant_id': project_id}) BOTTOM1_PORT.append({'id': port1_id, 'network_id': net1_id, 'device_owner': 'compute:None', 'binding:vif_type': 'ovs', 'binding:host_id': 'host1', 'device_id': None, 'mac_address': 'fa:16:3e:d4:01:03', 'fixed_ips': [{'subnet_id': subnet1_id, 'ip_address': '10.0.1.3'}]}) BOTTOM2_PORT.append({'id': port2_id, 'network_id': net1_id, 'device_owner': 'compute:None', 'binding:vif_type': 'ovs', 'binding:host_id': 'host2', 'device_id': None, 'mac_address': 'fa:16:3e:d4:01:03', 'fixed_ips': [{'subnet_id': subnet1_id, 'ip_address': '10.0.1.4'}]}) db_api.ensure_agent_exists( self.context, 'pod_id_1', 'host1', q_constants.AGENT_TYPE_OVS, '192.168.1.101') db_api.ensure_agent_exists( self.context, 'pod_id_2', 'host2', q_constants.AGENT_TYPE_OVS, '192.168.1.102') resource_id = 'pod_id_1#' + net1_id db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) # check shadow port in pod1 is created and updated client1 = FakeClient('pod_1') sd_ports = client1.list_ports( self.context, [{'key': 'device_owner', 'comparator': 'eq', 'value': constants.DEVICE_OWNER_SHADOW}]) self.assertEqual(sd_ports[0]['fixed_ips'][0]['ip_address'], '10.0.1.4') self.assertIn(constants.PROFILE_FORCE_UP, sd_ports[0]['binding:profile']) # check job to setup shadow ports for pod2 is registered mock_setup.assert_called_once_with(self.context, project_id, 'pod_id_2', net1_id) # update shadow port to down and test again, this is possible when we # succeed to create shadow port but fail to update it to active profile = sd_ports[0]['binding:profile'] profile.pop(constants.PROFILE_FORCE_UP) client1.update_ports(self.context, sd_ports[0]['id'], {'port': {'status': q_constants.PORT_STATUS_DOWN, 'binding:profile': profile}}) db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) # check shadow port is udpated to active again sd_port = client1.get_ports(self.context, sd_ports[0]['id']) self.assertIn(constants.PROFILE_FORCE_UP, sd_port['binding:profile']) # manually trigger shadow ports setup in pod2 resource_id = 'pod_id_2#' + net1_id db_api.new_job(self.context, project_id, constants.JT_SHADOW_PORT_SETUP, resource_id) self.xmanager.setup_shadow_ports( self.context, payload={constants.JT_SHADOW_PORT_SETUP: resource_id}) client2 = FakeClient('pod_2') sd_ports = client2.list_ports( self.context, [{'key': 'device_owner', 'comparator': 'eq', 'value': constants.DEVICE_OWNER_SHADOW}]) self.assertEqual(sd_ports[0]['fixed_ips'][0]['ip_address'], '10.0.1.3')