def create_shadow_agent_if_needed(t_ctx, profile, pod): if t_constants.PROFILE_HOST not in profile: return agent_host = profile[t_constants.PROFILE_HOST] agent_type = profile[t_constants.PROFILE_AGENT_TYPE] agent_tunnel = profile[t_constants.PROFILE_TUNNEL_IP] db_api.ensure_agent_exists(t_ctx, pod['pod_id'], agent_host, agent_type, agent_tunnel)
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')
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')