'Helper function to test get_instance_nw_info.
:param network_cache - data already in the nova network cache.
:param current_neutron_ports - updated list of ports from neutron.
:param networks - networks of ports being added to instance.
:param port_ids - new ports being added to instance.'

def _fake_get_instance_nw_info_helper(self, network_cache, current_neutron_ports, networks=None, port_ids=None):

    original_port_ids = copy.copy(port_ids)
    original_networks = copy.copy(networks)
    api = neutronapi.API()
    self.mox.StubOutWithMock(api.db, 'instance_info_cache_update')
    api.db.instance_info_cache_update(mox.IgnoreArg(), self.instance['uuid'], mox.IgnoreArg()).AndReturn(fake_info_cache)
    neutronapi.get_client(mox.IgnoreArg(), admin=True).MultipleTimes().AndReturn(self.moxed_client)
    self.moxed_client.list_ports(tenant_id=self.instance['project_id'], device_id=self.instance['uuid']).AndReturn({'ports': current_neutron_ports})
    ifaces = network_cache['info_cache']['network_info']
    if (port_ids is None):
        port_ids = [iface['id'] for iface in ifaces]
        net_ids = [iface['network']['id'] for iface in ifaces]
        nets = [{'id': iface['network']['id'], 'name': iface['network']['label'], 'tenant_id': iface['network']['meta']['tenant_id']} for iface in ifaces]
    if (networks is None):
        if ifaces:
            self.moxed_client.list_networks(id=net_ids).AndReturn({'networks': nets})
        else:
            non_shared_nets = [{'id': iface['network']['id'], 'name': iface['network']['label'], 'tenant_id': iface['network']['meta']['tenant_id']} for iface in ifaces if (not iface['shared'])]
            shared_nets = [{'id': iface['network']['id'], 'name': iface['network']['label'], 'tenant_id': iface['network']['meta']['tenant_id']} for iface in ifaces if iface['shared']]
            self.moxed_client.list_networks(shared=False, tenant_id=self.instance['project_id']).AndReturn({'networks': non_shared_nets})
            self.moxed_client.list_networks(shared=True).AndReturn({'networks': shared_nets})
    else:
        networks = (networks + [dict(id=iface['network']['id'], name=iface['network']['label'], tenant_id=iface['network']['meta']['tenant_id']) for iface in ifaces])
        port_ids = ([iface['id'] for iface in ifaces] + port_ids)
    index = 0
    current_neutron_port_map = {}
    for current_neutron_port in current_neutron_ports:
        current_neutron_port_map[current_neutron_port['id']] = current_neutron_port
    for port_id in port_ids:
        current_neutron_port = current_neutron_port_map.get(port_id)
        if current_neutron_port:
            for ip in current_neutron_port['fixed_ips']:
                self.moxed_client.list_floatingips(fixed_ip_address=ip['ip_address'], port_id=current_neutron_port['id']).AndReturn({'floatingips': [self.float_data2[index]]})
                self.moxed_client.list_subnets(id=mox.SameElementsAs([ip['subnet_id']])).AndReturn({'subnets': [self.subnet_data_n[index]]})
                self.moxed_client.list_ports(network_id=current_neutron_port['network_id'], device_owner='network:dhcp').AndReturn({'ports': self.dhcp_port_data1})
                index += 1
    self.instance['info_cache'] = self._fake_instance_info_cache(network_cache['info_cache']['network_info'], self.instance['uuid'])
    self.mox.StubOutWithMock(api.db, 'instance_info_cache_get')
    api.db.instance_info_cache_get(mox.IgnoreArg(), self.instance['uuid']).MultipleTimes().AndReturn(self.instance['info_cache'])
    self.mox.ReplayAll()
    instance = self._fake_instance_object_with_info_cache(self.instance)
    nw_infs = api.get_instance_nw_info(self.context, instance, networks=original_networks, port_ids=original_port_ids)
    self.assertEqual(index, len(nw_infs))
    for iface_index in range(index):
        self.assertEqual(port_ids[iface_index], nw_infs[iface_index]['id'])
