diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py index 4a26ee42fee0..45987d40e50b 100644 --- a/nova/api/openstack/common.py +++ b/nova/api/openstack/common.py @@ -113,21 +113,31 @@ def get_pagination_params(request): """ params = {} - for param in ['marker', 'limit']: - if not param in request.GET: - continue - try: - params[param] = int(request.GET[param]) - except ValueError: - msg = _('%s param must be an integer') % param - raise webob.exc.HTTPBadRequest(explanation=msg) - if params[param] < 0: - msg = _('%s param must be positive') % param - raise webob.exc.HTTPBadRequest(explanation=msg) - + if 'limit' in request.GET: + params['limit'] = _get_limit_param(request) + if 'marker' in request.GET: + params['marker'] = _get_marker_param(request) return params +def _get_limit_param(request): + """Extract integer limit from request or fail""" + try: + limit = int(request.GET['limit']) + except ValueError: + msg = _('limit param must be an integer') + raise webob.exc.HTTPBadRequest(explanation=msg) + if limit < 0: + msg = _('limit param must be positive') + raise webob.exc.HTTPBadRequest(explanation=msg) + return limit + + +def _get_marker_param(request): + """Extract marker id from request or fail""" + return request.GET['marker'] + + def limited(items, request, max_limit=FLAGS.osapi_max_limit): """ Return a slice of items according to requested offset and limit. @@ -178,7 +188,7 @@ def limited_by_marker(items, request, max_limit=FLAGS.osapi_max_limit): if marker: start_index = -1 for i, item in enumerate(items): - if item['id'] == marker: + if item['id'] == marker or item.get('uuid') == marker: start_index = i + 1 break if start_index < 0: diff --git a/nova/api/openstack/contrib/volumes.py b/nova/api/openstack/contrib/volumes.py index f706ebfe1183..da856b4a143f 100644 --- a/nova/api/openstack/contrib/volumes.py +++ b/nova/api/openstack/contrib/volumes.py @@ -19,6 +19,7 @@ from webob import exc import webob from nova import compute +from nova import db from nova import exception from nova import flags from nova import log as logging @@ -200,8 +201,8 @@ def _translate_attachment_summary_view(_context, vol): d['id'] = volume_id d['volumeId'] = volume_id - if vol.get('instance_id'): - d['serverId'] = vol['instance_id'] + if vol.get('instance'): + d['serverId'] = vol['instance']['uuid'] if vol.get('mountpoint'): d['device'] = vol['mountpoint'] @@ -245,7 +246,8 @@ class VolumeAttachmentController(object): LOG.debug("volume_id not found") return faults.Fault(exc.HTTPNotFound()) - if str(vol['instance_id']) != server_id: + instance = vol['instance'] + if instance is None or str(instance['uuid']) != server_id: LOG.debug("instance_id != server_id") return faults.Fault(exc.HTTPNotFound()) @@ -307,7 +309,8 @@ class VolumeAttachmentController(object): except exception.NotFound: return faults.Fault(exc.HTTPNotFound()) - if str(vol['instance_id']) != server_id: + instance = vol['instance'] + if instance is None or str(instance['uuid']) != server_id: LOG.debug("instance_id != server_id") return faults.Fault(exc.HTTPNotFound()) diff --git a/nova/api/openstack/schemas/v1.1/server.rng b/nova/api/openstack/schemas/v1.1/server.rng index 4eb1a0b85f66..2e86ccffee61 100644 --- a/nova/api/openstack/schemas/v1.1/server.rng +++ b/nova/api/openstack/schemas/v1.1/server.rng @@ -4,7 +4,6 @@ - diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 23acfe66d347..f4844e0e7b8d 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -204,7 +204,7 @@ class Controller(object): return kernel_id, ramdisk_id @staticmethod - def _do_get_kernel_ramdisk_from_image(image_meta): + def _do_get_kernel_ramdisk_from_image(image_meta): """Given an ImageService image_meta, return kernel and ramdisk image ids if present. @@ -585,6 +585,7 @@ class Controller(object): rotation factor to be deleted. """ + context = req.environ["nova.context"] entity = input_dict["createBackup"] try: @@ -607,13 +608,10 @@ class Controller(object): raise exc.HTTPBadRequest(explanation=msg) # preserve link to server in image properties - server_ref = os.path.join(req.application_url, - 'servers', - str(instance_id)) + server_ref = os.path.join(req.application_url, 'servers', instance_id) props = {'instance_ref': server_ref} metadata = entity.get('metadata', {}) - context = req.environ["nova.context"] common.check_img_metadata_quota_limit(context, metadata) try: props.update(metadata) @@ -678,44 +676,6 @@ class Controller(object): raise exc.HTTPUnprocessableEntity() return webob.Response(status_int=202) - @exception.novaclient_converter - @scheduler_api.redirect_handler - def get_lock(self, req, id): - """ - return the boolean state of (instance with id)'s lock - - """ - context = req.environ['nova.context'] - try: - self.compute_api.get_lock(context, id) - except Exception: - readable = traceback.format_exc() - LOG.exception(_("Compute.api::get_lock %s"), readable) - raise exc.HTTPUnprocessableEntity() - return webob.Response(status_int=202) - - @exception.novaclient_converter - @scheduler_api.redirect_handler - def get_ajax_console(self, req, id): - """Returns a url to an instance's ajaxterm console.""" - try: - self.compute_api.get_ajax_console(req.environ['nova.context'], - int(id)) - except exception.NotFound: - raise exc.HTTPNotFound() - return webob.Response(status_int=202) - - @exception.novaclient_converter - @scheduler_api.redirect_handler - def get_vnc_console(self, req, id): - """Returns a url to an instance's ajaxterm console.""" - try: - self.compute_api.get_vnc_console(req.environ['nova.context'], - int(id)) - except exception.NotFound: - raise exc.HTTPNotFound() - return webob.Response(status_int=202) - @exception.novaclient_converter @scheduler_api.redirect_handler def diagnostics(self, req, id): @@ -737,7 +697,7 @@ class Controller(object): error=item.error)) return dict(actions=actions) - def resize(self, req, instance_id, flavor_id): + def _resize(self, req, instance_id, flavor_id): """Begin the resize process with given instance/flavor.""" context = req.environ["nova.context"] @@ -870,7 +830,7 @@ class Controller(object): msg = _("Resize requests require 'flavorRef' attribute.") raise exc.HTTPBadRequest(explanation=msg) - return self.resize(req, id, flavor_ref) + return self._resize(req, id, flavor_ref) def _action_rebuild(self, info, request, instance_id): context = request.environ['nova.context'] @@ -915,6 +875,7 @@ class Controller(object): @common.check_snapshots_enabled def _action_create_image(self, input_dict, req, instance_id): """Snapshot a server instance.""" + context = req.environ['nova.context'] entity = input_dict.get("createImage", {}) try: @@ -929,13 +890,10 @@ class Controller(object): raise exc.HTTPBadRequest(explanation=msg) # preserve link to server in image properties - server_ref = os.path.join(req.application_url, - 'servers', - str(instance_id)) + server_ref = os.path.join(req.application_url, 'servers', instance_id) props = {'instance_ref': server_ref} metadata = entity.get('metadata', {}) - context = req.environ['nova.context'] common.check_img_metadata_quota_limit(context, metadata) try: props.update(metadata) @@ -998,7 +956,6 @@ def make_server(elem, detailed=False): elem.set('id') if detailed: - elem.set('uuid') elem.set('userId', 'user_id') elem.set('tenantId', 'tenant_id') elem.set('updated') diff --git a/nova/api/openstack/views/servers.py b/nova/api/openstack/views/servers.py index d756ec04f7db..288730efe36c 100644 --- a/nova/api/openstack/views/servers.py +++ b/nova/api/openstack/views/servers.py @@ -52,13 +52,12 @@ class ViewBuilder(object): server = self._build_simple(inst) self._build_links(server['server'], inst) - server['server']['uuid'] = inst['uuid'] return server def _build_simple(self, inst): """Return a simple model of a server.""" - return dict(server=dict(id=inst['id'], name=inst['display_name'])) + return dict(server=dict(id=inst['uuid'], name=inst['display_name'])) def _build_detail(self, inst): """Returns a detailed model of a server.""" @@ -66,7 +65,7 @@ class ViewBuilder(object): task_state = inst.get('task_state') inst_dict = { - 'id': inst['id'], + 'id': inst['uuid'], 'name': inst['display_name'], 'user_id': inst.get('user_id', ''), 'tenant_id': inst.get('project_id', ''), @@ -137,8 +136,8 @@ class ViewBuilder(object): } def _build_links(self, response, inst): - href = self.generate_href(inst["id"]) - bookmark = self.generate_bookmark(inst["id"]) + href = self.generate_href(inst["uuid"]) + bookmark = self.generate_bookmark(inst["uuid"]) links = [ { diff --git a/nova/compute/api.py b/nova/compute/api.py index ae8e95cc5efb..2305c09ab51c 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -1481,7 +1481,7 @@ class API(base.Base): self.db.queue_get_for(context, FLAGS.compute_topic, host), {"method": "attach_volume", "args": {"volume_id": volume_id, - "instance_id": instance_id, + "instance_id": instance['id'], "mountpoint": device}}) def detach_volume(self, context, volume_id): diff --git a/nova/tests/api/openstack/contrib/test_admin_actions.py b/nova/tests/api/openstack/contrib/test_admin_actions.py index 98178d111673..8b569760912c 100644 --- a/nova/tests/api/openstack/contrib/test_admin_actions.py +++ b/nova/tests/api/openstack/contrib/test_admin_actions.py @@ -45,7 +45,7 @@ class AdminActionsTest(test.TestCase): def test_admin_api_enabled(self): app = fakes.wsgi_app() for _action in self._actions: - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank('/v1.1/fake/servers/abcd/action') req.method = 'POST' req.body = json.dumps({_action: None}) req.content_type = 'application/json' @@ -56,7 +56,7 @@ class AdminActionsTest(test.TestCase): FLAGS.allow_admin_api = False app = fakes.wsgi_app() for _action in self._actions: - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank('/v1.1/fake/servers/abcd/action') req.method = 'POST' req.body = json.dumps({_action: None}) req.content_type = 'application/json' diff --git a/nova/tests/api/openstack/contrib/test_diskconfig.py b/nova/tests/api/openstack/contrib/test_diskconfig.py index b6843b49c59b..5cdb3917de5f 100644 --- a/nova/tests/api/openstack/contrib/test_diskconfig.py +++ b/nova/tests/api/openstack/contrib/test_diskconfig.py @@ -27,18 +27,23 @@ from nova.tests.api.openstack import fakes class DiskConfigTest(test.TestCase): + def setUp(self): + super(DiskConfigTest, self).setUp() + self.uuid = '70f6db34-de8d-4fbd-aafb-4065bdfa6114' + self.url = '/v1.1/openstack/servers/%s/os-disk-config' % self.uuid + def test_retrieve_disk_config(self): def fake_compute_get(*args, **kwargs): return {'managed_disk': True} self.stubs.Set(compute.api.API, 'routing_get', fake_compute_get) - req = webob.Request.blank('/v1.1/openstack/servers/50/os-disk-config') + req = webob.Request.blank(self.url) req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) body = json.loads(res.body) self.assertEqual(body['server']['managed_disk'], True) - self.assertEqual(int(body['server']['id']), 50) + self.assertEqual(body['server']['id'], self.uuid) def test_set_disk_config(self): def fake_compute_get(*args, **kwargs): @@ -50,7 +55,7 @@ class DiskConfigTest(test.TestCase): self.stubs.Set(compute.api.API, 'update', fake_compute_update) self.stubs.Set(compute.api.API, 'routing_get', fake_compute_get) - req = webob.Request.blank('/v1.1/openstack/servers/50/os-disk-config') + req = webob.Request.blank(self.url) req.method = 'PUT' req.headers['Accept'] = 'application/json' req.headers['Content-Type'] = 'application/json' @@ -60,14 +65,14 @@ class DiskConfigTest(test.TestCase): self.assertEqual(res.status_int, 200) body = json.loads(res.body) self.assertEqual(body['server']['managed_disk'], False) - self.assertEqual(int(body['server']['id']), 50) + self.assertEqual(body['server']['id'], self.uuid) def test_retrieve_disk_config_bad_server_fails(self): def fake_compute_get(*args, **kwargs): raise exception.NotFound() self.stubs.Set(compute.api.API, 'routing_get', fake_compute_get) - req = webob.Request.blank('/v1.1/openstack/servers/50/os-disk-config') + req = webob.Request.blank(self.url) req.headers['Accept'] = 'application/json' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 404) @@ -84,7 +89,7 @@ class DiskConfigTest(test.TestCase): self.stubs.Set(compute.api.API, 'update', fake_compute_update) self.stubs.Set(compute.api.API, 'routing_get', fake_compute_get) - req = webob.Request.blank('/v1.1/openstack/servers/50/os-disk-config') + req = webob.Request.blank(self.url) req.method = 'PUT' req.headers['Accept'] = 'application/json' req.headers['Content-Type'] = 'application/json' diff --git a/nova/tests/api/openstack/contrib/test_virtual_interfaces.py b/nova/tests/api/openstack/contrib/test_virtual_interfaces.py index 0260e89d4ac6..0e3b45c105f2 100644 --- a/nova/tests/api/openstack/contrib/test_virtual_interfaces.py +++ b/nova/tests/api/openstack/contrib/test_virtual_interfaces.py @@ -42,7 +42,8 @@ class ServerVirtualInterfaceTest(test.TestCase): super(ServerVirtualInterfaceTest, self).tearDown() def test_get_virtual_interfaces_list(self): - req = webob.Request.blank('/v1.1/123/servers/1/os-virtual-interfaces') + url = '/v1.1/123/servers/abcd/os-virtual-interfaces' + req = webob.Request.blank(url) res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) diff --git a/nova/tests/api/openstack/contrib/test_volumes.py b/nova/tests/api/openstack/contrib/test_volumes.py index e911c5ffbb71..bac16e93985a 100644 --- a/nova/tests/api/openstack/contrib/test_volumes.py +++ b/nova/tests/api/openstack/contrib/test_volumes.py @@ -22,12 +22,14 @@ from nova import flags from nova import test from nova.compute import instance_types from nova.tests.api.openstack import fakes -from nova.tests.api.openstack.test_servers import fake_gen_uuid FLAGS = flags.FLAGS +FAKE_UUID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' + + def fake_compute_api_create(cls, context, instance_type, image_href, **kwargs): global _block_device_mapping_seen _block_device_mapping_seen = kwargs.get('block_device_mapping') @@ -36,7 +38,7 @@ def fake_compute_api_create(cls, context, instance_type, image_href, **kwargs): resv_id = None return ([{'id': 1, 'display_name': 'test_server', - 'uuid': fake_gen_uuid(), + 'uuid': FAKE_UUID, 'instance_type': dict(inst_type), 'access_ip_v4': '1.2.3.4', 'access_ip_v6': 'fead::1234', @@ -76,7 +78,7 @@ class BootFromVolumeTest(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) server = json.loads(res.body)['server'] - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertEqual(2, int(server['flavor']['id'])) self.assertEqual(u'test_server', server['name']) self.assertEqual(3, int(server['image']['id'])) diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index f7e8ae9f41fd..2240212bb247 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -149,22 +149,34 @@ def stub_out_networking(stubs): stubs.Set(nova.flags, '_get_my_ip', get_my_ip) -def stub_out_compute_api_snapshot(stubs): +class stub_out_compute_api_snapshot(object): + + def __init__(self, stubs): + self.stubs = stubs + self.extra_props_last_call = None + stubs.Set(nova.compute.API, 'snapshot', self.snapshot) + def snapshot(self, context, instance_id, name, extra_properties=None): + self.extra_props_last_call = extra_properties props = dict(instance_id=instance_id, instance_ref=instance_id) props.update(extra_properties or {}) return dict(id='123', status='ACTIVE', name=name, properties=props) - stubs.Set(nova.compute.API, 'snapshot', snapshot) -def stub_out_compute_api_backup(stubs): +class stub_out_compute_api_backup(object): + + def __init__(self, stubs): + self.stubs = stubs + self.extra_props_last_call = None + stubs.Set(nova.compute.API, 'backup', self.backup) + def backup(self, context, instance_id, name, backup_type, rotation, extra_properties=None): + self.extra_props_last_call = extra_properties props = dict(instance_id=instance_id, instance_ref=instance_id, backup_type=backup_type, rotation=rotation) props.update(extra_properties or {}) return dict(id='123', status='ACTIVE', name=name, properties=props) - stubs.Set(nova.compute.API, 'backup', backup) def stub_out_nw_api_get_instance_nw_info(stubs, func=None): @@ -225,7 +237,8 @@ def _make_image_fixtures(): image_id += 1 # Snapshot for User 1 - server_ref = 'http://localhost/v1.1/servers/42' + uuid = 'aa640691-d1a7-4a67-9d3c-d35ee6b3cc74' + server_ref = 'http://localhost/v1.1/servers/' + uuid snapshot_properties = {'instance_ref': server_ref, 'user_id': 'fake'} for status in ('queued', 'saving', 'active', 'killed', 'deleted', 'pending_delete'): diff --git a/nova/tests/api/openstack/test_common.py b/nova/tests/api/openstack/test_common.py index 1628ad1c8d09..9cbbd1cdd333 100644 --- a/nova/tests/api/openstack/test_common.py +++ b/nova/tests/api/openstack/test_common.py @@ -172,14 +172,9 @@ class PaginationParamsTest(test.TestCase): def test_valid_marker(self): """ Test valid marker param. """ - req = Request.blank('/?marker=1') - self.assertEqual(common.get_pagination_params(req), {'marker': 1}) - - def test_invalid_marker(self): - """ Test invalid marker param. """ - req = Request.blank('/?marker=-2') - self.assertRaises( - webob.exc.HTTPBadRequest, common.get_pagination_params, req) + req = Request.blank('/?marker=263abb28-1de6-412f-b00b-f0ee0c4333c2') + self.assertEqual(common.get_pagination_params(req), + {'marker': '263abb28-1de6-412f-b00b-f0ee0c4333c2'}) def test_valid_limit(self): """ Test valid limit param. """ @@ -194,9 +189,10 @@ class PaginationParamsTest(test.TestCase): def test_valid_limit_and_marker(self): """ Test valid limit and marker parameters. """ - req = Request.blank('/?limit=20&marker=40') + marker = '263abb28-1de6-412f-b00b-f0ee0c4333c2' + req = Request.blank('/?limit=20&marker=%s' % marker) self.assertEqual(common.get_pagination_params(req), - {'marker': 40, 'limit': 20}) + {'marker': marker, 'limit': 20}) class MiscFunctionsTest(test.TestCase): diff --git a/nova/tests/api/openstack/test_extensions.py b/nova/tests/api/openstack/test_extensions.py index f3a9c26e3519..d96a67d7de88 100644 --- a/nova/tests/api/openstack/test_extensions.py +++ b/nova/tests/api/openstack/test_extensions.py @@ -297,7 +297,7 @@ class ActionExtensionTest(ExtensionTestCase): def test_extended_action(self): body = dict(add_tweedle=dict(name="test")) - url = "/123/servers/1/action" + url = "/123/servers/abcd/action" response = self._send_server_action_request(url, body) self.assertEqual(200, response.status_int) self.assertEqual("Tweedle Beetle Added.", response.body) @@ -309,7 +309,7 @@ class ActionExtensionTest(ExtensionTestCase): def test_invalid_action_body(self): body = dict(blah=dict(name="test")) # Doesn't exist - url = "/123/servers/1/action" + url = "/123/servers/abcd/action" response = self._send_server_action_request(url, body) self.assertEqual(400, response.status_int) diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index ab09e828bbd2..d42c66940c22 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -69,8 +69,9 @@ class ImagesControllerTest(test.TestCase): href = "http://localhost/v1.1/fake/images/124" bookmark = "http://localhost/fake/images/124" alternate = "%s/fake/images/124" % utils.generate_glance_url() - server_href = "http://localhost/v1.1/servers/42" - server_bookmark = "http://localhost/servers/42" + server_uuid = "aa640691-d1a7-4a67-9d3c-d35ee6b3cc74" + server_href = "http://localhost/v1.1/servers/" + server_uuid + server_bookmark = "http://localhost/servers/" + server_uuid expected_image = { "image": { @@ -83,7 +84,7 @@ class ImagesControllerTest(test.TestCase): "minDisk": 0, "minRam": 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -94,7 +95,7 @@ class ImagesControllerTest(test.TestCase): }], }, "metadata": { - "instance_ref": "http://localhost/v1.1/servers/42", + "instance_ref": server_href, "user_id": "fake", }, "links": [{ @@ -113,7 +114,7 @@ class ImagesControllerTest(test.TestCase): }, } - self.assertEqual(expected_image, actual_image) + self.assertDictMatch(expected_image, actual_image) def test_get_image_404(self): fake_req = fakes.HTTPRequest.blank('/v1.1/fake/images/unknown') @@ -395,8 +396,9 @@ class ImagesControllerTest(test.TestCase): response = self.controller.detail(request) response_list = response["images"] - server_href = "http://localhost/v1.1/servers/42" - server_bookmark = "http://localhost/servers/42" + server_uuid = "aa640691-d1a7-4a67-9d3c-d35ee6b3cc74" + server_href = "http://localhost/v1.1/servers/" + server_uuid + server_bookmark = "http://localhost/servers/" + server_uuid alternate = "%s/fake/images/%s" expected = [{ @@ -427,7 +429,7 @@ class ImagesControllerTest(test.TestCase): 'id': '124', 'name': 'queued snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -437,7 +439,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -465,7 +467,7 @@ class ImagesControllerTest(test.TestCase): 'id': '125', 'name': 'saving snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -475,7 +477,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -503,7 +505,7 @@ class ImagesControllerTest(test.TestCase): 'id': '126', 'name': 'active snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -513,7 +515,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -541,7 +543,7 @@ class ImagesControllerTest(test.TestCase): 'id': '127', 'name': 'killed snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -551,7 +553,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -579,7 +581,7 @@ class ImagesControllerTest(test.TestCase): 'id': '128', 'name': 'deleted snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -589,7 +591,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -617,7 +619,7 @@ class ImagesControllerTest(test.TestCase): 'id': '129', 'name': 'pending_delete snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -627,7 +629,7 @@ class ImagesControllerTest(test.TestCase): 'minDisk': 0, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -685,8 +687,9 @@ class ImagesControllerTest(test.TestCase): response_list = response["images"] response_links = response["images_links"] - server_href = "http://localhost/v1.1/servers/42" - server_bookmark = "http://localhost/servers/42" + server_uuid = "aa640691-d1a7-4a67-9d3c-d35ee6b3cc74" + server_href = "http://localhost/v1.1/servers/" + server_uuid + server_bookmark = "http://localhost/servers/" + server_uuid alternate = "%s/fake/images/%s" expected = [{ @@ -717,7 +720,7 @@ class ImagesControllerTest(test.TestCase): 'id': '124', 'name': 'queued snapshot', 'metadata': { - u'instance_ref': u'http://localhost/v1.1/servers/42', + u'instance_ref': server_href, u'user_id': u'fake', }, 'updated': NOW_API_FORMAT, @@ -727,7 +730,7 @@ class ImagesControllerTest(test.TestCase): 'progress': 25, 'minRam': 0, 'server': { - 'id': '42', + 'id': server_uuid, "links": [{ "rel": "self", "href": server_href, @@ -817,10 +820,10 @@ class ImagesControllerTest(test.TestCase): def test_image_filter_server(self): image_service = self.mox.CreateMockAnything() - # 'server' should be converted to 'property-instance_ref' - filters = {'property-instance_ref': 'http://localhost:8774/servers/12'} - request = fakes.HTTPRequest.blank('/v1.1/images?server=' - 'http://localhost:8774/servers/12') + uuid = 'fa95aaf5-ab3b-4cd8-88c0-2be7dd051aaf' + ref = 'http://localhost:8774/servers/' + uuid + filters = {'property-instance_ref': ref} + request = fakes.HTTPRequest.blank('/v1.1/images?server=' + ref) context = request.environ['nova.context'] image_service.index(context, filters=filters).AndReturn([]) self.mox.ReplayAll() @@ -912,10 +915,11 @@ class ImagesControllerTest(test.TestCase): def test_image_detail_filter_server(self): image_service = self.mox.CreateMockAnything() - # 'server' should be converted to 'property-instance_ref' - filters = {'property-instance_ref': 'http://localhost:8774/servers/12'} - request = fakes.HTTPRequest.blank('/v1.1/fake/images/detail?server=' - 'http://localhost:8774/servers/12') + uuid = 'fa95aaf5-ab3b-4cd8-88c0-2be7dd051aaf' + ref = 'http://localhost:8774/servers/' + uuid + url = '/v1.1/fake/images/detail?server=' + ref + filters = {'property-instance_ref': ref} + request = fakes.HTTPRequest.blank(url) context = request.environ['nova.context'] image_service.index(context, filters=filters).AndReturn([]) self.mox.ReplayAll() @@ -991,8 +995,9 @@ class ImagesControllerTest(test.TestCase): class ImageXMLSerializationTest(test.TestCase): TIMESTAMP = "2010-10-11T10:30:22Z" - SERVER_HREF = 'http://localhost/v1.1/servers/123' - SERVER_BOOKMARK = 'http://localhost/servers/123' + SERVER_UUID = 'aa640691-d1a7-4a67-9d3c-d35ee6b3cc74' + SERVER_HREF = 'http://localhost/v1.1/servers/' + SERVER_UUID + SERVER_BOOKMARK = 'http://localhost/servers/' + SERVER_UUID IMAGE_HREF = 'http://localhost/v1.1/fake/images/%s' IMAGE_NEXT = 'http://localhost/v1.1/fake/images?limit=%s&marker=%s' IMAGE_BOOKMARK = 'http://localhost/fake/images/%s' @@ -1009,7 +1014,7 @@ class ImageXMLSerializationTest(test.TestCase): 'status': 'ACTIVE', 'progress': 80, 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1055,7 +1060,7 @@ class ImageXMLSerializationTest(test.TestCase): 'minRam': 10, 'minDisk': 100, 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1124,7 +1129,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1186,7 +1191,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1300,7 +1305,7 @@ class ImageXMLSerializationTest(test.TestCase): 'progress': 80, 'minRam': 256, 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1372,7 +1377,7 @@ class ImageXMLSerializationTest(test.TestCase): 'progress': 80, 'minDisk': 5, 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, @@ -1574,7 +1579,7 @@ class ImageXMLSerializationTest(test.TestCase): 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { - 'id': '1', + 'id': self.SERVER_UUID, 'links': [ { 'href': self.SERVER_HREF, diff --git a/nova/tests/api/openstack/test_server_actions.py b/nova/tests/api/openstack/test_server_actions.py index 95e75015b61b..96712ddbe3c0 100644 --- a/nova/tests/api/openstack/test_server_actions.py +++ b/nova/tests/api/openstack/test_server_actions.py @@ -19,12 +19,17 @@ from nova.tests.api.openstack import fakes FLAGS = flags.FLAGS +FAKE_UUID = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" def return_server_by_id(context, id): return stub_instance(id) +def return_server_by_uuid(context, uuid): + return stub_instance(1, uuid=uuid) + + def instance_update(context, instance_id, kwargs): return stub_instance(instance_id) @@ -47,12 +52,15 @@ def return_server_with_uuid_and_state(vm_state, task_state=None): def stub_instance(id, metadata=None, image_ref="10", flavor_id="1", - name=None, vm_state=None, task_state=None): + name=None, vm_state=None, task_state=None, uuid=None): if metadata is not None: metadata_items = [{'key':k, 'value':v} for k, v in metadata.items()] else: metadata_items = [{'key':'seq', 'value':id}] + if uuid is None: + uuid = FAKE_UUID + inst_type = instance_types.get_instance_type_by_flavor_id(int(flavor_id)) instance = { @@ -89,7 +97,7 @@ def stub_instance(id, metadata=None, image_ref="10", flavor_id="1", "metadata": metadata_items, "access_ip_v4": "", "access_ip_v6": "", - "uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + "uuid": uuid, "virtual_interfaces": [], "progress": 0, } @@ -121,11 +129,13 @@ class ServerActionsTest(test.TestCase): self.stubs = stubout.StubOutForTesting() fakes.stub_out_auth(self.stubs) self.stubs.Set(nova.db.api, 'instance_get', return_server_by_id) + self.stubs.Set(nova.db, 'instance_get_by_uuid', return_server_by_uuid) self.stubs.Set(nova.db.api, 'instance_update', instance_update) fakes.stub_out_glance(self.stubs) fakes.stub_out_nw_api(self.stubs) - fakes.stub_out_compute_api_snapshot(self.stubs) + self.snapshot = fakes.stub_out_compute_api_snapshot(self.stubs) + self.backup = fakes.stub_out_compute_api_backup(self.stubs) service_class = 'nova.image.glance.GlanceImageService' self.service = utils.import_object(service_class) self.context = context.RequestContext(1, None) @@ -133,6 +143,8 @@ class ServerActionsTest(test.TestCase): self.sent_to_glance = {} fakes.stub_out_glance_add_image(self.stubs, self.sent_to_glance) self.flags(allow_instance_snapshots=True) + self.uuid = FAKE_UUID + self.url = '/v1.1/fake/servers/%s/action' % self.uuid def tearDown(self): self.stubs.UnsetAll() @@ -140,7 +152,7 @@ class ServerActionsTest(test.TestCase): def test_server_bad_body(self): body = {} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -149,7 +161,7 @@ class ServerActionsTest(test.TestCase): def test_server_unknown_action(self): body = {'sockTheFox': {'fakekey': '1234'}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -160,19 +172,19 @@ class ServerActionsTest(test.TestCase): mock_method = MockSetAdminPassword() self.stubs.Set(nova.compute.api.API, 'set_admin_password', mock_method) body = {'changePassword': {'adminPass': '1234pass'}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 202) - self.assertEqual(mock_method.instance_id, '1') + self.assertEqual(mock_method.instance_id, self.uuid) self.assertEqual(mock_method.password, '1234pass') def test_server_change_password_xml(self): mock_method = MockSetAdminPassword() self.stubs.Set(nova.compute.api.API, 'set_admin_password', mock_method) - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = "application/xml" req.body = """ @@ -181,12 +193,12 @@ class ServerActionsTest(test.TestCase): adminPass="1234pass"/>""" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 202) - self.assertEqual(mock_method.instance_id, '1') + self.assertEqual(mock_method.instance_id, self.uuid) self.assertEqual(mock_method.password, '1234pass') def test_server_change_password_not_a_string(self): body = {'changePassword': {'adminPass': 1234}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -195,7 +207,7 @@ class ServerActionsTest(test.TestCase): def test_server_change_password_bad_request(self): body = {'changePassword': {'pass': '12345'}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -204,7 +216,7 @@ class ServerActionsTest(test.TestCase): def test_server_change_password_empty_string(self): body = {'changePassword': {'adminPass': ''}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -213,7 +225,7 @@ class ServerActionsTest(test.TestCase): def test_server_change_password_none(self): body = {'changePassword': {'adminPass': None}} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -222,7 +234,7 @@ class ServerActionsTest(test.TestCase): def test_server_reboot_hard(self): body = dict(reboot=dict(type="HARD")) - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -231,7 +243,7 @@ class ServerActionsTest(test.TestCase): def test_server_reboot_soft(self): body = dict(reboot=dict(type="SOFT")) - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -240,7 +252,7 @@ class ServerActionsTest(test.TestCase): def test_server_reboot_incorrect_type(self): body = dict(reboot=dict(type="NOT_A_TYPE")) - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -249,7 +261,7 @@ class ServerActionsTest(test.TestCase): def test_server_reboot_missing_type(self): body = dict(reboot=dict()) - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -266,7 +278,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -285,13 +297,12 @@ class ServerActionsTest(test.TestCase): }, } - state = vm_states.BUILDING - new_return_server = return_server_with_state(state) - self.stubs.Set(nova.db.api, 'instance_get', new_return_server) - self.stubs.Set(nova.db, 'instance_get_by_uuid', - return_server_with_uuid_and_state(state)) + def fake_rebuild(*args, **kwargs): + raise exception.RebuildRequiresActiveInstance - req = webob.Request.blank('/v1.1/fake/servers/1/action') + self.stubs.Set(nova.compute.api.API, 'rebuild', fake_rebuild) + + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -312,7 +323,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -330,7 +341,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -345,7 +356,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -364,7 +375,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -383,7 +394,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -404,7 +415,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -426,7 +437,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.content_type = 'application/json' req.body = json.dumps(body) @@ -436,7 +447,7 @@ class ServerActionsTest(test.TestCase): def test_resize_server(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(resize=dict(flavorRef="http://localhost/3")) @@ -454,7 +465,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(self.resize_called, True) def test_resize_server_no_flavor(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(resize=dict()) @@ -464,7 +475,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(res.status_int, 400) def test_resize_server_no_flavor_ref(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(resize=dict(flavorRef=None)) @@ -474,7 +485,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(res.status_int, 400) def test_confirm_resize_server(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(confirmResize=None) @@ -492,7 +503,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(self.confirm_resize_called, True) def test_confirm_resize_migration_not_found(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(confirmResize=None) @@ -510,7 +521,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(res.status_int, 400) def test_revert_resize_migration_not_found(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(revertResize=None) @@ -528,7 +539,7 @@ class ServerActionsTest(test.TestCase): self.assertEqual(res.status_int, 400) def test_revert_resize_server(self): - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.content_type = 'application/json' req.method = 'POST' body_dict = dict(revertResize=None) @@ -551,7 +562,7 @@ class ServerActionsTest(test.TestCase): 'name': 'Snapshot 1', }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -559,6 +570,9 @@ class ServerActionsTest(test.TestCase): self.assertEqual(202, response.status_int) location = response.headers['Location'] self.assertEqual('http://localhost/v1.1/fake/images/123', location) + server_location = self.snapshot.extra_props_last_call['instance_ref'] + expected_server_location = 'http://localhost/v1.1/servers/' + self.uuid + self.assertEqual(expected_server_location, server_location) def test_create_image_snapshots_disabled(self): """Don't permit a snapshot if the allow_instance_snapshots flag is @@ -570,7 +584,7 @@ class ServerActionsTest(test.TestCase): 'name': 'Snapshot 1', }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -584,7 +598,7 @@ class ServerActionsTest(test.TestCase): 'metadata': {'key': 'asdf'}, }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -602,7 +616,7 @@ class ServerActionsTest(test.TestCase): } for num in range(FLAGS.quota_metadata_items + 1): body['createImage']['metadata']['foo%i' % num] = "bar" - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -613,7 +627,7 @@ class ServerActionsTest(test.TestCase): body = { 'createImage': {}, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -627,7 +641,7 @@ class ServerActionsTest(test.TestCase): 'metadata': 'henry', }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -640,7 +654,7 @@ class ServerActionsTest(test.TestCase): raise exception.InstanceSnapshotting self.stubs.Set(nova.compute.API, 'snapshot', snapshot) - req = webob.Request.blank('/v1.1/fakes/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps({ "createImage": { @@ -663,13 +677,16 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" response = req.get_response(fakes.wsgi_app()) self.assertEqual(202, response.status_int) self.assertTrue(response.headers['Location']) + server_location = self.backup.extra_props_last_call['instance_ref'] + expected_server_location = 'http://localhost/v1.1/servers/' + self.uuid + self.assertEqual(expected_server_location, server_location) def test_create_backup_admin_api_off(self): """The happy path for creating backups""" @@ -683,7 +700,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -702,7 +719,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -723,7 +740,7 @@ class ServerActionsTest(test.TestCase): } for num in range(FLAGS.quota_metadata_items + 1): body['createBackup']['metadata']['foo%i' % num] = "bar" - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -741,7 +758,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -759,7 +776,7 @@ class ServerActionsTest(test.TestCase): }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -777,7 +794,7 @@ class ServerActionsTest(test.TestCase): 'rotation': 1, }, } - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -789,7 +806,7 @@ class ServerActionsTest(test.TestCase): self.flags(allow_admin_api=True) body = {'createBackup': 'go'} - req = webob.Request.blank('/v1.1/fake/servers/1/action') + req = webob.Request.blank(self.url) req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" diff --git a/nova/tests/api/openstack/test_server_metadata.py b/nova/tests/api/openstack/test_server_metadata.py index 52f0aafc4c13..bcacc2388258 100644 --- a/nova/tests/api/openstack/test_server_metadata.py +++ b/nova/tests/api/openstack/test_server_metadata.py @@ -23,6 +23,7 @@ import nova.db.api from nova import exception from nova import flags from nova import test +from nova import utils from nova.tests.api.openstack import fakes @@ -79,15 +80,18 @@ class ServerMetaDataTest(test.TestCase): super(ServerMetaDataTest, self).setUp() fakes.stub_out_key_pair_funcs(self.stubs) self.stubs.Set(nova.db.api, 'instance_get', return_server) + self.stubs.Set(nova.db, 'instance_get_by_uuid', return_server) self.stubs.Set(nova.db.api, 'instance_metadata_get', return_server_metadata) self.controller = server_metadata.Controller() + self.uuid = utils.gen_uuid() + self.url = '/v1.1/fake/servers/%s/metadata' % self.uuid def test_index(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') - res_dict = self.controller.index(req, '1') + req = fakes.HTTPRequest.blank(self.url) + res_dict = self.controller.index(req, self.uuid) expected = { 'metadata': { @@ -101,75 +105,75 @@ class ServerMetaDataTest(test.TestCase): def test_index_nonexistant_server(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_server_nonexistant) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/100/metadata') + req = fakes.HTTPRequest.blank(self.url) self.assertRaises(webob.exc.HTTPNotFound, - self.controller.index, req, '100') + self.controller.index, req, self.url) def test_index_no_data(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_empty_server_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') - res_dict = self.controller.index(req, '1') + req = fakes.HTTPRequest.blank(self.url) + res_dict = self.controller.index(req, self.uuid) expected = {'metadata': {}} self.assertEqual(expected, res_dict) def test_show(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key2') - res_dict = self.controller.show(req, '1', 'key2') + req = fakes.HTTPRequest.blank(self.url + '/key2') + res_dict = self.controller.show(req, self.uuid, 'key2') expected = {'meta': {'key2': 'value2'}} self.assertEqual(expected, res_dict) def test_show_nonexistant_server(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_server_nonexistant) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/100/metadata/key2') + req = fakes.HTTPRequest.blank(self.url + '/key2') self.assertRaises(webob.exc.HTTPNotFound, - self.controller.show, req, '100', 'key2') + self.controller.show, req, self.uuid, 'key2') def test_show_meta_not_found(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_empty_server_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key6') + req = fakes.HTTPRequest.blank(self.url + '/key6') self.assertRaises(webob.exc.HTTPNotFound, - self.controller.show, req, '1', 'key6') + self.controller.show, req, self.uuid, 'key6') def test_delete(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_server_metadata) self.stubs.Set(nova.db.api, 'instance_metadata_delete', delete_server_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key2') + req = fakes.HTTPRequest.blank(self.url + '/key2') req.method = 'DELETE' - res = self.controller.delete(req, '1', 'key2') + res = self.controller.delete(req, self.uuid, 'key2') self.assertEqual(None, res) def test_delete_nonexistant_server(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_nonexistant) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key1') + req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'DELETE' self.assertRaises(webob.exc.HTTPNotFound, - self.controller.delete, req, '1', 'key1') + self.controller.delete, req, self.uuid, 'key1') def test_delete_meta_not_found(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_empty_server_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key6') + req = fakes.HTTPRequest.blank(self.url + '/key6') req.method = 'DELETE' self.assertRaises(webob.exc.HTTPNotFound, - self.controller.delete, req, '1', 'key6') + self.controller.delete, req, self.uuid, 'key6') def test_create(self): self.stubs.Set(nova.db.api, 'instance_metadata_get', return_server_metadata) self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'POST' req.content_type = "application/json" body = {"metadata": {"key9": "value9"}} req.body = json.dumps(body) - res_dict = self.controller.create(req, '1', body) + res_dict = self.controller.create(req, self.uuid, body) body['metadata'].update({ "key1": "value1", @@ -181,28 +185,28 @@ class ServerMetaDataTest(test.TestCase): def test_create_empty_body(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'POST' req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, req, '1', None) + self.controller.create, req, self.uuid, None) def test_create_nonexistant_server(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_nonexistant) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/100/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'POST' body = {"metadata": {"key1": "value1"}} req.body = json.dumps(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, - self.controller.create, req, '100', body) + self.controller.create, req, self.uuid, body) def test_update_all(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.content_type = "application/json" expected = { @@ -212,49 +216,49 @@ class ServerMetaDataTest(test.TestCase): }, } req.body = json.dumps(expected) - res_dict = self.controller.update_all(req, '1', expected) + res_dict = self.controller.update_all(req, self.uuid, expected) self.assertEqual(expected, res_dict) def test_update_all_empty_container(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': {}} req.body = json.dumps(expected) - res_dict = self.controller.update_all(req, '1', expected) + res_dict = self.controller.update_all(req, self.uuid, expected) self.assertEqual(expected, res_dict) def test_update_all_malformed_container(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.content_type = "application/json" expected = {'meta': {}} req.body = json.dumps(expected) self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update_all, req, '1', expected) + self.controller.update_all, req, self.uuid, expected) def test_update_all_malformed_data(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.content_type = "application/json" expected = {'metadata': ['asdf']} req.body = json.dumps(expected) self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update_all, req, '1', expected) + self.controller.update_all, req, self.uuid, expected) def test_update_all_nonexistant_server(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_nonexistant) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/100/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.content_type = "application/json" body = {'metadata': {'key10': 'value10'}} @@ -266,12 +270,12 @@ class ServerMetaDataTest(test.TestCase): def test_update_item(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key1') + req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1"}} req.body = json.dumps(body) req.headers["content-type"] = "application/json" - res_dict = self.controller.update(req, '1', 'key1', body) + res_dict = self.controller.update(req, self.uuid, 'key1', body) expected = {'meta': {'key1': 'value1'}} self.assertEqual(expected, res_dict) @@ -284,41 +288,41 @@ class ServerMetaDataTest(test.TestCase): req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPNotFound, - self.controller.update, req, '1', 'key1', body) + self.controller.update, req, self.uuid, 'key1', body) def test_update_item_empty_body(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key1') + req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update, req, '1', 'key1', None) + self.controller.update, req, self.uuid, 'key1', None) def test_update_item_too_many_keys(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/key1') + req = fakes.HTTPRequest.blank(self.url + '/key1') req.method = 'PUT' body = {"meta": {"key1": "value1", "key2": "value2"}} req.body = json.dumps(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update, req, '1', 'key1', body) + self.controller.update, req, self.uuid, 'key1', body) def test_update_item_body_uri_mismatch(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', return_create_instance_metadata) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata/bad') + req = fakes.HTTPRequest.blank(self.url + '/bad') req.method = 'PUT' body = {"meta": {"key1": "value1"}} req.body = json.dumps(body) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.update, req, '1', 'bad', body) + self.controller.update, req, self.uuid, 'bad', body) def test_too_many_metadata_items_on_create(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', @@ -326,13 +330,13 @@ class ServerMetaDataTest(test.TestCase): data = {"metadata": {}} for num in range(FLAGS.quota_metadata_items + 1): data['metadata']['key%i' % num] = "blah" - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'POST' req.body = json.dumps(data) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, - self.controller.create, req, '1', data) + self.controller.create, req, self.uuid, data) def test_too_many_metadata_items_on_update_item(self): self.stubs.Set(nova.db.api, 'instance_metadata_update', @@ -340,10 +344,10 @@ class ServerMetaDataTest(test.TestCase): data = {"metadata": {}} for num in range(FLAGS.quota_metadata_items + 1): data['metadata']['key%i' % num] = "blah" - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/metadata') + req = fakes.HTTPRequest.blank(self.url) req.method = 'PUT' req.body = json.dumps(data) req.headers["content-type"] = "application/json" self.assertRaises(webob.exc.HTTPRequestEntityTooLarge, - self.controller.update_all, req, '1', data) + self.controller.update_all, req, self.uuid, data) diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py index f3c61e48eaac..d422a2a2899a 100644 --- a/nova/tests/api/openstack/test_servers.py +++ b/nova/tests/api/openstack/test_servers.py @@ -50,7 +50,8 @@ from nova import utils FLAGS = flags.FLAGS -FAKE_UUID = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' +FAKE_UUIDS = {0: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'} +FAKE_UUID = FAKE_UUIDS[0] NS = "{http://docs.openstack.org/compute/api/v1.1}" ATOMNS = "{http://www.w3.org/2005/Atom}" XPATH_NS = { @@ -59,6 +60,12 @@ XPATH_NS = { } +def get_fake_uuid(token=0): + if not token in FAKE_UUIDS: + FAKE_UUIDS[token] = str(utils.gen_uuid()) + return FAKE_UUIDS[token] + + def fake_gen_uuid(): return FAKE_UUID @@ -79,8 +86,9 @@ def return_server_with_attributes(**kwargs): def return_server_with_state(vm_state, task_state=None): - def _return_server(context, id): - return stub_instance(id, vm_state=vm_state, task_state=task_state) + def _return_server(context, uuid): + return stub_instance(1, uuid=uuid, vm_state=vm_state, + task_state=task_state) return _return_server @@ -94,7 +102,11 @@ def return_server_with_uuid_and_state(vm_state, task_state): def return_servers(context, *args, **kwargs): - return [stub_instance(i, 'fake', 'fake') for i in xrange(5)] + servers = [] + for i in xrange(5): + server = stub_instance(i, 'fake', 'fake', uuid=get_fake_uuid(i)) + servers.append(server) + return servers def return_servers_by_reservation(context, reservation_id=""): @@ -234,6 +246,8 @@ class ServersControllerTest(test.TestCase): self.stubs.Set(nova.db.api, 'instance_get', return_server_by_id) self.stubs.Set(nova.db, 'instance_get_by_uuid', return_server_by_uuid) + self.stubs.Set(nova.db.api, 'instance_get_by_uuid', + return_server_by_uuid) self.stubs.Set(nova.db.api, 'instance_get_all_by_project', return_servers) self.stubs.Set(nova.db.api, 'instance_add_security_group', @@ -276,8 +290,7 @@ class ServersControllerTest(test.TestCase): """ req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) res_dict = self.controller.show(req, FAKE_UUID) - self.assertEqual(res_dict['server']['id'], 1) - self.assertEqual(res_dict['server']['uuid'], FAKE_UUID) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) def test_get_server_by_id(self): image_bookmark = "http://localhost/fake/images/10" @@ -302,12 +315,12 @@ class ServersControllerTest(test.TestCase): fakes.stub_out_nw_api_get_floating_ips_by_fixed_address(self.stubs, get_floats) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + uuid = FAKE_UUID + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % uuid) + res_dict = self.controller.show(req, uuid) expected_server = { "server": { - "id": 1, - "uuid": FAKE_UUID, + "id": uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -358,12 +371,11 @@ class ServersControllerTest(test.TestCase): "links": [ { "rel": "self", - #FIXME(wwolf) Do we want the links to be id or uuid? - "href": "http://localhost/v1.1/fake/servers/1", + "href": "http://localhost/v1.1/fake/servers/%s" % uuid, }, { "rel": "bookmark", - "href": "http://localhost/fake/servers/1", + "href": "http://localhost/fake/servers/%s" % uuid, }, ], } @@ -397,12 +409,12 @@ class ServersControllerTest(test.TestCase): vm_state=vm_states.ACTIVE, progress=100) self.stubs.Set(nova.db.api, 'instance_get', new_return_server) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + uuid = FAKE_UUID + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % uuid) + res_dict = self.controller.show(req, uuid) expected_server = { "server": { - "id": 1, - "uuid": FAKE_UUID, + "id": uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -453,11 +465,11 @@ class ServersControllerTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/fake/servers/1", + "href": "http://localhost/v1.1/fake/servers/%s" % uuid, }, { "rel": "bookmark", - "href": "http://localhost/fake/servers/1", + "href": "http://localhost/fake/servers/%s" % uuid, }, ], } @@ -494,12 +506,12 @@ class ServersControllerTest(test.TestCase): image_ref=image_ref, flavor_id=flavor_id, progress=100) self.stubs.Set(nova.db.api, 'instance_get', new_return_server) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + uuid = FAKE_UUID + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % uuid) + res_dict = self.controller.show(req, uuid) expected_server = { "server": { - "id": 1, - "uuid": FAKE_UUID, + "id": uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -550,11 +562,11 @@ class ServersControllerTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/fake/servers/1", + "href": "http://localhost/v1.1/fake/servers/%s" % uuid, }, { "rel": "bookmark", - "href": "http://localhost/fake/servers/1", + "href": "http://localhost/fake/servers/%s" % uuid, }, ], } @@ -579,10 +591,10 @@ class ServersControllerTest(test.TestCase): self.stubs.Set(nova.db.api, 'instance_get', return_server_by_id) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) + res_dict = self.controller.show(req, FAKE_UUID) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server1') def test_get_server_by_id_with_addresses(self): @@ -606,10 +618,10 @@ class ServersControllerTest(test.TestCase): fakes.stub_out_nw_api_get_floating_ips_by_fixed_address(self.stubs, get_floats) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) + res_dict = self.controller.show(req, FAKE_UUID) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server1') addresses = res_dict['server']['addresses'] expected = { @@ -652,10 +664,10 @@ class ServersControllerTest(test.TestCase): fakes.stub_out_nw_api_get_floating_ips_by_fixed_address(self.stubs, get_floats) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - res_dict = self.controller.show(req, '1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) + res_dict = self.controller.show(req, FAKE_UUID) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server1') addresses = res_dict['server']['addresses'] expected = { @@ -700,8 +712,8 @@ class ServersControllerTest(test.TestCase): fakes.stub_out_nw_api_get_floating_ips_by_fixed_address(self.stubs, get_floats) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/ips') - res_dict = self.ips_controller.index(req, '1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s/ips' % FAKE_UUID) + res_dict = self.ips_controller.index(req, FAKE_UUID) expected = { 'addresses': { @@ -749,8 +761,9 @@ class ServersControllerTest(test.TestCase): fakes.stub_out_nw_api_get_floating_ips_by_fixed_address(self.stubs, get_floats) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/ips/network_2') - res_dict = self.ips_controller.show(req, '1', 'network_2') + url = '/v1.1/fake/servers/%s/ips/network_2' % FAKE_UUID + req = fakes.HTTPRequest.blank(url) + res_dict = self.ips_controller.show(req, FAKE_UUID, 'network_2') expected = { 'network_2': [ @@ -767,9 +780,10 @@ class ServersControllerTest(test.TestCase): self.assertTrue(ip in res_dict['network_2']) def test_get_server_addresses_nonexistant_network(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1/ips/network_0') - self.assertRaises(webob.exc.HTTPNotFound, - self.ips_controller.show, req, '1', 'network_0') + url = '/v1.1/fake/servers/%s/ips/network_0' % FAKE_UUID + req = fakes.HTTPRequest.blank(url) + self.assertRaises(webob.exc.HTTPNotFound, self.ips_controller.show, + req, FAKE_UUID, 'network_0') def test_get_server_addresses_nonexistant_server(self): def fake(*args, **kwargs): @@ -778,12 +792,13 @@ class ServersControllerTest(test.TestCase): def fake_instance_get(*args, **kwargs): raise nova.exception.InstanceNotFound() - self.stubs.Set(nova.db.api, 'instance_get', fake_instance_get) + self.stubs.Set(nova.db.api, 'instance_get_by_uuid', fake_instance_get) self.stubs.Set(nova.network.API, 'get_instance_nw_info', fake) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/600/ips') + server_id = str(utils.gen_uuid()) + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s/ips' % server_id) self.assertRaises(webob.exc.HTTPNotFound, - self.ips_controller.index, req, '600') + self.ips_controller.index, req, server_id) def test_get_server_list_with_reservation_id(self): self.stubs.Set(nova.db.api, 'instance_get_all_by_reservation', @@ -844,7 +859,7 @@ class ServersControllerTest(test.TestCase): self.assertEqual(len(res_dict['servers']), 5) for i, s in enumerate(res_dict['servers']): - self.assertEqual(s['id'], i) + self.assertEqual(s['id'], get_fake_uuid(i)) self.assertEqual(s['name'], 'server%d' % i) self.assertEqual(s.get('image', None), None) @@ -866,14 +881,16 @@ class ServersControllerTest(test.TestCase): res_dict = self.controller.index(req) servers = res_dict['servers'] - self.assertEqual([s['id'] for s in servers], [0, 1, 2]) + self.assertEqual([s['id'] for s in servers], + [get_fake_uuid(i) for i in xrange(len(servers))]) servers_links = res_dict['servers_links'] self.assertEqual(servers_links[0]['rel'], 'next') href_parts = urlparse.urlparse(servers_links[0]['href']) self.assertEqual('/v1.1/fake/servers', href_parts.path) params = urlparse.parse_qs(href_parts.query) - self.assertDictMatch({'limit': ['3'], 'marker': ['2']}, params) + expected_params = {'limit': ['3'], 'marker': [get_fake_uuid(2)]} + self.assertDictMatch(expected_params, params) def test_get_servers_with_limit_bad_value(self): req = fakes.HTTPRequest.blank('/v1.1/fake/servers?limit=aaa') @@ -885,7 +902,8 @@ class ServersControllerTest(test.TestCase): res = self.controller.detail(req) servers = res['servers'] - self.assertEqual([s['id'] for s in servers], [0, 1, 2]) + self.assertEqual([s['id'] for s in servers], + [get_fake_uuid(i) for i in xrange(len(servers))]) servers_links = res['servers_links'] self.assertEqual(servers_links[0]['rel'], 'next') @@ -893,7 +911,8 @@ class ServersControllerTest(test.TestCase): href_parts = urlparse.urlparse(servers_links[0]['href']) self.assertEqual('/v1.1/fake/servers', href_parts.path) params = urlparse.parse_qs(href_parts.query) - self.assertDictMatch({'limit': ['3'], 'marker': ['2']}, params) + expected = {'limit': ['3'], 'marker': [get_fake_uuid(2)]} + self.assertDictMatch(expected, params) def test_get_server_details_with_limit_bad_value(self): req = fakes.HTTPRequest.blank('/v1.1/fake/servers/detail?limit=aaa') @@ -906,7 +925,8 @@ class ServersControllerTest(test.TestCase): res = self.controller.detail(req) servers = res['servers'] - self.assertEqual([s['id'] for s in servers], [0, 1, 2]) + self.assertEqual([s['id'] for s in servers], + [get_fake_uuid(i) for i in xrange(len(servers))]) servers_links = res['servers_links'] self.assertEqual(servers_links[0]['rel'], 'next') @@ -914,8 +934,9 @@ class ServersControllerTest(test.TestCase): href_parts = urlparse.urlparse(servers_links[0]['href']) self.assertEqual('/v1.1/fake/servers', href_parts.path) params = urlparse.parse_qs(href_parts.query) + self.assertDictMatch({'limit': ['3'], 'blah': ['2:t'], - 'marker': ['2']}, params) + 'marker': [get_fake_uuid(2)]}, params) def test_get_servers_with_too_big_limit(self): req = fakes.HTTPRequest.blank('/v1.1/fake/servers?limit=30') @@ -923,17 +944,19 @@ class ServersControllerTest(test.TestCase): self.assertTrue('servers_links' not in res_dict) def test_get_servers_with_bad_limit(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers?limit=asdf&offset=1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers?limit=asdf') self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, req) def test_get_servers_with_marker(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers?marker=2') + url = '/v1.1/fake/servers?marker=%s' % get_fake_uuid(2) + req = fakes.HTTPRequest.blank(url) servers = self.controller.index(req)['servers'] self.assertEqual([s['name'] for s in servers], ["server3", "server4"]) def test_get_servers_with_limit_and_marker(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers?limit=2&marker=1') + url = '/v1.1/fake/servers?limit=2&marker=%s' % get_fake_uuid(1) + req = fakes.HTTPRequest.blank(url) servers = self.controller.index(req)['servers'] self.assertEqual([s['name'] for s in servers], ['server2', 'server3']) @@ -943,20 +966,10 @@ class ServersControllerTest(test.TestCase): self.controller.index, req) def test_get_servers_with_bad_option(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): - return [stub_instance(100)] - - self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) - - req = fakes.HTTPRequest.blank('/v1.1/fake/servers?unknownoption=whee') - servers = self.controller.index(req)['servers'] - self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) - - def test_get_servers_with_bad_option(self): - # 1.1 API also ignores unknown options - def fake_get_all(compute_self, context, search_opts=None): - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -964,14 +977,16 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_allows_image(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('image' in search_opts) self.assertEqual(search_opts['image'], '12345') - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) self.flags(allow_admin_api=False) @@ -980,7 +995,7 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_tenant_id_filter_converts_to_project_id_for_admin(self): def fake_get_all(context, filters=None, instances=None): @@ -1000,12 +1015,14 @@ class ServersControllerTest(test.TestCase): self.assertTrue('servers' in res) def test_get_servers_allows_flavor(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('flavor' in search_opts) # flavor is an integer ID self.assertEqual(search_opts['flavor'], '12345') - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) self.flags(allow_admin_api=False) @@ -1014,14 +1031,16 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_allows_status(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('vm_state' in search_opts) self.assertEqual(search_opts['vm_state'], vm_states.ACTIVE) - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) self.flags(allow_admin_api=False) @@ -1030,7 +1049,7 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_invalid_status(self): """Test getting servers by invalid status""" @@ -1039,11 +1058,13 @@ class ServersControllerTest(test.TestCase): self.assertRaises(webob.exc.HTTPBadRequest, self.controller.index, req) def test_get_servers_allows_name(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('name' in search_opts) self.assertEqual(search_opts['name'], 'whee.*') - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) self.flags(allow_admin_api=False) @@ -1052,16 +1073,18 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_allows_changes_since(self): + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('changes-since' in search_opts) changes_since = datetime.datetime(2011, 1, 24, 17, 8, 1) self.assertEqual(search_opts['changes-since'], changes_since) self.assertTrue('deleted' not in search_opts) - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1070,7 +1093,7 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_allows_changes_since_bad_value(self): params = 'changes-since=asdf' @@ -1086,6 +1109,8 @@ class ServersControllerTest(test.TestCase): self.flags(allow_admin_api=False) + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) # Allowed by user @@ -1094,7 +1119,7 @@ class ServersControllerTest(test.TestCase): # Allowed only by admins with admin API on self.assertFalse('ip' in search_opts) self.assertFalse('unknown_option' in search_opts) - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1105,7 +1130,7 @@ class ServersControllerTest(test.TestCase): servers = res['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_unknown_or_admin_options2(self): """Test getting servers by admin-only or unknown options. @@ -1116,6 +1141,8 @@ class ServersControllerTest(test.TestCase): self.flags(allow_admin_api=True) + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) # Allowed by user @@ -1124,7 +1151,7 @@ class ServersControllerTest(test.TestCase): # Allowed only by admins with admin API on self.assertFalse('ip' in search_opts) self.assertFalse('unknown_option' in search_opts) - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1134,7 +1161,7 @@ class ServersControllerTest(test.TestCase): servers = res['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_unknown_or_admin_options3(self): """Test getting servers by admin-only or unknown options. @@ -1144,6 +1171,8 @@ class ServersControllerTest(test.TestCase): self.flags(allow_admin_api=True) + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) # Allowed by user @@ -1152,7 +1181,7 @@ class ServersControllerTest(test.TestCase): # Allowed only by admins with admin API on self.assertTrue('ip' in search_opts) self.assertTrue('unknown_option' in search_opts) - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1162,7 +1191,7 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_admin_allows_ip(self): """Test getting servers by ip with admin_api enabled and @@ -1170,11 +1199,13 @@ class ServersControllerTest(test.TestCase): """ self.flags(allow_admin_api=True) + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('ip' in search_opts) self.assertEqual(search_opts['ip'], '10\..*') - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1183,7 +1214,7 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_get_servers_admin_allows_ip6(self): """Test getting servers by ip6 with admin_api enabled and @@ -1191,11 +1222,13 @@ class ServersControllerTest(test.TestCase): """ self.flags(allow_admin_api=True) + server_uuid = str(utils.gen_uuid()) + def fake_get_all(compute_self, context, search_opts=None): self.assertNotEqual(search_opts, None) self.assertTrue('ip6' in search_opts) self.assertEqual(search_opts['ip6'], 'ffff.*') - return [stub_instance(100)] + return [stub_instance(100, uuid=server_uuid)] self.stubs.Set(nova.compute.API, 'get_all', fake_get_all) @@ -1204,21 +1237,21 @@ class ServersControllerTest(test.TestCase): servers = self.controller.index(req)['servers'] self.assertEqual(len(servers), 1) - self.assertEqual(servers[0]['id'], 100) + self.assertEqual(servers[0]['id'], server_uuid) def test_update_server_no_body(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) req.method = 'PUT' self.assertRaises(webob.exc.HTTPUnprocessableEntity, - self.controller.update, req, '1', None) + self.controller.update, req, FAKE_UUID, None) def test_update_server_all_attributes(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_with_attributes(name='server_test', access_ipv4='0.0.0.0', access_ipv6='beef::0123')) - req = fakes.HTTPRequest.blank('/v1.1/123/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/123/servers/%s' % FAKE_UUID) req.method = 'PUT' req.content_type = 'application/json' body = {'server': { @@ -1227,9 +1260,9 @@ class ServersControllerTest(test.TestCase): 'accessIPv6': 'beef::0123', }} req.body = json.dumps(body) - res_dict = self.controller.update(req, '1', body) + res_dict = self.controller.update(req, FAKE_UUID, body) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server_test') self.assertEqual(res_dict['server']['accessIPv4'], '0.0.0.0') self.assertEqual(res_dict['server']['accessIPv6'], 'beef::0123') @@ -1237,40 +1270,40 @@ class ServersControllerTest(test.TestCase): def test_update_server_name(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_with_attributes(name='server_test')) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) req.method = 'PUT' req.content_type = 'application/json' body = {'server': {'name': 'server_test'}} req.body = json.dumps(body) - res_dict = self.controller.update(req, '1', body) + res_dict = self.controller.update(req, FAKE_UUID, body) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server_test') def test_update_server_access_ipv4(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_with_attributes(access_ipv4='0.0.0.0')) - req = fakes.HTTPRequest.blank('/v1.1/123/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/123/servers/%s' % FAKE_UUID) req.method = 'PUT' req.content_type = 'application/json' body = {'server': {'accessIPv4': '0.0.0.0'}} req.body = json.dumps(body) - res_dict = self.controller.update(req, '1', body) + res_dict = self.controller.update(req, FAKE_UUID, body) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['accessIPv4'], '0.0.0.0') def test_update_server_access_ipv6(self): self.stubs.Set(nova.db.api, 'instance_get', return_server_with_attributes(access_ipv6='beef::0123')) - req = fakes.HTTPRequest.blank('/v1.1/123/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/123/servers/%s' % FAKE_UUID) req.method = 'PUT' req.content_type = 'application/json' body = {'server': {'accessIPv6': 'beef::0123'}} req.body = json.dumps(body) - res_dict = self.controller.update(req, '1', body) + res_dict = self.controller.update(req, FAKE_UUID, body) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['accessIPv6'], 'beef::0123') def test_update_server_adminPass_ignored(self): @@ -1286,13 +1319,13 @@ class ServersControllerTest(test.TestCase): self.stubs.Set(nova.db.api, 'instance_get', return_server_with_attributes(name='server_test')) - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) req.method = 'PUT' req.content_type = "application/json" req.body = json.dumps(body) - res_dict = self.controller.update(req, '1', body) + res_dict = self.controller.update(req, FAKE_UUID, body) - self.assertEqual(res_dict['server']['id'], 1) + self.assertEqual(res_dict['server']['id'], FAKE_UUID) self.assertEqual(res_dict['server']['name'], 'server_test') def test_get_all_server_details(self): @@ -1318,7 +1351,7 @@ class ServersControllerTest(test.TestCase): res_dict = self.controller.detail(req) for i, s in enumerate(res_dict['servers']): - self.assertEqual(s['id'], i) + self.assertEqual(s['id'], get_fake_uuid(i)) self.assertEqual(s['hostId'], '') self.assertEqual(s['name'], 'server%d' % i) self.assertEqual(s['image'], expected_image) @@ -1335,7 +1368,8 @@ class ServersControllerTest(test.TestCase): ''' def return_servers_with_host(context, *args, **kwargs): - return [stub_instance(i, 'fake', 'fake', i % 2) + return [stub_instance(i, 'fake', 'fake', i % 2, + uuid=get_fake_uuid(i)) for i in xrange(5)] self.stubs.Set(nova.db.api, 'instance_get_all_by_filters', @@ -1350,12 +1384,12 @@ class ServersControllerTest(test.TestCase): self.assertNotEqual(host_ids[0], host_ids[1]) for i, s in enumerate(server_list): - self.assertEqual(s['id'], i) + self.assertEqual(s['id'], get_fake_uuid(i)) self.assertEqual(s['hostId'], host_ids[i % 2]) self.assertEqual(s['name'], 'server%d' % i) def test_delete_server_instance(self): - req = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') + req = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) req.method = 'DELETE' self.server_delete_called = False @@ -1366,7 +1400,7 @@ class ServersControllerTest(test.TestCase): self.stubs.Set(nova.db.api, 'instance_destroy', instance_destroy_mock) - self.controller.delete(req, '1') + self.controller.delete(req, FAKE_UUID) self.assertEqual(self.server_delete_called, True) @@ -1381,10 +1415,11 @@ class ServerStatusTest(test.TestCase): def _get_with_state(self, vm_state, task_state=None): new_server = return_server_with_state(vm_state, task_state) + self.stubs.Set(nova.db, 'instance_get_by_uuid', new_server) self.stubs.Set(nova.db.api, 'instance_get', new_server) - request = fakes.HTTPRequest.blank('/v1.1/fake/servers/1') - return self.controller.show(request, '1') + request = fakes.HTTPRequest.blank('/v1.1/fake/servers/%s' % FAKE_UUID) + return self.controller.show(request, FAKE_UUID) def test_active(self): response = self._get_with_state(vm_states.ACTIVE) @@ -1530,10 +1565,9 @@ class ServersControllerCreateTest(test.TestCase): self.assertEqual(FLAGS.password_length, len(server['adminPass'])) self.assertEqual('server_test', server['name']) - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertEqual('2', server['flavor']['id']) self.assertEqual('3', server['image']['id']) - self.assertEqual(FAKE_UUID, server['uuid']) def test_create_multiple_instances(self): """Test creating multiple instances but not asking for @@ -1700,7 +1734,7 @@ class ServersControllerCreateTest(test.TestCase): server = res['server'] self.assertEqual(FLAGS.password_length, len(server['adminPass'])) - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertEqual(0, server['progress']) self.assertEqual('server_test', server['name']) self.assertEqual(expected_flavor, server['flavor']) @@ -1756,7 +1790,7 @@ class ServersControllerCreateTest(test.TestCase): server = res['server'] self.assertEqual(FLAGS.password_length, len(server['adminPass'])) - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertEqual("BUILD", server["status"]) self.assertEqual(0, server['progress']) self.assertEqual('server_test', server['name']) @@ -1863,7 +1897,7 @@ class ServersControllerCreateTest(test.TestCase): res = self.controller.create(req, body) server = res['server'] - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertTrue(server['config_drive']) def test_create_instance_with_config_drive_as_id(self): @@ -1891,7 +1925,7 @@ class ServersControllerCreateTest(test.TestCase): res = self.controller.create(req, body) server = res['server'] - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertTrue(server['config_drive']) self.assertEqual(2, server['config_drive']) @@ -1945,7 +1979,7 @@ class ServersControllerCreateTest(test.TestCase): res = self.controller.create(req, body) server = res['server'] - self.assertEqual(1, server['id']) + self.assertEqual(FAKE_UUID, server['id']) self.assertFalse(server['config_drive']) def test_create_instance_bad_href(self): @@ -2570,6 +2604,7 @@ class ServersViewBuilderTest(test.TestCase): super(ServersViewBuilderTest, self).setUp() self.flags(use_ipv6=True) self.instance = self._get_instance() + self.uuid = self.instance['uuid'] self.view_builder = self._get_view_builder() public_ip = '192.168.0.3' @@ -2656,17 +2691,16 @@ class ServersViewBuilderTest(test.TestCase): def test_build_server(self): expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "name": "test_server", "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -2678,17 +2712,17 @@ class ServersViewBuilderTest(test.TestCase): def test_build_server_with_project_id(self): expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "name": "test_server", "links": [ { "rel": "self", - "href": "http://localhost/v1.1/fake/servers/1", + "href": "http://localhost/v1.1/fake/servers/%s" % + self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/fake/servers/1", + "href": "http://localhost/fake/servers/%s" % self.uuid, }, ], } @@ -2703,8 +2737,7 @@ class ServersViewBuilderTest(test.TestCase): flavor_bookmark = "http://localhost/flavors/1" expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -2745,11 +2778,11 @@ class ServersViewBuilderTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -2766,8 +2799,7 @@ class ServersViewBuilderTest(test.TestCase): flavor_bookmark = "http://localhost/flavors/1" expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -2808,11 +2840,11 @@ class ServersViewBuilderTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -2829,8 +2861,7 @@ class ServersViewBuilderTest(test.TestCase): flavor_bookmark = "http://localhost/flavors/1" expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -2871,11 +2902,11 @@ class ServersViewBuilderTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -2892,8 +2923,7 @@ class ServersViewBuilderTest(test.TestCase): flavor_bookmark = "http://localhost/flavors/1" expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -2934,11 +2964,11 @@ class ServersViewBuilderTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -2958,8 +2988,7 @@ class ServersViewBuilderTest(test.TestCase): flavor_bookmark = "http://localhost/flavors/1" expected_server = { "server": { - "id": 1, - "uuid": self.instance['uuid'], + "id": self.uuid, "user_id": "fake", "tenant_id": "fake", "updated": "2010-11-11T11:00:00Z", @@ -3003,11 +3032,11 @@ class ServersViewBuilderTest(test.TestCase): "links": [ { "rel": "self", - "href": "http://localhost/v1.1/servers/1", + "href": "http://localhost/v1.1/servers/%s" % self.uuid, }, { "rel": "bookmark", - "href": "http://localhost/servers/1", + "href": "http://localhost/servers/%s" % self.uuid, }, ], } @@ -3020,9 +3049,9 @@ class ServersViewBuilderTest(test.TestCase): class ServerXMLSerializationTest(test.TestCase): TIMESTAMP = "2010-10-11T10:30:22Z" - SERVER_HREF = 'http://localhost/v1.1/servers/123' + SERVER_HREF = 'http://localhost/v1.1/servers/%s' % FAKE_UUID SERVER_NEXT = 'http://localhost/v1.1/servers?limit=%s&marker=%s' - SERVER_BOOKMARK = 'http://localhost/servers/123' + SERVER_BOOKMARK = 'http://localhost/servers/%s' % FAKE_UUID IMAGE_BOOKMARK = 'http://localhost/images/5' FLAVOR_BOOKMARK = 'http://localhost/flavors/1' @@ -3035,8 +3064,7 @@ class ServerXMLSerializationTest(test.TestCase): fixture = { "server": { - 'id': 1, - 'uuid': FAKE_UUID, + 'id': FAKE_UUID, 'user_id': 'fake_user_id', 'tenant_id': 'fake_tenant_id', 'created': self.TIMESTAMP, @@ -3114,10 +3142,9 @@ class ServerXMLSerializationTest(test.TestCase): fixture = { "server": { - "id": 1, + "id": FAKE_UUID, "user_id": "fake", "tenant_id": "fake", - "uuid": FAKE_UUID, 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, "progress": 0, @@ -3194,10 +3221,9 @@ class ServerXMLSerializationTest(test.TestCase): expected_image_bookmark = self.IMAGE_BOOKMARK expected_flavor_bookmark = self.FLAVOR_BOOKMARK expected_now = self.TIMESTAMP - expected_uuid = FAKE_UUID server_dict = fixture['server'] - for key in ['name', 'id', 'uuid', 'created', 'accessIPv4', + for key in ['name', 'id', 'created', 'accessIPv4', 'updated', 'progress', 'status', 'hostId', 'accessIPv6']: self.assertEqual(root.get(key), str(server_dict[key])) @@ -3252,8 +3278,7 @@ class ServerXMLSerializationTest(test.TestCase): fixture = { "server": { - "id": 1, - "uuid": FAKE_UUID, + "id": FAKE_UUID, "user_id": "fake", "tenant_id": "fake", 'created': self.TIMESTAMP, @@ -3332,10 +3357,9 @@ class ServerXMLSerializationTest(test.TestCase): expected_image_bookmark = self.IMAGE_BOOKMARK expected_flavor_bookmark = self.FLAVOR_BOOKMARK expected_now = self.TIMESTAMP - expected_uuid = FAKE_UUID server_dict = fixture['server'] - for key in ['name', 'id', 'uuid', 'created', 'accessIPv4', + for key in ['name', 'id', 'created', 'accessIPv4', 'updated', 'progress', 'status', 'hostId', 'accessIPv6', 'adminPass']: self.assertEqual(root.get(key), str(server_dict[key])) @@ -3388,13 +3412,15 @@ class ServerXMLSerializationTest(test.TestCase): def test_index(self): serializer = servers.ServerXMLSerializer() - expected_server_href = 'http://localhost/v1.1/servers/1' - expected_server_bookmark = 'http://localhost/servers/1' - expected_server_href_2 = 'http://localhost/v1.1/servers/2' - expected_server_bookmark_2 = 'http://localhost/servers/2' + uuid1 = get_fake_uuid(1) + uuid2 = get_fake_uuid(2) + expected_server_href = 'http://localhost/v1.1/servers/%s' % uuid1 + expected_server_bookmark = 'http://localhost/servers/%s' % uuid1 + expected_server_href_2 = 'http://localhost/v1.1/servers/%s' % uuid2 + expected_server_bookmark_2 = 'http://localhost/servers/%s' % uuid2 fixture = {"servers": [ { - "id": 1, + "id": get_fake_uuid(1), "name": "test_server", 'links': [ { @@ -3408,7 +3434,7 @@ class ServerXMLSerializationTest(test.TestCase): ], }, { - "id": 2, + "id": get_fake_uuid(2), "name": "test_server_2", 'links': [ { @@ -3443,14 +3469,16 @@ class ServerXMLSerializationTest(test.TestCase): def test_index_with_servers_links(self): serializer = servers.ServerXMLSerializer() - expected_server_href = 'http://localhost/v1.1/servers/1' + uuid1 = get_fake_uuid(1) + uuid2 = get_fake_uuid(2) + expected_server_href = 'http://localhost/v1.1/servers/%s' % uuid1 expected_server_next = self.SERVER_NEXT % (2, 2) - expected_server_bookmark = 'http://localhost/servers/1' - expected_server_href_2 = 'http://localhost/v1.1/servers/2' - expected_server_bookmark_2 = 'http://localhost/servers/2' + expected_server_bookmark = 'http://localhost/servers/%s' % uuid1 + expected_server_href_2 = 'http://localhost/v1.1/servers/%s' % uuid2 + expected_server_bookmark_2 = 'http://localhost/servers/%s' % uuid2 fixture = {"servers": [ { - "id": 1, + "id": get_fake_uuid(1), "name": "test_server", 'links': [ { @@ -3464,7 +3492,7 @@ class ServerXMLSerializationTest(test.TestCase): ], }, { - "id": 2, + "id": get_fake_uuid(2), "name": "test_server_2", 'links': [ { @@ -3511,19 +3539,19 @@ class ServerXMLSerializationTest(test.TestCase): def test_detail(self): serializer = servers.ServerXMLSerializer() - expected_server_href = 'http://localhost/v1.1/servers/1' - expected_server_bookmark = 'http://localhost/servers/1' + uuid1 = get_fake_uuid(1) + expected_server_href = 'http://localhost/v1.1/servers/%s' % uuid1 + expected_server_bookmark = 'http://localhost/servers/%s' % uuid1 expected_image_bookmark = self.IMAGE_BOOKMARK expected_flavor_bookmark = self.FLAVOR_BOOKMARK expected_now = self.TIMESTAMP - expected_uuid = FAKE_UUID - expected_server_href_2 = 'http://localhost/v1.1/servers/2' - expected_server_bookmark_2 = 'http://localhost/servers/2' + uuid2 = get_fake_uuid(2) + expected_server_href_2 = 'http://localhost/v1.1/servers/%s' % uuid2 + expected_server_bookmark_2 = 'http://localhost/servers/%s' % uuid2 fixture = {"servers": [ { - "id": 1, - "uuid": FAKE_UUID, + "id": get_fake_uuid(1), "user_id": "fake", "tenant_id": "fake", 'created': self.TIMESTAMP, @@ -3579,8 +3607,7 @@ class ServerXMLSerializationTest(test.TestCase): ], }, { - "id": 2, - "uuid": FAKE_UUID, + "id": get_fake_uuid(2), "user_id": 'fake', "tenant_id": 'fake', 'created': self.TIMESTAMP, @@ -3638,7 +3665,6 @@ class ServerXMLSerializationTest(test.TestCase): ]} output = serializer.serialize(fixture, 'detail') - print output root = etree.XML(output) xmlutil.validate_schema(root, 'servers') server_elems = root.findall('{0}server'.format(NS)) @@ -3646,7 +3672,7 @@ class ServerXMLSerializationTest(test.TestCase): for i, server_elem in enumerate(server_elems): server_dict = fixture['servers'][i] - for key in ['name', 'id', 'uuid', 'created', 'accessIPv4', + for key in ['name', 'id', 'created', 'accessIPv4', 'updated', 'progress', 'status', 'hostId', 'accessIPv6']: self.assertEqual(server_elem.get(key), str(server_dict[key])) @@ -3701,10 +3727,9 @@ class ServerXMLSerializationTest(test.TestCase): fixture = { "server": { - "id": 1, + "id": FAKE_UUID, "user_id": "fake", "tenant_id": "fake", - "uuid": FAKE_UUID, 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, "progress": 0, @@ -3780,10 +3805,9 @@ class ServerXMLSerializationTest(test.TestCase): expected_image_bookmark = self.IMAGE_BOOKMARK expected_flavor_bookmark = self.FLAVOR_BOOKMARK expected_now = self.TIMESTAMP - expected_uuid = FAKE_UUID server_dict = fixture['server'] - for key in ['name', 'id', 'uuid', 'created', 'accessIPv4', + for key in ['name', 'id', 'created', 'accessIPv4', 'updated', 'progress', 'status', 'hostId', 'accessIPv6']: self.assertEqual(root.get(key), str(server_dict[key])) @@ -3838,8 +3862,7 @@ class ServerXMLSerializationTest(test.TestCase): fixture = { "server": { - "id": 1, - "uuid": FAKE_UUID, + "id": FAKE_UUID, "user_id": "fake", "tenant_id": "fake", 'created': self.TIMESTAMP, @@ -3917,10 +3940,9 @@ class ServerXMLSerializationTest(test.TestCase): expected_image_bookmark = self.IMAGE_BOOKMARK expected_flavor_bookmark = self.FLAVOR_BOOKMARK expected_now = self.TIMESTAMP - expected_uuid = FAKE_UUID server_dict = fixture['server'] - for key in ['name', 'id', 'uuid', 'created', 'accessIPv4', + for key in ['name', 'id', 'created', 'accessIPv4', 'updated', 'progress', 'status', 'hostId', 'accessIPv6', 'adminPass']: self.assertEqual(root.get(key), str(server_dict[key])) diff --git a/nova/tests/api/openstack/test_versions.py b/nova/tests/api/openstack/test_versions.py index 5e6314951b77..a6e30187bc2b 100644 --- a/nova/tests/api/openstack/test_versions.py +++ b/nova/tests/api/openstack/test_versions.py @@ -15,25 +15,29 @@ # License for the specific language governing permissions and limitations # under the License. -import feedparser import json + +import feedparser +from lxml import etree import stubout import webob -from lxml import etree -from nova import context -from nova import test from nova.api.openstack import versions from nova.api.openstack import views from nova.api.openstack import wsgi from nova.api.openstack import xmlutil +from nova import context +from nova import test from nova.tests.api.openstack import common from nova.tests.api.openstack import fakes +from nova import utils + NS = { 'atom': 'http://www.w3.org/2005/Atom', 'ns': 'http://docs.openstack.org/compute/api/v1.1' } + VERSIONS = { "v1.1": { "id": "v1.1", @@ -382,14 +386,15 @@ class VersionsTest(test.TestCase): Make sure multi choice responses do not have content-type application/atom+xml (should use default of json) """ - req = webob.Request.blank('/servers/2') + req = webob.Request.blank('/servers') req.accept = "application/atom+xml" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) self.assertEqual(res.content_type, "application/json") def test_multi_choice_server(self): - req = webob.Request.blank('/servers/2') + uuid = str(utils.gen_uuid()) + req = webob.Request.blank('/servers/' + uuid) req.accept = "application/json" res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 300) @@ -402,7 +407,7 @@ class VersionsTest(test.TestCase): "status": "CURRENT", "links": [ { - "href": "http://localhost/v1.1/servers/2", + "href": "http://localhost/v1.1/servers/" + uuid, "rel": "self", }, ], diff --git a/nova/tests/integrated/test_volumes.py b/nova/tests/integrated/test_volumes.py index 7d803736fb6c..9e19f2cff892 100644 --- a/nova/tests/integrated/test_volumes.py +++ b/nova/tests/integrated/test_volumes.py @@ -285,7 +285,6 @@ class VolumesTest(integrated_helpers._IntegratedTestBase): undisco_move = last_days_of_disco_moves[0] self.assertEquals(undisco_move['id'], volume_id) self.assertEquals(undisco_move['mountpoint'], device) - self.assertEquals(undisco_move['instance_id'], server_id) def test_create_volume_with_metadata(self): """Creates and deletes a volume."""