'Shared implementation for tests below that create instance.'

def setUp(self):

    super(ServersControllerCreateTest, self).setUp()
    self.flags(enable_instance_password=True, group='api')
    self.instance_cache_num = 0
    self.instance_cache_by_id = {}
    self.instance_cache_by_uuid = {}
    fakes.stub_out_nw_api(self)
    ext_info = extension_info.LoadedExtensionInfo()
    self.controller = servers.ServersController(extension_info=ext_info)
    def instance_create(context, inst):
        inst_type = flavors.get_flavor_by_flavor_id(3)
        image_uuid = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6'
        def_image_ref = ('http://localhost/fake/images/%s' % image_uuid)
        self.instance_cache_num += 1
        instance = fake_instance.fake_db_instance(**{'id': self.instance_cache_num, 'display_name': (inst['display_name'] or 'test'), 'display_description': (inst['display_description'] or ''), 'uuid': FAKE_UUID, 'instance_type': inst_type, 'image_ref': inst.get('image_ref', def_image_ref), 'user_id': 'fake', 'project_id': 'fake', 'reservation_id': inst['reservation_id'], 'created_at': datetime.datetime(2010, 10, 10, 12, 0, 0), 'updated_at': datetime.datetime(2010, 11, 11, 11, 0, 0), 'config_drive': None, 'progress': 0, 'fixed_ips': [], 'task_state': '', 'vm_state': '', 'root_device_name': inst.get('root_device_name', 'vda')})
        self.instance_cache_by_id[instance['id']] = instance
        self.instance_cache_by_uuid[instance['uuid']] = instance
        return instance
    def instance_get(context, instance_id):
        'Stub    for    compute/api    create()    pulling    in    instance    after\n                                                scheduling\n                                                '
        return self.instance_cache_by_id[instance_id]
    def instance_update(context, uuid, values):
        instance = self.instance_cache_by_uuid[uuid]
        instance.update(values)
        return instance
    def server_update_and_get_original(context, instance_uuid, params, columns_to_join=None):
        inst = self.instance_cache_by_uuid[instance_uuid]
        inst.update(params)
        return (inst, inst)
    def fake_method(*args, **kwargs):
        pass
    def project_get_networks(context, user_id):
        return dict(id='1', host='localhost')
    fakes.stub_out_key_pair_funcs(self)
    fake.stub_out_image_service(self)
    self.stubs.Set(uuid, 'uuid4', fake_gen_uuid)
    self.stub_out('nova.db.project_get_networks', project_get_networks)
    self.stub_out('nova.db.instance_create', instance_create)
    self.stub_out('nova.db.instance_system_metadata_update', fake_method)
    self.stub_out('nova.db.instance_get', instance_get)
    self.stub_out('nova.db.instance_update', instance_update)
    self.stub_out('nova.db.instance_update_and_get_original', server_update_and_get_original)
    self.stubs.Set(manager.VlanManager, 'allocate_fixed_ip', fake_method)
    self.body = {'server': {'name': 'server_test', 'imageRef': self.image_uuid, 'flavorRef': self.flavor_ref, 'metadata': {'hello': 'world', 'open': 'stack'}, 'personality': [{'path': '/etc/banner.txt', 'contents': 'MQ=='}]}}
    self.bdm = [{'delete_on_termination': 1, 'device_name': 123, 'volume_size': 1, 'volume_id': '11111111-1111-1111-1111-111111111111'}]
    self.req = fakes.HTTPRequest.blank('/fake/servers')
    self.req.method = 'POST'
    self.req.headers['content-type'] = 'application/json'
