diff --git a/nova/api/openstack/compute/multiple_create.py b/nova/api/openstack/compute/multiple_create.py deleted file mode 100644 index 9e3dbc3930..0000000000 --- a/nova/api/openstack/compute/multiple_create.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2013 IBM Corp. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -from webob import exc - -from nova.i18n import _ - -MIN_ATTRIBUTE_NAME = "min_count" -MAX_ATTRIBUTE_NAME = "max_count" -RRID_ATTRIBUTE_NAME = "return_reservation_id" - - -# NOTE(gmann): This function is not supposed to use 'body_deprecated_param' -# parameter as this is placed to handle scheduler_hint extension for V2.1. -def server_create(server_dict, create_kwargs, body_deprecated_param): - # min_count and max_count are optional. If they exist, they may come - # in as strings. Verify that they are valid integers and > 0. - # Also, we want to default 'min_count' to 1, and default - # 'max_count' to be 'min_count'. - min_count = int(server_dict.get(MIN_ATTRIBUTE_NAME, 1)) - max_count = int(server_dict.get(MAX_ATTRIBUTE_NAME, min_count)) - return_id = server_dict.get(RRID_ATTRIBUTE_NAME, False) - - if min_count > max_count: - msg = _('min_count must be <= max_count') - raise exc.HTTPBadRequest(explanation=msg) - - create_kwargs['min_count'] = min_count - create_kwargs['max_count'] = max_count - create_kwargs['return_reservation_id'] = return_id diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index 714db029d7..bae4b0d639 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -32,7 +32,6 @@ from nova.api.openstack.compute import block_device_mapping_v1 from nova.api.openstack.compute import config_drive from nova.api.openstack.compute import helpers from nova.api.openstack.compute import keypairs -from nova.api.openstack.compute import multiple_create from nova.api.openstack.compute.schemas import servers as schema_servers from nova.api.openstack.compute.views import servers as views_servers from nova.api.openstack import wsgi @@ -70,7 +69,6 @@ class ServersController(wsgi.Controller): block_device_mapping_v1.server_create, config_drive.server_create, keypairs.server_create, - multiple_create.server_create, ] @staticmethod @@ -445,6 +443,20 @@ class ServersController(wsgi.Controller): scheduler_hints = body['OS-SCH-HNT:scheduler_hints'] create_kwargs['scheduler_hints'] = scheduler_hints + # min_count and max_count are optional. If they exist, they may come + # in as strings. Verify that they are valid integers and > 0. + # Also, we want to default 'min_count' to 1, and default + # 'max_count' to be 'min_count'. + min_count = int(server_dict.get('min_count', 1)) + max_count = int(server_dict.get('max_count', min_count)) + return_id = server_dict.get('return_reservation_id', False) + if min_count > max_count: + msg = _('min_count must be <= max_count') + raise exc.HTTPBadRequest(explanation=msg) + create_kwargs['min_count'] = min_count + create_kwargs['max_count'] = max_count + create_kwargs['return_reservation_id'] = return_id + availability_zone = server_dict.pop("availability_zone", None) if api_version_request.is_supported(req, min_version='2.52'): diff --git a/nova/tests/unit/api/openstack/compute/test_multiple_create.py b/nova/tests/unit/api/openstack/compute/test_multiple_create.py deleted file mode 100644 index ac9b1db1d5..0000000000 --- a/nova/tests/unit/api/openstack/compute/test_multiple_create.py +++ /dev/null @@ -1,464 +0,0 @@ -# Copyright 2013 IBM Corp. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -import webob - -from nova.api.openstack.compute import block_device_mapping \ - as block_device_mapping_v21 -from nova.api.openstack.compute import multiple_create as multiple_create_v21 -from nova.api.openstack.compute import servers as servers_v21 -from nova.compute import api as compute_api -import nova.conf -from nova import exception -from nova import test -from nova.tests.unit.api.openstack import fakes -from nova.tests.unit.image import fake - -CONF = nova.conf.CONF - - -def return_security_group(context, instance_id, security_group_id): - pass - - -class MultiCreateExtensionTestV21(test.TestCase): - validation_error = exception.ValidationError - - def setUp(self): - """Shared implementation for tests below that create instance.""" - super(MultiCreateExtensionTestV21, self).setUp() - - self.flags(enable_instance_password=True, group='api') - self.instance_cache_num = 0 - self.instance_cache_by_id = {} - self.instance_cache_by_uuid = {} - - # Network API needs to be stubbed out before creating the controllers. - fakes.stub_out_nw_api(self) - - self.controller = servers_v21.ServersController() - - def instance_get(context, instance_id): - """Stub for compute/api create() pulling in instance after - scheduling - """ - return self.instance_cache_by_id[instance_id] - - def instance_update(context, uuid, values): - instance = self.instance_cache_by_uuid[uuid] - instance.update(values) - return instance - - def server_update(context, instance_uuid, params, - columns_to_join=None): - inst = self.instance_cache_by_uuid[instance_uuid] - inst.update(params) - return (inst, inst) - - def fake_method(*args, **kwargs): - pass - - def project_get_networks(context, user_id): - return dict(id='1', host='localhost') - - def create_db_entry_for_new_instance(*args, **kwargs): - instance = args[4] - self.instance_cache_by_uuid[instance.uuid] = instance - return instance - - fakes.stub_out_key_pair_funcs(self) - fake.stub_out_image_service(self) - self.stub_out('nova.db.api.instance_add_security_group', - return_security_group) - self.stub_out('nova.db.api.project_get_networks', project_get_networks) - self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', - create_db_entry_for_new_instance) - self.stub_out('nova.db.api.instance_system_metadata_update', - fake_method) - self.stub_out('nova.db.api.instance_get', instance_get) - self.stub_out('nova.db.api.instance_update', instance_update) - self.stub_out('nova.db.api.instance_update_and_get_original', - server_update) - self.stub_out('nova.network.manager.VlanManager.allocate_fixed_ip', - fake_method) - self.req = fakes.HTTPRequest.blank('') - - def _test_create_extra(self, params, no_image=False): - image_uuid = 'c905cedb-7281-47e4-8a62-f26bc5fc4c77' - server = dict(name='server_test', imageRef=image_uuid, flavorRef=2) - if no_image: - server.pop('imageRef', None) - server.update(params) - body = dict(server=server) - server = self.controller.create(self.req, - body=body).obj['server'] - - def test_multiple_create_with_string_type_min_and_max(self): - min_count = '2' - max_count = '3' - params = { - multiple_create_v21.MIN_ATTRIBUTE_NAME: min_count, - multiple_create_v21.MAX_ATTRIBUTE_NAME: max_count, - } - old_create = compute_api.API.create - - def create(*args, **kwargs): - self.assertIsInstance(kwargs['min_count'], int) - self.assertIsInstance(kwargs['max_count'], int) - self.assertEqual(kwargs['min_count'], 2) - self.assertEqual(kwargs['max_count'], 3) - return old_create(*args, **kwargs) - - self.stub_out('nova.compute.api.API.create', create) - self._test_create_extra(params) - - def test_create_instance_with_multiple_create_enabled(self): - min_count = 2 - max_count = 3 - params = { - multiple_create_v21.MIN_ATTRIBUTE_NAME: min_count, - multiple_create_v21.MAX_ATTRIBUTE_NAME: max_count, - } - old_create = compute_api.API.create - - def create(*args, **kwargs): - self.assertEqual(kwargs['min_count'], 2) - self.assertEqual(kwargs['max_count'], 3) - return old_create(*args, **kwargs) - - self.stub_out('nova.compute.api.API.create', create) - self._test_create_extra(params) - - def test_create_instance_invalid_negative_min(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: -1, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_instance_invalid_negative_max(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MAX_ATTRIBUTE_NAME: -1, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_instance_with_blank_min(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: '', - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_instance_with_blank_max(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MAX_ATTRIBUTE_NAME: '', - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_instance_invalid_min_greater_than_max(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 4, - multiple_create_v21.MAX_ATTRIBUTE_NAME: 2, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(webob.exc.HTTPBadRequest, - self.controller.create, - self.req, - body=body) - - def test_create_instance_invalid_alpha_min(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 'abcd', - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_instance_invalid_alpha_max(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - - body = { - 'server': { - multiple_create_v21.MAX_ATTRIBUTE_NAME: 'abcd', - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(self.validation_error, - self.controller.create, - self.req, - body=body) - - def test_create_multiple_instances(self): - """Test creating multiple instances but not asking for - reservation_id - """ - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 2, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - 'metadata': {'hello': 'world', - 'open': 'stack'}, - } - } - - res = self.controller.create(self.req, body=body).obj - - instance_uuids = self.instance_cache_by_uuid.keys() - self.assertIn(res["server"]["id"], instance_uuids) - self._check_admin_password_len(res["server"]) - - def test_create_multiple_instances_pass_disabled(self): - """Test creating multiple instances but not asking for - reservation_id - """ - self.flags(enable_instance_password=False, group='api') - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 2, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - 'metadata': {'hello': 'world', - 'open': 'stack'}, - } - } - - res = self.controller.create(self.req, body=body).obj - - instance_uuids = self.instance_cache_by_uuid.keys() - self.assertIn(res["server"]["id"], instance_uuids) - self._check_admin_password_missing(res["server"]) - - def _check_admin_password_len(self, server_dict): - """utility function - check server_dict for admin_password length.""" - self.assertEqual(CONF.password_length, - len(server_dict["adminPass"])) - - def _check_admin_password_missing(self, server_dict): - """utility function - check server_dict for admin_password absence.""" - self.assertNotIn("admin_password", server_dict) - - def _create_multiple_instances_resv_id_return(self, resv_id_return): - """Test creating multiple instances with asking for - reservation_id - """ - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 2, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - 'metadata': {'hello': 'world', - 'open': 'stack'}, - multiple_create_v21.RRID_ATTRIBUTE_NAME: resv_id_return - } - } - - res = self.controller.create(self.req, body=body) - reservation_id = res.obj['reservation_id'] - self.assertNotEqual(reservation_id, "") - self.assertIsNotNone(reservation_id) - self.assertGreater(len(reservation_id), 1) - - def test_create_multiple_instances_with_resv_id_return(self): - self._create_multiple_instances_resv_id_return(True) - - def test_create_multiple_instances_with_string_resv_id_return(self): - self._create_multiple_instances_resv_id_return("True") - - def test_create_multiple_instances_with_multiple_volume_bdm(self): - """Test that a BadRequest is raised if multiple instances - are requested with a list of block device mappings for volumes. - """ - min_count = 2 - bdm = [{'source_type': 'volume', 'uuid': 'vol-xxxx'}, - {'source_type': 'volume', 'uuid': 'vol-yyyy'} - ] - params = { - block_device_mapping_v21.ATTRIBUTE_NAME: bdm, - multiple_create_v21.MIN_ATTRIBUTE_NAME: min_count - } - old_create = compute_api.API.create - - def create(*args, **kwargs): - self.assertEqual(kwargs['min_count'], 2) - self.assertEqual(len(kwargs['block_device_mapping']), 2) - return old_create(*args, **kwargs) - - self.stub_out('nova.compute.api.API.create', create) - exc = self.assertRaises(webob.exc.HTTPBadRequest, - self._test_create_extra, params, no_image=True) - self.assertEqual("Cannot attach one or more volumes to multiple " - "instances", exc.explanation) - - def test_create_multiple_instances_with_single_volume_bdm(self): - """Test that a BadRequest is raised if multiple instances - are requested to boot from a single volume. - """ - min_count = 2 - bdm = [{'source_type': 'volume', 'uuid': 'vol-xxxx'}] - params = { - block_device_mapping_v21.ATTRIBUTE_NAME: bdm, - multiple_create_v21.MIN_ATTRIBUTE_NAME: min_count - } - old_create = compute_api.API.create - - def create(*args, **kwargs): - self.assertEqual(kwargs['min_count'], 2) - self.assertEqual(kwargs['block_device_mapping'][0]['volume_id'], - 'vol-xxxx') - return old_create(*args, **kwargs) - - self.stub_out('nova.compute.api.API.create', create) - exc = self.assertRaises(webob.exc.HTTPBadRequest, - self._test_create_extra, params, no_image=True) - self.assertEqual("Cannot attach one or more volumes to multiple " - "instances", exc.explanation) - - def test_create_multiple_instance_with_non_integer_max_count(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MAX_ATTRIBUTE_NAME: 2.5, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - 'metadata': {'hello': 'world', - 'open': 'stack'}, - } - } - - self.assertRaises(self.validation_error, - self.controller.create, self.req, body=body) - - def test_create_multiple_instance_with_non_integer_min_count(self): - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 2.5, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - 'metadata': {'hello': 'world', - 'open': 'stack'}, - } - } - - self.assertRaises(self.validation_error, - self.controller.create, self.req, body=body) - - def test_create_multiple_instance_max_count_overquota_min_count_ok(self): - self.flags(instances=3, group='quota') - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 2, - multiple_create_v21.MAX_ATTRIBUTE_NAME: 5, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - res = self.controller.create(self.req, body=body).obj - instance_uuids = self.instance_cache_by_uuid.keys() - self.assertIn(res["server"]["id"], instance_uuids) - - def test_create_multiple_instance_max_count_overquota_min_count_over(self): - self.flags(instances=3, group='quota') - image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' - flavor_ref = 'http://localhost/123/flavors/3' - body = { - 'server': { - multiple_create_v21.MIN_ATTRIBUTE_NAME: 4, - multiple_create_v21.MAX_ATTRIBUTE_NAME: 5, - 'name': 'server_test', - 'imageRef': image_href, - 'flavorRef': flavor_ref, - } - } - self.assertRaises(webob.exc.HTTPForbidden, self.controller.create, - self.req, body=body) diff --git a/nova/tests/unit/api/openstack/compute/test_serversV21.py b/nova/tests/unit/api/openstack/compute/test_serversV21.py index c46a01ba78..6f5612968b 100644 --- a/nova/tests/unit/api/openstack/compute/test_serversV21.py +++ b/nova/tests/unit/api/openstack/compute/test_serversV21.py @@ -3015,7 +3015,6 @@ class ServersControllerCreateTest(test.TestCase): fakes.stub_out_key_pair_funcs(self) fake.stub_out_image_service(self) - self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.stub_out('nova.db.api.project_get_networks', lambda c, u: dict(id='1', host='localhost')) self.stub_out('nova.db.api.instance_create', instance_create) @@ -3060,6 +3059,7 @@ class ServersControllerCreateTest(test.TestCase): self.assertNotIn("adminPass", server_dict) def _test_create_instance(self, flavor=2): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) image_uuid = 'c905cedb-7281-47e4-8a62-f26bc5fc4c77' self.body['server']['imageRef'] = image_uuid self.body['server']['flavorRef'] = flavor @@ -3313,7 +3313,7 @@ class ServersControllerCreateTest(test.TestCase): def test_create_instance_with_pass_disabled(self): # test with admin passwords disabled See lp bug 921814 self.flags(enable_instance_password=False, group='api') - + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.flags(enable_instance_password=False, group='api') self.req.body = jsonutils.dump_as_bytes(self.body) res = self.controller.create(self.req, body=self.body).obj @@ -3385,6 +3385,7 @@ class ServersControllerCreateTest(test.TestCase): self.controller.create(self.req, body=self.body) def test_create_instance(self): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.req.body = jsonutils.dump_as_bytes(self.body) res = self.controller.create(self.req, body=self.body).obj @@ -3423,6 +3424,7 @@ class ServersControllerCreateTest(test.TestCase): fake_keypair_server_create) def test_create_instance_pass_disabled(self): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.flags(enable_instance_password=False, group='api') self.req.body = jsonutils.dump_as_bytes(self.body) res = self.controller.create(self.req, body=self.body).obj @@ -3516,6 +3518,7 @@ class ServersControllerCreateTest(test.TestCase): self.controller.create, self.req, body=self.body) def test_create_instance_valid_key_name(self): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.body['server']['key_name'] = 'key' self.req.body = jsonutils.dump_as_bytes(self.body) res = self.controller.create(self.req, body=self.body).obj @@ -3569,6 +3572,7 @@ class ServersControllerCreateTest(test.TestCase): self.controller.create, self.req, body=self.body) def test_create_instance_local_href(self): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) self.req.body = jsonutils.dump_as_bytes(self.body) res = self.controller.create(self.req, body=self.body).obj @@ -3604,6 +3608,7 @@ class ServersControllerCreateTest(test.TestCase): self.controller.create(self.req, body=self.body) def test_create_location(self): + self.stub_out('uuid.uuid4', lambda: FAKE_UUID) selfhref = 'http://localhost/v2/fake/servers/%s' % FAKE_UUID self.req.body = jsonutils.dump_as_bytes(self.body) robj = self.controller.create(self.req, body=self.body) @@ -3806,6 +3811,381 @@ class ServersControllerCreateTest(test.TestCase): self.assertRaises(webob.exc.HTTPForbidden, self._test_create_extra, params) + def test_multiple_create_with_string_type_min_and_max(self): + min_count = '2' + max_count = '3' + params = { + 'min_count': min_count, + 'max_count': max_count, + } + old_create = compute_api.API.create + + def create(*args, **kwargs): + self.assertIsInstance(kwargs['min_count'], int) + self.assertIsInstance(kwargs['max_count'], int) + self.assertEqual(kwargs['min_count'], 2) + self.assertEqual(kwargs['max_count'], 3) + return old_create(*args, **kwargs) + + self.stub_out('nova.compute.api.API.create', create) + self._test_create_extra(params) + + def test_create_instance_with_multiple_create_enabled(self): + min_count = 2 + max_count = 3 + params = { + 'min_count': min_count, + 'max_count': max_count, + } + old_create = compute_api.API.create + + def create(*args, **kwargs): + self.assertEqual(kwargs['min_count'], 2) + self.assertEqual(kwargs['max_count'], 3) + return old_create(*args, **kwargs) + + self.stub_out('nova.compute.api.API.create', create) + self._test_create_extra(params) + + def test_create_instance_invalid_negative_min(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'min_count': -1, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_instance_invalid_negative_max(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'max_count': -1, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_instance_with_blank_min(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'min_count': '', + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_instance_with_blank_max(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'max_count': '', + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_instance_invalid_min_greater_than_max(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'min_count': 4, + 'max_count': 2, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + self.req, + body=body) + + def test_create_instance_invalid_alpha_min(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'min_count': 'abcd', + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_instance_invalid_alpha_max(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + + body = { + 'server': { + 'max_count': 'abcd', + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(exception.ValidationError, + self.controller.create, + self.req, + body=body) + + def test_create_multiple_instances(self): + """Test creating multiple instances but not asking for + reservation_id + """ + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 2, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': {'hello': 'world', + 'open': 'stack'}, + } + } + + def create_db_entry_for_new_instance(*args, **kwargs): + instance = args[4] + self.instance_cache_by_uuid[instance.uuid] = instance + return instance + self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', + create_db_entry_for_new_instance) + res = self.controller.create(self.req, body=body).obj + + instance_uuids = self.instance_cache_by_uuid.keys() + self.assertIn(res["server"]["id"], instance_uuids) + self._check_admin_password_len(res["server"]) + + def test_create_multiple_instances_pass_disabled(self): + """Test creating multiple instances but not asking for + reservation_id + """ + self.flags(enable_instance_password=False, group='api') + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 2, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': {'hello': 'world', + 'open': 'stack'}, + } + } + + def create_db_entry_for_new_instance(*args, **kwargs): + instance = args[4] + self.instance_cache_by_uuid[instance.uuid] = instance + return instance + self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', + create_db_entry_for_new_instance) + res = self.controller.create(self.req, body=body).obj + + instance_uuids = self.instance_cache_by_uuid.keys() + self.assertIn(res["server"]["id"], instance_uuids) + self._check_admin_password_missing(res["server"]) + + def _create_multiple_instances_resv_id_return(self, resv_id_return): + """Test creating multiple instances with asking for + reservation_id + """ + def create_db_entry_for_new_instance(*args, **kwargs): + instance = args[4] + self.instance_cache_by_uuid[instance.uuid] = instance + return instance + + self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', + create_db_entry_for_new_instance) + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 2, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': {'hello': 'world', + 'open': 'stack'}, + 'return_reservation_id': resv_id_return + } + } + + res = self.controller.create(self.req, body=body) + reservation_id = res.obj['reservation_id'] + self.assertNotEqual(reservation_id, "") + self.assertIsNotNone(reservation_id) + self.assertGreater(len(reservation_id), 1) + + def test_create_multiple_instances_with_resv_id_return(self): + self._create_multiple_instances_resv_id_return(True) + + def test_create_multiple_instances_with_string_resv_id_return(self): + self._create_multiple_instances_resv_id_return("True") + + def test_create_multiple_instances_with_multiple_volume_bdm(self): + """Test that a BadRequest is raised if multiple instances + are requested with a list of block device mappings for volumes. + """ + min_count = 2 + bdm = [{'source_type': 'volume', 'uuid': 'vol-xxxx'}, + {'source_type': 'volume', 'uuid': 'vol-yyyy'} + ] + params = { + 'block_device_mapping_v2': bdm, + 'min_count': min_count + } + old_create = compute_api.API.create + + def create(*args, **kwargs): + self.assertEqual(kwargs['min_count'], 2) + self.assertEqual(len(kwargs['block_device_mapping']), 2) + return old_create(*args, **kwargs) + + self.stub_out('nova.compute.api.API.create', create) + exc = self.assertRaises(webob.exc.HTTPBadRequest, + self._test_create_extra, params, no_image=True) + self.assertEqual("Cannot attach one or more volumes to multiple " + "instances", exc.explanation) + + def test_create_multiple_instances_with_single_volume_bdm(self): + """Test that a BadRequest is raised if multiple instances + are requested to boot from a single volume. + """ + min_count = 2 + bdm = [{'source_type': 'volume', 'uuid': 'vol-xxxx'}] + params = { + 'block_device_mapping_v2': bdm, + 'min_count': min_count + } + old_create = compute_api.API.create + + def create(*args, **kwargs): + self.assertEqual(kwargs['min_count'], 2) + self.assertEqual(kwargs['block_device_mapping'][0]['volume_id'], + 'vol-xxxx') + return old_create(*args, **kwargs) + + self.stub_out('nova.compute.api.API.create', create) + exc = self.assertRaises(webob.exc.HTTPBadRequest, + self._test_create_extra, params, no_image=True) + self.assertEqual("Cannot attach one or more volumes to multiple " + "instances", exc.explanation) + + def test_create_multiple_instance_with_non_integer_max_count(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'max_count': 2.5, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': {'hello': 'world', + 'open': 'stack'}, + } + } + + self.assertRaises(exception.ValidationError, + self.controller.create, self.req, body=body) + + def test_create_multiple_instance_with_non_integer_min_count(self): + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 2.5, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + 'metadata': {'hello': 'world', + 'open': 'stack'}, + } + } + + self.assertRaises(exception.ValidationError, + self.controller.create, self.req, body=body) + + def test_create_multiple_instance_max_count_overquota_min_count_ok(self): + self.flags(instances=3, group='quota') + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 2, + 'max_count': 5, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + + def create_db_entry_for_new_instance(*args, **kwargs): + instance = args[4] + self.instance_cache_by_uuid[instance.uuid] = instance + return instance + self.stub_out('nova.compute.api.API.create_db_entry_for_new_instance', + create_db_entry_for_new_instance) + res = self.controller.create(self.req, body=body).obj + instance_uuids = self.instance_cache_by_uuid.keys() + self.assertIn(res["server"]["id"], instance_uuids) + + def test_create_multiple_instance_max_count_overquota_min_count_over(self): + self.flags(instances=3, group='quota') + image_href = '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6' + flavor_ref = 'http://localhost/123/flavors/3' + body = { + 'server': { + 'min_count': 4, + 'max_count': 5, + 'name': 'server_test', + 'imageRef': image_href, + 'flavorRef': flavor_ref, + } + } + self.assertRaises(webob.exc.HTTPForbidden, self.controller.create, + self.req, body=body) + @mock.patch.object(compute_api.API, 'create') def test_create_multiple_instance_with_specified_ip_neutronv2(self, _api_mock):