'Convenience method that does most of the work for the
test_finish_revert_resize tests.
:param power_on -- True if testing resize from ACTIVE state, False if
testing resize from STOPPED state.
:param remove_old_vm_state -- True if testing a case where the
\'old_vm_state\' system_metadata is not present when the
finish_revert_resize method is called.'

def _test_finish_revert_resize(self, power_on, remove_old_vm_state=False, numa_topology=None):

    def fake(*args, **kwargs):
        pass
    def fake_finish_revert_migration_driver(*args, **kwargs):
        inst = args[2]
        self.assertEqual('1', inst.flavor.flavorid)
    old_vm_state = None
    if power_on:
        old_vm_state = vm_states.ACTIVE
    else:
        old_vm_state = vm_states.STOPPED
    params = {'vm_state': old_vm_state}
    instance = self._create_fake_instance_obj(params)
    self.stub_out('nova.virt.fake.FakeDriver.finish_migration', fake)
    self.stub_out('nova.virt.fake.FakeDriver.finish_revert_migration', fake_finish_revert_migration_driver)
    self._stub_out_resize_network_methods()
    memory_mb_used = self.rt.compute_nodes[NODENAME].memory_mb_used
    self.compute.build_and_run_instance(self.context, instance, {}, {}, {}, block_device_mapping=[])
    instance.refresh()
    flavor = objects.Flavor.get_by_id(self.context, instance.instance_type_id)
    self.assertEqual(flavor.flavorid, '1')
    self.assertEqual(self.rt.compute_nodes[NODENAME].memory_mb_used, (memory_mb_used + flavor.memory_mb))
    old_vm_state = instance['vm_state']
    instance.vm_state = old_vm_state
    instance.numa_topology = numa_topology
    instance.save()
    new_instance_type_ref = flavors.get_flavor_by_flavor_id(3)
    self.compute.prep_resize(self.context, instance=instance, instance_type=new_instance_type_ref, image={}, reservations=[], request_spec={}, filter_properties={}, node=None, clean_shutdown=True)
    self.assertEqual(self.rt.compute_nodes[NODENAME].memory_mb_used, ((memory_mb_used + flavor.memory_mb) + new_instance_type_ref.memory_mb))
    migration = objects.Migration.get_by_instance_and_status(self.context.elevated(), instance.uuid, 'pre-migrating')
    migration_context = objects.MigrationContext.get_by_instance_uuid(self.context.elevated(), instance.uuid)
    self.assertIsInstance(migration_context.old_numa_topology, numa_topology.__class__)
    sys_meta = instance.system_metadata
    self.assertEqual(old_vm_state, sys_meta['old_vm_state'])
    instance.task_state = task_states.RESIZE_PREP
    instance.save()
    self.compute.resize_instance(self.context, instance=instance, migration=migration, image={}, reservations=[], instance_type=new_instance_type_ref, clean_shutdown=True)
    self.compute.finish_resize(self.context, migration=migration, reservations=[], disk_info={}, image={}, instance=instance)
    self.assertEqual(self.rt.compute_nodes[NODENAME].memory_mb_used, ((memory_mb_used + flavor.memory_mb) + new_instance_type_ref.memory_mb))
    instance_type_ref = flavors.get_flavor_by_flavor_id(3)
    self.assertEqual(instance_type_ref['flavorid'], '3')
    self.assertIsNone(instance.numa_topology)
    instance.task_state = task_states.RESIZE_REVERTING
    instance.save()
    self.compute.revert_resize(self.context, migration=migration, instance=instance, reservations=[])
    self.assertEqual(self.rt.compute_nodes[NODENAME].memory_mb_used, (memory_mb_used + flavor.memory_mb))
    instance.refresh()
    if remove_old_vm_state:
        sys_meta = instance.system_metadata
        sys_meta.pop('old_vm_state')
        instance.system_metadata = sys_meta
        instance.save()
    self.compute.finish_revert_resize(self.context, migration=migration, instance=instance, reservations=[])
    self.assertIsNone(instance.task_state)
    flavor = objects.Flavor.get_by_id(self.context, instance['instance_type_id'])
    self.assertEqual(flavor.flavorid, '1')
    self.assertEqual(instance.host, migration.source_compute)
    self.assertIsInstance(instance.numa_topology, numa_topology.__class__)
    if remove_old_vm_state:
        self.assertEqual(vm_states.ACTIVE, instance.vm_state)
    else:
        self.assertEqual(old_vm_state, instance.vm_state)
