def test_show(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get()) req = webob.Request.blank("/v3/servers/1") req.headers["Content-Type"] = "application/json" response = req.get_response(fakes.wsgi_app_v3(init_only=("servers", "os-config-drive"))) self.assertEqual(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertIn(config_drive.ATTRIBUTE_NAME, res_dict["server"])
def test_get_server_by_id_verify_security_groups_json(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get()) req = webob.Request.blank("/v2/fake/os-create-server-ext/1") req.headers["Content-Type"] = "application/json" response = req.get_response(fakes.wsgi_app(init_only=("os-create-server-ext", "servers"))) self.assertEqual(response.status_int, 200) res_dict = jsonutils.loads(response.body) expected_security_group = [{"name": "test"}] self.assertEqual(res_dict["server"].get("security_groups"), expected_security_group)
def test_show_server(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get()) req = webob.Request.blank("/v3/servers/1") req.headers["Content-Type"] = "application/json" response = req.get_response(self.app) self.assertEqual(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertIn("key_name", res_dict["server"]) self.assertEqual(res_dict["server"]["key_name"], "")
def test_show(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get()) req = webob.Request.blank('/v2/fake/servers/1') req.headers['Content-Type'] = 'application/json' response = req.get_response(fakes.wsgi_app(init_only=('servers',))) self.assertEquals(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertIn('config_drive', res_dict['server'])
def test_show(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get()) req = webob.Request.blank(self.base_url + '1') req.headers['Content-Type'] = 'application/json' response = req.get_response(self.app) self.assertEqual(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertIn('config_drive', res_dict['server'])
def test_get_server_by_id_verify_security_groups_json(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get()) req = webob.Request.blank('/v2/fake/os-create-server-ext/1') req.headers['Content-Type'] = 'application/json' response = req.get_response(fakes.wsgi_app( init_only=('os-create-server-ext', 'servers'))) self.assertEquals(response.status_int, 200) res_dict = jsonutils.loads(response.body) expected_security_group = [{"name": "test"}] self.assertEquals(res_dict['server'].get('security_groups'), expected_security_group)
def test_show_server(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get()) req = webob.Request.blank('/v3/servers/1') req.headers['Content-Type'] = 'application/json' response = req.get_response(self.app) self.assertEquals(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertTrue('key_name' in res_dict['server']) self.assertEquals(res_dict['server']['key_name'], '')
def test_get_server_by_id_verify_security_groups_xml(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get()) req = webob.Request.blank("/v2/fake/os-create-server-ext/1") req.headers["Accept"] = "application/xml" response = req.get_response(fakes.wsgi_app(init_only=("os-create-server-ext", "servers"))) self.assertEqual(response.status_int, 200) dom = minidom.parseString(response.body) server = dom.childNodes[0] sec_groups = server.getElementsByTagName("security_groups")[0] sec_group = sec_groups.getElementsByTagName("security_group")[0] self.assertEqual("test", sec_group.getAttribute("name"))
def test_get_server_by_id_verify_security_groups_xml(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get()) req = webob.Request.blank('/v2/fake/os-create-server-ext/1') req.headers['Accept'] = 'application/xml' response = req.get_response(fakes.wsgi_app( init_only=('os-create-server-ext', 'servers'))) self.assertEquals(response.status_int, 200) dom = minidom.parseString(response.body) server = dom.childNodes[0] sec_groups = server.getElementsByTagName('security_groups')[0] sec_group = sec_groups.getElementsByTagName('security_group')[0] self.assertEqual('test', sec_group.getAttribute("name"))
def setUp(self): super(ServerActionsControllerTest, self).setUp() self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get(vm_state=vm_states.ACTIVE, host="fake_host")) self.stubs.Set(db, "instance_update_and_get_original", instance_update_and_get_original) fakes.stub_out_glance(self.stubs) fakes.stub_out_nw_api(self.stubs) fakes.stub_out_compute_api_snapshot(self.stubs) fake.stub_out_image_service(self.stubs) service_class = "nova.image.glance.GlanceImageService" self.service = importutils.import_object(service_class) self.sent_to_glance = {} fakes.stub_out_glanceclient_create(self.stubs, self.sent_to_glance) self.flags(allow_instance_snapshots=True, enable_instance_password=True) self.uuid = FAKE_UUID self.url = "/v2/fake/servers/%s/action" % self.uuid self._image_href = "155d900f-4e14-4e4c-a73d-069cbf4541e6" class FakeExtManager(object): def is_loaded(self, ext): return False self.controller = servers.Controller(ext_mgr=FakeExtManager()) self.compute_api = self.controller.compute_api self.context = context.RequestContext("fake", "fake") self.app = fakes.wsgi_app(init_only=("servers",), fake_auth_context=self.context)
def _test_rebuild_preserve_ephemeral(self, value=None): def fake_is_loaded(ext): return ext == "os-preserve-ephemeral-rebuild" self.stubs.Set(self.controller.ext_mgr, "is_loaded", fake_is_loaded) return_server = fakes.fake_instance_get(image_ref="2", vm_state=vm_states.ACTIVE, host="fake_host") self.stubs.Set(db, "instance_get_by_uuid", return_server) body = {"rebuild": {"imageRef": self._image_href}} if value is not None: body["rebuild"]["preserve_ephemeral"] = value req = fakes.HTTPRequest.blank(self.url) context = req.environ["nova.context"] self.mox.StubOutWithMock(compute_api.API, "rebuild") if value is not None: compute_api.API.rebuild( context, mox.IgnoreArg(), self._image_href, mox.IgnoreArg(), preserve_ephemeral=value, files_to_inject=None, ) else: compute_api.API.rebuild(context, mox.IgnoreArg(), self._image_href, mox.IgnoreArg(), files_to_inject=None) self.mox.ReplayAll() self.controller._action_rebuild(req, FAKE_UUID, body)
def setUp(self): super(ServerUsageTest, self).setUp() fakes.stub_out_nw_api(self.stubs) self.stubs.Set(compute.api.API, 'get', fake_compute_get) self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all) return_server = fakes.fake_instance_get() self.stubs.Set(db, 'instance_get_by_uuid', return_server)
def setUp(self): self.maxDiff = None super(ServerActionsControllerTest, self).setUp() self.stubs = stubout.StubOutForTesting() fakes.stub_out_auth(self.stubs) self.stubs.Set(nova.db, 'instance_get_by_uuid', fakes.fake_instance_get(vm_state=vm_states.ACTIVE, host='fake_host')) self.stubs.Set(nova.db, 'instance_update', instance_update) fakes.stub_out_glance(self.stubs) fakes.stub_out_nw_api(self.stubs) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_compute_api_snapshot(self.stubs) fakes.stub_out_image_service(self.stubs) service_class = 'nova.image.glance.GlanceImageService' self.service = utils.import_object(service_class) self.context = context.RequestContext(1, None) self.service.delete_all() self.sent_to_glance = {} fakes.stub_out_glance_add_image(self.stubs, self.sent_to_glance) self.flags(allow_instance_snapshots=True, enable_instance_password=True) self.uuid = FAKE_UUID self.url = '/v2/fake/servers/%s/action' % self.uuid self._image_href = '155d900f-4e14-4e4c-a73d-069cbf4541e6' self.controller = servers.Controller()
def test_detail_servers(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) req = fakes.HTTPRequest.blank("/v2/fake/servers/detail") res = req.get_response(fakes.wsgi_app()) server_dicts = jsonutils.loads(res.body)["servers"] for server_dict in server_dicts: self.asserTrue("config_drive" in server_dict)
def setUp(self): super(ExtendedStatusTest, self).setUp() fakes.stub_out_nw_api(self.stubs) self.stubs.Set(compute.api.API, "get", fake_compute_get) self.stubs.Set(compute.api.API, "get_all", fake_compute_get_all) return_server = fakes.fake_instance_get() self.stubs.Set(db, "instance_get_by_uuid", return_server)
def setUp(self): super(ServerActionsControllerTest, self).setUp() self.stubs.Set(nova.db, 'instance_get_by_uuid', fakes.fake_instance_get(vm_state=vm_states.ACTIVE, host='fake_host')) self.stubs.Set(nova.db, 'instance_update_and_get_original', instance_update) fakes.stub_out_glance(self.stubs) fakes.stub_out_nw_api(self.stubs) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_compute_api_snapshot(self.stubs) nova.tests.image.fake.stub_out_image_service(self.stubs) service_class = 'nova.image.glance.GlanceImageService' self.service = importutils.import_object(service_class) self.sent_to_glance = {} fakes.stub_out_glanceclient_create(self.stubs, self.sent_to_glance) self.flags(allow_instance_snapshots=True, enable_instance_password=True) self.uuid = FAKE_UUID self.url = '/v2/fake/servers/%s/action' % self.uuid self._image_href = '155d900f-4e14-4e4c-a73d-069cbf4541e6' self.controller = servers.Controller()
def _test_rebuild_preserve_ephemeral(self, value=None): return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) body = { "rebuild": { "imageRef": self._image_href, }, } if value is not None: body['rebuild']['preserve_ephemeral'] = value req = fakes.HTTPRequestV3.blank(self.url) context = req.environ['nova.context'] self.mox.StubOutWithMock(compute_api.API, 'rebuild') if value is not None: compute_api.API.rebuild(context, mox.IgnoreArg(), self._image_href, mox.IgnoreArg(), preserve_ephemeral=value) else: compute_api.API.rebuild(context, mox.IgnoreArg(), self._image_href, mox.IgnoreArg()) self.mox.ReplayAll() self.controller._action_rebuild(req, FAKE_UUID, body=body)
def test_reboot_hard_with_soft_in_progress_does_not_raise(self): body = dict(reboot=dict(type="HARD")) req = fakes.HTTPRequestV3.blank(self.url) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get(vm_state=vm_states.ACTIVE, task_state=task_states.REBOOTING)) self.controller._action_reboot(req, FAKE_UUID, body)
def setUp(self): super(ServerActionsControllerTest, self).setUp() CONF.set_override('host', 'localhost', group='glance') self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get(vm_state=vm_states.ACTIVE, host='fake_host')) self.stubs.Set(db, 'instance_update_and_get_original', instance_update_and_get_original) fakes.stub_out_nw_api(self.stubs) fakes.stub_out_compute_api_snapshot(self.stubs) fake.stub_out_image_service(self.stubs) self.flags(allow_instance_snapshots=True, enable_instance_password=True) self.uuid = FAKE_UUID self.url = '/servers/%s/action' % self.uuid self._image_href = '155d900f-4e14-4e4c-a73d-069cbf4541e6' ext_info = plugins.LoadedExtensionInfo() self.controller = servers.ServersController(extension_info=ext_info) self.compute_api = self.controller.compute_api self.context = context.RequestContext('fake', 'fake') self.app = fakes.wsgi_app_v3(init_only=('servers',), fake_auth_context=self.context)
def _test_create_volume_backed_image_with_metadata_from_volume(self, extra_metadata=None): def _fake_id(x): return "%s-%s-%s-%s" % (x * 8, x * 4, x * 4, x * 12) body = dict(create_image=dict(name="snapshot_of_volume_backed")) if extra_metadata: body["create_image"]["metadata"] = extra_metadata image_service = glance.get_default_image_service() def fake_block_device_mapping_get_all_by_instance(context, inst_id, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict( { "volume_id": _fake_id("a"), "source_type": "snapshot", "destination_type": "volume", "volume_size": 1, "device_name": "vda", "snapshot_id": 1, "boot_index": 0, "delete_on_termination": False, "no_device": None, } ) ] self.stubs.Set(db, "block_device_mapping_get_all_by_instance", fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref="", vm_state=vm_states.ACTIVE, root_device_name="/dev/vda") self.stubs.Set(db, "instance_get_by_uuid", instance) fake_metadata = {"test_key1": "test_value1", "test_key2": "test_value2"} volume = dict( id=_fake_id("a"), size=1, host="fake", display_description="fake", volume_image_metadata=fake_metadata ) snapshot = dict(id=_fake_id("d")) self.mox.StubOutWithMock(self.controller.compute_api, "volume_api") volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume) volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume["id"], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn( snapshot ) req = fakes.HTTPRequestV3.blank(self.url) self.mox.ReplayAll() response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers["Location"] image_id = location.replace("http://localhost:9292/images/", "") image = image_service.show(None, image_id) properties = image["properties"] self.assertEqual(properties["test_key1"], "test_value1") self.assertEqual(properties["test_key2"], "test_value2") if extra_metadata: for key, val in extra_metadata.items(): self.assertEqual(properties[key], val)
def setUp(self): super(ExtendedStatusTestV21, self).setUp() fakes.stub_out_nw_api(self.stubs) self.stubs.Set(compute.api.API, 'get', fake_compute_get) self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all) self._set_flags() return_server = fakes.fake_instance_get() self.stubs.Set(db, 'instance_get_by_uuid', return_server)
def test_show(self): self.stubs.Set(db, "instance_get", fakes.fake_instance_get()) req = webob.Request.blank("/v2/fake/servers/1") req.headers["Content-Type"] = "application/json" response = req.get_response(fakes.wsgi_app()) self.assertEquals(response.status_int, 200) res_dict = jsonutils.loads(response.body) self.assertTrue("config_drive" in res_dict["server"])
def test_detail_servers(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) req = fakes.HTTPRequest.blank('/v2/fake/servers/detail') res = req.get_response(fakes.wsgi_app(init_only=('servers,'))) server_dicts = jsonutils.loads(res.body)['servers'] for server_dict in server_dicts: self.asserTrue('config_drive' in server_dict)
def test_reboot_hard_with_hard_in_progress_raises_conflict(self): body = dict(reboot=dict(type="HARD")) req = fakes.HTTPRequestV3.blank(self.url) self.stubs.Set(db, 'instance_get_by_uuid', fakes.fake_instance_get(vm_state=vm_states.ACTIVE, task_state=task_states.REBOOTING_HARD)) self.assertRaises(webob.exc.HTTPConflict, self.controller._action_reboot, req, FAKE_UUID, body)
def setUp(self): super(HideServerAddressesTest, self).setUp() fakes.stub_out_nw_api(self.stubs) self.flags( osapi_compute_extension=[ 'nova.api.openstack.compute.contrib.select_extensions'], osapi_compute_ext_list=['Hide_server_addresses']) return_server = fakes.fake_instance_get() self.stubs.Set(db, 'instance_get_by_uuid', return_server)
def test_detail_servers(self): self.stubs.Set(db, "instance_get_all_by_filters", fakes.fake_instance_get_all_by_filters()) self.stubs.Set(db, "instance_get_by_uuid", fakes.fake_instance_get()) req = fakes.HTTPRequestV3.blank("/v3/servers/detail") res = req.get_response(fakes.wsgi_app_v3(init_only=("servers", "os-config-drive"))) server_dicts = jsonutils.loads(res.body)["servers"] self.assertNotEqual(len(server_dicts), 0) for server_dict in server_dicts: self.assertIn(config_drive.ATTRIBUTE_NAME, server_dict)
def setUp(self): super(ExtendedServerAttributesTest, self).setUp() availability_zones.reset_cache() fakes.stub_out_nw_api(self.stubs) self.stubs.Set(compute.api.API, "get", fake_compute_get) self.stubs.Set(compute.api.API, "get_all", fake_compute_get_all) self.stubs.Set(availability_zones, "get_host_availability_zone", fake_get_host_availability_zone) return_server = fakes.fake_instance_get() self.stubs.Set(db, "instance_get_by_uuid", return_server)
def test_create_volume_backed_image_with_metadata_from_volume(self): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(create_image=dict(name='snapshot_of_volume_backed')) image_service = glance.get_default_image_service() def fake_block_device_mapping_get_all_by_instance(context, inst_id, use_slave=False): return [fake_block_device.FakeDbBlockDeviceDict( {'volume_id': _fake_id('a'), 'source_type': 'snapshot', 'destination_type': 'volume', 'volume_size': 1, 'device_name': 'vda', 'snapshot_id': 1, 'boot_index': 0, 'delete_on_termination': False, 'no_device': None})] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref='', vm_state=vm_states.ACTIVE, root_device_name='/dev/vda') self.stubs.Set(db, 'instance_get_by_uuid', instance) fake_metadata = {'test_key1': 'test_value1', 'test_key2': 'test_value2'} volume = dict(id=_fake_id('a'), size=1, host='fake', display_description='fake', volume_image_metadata=fake_metadata) snapshot = dict(id=_fake_id('d')) self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api') volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot) req = fakes.HTTPRequestV3.blank(self.url) self.mox.ReplayAll() response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace('http://localhost:9292/images/', '') image = image_service.show(None, image_id) properties = image['properties'] self.assertEqual(properties['test_key1'], 'test_value1') self.assertEqual(properties['test_key2'], 'test_value2')
def setUp(self): super(ExtendedAvailabilityZoneTestV21, self).setUp() availability_zones.reset_cache() fakes.stub_out_nw_api(self.stubs) self.stubs.Set(compute.api.API, 'get', fake_compute_get) self.stubs.Set(compute.api.API, 'get_all', fake_compute_get_all) self.stubs.Set(availability_zones, 'get_host_availability_zone', fake_get_host_availability_zone) return_server = fakes.fake_instance_get() self.stubs.Set(db, 'instance_get_by_uuid', return_server)
def test_detail_servers(self): self.stubs.Set(db, 'instance_get', fakes.fake_instance_get()) req = fakes.HTTPRequest.blank('/v2/fake/servers/detail') res = req.get_response(fakes.wsgi_app()) server_dicts = jsonutils.loads(res.body)['servers'] for server_dict in server_dicts: self.asserTrue('key_name' in server_dict) self.assertEquals(server_dict['key_name'], '')
def test_rebuild_admin_password(self): return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) body = { "rebuild": { "image_ref": self._image_href, "admin_password": "******", }, } req = fakes.HTTPRequestV3.blank(self.url) body = self.controller._action_rebuild(req, FAKE_UUID, body=body).obj self.assertEqual(body['server']['image']['id'], '2') self.assertEqual(body['server']['admin_password'], 'asdf')
def test_rebuild_accepted_with_metadata(self): metadata = {'new': 'metadata'} return_server = fakes.fake_instance_get(metadata=metadata, vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) body = { "rebuild": { "imageRef": self._image_href, "metadata": metadata, }, } req = fakes.HTTPRequest.blank(self.url) body = self.controller._action_rebuild(req, FAKE_UUID, body).obj self.assertEqual(body['server']['metadata'], metadata)
def test_rebuild_instance_with_image_href_uses_uuid(self): info = dict(image_href_in_call=None) def rebuild(self2, context, instance, image_href, *args, **kwargs): info['image_href_in_call'] = image_href self.stubs.Set(db, 'instance_get', fakes.fake_instance_get(vm_state=vm_states.ACTIVE)) self.stubs.Set(compute_api.API, 'rebuild', rebuild) body = { 'rebuild': { 'imageRef': self.image_href, }, } req = fakes.HTTPRequestV3.blank('/v2/fake/servers/a/action') self.controller._action_rebuild(req, FAKE_UUID, body=body) self.assertEqual(info['image_href_in_call'], self.image_uuid)
def test_rebuild_accepted_minimum(self): return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) self_href = 'http://localhost/v2/fake/servers/%s' % FAKE_UUID body = { "rebuild": { "imageRef": self._image_href, }, } req = fakes.HTTPRequest.blank(self.url) robj = self.controller._action_rebuild(req, FAKE_UUID, body) body = robj.obj self.assertEqual(body['server']['image']['id'], '2') self.assertEqual(len(body['server']['adminPass']), CONF.password_length) self.assertEqual(robj['location'], self_href)
def test_rebuild_admin_pass_pass_disabled(self): # run with enable_instance_password disabled to verify adminPass # is missing from response. See lp bug 921814 self.flags(enable_instance_password=False) return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) body = { "rebuild": { "imageRef": self._image_href, "adminPass": "******", }, } req = fakes.HTTPRequest.blank(self.url) body = self.controller._action_rebuild(req, FAKE_UUID, body).obj self.assertEqual(body['server']['image']['id'], '2') self.assertTrue('adminPass' not in body['server'])
def test_rebuild_instance_with_image_href_uses_uuid(self): info = dict(image_href_in_call=None) def rebuild(self2, context, instance, image_href, *args, **kwargs): info['image_href_in_call'] = image_href self.stubs.Set(db, 'instance_get', fakes.fake_instance_get(vm_state=vm_states.ACTIVE)) self.stubs.Set(compute_api.API, 'rebuild', rebuild) # proper local hrefs must start with 'http://localhost/v2/' image_uuid = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' image_href = 'http://localhost/v2/fake/images/%s' % image_uuid body = { 'rebuild': { 'imageRef': image_href, }, } req = fakes.HTTPRequest.blank('/v2/fake/servers/a/action') self.controller._action_rebuild(req, FAKE_UUID, body) self.assertEqual(info['image_href_in_call'], image_uuid)
def test_rebuild_accepted_minimum_pass_disabled(self): # run with enable_instance_password disabled to verify adminPass # is missing from response. See lp bug 921814 self.flags(enable_instance_password=False) return_server = fakes.fake_instance_get(image_ref='2', vm_state=vm_states.ACTIVE, host='fake_host') self.stubs.Set(db, 'instance_get_by_uuid', return_server) self_href = 'http://localhost/v2/fake/servers/%s' % FAKE_UUID body = { "rebuild": { "imageRef": self._image_href, }, } req = fakes.HTTPRequest.blank(self.url) robj = self.controller._action_rebuild(req, FAKE_UUID, body) body = robj.obj self.assertEqual(body['server']['image']['id'], '2') self.assertTrue("adminPass" not in body['server']) self.assertEqual(robj['location'], self_href)
def _do_test_create_volume_backed_image(self, extra_properties): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(createImage=dict(name='snapshot_of_volume_backed')) if extra_properties: body['createImage']['metadata'] = extra_properties image_service = nova.image.glance.get_default_image_service() bdm = [ dict(snapshot_id=_fake_id('a'), volume_size=1, device_name='sda1', delete_on_termination=False) ] props = dict(kernel_id=_fake_id('b'), ramdisk_id=_fake_id('c'), root_device_name='/dev/sda1', block_device_mapping=bdm) original_image = dict(properties=props, container_format='ami', status='active', is_public=True) image_service.create(None, original_image) def fake_block_device_mapping_get_all_by_instance(context, inst_id): class BDM(object): def __init__(self): self.no_device = None self.values = dict(snapshot_id=_fake_id('a'), volume_id=_fake_id('d'), virtual_name=None, volume_size=1, device_name='sda1', delete_on_termination=False) def __getattr__(self, name): return self.values.get(name) def __getitem__(self, key): return self.values.get(key) return [BDM()] self.stubs.Set(nova.db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref=original_image['id'], vm_state=vm_states.ACTIVE, root_device_name='/dev/sda1') self.stubs.Set(nova.db, 'instance_get_by_uuid', instance) def fake_volume_get(context, volume_id): return dict(id=volume_id, size=1, host='fake', display_description='fake') self.stubs.Set(nova.db, 'volume_get', fake_volume_get) req = fakes.HTTPRequest.blank(self.url) response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace('http://localhost/v2/fake/images/', '') snapshot = image_service.show(None, image_id) self.assertEquals(snapshot['name'], 'snapshot_of_volume_backed') properties = snapshot['properties'] self.assertEquals(properties['kernel_id'], _fake_id('b')) self.assertEquals(properties['ramdisk_id'], _fake_id('c')) self.assertEquals(properties['root_device_name'], '/dev/sda1') bdms = properties['block_device_mapping'] self.assertEquals(len(bdms), 1) self.assertEquals(bdms[0]['device_name'], 'sda1') for k in extra_properties.keys(): self.assertEquals(properties[k], extra_properties[k])
def setUp(self): """Shared implementation for tests below that create instance.""" super(ServersControllerCreateTest, self).setUp() self.flags(verbose=True, enable_instance_password=True) self.instance_cache_num = 0 self.instance_cache_by_id = {} self.instance_cache_by_uuid = {} ext_info = plugins.LoadedExtensionInfo() self.controller = servers.ServersController(extension_info=ext_info) CONF.set_override('extensions_blacklist', 'os-disk-config', 'osapi_v3') self.no_disk_config_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/v3/os-images/%s' % image_uuid self.instance_cache_num += 1 instance = { 'id': self.instance_cache_num, 'display_name': inst['display_name'] or 'test', 'uuid': MANUAL_INSTANCE_UUID, 'instance_type': dict(inst_type), 'access_ip_v4': '1.2.3.4', 'access_ip_v6': 'fead::1234', '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), "user_data": None, "progress": 0, "fixed_ips": [], "task_state": "", "vm_state": "", } 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 scheduling """ 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 rpc_call_wrapper(context, topic, msg, timeout=None): """Stub out the scheduler creating the instance entry.""" if (topic == CONF.scheduler_topic and msg['method'] == 'run_instance'): request_spec = msg['args']['request_spec'] num_instances = request_spec.get('num_instances', 1) instances = [] for x in xrange(num_instances): instances.append( instance_create(context, request_spec['instance_properties'])) return instances def fake_method(*args, **kwargs): pass def queue_get_for(context, *args): return 'network_topic' def return_security_group(context, instance_id, security_group_id): pass fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_key_pair_funcs(self.stubs) fake.stub_out_image_service(self.stubs) fakes.stub_out_nw_api(self.stubs) self.stubs.Set(db, 'instance_add_security_group', return_security_group) self.stubs.Set(db, 'instance_system_metadata_update', fake_method) self.stubs.Set(db, 'instance_get', instance_get) self.stubs.Set(db, 'instance_update', instance_update) self.stubs.Set(nova.openstack.common.rpc, 'cast', fake_method) self.stubs.Set(nova.openstack.common.rpc, 'call', rpc_call_wrapper) return_server = fakes.fake_instance_get() return_servers = fakes.fake_instance_get_all_by_filters() self.stubs.Set(db, 'instance_get_all_by_filters', return_servers) self.stubs.Set(db, 'instance_get_by_uuid', return_server) self.stubs.Set(db, 'instance_add_security_group', return_security_group)
def _test_create_volume_backed_image_with_metadata_from_volume( self, extra_metadata=None): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(create_image=dict(name='snapshot_of_volume_backed')) if extra_metadata: body['create_image']['metadata'] = extra_metadata image_service = glance.get_default_image_service() def fake_block_device_mapping_get_all_by_instance( context, inst_id, use_slave=False): return [ fake_block_device.FakeDbBlockDeviceDict({ 'volume_id': _fake_id('a'), 'source_type': 'snapshot', 'destination_type': 'volume', 'volume_size': 1, 'device_name': 'vda', 'snapshot_id': 1, 'boot_index': 0, 'delete_on_termination': False, 'no_device': None }) ] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref='', vm_state=vm_states.ACTIVE, root_device_name='/dev/vda') self.stubs.Set(db, 'instance_get_by_uuid', instance) fake_metadata = { 'test_key1': 'test_value1', 'test_key2': 'test_value2' } volume = dict(id=_fake_id('a'), size=1, host='fake', display_description='fake', volume_image_metadata=fake_metadata) snapshot = dict(id=_fake_id('d')) self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api') volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot) req = fakes.HTTPRequestV3.blank(self.url) self.mox.ReplayAll() response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace('http://localhost:9292/images/', '') image = image_service.show(None, image_id) properties = image['properties'] self.assertEqual(properties['test_key1'], 'test_value1') self.assertEqual(properties['test_key2'], 'test_value2') if extra_metadata: for key, val in extra_metadata.items(): self.assertEqual(properties[key], val)
def setUp(self): """Shared implementation for tests below that create instance.""" super(ServersControllerCreateTest, self).setUp() self.flags(verbose=True, enable_instance_password=True) self.instance_cache_num = 0 self.instance_cache_by_id = {} self.instance_cache_by_uuid = {} ext_info = plugins.LoadedExtensionInfo() self.controller = servers.ServersController(extension_info=ext_info) CONF.set_override('extensions_blacklist', 'os-personality', 'osapi_v3') self.no_personality_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/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', 'uuid': FAKE_UUID, 'instance_type': dict(inst_type), 'access_ip_v4': '1.2.3.4', 'access_ip_v6': 'fead::1234', '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": "", "security_groups": inst['security_groups'], }) 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 scheduling """ 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(context, instance_uuid, params): 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') def queue_get_for(context, *args): return 'network_topic' fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_key_pair_funcs(self.stubs) fake.stub_out_image_service(self.stubs) fakes.stub_out_nw_api(self.stubs) self.stubs.Set(uuid, 'uuid4', fake_gen_uuid) self.stubs.Set(db, 'instance_add_security_group', return_security_group) self.stubs.Set(db, 'project_get_networks', project_get_networks) self.stubs.Set(db, 'instance_create', instance_create) self.stubs.Set(db, 'instance_system_metadata_update', fake_method) self.stubs.Set(db, 'instance_get', instance_get) self.stubs.Set(db, 'instance_update', instance_update) self.stubs.Set(rpc, 'cast', fake_method) self.stubs.Set(db, 'instance_update_and_get_original', server_update) self.stubs.Set(rpc, 'queue_get_for', queue_get_for) self.stubs.Set(manager.VlanManager, 'allocate_fixed_ip', fake_method) return_server = fakes.fake_instance_get() return_servers = fakes.fake_instance_get_all_by_filters() self.stubs.Set(db, 'instance_get_all_by_filters', return_servers) self.stubs.Set(db, 'instance_get_by_uuid', return_server) self.stubs.Set(db, 'instance_add_security_group', return_security_group) self.stubs.Set(db, 'instance_update_and_get_original', instance_update)
def setUp(self): super(HideServerAddressesTestV21, self).setUp() fakes.stub_out_nw_api(self.stubs) return_server = fakes.fake_instance_get() self.stubs.Set(db, 'instance_get_by_uuid', return_server) self._setup_wsgi()
def _do_test_create_volume_backed_image(self, extra_properties): def _fake_id(x): return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12) body = dict(createImage=dict(name='snapshot_of_volume_backed')) if extra_properties: body['createImage']['metadata'] = extra_properties image_service = glance.get_default_image_service() bdm = [ dict(volume_id=_fake_id('a'), volume_size=1, device_name='vda', delete_on_termination=False) ] props = dict(kernel_id=_fake_id('b'), ramdisk_id=_fake_id('c'), root_device_name='/dev/vda', block_device_mapping=bdm) original_image = dict(properties=props, container_format='ami', status='active', is_public=True) image_service.create(None, original_image) def fake_block_device_mapping_get_all_by_instance(context, inst_id): return [ dict(volume_id=_fake_id('a'), virtual_name=None, volume_size=1, device_name='vda', snapshot_id=1, delete_on_termination=False, no_device=None) ] self.stubs.Set(db, 'block_device_mapping_get_all_by_instance', fake_block_device_mapping_get_all_by_instance) instance = fakes.fake_instance_get(image_ref=original_image['id'], vm_state=vm_states.ACTIVE, root_device_name='/dev/vda') self.stubs.Set(db, 'instance_get_by_uuid', instance) volume = dict(id=_fake_id('a'), size=1, host='fake', display_description='fake') snapshot = dict(id=_fake_id('d')) self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api') volume_api = self.controller.compute_api.volume_api volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume) volume_api.create_snapshot_force(mox.IgnoreArg(), volume, mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot) self.mox.ReplayAll() req = fakes.HTTPRequest.blank(self.url) response = self.controller._action_create_image(req, FAKE_UUID, body) location = response.headers['Location'] image_id = location.replace('http://localhost/v2/fake/images/', '') image = image_service.show(None, image_id) self.assertEquals(image['name'], 'snapshot_of_volume_backed') properties = image['properties'] self.assertEquals(properties['kernel_id'], _fake_id('b')) self.assertEquals(properties['ramdisk_id'], _fake_id('c')) self.assertEquals(properties['root_device_name'], '/dev/vda') bdms = properties['block_device_mapping'] self.assertEquals(len(bdms), 1) self.assertEquals(bdms[0]['device_name'], 'vda') self.assertEquals(bdms[0]['snapshot_id'], snapshot['id']) for k in extra_properties.keys(): self.assertEquals(properties[k], extra_properties[k])