From 4d39d8cc94dc361dec4eb333a028d7b8e1bb35d2 Mon Sep 17 00:00:00 2001 From: wangqun Date: Fri, 19 Feb 2016 06:15:43 +0000 Subject: [PATCH] Fix baymodel with invalid parameter can created. Now, when we create baymodel with the invalid flavor_id and master_flavor_id parameters, the baymodel can be created successfully. We need to validate the flavor_id and master_flavor before creating baymodel. Change-Id: Id6768d8246c97d56e529cbc1cab6c8c7d28ca43a Closed-Bug: #1547345 --- magnum/api/attr_validator.py | 6 +- magnum/api/controllers/v1/bay.py | 6 +- magnum/api/controllers/v1/baymodel.py | 2 +- .../tests/functional/api/v1/test_baymodel.py | 18 +-- magnum/tests/functional/common/datagen.py | 16 ++- .../tests/unit/api/controllers/v1/test_bay.py | 127 +++++++----------- .../unit/api/controllers/v1/test_baymodel.py | 74 +++------- magnum/tests/unit/api/test_attr_validator.py | 8 +- 8 files changed, 90 insertions(+), 167 deletions(-) diff --git a/magnum/api/attr_validator.py b/magnum/api/attr_validator.py index d1e64b478e..158a6263d4 100644 --- a/magnum/api/attr_validator.py +++ b/magnum/api/attr_validator.py @@ -18,7 +18,6 @@ from novaclient import exceptions as nova_exception from magnum.api import utils as api_utils from magnum.common import clients from magnum.common import exception -from magnum.objects.baymodel import BayModel def validate_image(cli, image): @@ -39,6 +38,8 @@ def validate_image(cli, image): def validate_flavor(cli, flavor): """Validate flavor""" + if flavor is None: + return flavor_id = None flavor_list = cli.nova().flavors.list() for f in flavor_list: @@ -80,10 +81,9 @@ def validate_fixed_network(cli, fixed_network): pass -def validate_os_resources(context, baymodel_id): +def validate_os_resources(context, baymodel): """Validate baymodel's OpenStack Resources""" - baymodel = BayModel.get_by_uuid(context, baymodel_id) cli = clients.OpenStackClients(context) for attr, validate_method in validators.items(): diff --git a/magnum/api/controllers/v1/bay.py b/magnum/api/controllers/v1/bay.py index 84da055c9f..dd441d9932 100644 --- a/magnum/api/controllers/v1/bay.py +++ b/magnum/api/controllers/v1/bay.py @@ -278,10 +278,10 @@ class BaysController(rest.RestController): :param bay: a bay within the request body. """ - bay_dict = bay.as_dict() context = pecan.request.context - attr_validator.validate_os_resources(context, - bay_dict.get('baymodel_id')) + baymodel = objects.BayModel.get_by_uuid(context, bay.baymodel_id) + attr_validator.validate_os_resources(context, baymodel.as_dict()) + bay_dict = bay.as_dict() bay_dict['project_id'] = context.project_id bay_dict['user_id'] = context.user_id if bay_dict.get('name') is None: diff --git a/magnum/api/controllers/v1/baymodel.py b/magnum/api/controllers/v1/baymodel.py index b09771788b..90c5c9ae1a 100644 --- a/magnum/api/controllers/v1/baymodel.py +++ b/magnum/api/controllers/v1/baymodel.py @@ -302,7 +302,7 @@ class BayModelsController(rest.RestController): baymodel_dict = baymodel.as_dict() context = pecan.request.context cli = clients.OpenStackClients(context) - attr_validator.validate_keypair(cli, baymodel_dict['keypair_id']) + attr_validator.validate_os_resources(context, baymodel_dict) image_data = attr_validator.validate_image(cli, baymodel_dict['image_id']) baymodel_dict['cluster_distro'] = image_data['os_distro'] diff --git a/magnum/tests/functional/api/v1/test_baymodel.py b/magnum/tests/functional/api/v1/test_baymodel.py index 4df7397b9b..48dbe76ac4 100644 --- a/magnum/tests/functional/api/v1/test_baymodel.py +++ b/magnum/tests/functional/api/v1/test_baymodel.py @@ -59,7 +59,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('positive') def test_list_baymodels(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() _, temp_model = self._create_baymodel(gen_model) resp, model = self.baymodel_client.list_baymodels() self.assertEqual(200, resp.status) @@ -69,12 +69,12 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('positive') def test_create_baymodel(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() resp, model = self._create_baymodel(gen_model) @testtools.testcase.attr('positive') def test_update_baymodel_by_uuid(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() resp, old_model = self._create_baymodel(gen_model) patch_model = datagen.baymodel_name_patch_data() @@ -89,7 +89,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('positive') def test_delete_baymodel_by_uuid(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() resp, model = self._create_baymodel(gen_model) resp, _ = self.baymodel_client.delete_baymodel(model.uuid) self.assertEqual(204, resp.status) @@ -97,7 +97,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('positive') def test_delete_baymodel_by_name(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() resp, model = self._create_baymodel(gen_model) resp, _ = self.baymodel_client.delete_baymodel(model.name) self.assertEqual(204, resp.status) @@ -153,7 +153,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('negative') def test_create_baymodel_missing_keypair(self): - gen_model = datagen.baymodel_data_with_valid_image_id() + gen_model = datagen.baymodel_data_with_valid_image_and_flavor() self.assertRaises( exceptions.NotFound, self.baymodel_client.post_baymodel, gen_model) @@ -161,7 +161,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('negative') def test_update_baymodel_invalid_patch(self): # get json object - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() resp, old_model = self._create_baymodel(gen_model) self.assertRaises( @@ -171,7 +171,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('negative') def test_create_baymodel_invalid_network_driver(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() gen_model.network_driver = 'invalid_network_driver' self.assertRaises( exceptions.BadRequest, @@ -179,7 +179,7 @@ class BayModelTest(base.BaseMagnumTest): @testtools.testcase.attr('negative') def test_create_baymodel_invalid_volume_driver(self): - gen_model = datagen.baymodel_data_with_valid_keypair_and_image_id() + gen_model = datagen.baymodel_data_with_valid_keypair_image_flavor() gen_model.volume_driver = 'invalid_volume_driver' self.assertRaises( exceptions.BadRequest, diff --git a/magnum/tests/functional/common/datagen.py b/magnum/tests/functional/common/datagen.py index b8d553a84c..9a51483117 100644 --- a/magnum/tests/functional/common/datagen.py +++ b/magnum/tests/functional/common/datagen.py @@ -110,7 +110,7 @@ def baymodel_data(**kwargs): "dns_nameserver": "8.8.8.8", "flavor_id": data_utils.rand_name('bay'), "master_flavor_id": data_utils.rand_name('bay'), - "external_network_id": str(data_utils.rand_uuid()), + "external_network_id": "public", "keypair_id": data_utils.rand_name('bay'), "image_id": data_utils.rand_name('bay') } @@ -136,14 +136,16 @@ def baymodel_name_patch_data(name=data_utils.rand_name('bay')): return baymodelpatch_model.BayModelPatchCollection.from_dict(data) -def baymodel_data_with_valid_keypair_and_image_id(): - """Generates random baymodel data with valid keypair and image +def baymodel_data_with_valid_keypair_image_flavor(): + """Generates random baymodel data with valid keypair,image and flavor :returns: BayModelEntity with generated data """ return baymodel_data(keypair_id=config.Config.keypair_id, - image_id=config.Config.image_id) + image_id=config.Config.image_id, + flavor_id=config.Config.flavor_id, + master_flavor_id=config.Config.master_flavor_id) def baymodel_data_with_valid_keypair(): @@ -166,13 +168,15 @@ def baymodel_valid_data_with_specific_coe(coe): image_id=config.Config.image_id, coe=coe) -def baymodel_data_with_valid_image_id(): +def baymodel_data_with_valid_image_and_flavor(): """Generates random baymodel data with valid image :returns: BayModelEntity with generated data """ - return baymodel_data(image_id=config.Config.image_id) + return baymodel_data(image_id=config.Config.image_id, + flavor_id=config.Config.flavor_id, + master_flavor_id=config.Config.master_flavor_id) def valid_swarm_baymodel(): diff --git a/magnum/tests/unit/api/controllers/v1/test_bay.py b/magnum/tests/unit/api/controllers/v1/test_bay.py index ce1f070cc1..2674394702 100644 --- a/magnum/tests/unit/api/controllers/v1/test_bay.py +++ b/magnum/tests/unit/api/controllers/v1/test_bay.py @@ -18,6 +18,7 @@ from oslo_config import cfg from oslo_utils import timeutils from six.moves.urllib import parse as urlparse +from magnum.api import attr_validator from magnum.api.controllers.v1 import bay as api_bay from magnum.common import exception from magnum.common import utils @@ -385,18 +386,19 @@ class TestPost(api_base.FunctionalTest): self.mock_bay_create = p.start() self.mock_bay_create.side_effect = self._simulate_rpc_bay_create self.addCleanup(p.stop) + p = mock.patch.object(attr_validator, 'validate_os_resources') + self.mock_valid_os_res = p.start() + self.addCleanup(p.stop) def _simulate_rpc_bay_create(self, bay, bay_create_timeout): bay.create() return bay - @mock.patch('magnum.api.attr_validator.validate_os_resources') @mock.patch('oslo_utils.timeutils.utcnow') - def test_create_bay(self, mock_utcnow, mock_valid_os_res): + def test_create_bay(self, mock_utcnow): bdict = apiutils.bay_post_data() test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict) self.assertEqual('application/json', response.content_type) @@ -414,10 +416,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(bdict['bay_create_timeout'], response.json['bay_create_timeout']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_set_project_id_and_user_id(self, mock_valid_os_res): + def test_create_bay_set_project_id_and_user_id(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.return_value = None def _simulate_rpc_bay_create(bay, bay_create_timeout): self.assertEqual(self.context.project_id, bay.project_id) @@ -428,23 +428,19 @@ class TestPost(api_base.FunctionalTest): self.post_json('/bays', bdict) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_doesnt_contain_id(self, mock_valid_os_res): + def test_create_bay_doesnt_contain_id(self): with mock.patch.object(self.dbapi, 'create_bay', wraps=self.dbapi.create_bay) as cc_mock: bdict = apiutils.bay_post_data(name='bay_example_A') - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict) self.assertEqual(bdict['name'], response.json['name']) cc_mock.assert_called_once_with(mock.ANY) # Check that 'id' is not in first arg of positional args self.assertNotIn('id', cc_mock.call_args[0][0]) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_generate_uuid(self, mock_valid_os_res): + def test_create_bay_generate_uuid(self): bdict = apiutils.bay_post_data() del bdict['uuid'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict) self.assertEqual('application/json', response.content_type) @@ -452,120 +448,95 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(bdict['name'], response.json['name']) self.assertTrue(utils.is_uuid_like(response.json['uuid'])) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_no_baymodel_id(self, mock_valid_os_res): + def test_create_bay_no_baymodel_id(self): bdict = apiutils.bay_post_data() del bdict['baymodel_id'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_non_existent_baymodel_id(self, - mock_valid_os_res): + def test_create_bay_with_non_existent_baymodel_id(self): bdict = apiutils.bay_post_data(baymodel_id=utils.generate_uuid()) - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_baymodel_name(self, mock_valid_os_res): + def test_create_bay_with_baymodel_name(self): bdict = apiutils.bay_post_data(baymodel_id=self.baymodel.name) - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_node_count_zero(self, mock_valid_os_res): + def test_create_bay_with_node_count_zero(self): bdict = apiutils.bay_post_data() bdict['node_count'] = 0 - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_node_count_negative(self, mock_valid_os_res): + def test_create_bay_with_node_count_negative(self): bdict = apiutils.bay_post_data() bdict['node_count'] = -1 - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_no_node_count(self, mock_valid_os_res): + def test_create_bay_with_no_node_count(self): bdict = apiutils.bay_post_data() del bdict['node_count'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) self.assertEqual(1, response.json['node_count']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_master_count_zero(self, mock_valid_os_res): + def test_create_bay_with_master_count_zero(self): bdict = apiutils.bay_post_data() bdict['master_count'] = 0 - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_no_master_count(self, mock_valid_os_res): + def test_create_bay_with_no_master_count(self): bdict = apiutils.bay_post_data() del bdict['master_count'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) self.assertEqual(1, response.json['master_count']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_invalid_long_name(self, mock_valid_os_res): + def test_create_bay_with_invalid_long_name(self): bdict = apiutils.bay_post_data(name='x' * 256) response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_invalid_empty_name(self, mock_valid_os_res): + def test_create_bay_with_invalid_empty_name(self): bdict = apiutils.bay_post_data(name='') - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_without_name(self, mock_valid_os_res): + def test_create_bay_without_name(self): bdict = apiutils.bay_post_data() del bdict['name'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_timeout_none(self, mock_valid_os_res): + def test_create_bay_with_timeout_none(self): bdict = apiutils.bay_post_data() bdict['bay_create_timeout'] = None - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_no_timeout(self, mock_valid_os_res): + def test_create_bay_with_no_timeout(self): def _simulate_rpc_bay_create(bay, bay_create_timeout): self.assertEqual(0, bay_create_timeout) bay.create() @@ -573,82 +544,76 @@ class TestPost(api_base.FunctionalTest): self.mock_bay_create.side_effect = _simulate_rpc_bay_create bdict = apiutils.bay_post_data() del bdict['bay_create_timeout'] - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_timeout_negative(self, mock_valid_os_res): + def test_create_bay_with_timeout_negative(self): bdict = apiutils.bay_post_data() bdict['bay_create_timeout'] = -1 - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(400, response.status_int) self.assertTrue(response.json['error_message']) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_timeout_zero(self, mock_valid_os_res): + def test_create_bay_with_timeout_zero(self): bdict = apiutils.bay_post_data() bdict['bay_create_timeout'] = 0 - mock_valid_os_res.return_value = None response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) self.assertEqual(201, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_invalid_flavor(self, mock_valid_os_res): + def test_create_bay_with_invalid_flavor(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.FlavorNotFound('test-flavor') + self.mock_valid_os_res.side_effect = exception.FlavorNotFound( + 'test-flavor') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(400, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_invalid_ext_network(self, mock_valid_os_res): + def test_create_bay_with_invalid_ext_network(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.NetworkNotFound('test-net') + self.mock_valid_os_res.side_effect = exception.NetworkNotFound( + 'test-net') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(400, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_invalid_keypair(self, mock_valid_os_res): + def test_create_bay_with_invalid_keypair(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.KeyPairNotFound('test-key') + self.mock_valid_os_res.side_effect = exception.KeyPairNotFound( + 'test-key') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(404, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_nonexist_image(self, mock_valid_os_res): + def test_create_bay_with_nonexist_image(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.ImageNotFound('test-img') + self.mock_valid_os_res.side_effect = exception.ImageNotFound( + 'test-img') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(400, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_multi_images_same_name(self, mock_valid_os_res): + def test_create_bay_with_multi_images_same_name(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.Conflict('test-img') + self.mock_valid_os_res.side_effect = exception.Conflict('test-img') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(409, response.status_int) - @mock.patch('magnum.api.attr_validator.validate_os_resources') - def test_create_bay_with_on_os_distro_image(self, mock_valid_os_res): + def test_create_bay_with_on_os_distro_image(self): bdict = apiutils.bay_post_data() - mock_valid_os_res.side_effect = exception.OSDistroFieldNotFound('img') + self.mock_valid_os_res.side_effect = exception.OSDistroFieldNotFound( + 'img') response = self.post_json('/bays', bdict, expect_errors=True) self.assertEqual('application/json', response.content_type) - self.assertTrue(mock_valid_os_res.called) + self.assertTrue(self.mock_valid_os_res.called) self.assertEqual(400, response.status_int) diff --git a/magnum/tests/unit/api/controllers/v1/test_baymodel.py b/magnum/tests/unit/api/controllers/v1/test_baymodel.py index 22e72839e7..a5aadb0c06 100644 --- a/magnum/tests/unit/api/controllers/v1/test_baymodel.py +++ b/magnum/tests/unit/api/controllers/v1/test_baymodel.py @@ -20,6 +20,7 @@ from six.moves.urllib import parse as urlparse from webtest.app import AppError from wsme import types as wtypes +from magnum.api import attr_validator from magnum.api.controllers.v1 import baymodel as api_baymodel from magnum.common import exception from magnum.common import policy as magnum_policy @@ -369,13 +370,17 @@ class TestPatch(api_base.FunctionalTest): class TestPost(api_base.FunctionalTest): + def setUp(self): + super(TestPost, self).setUp() + p = mock.patch.object(attr_validator, 'validate_os_resources') + self.mock_valid_os_res = p.start() + self.addCleanup(p.stop) + @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') @mock.patch('oslo_utils.timeutils.utcnow') def test_create_baymodel(self, mock_utcnow, - mock_keypair_exists, mock_image_data): + mock_image_data): bdict = apiutils.baymodel_post_data() - mock_keypair_exists.return_value = None test_time = datetime.datetime(2000, 1, 1, 0, 0) mock_utcnow.return_value = test_time mock_image_data.return_value = {'name': 'mock_name', @@ -395,13 +400,10 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(test_time, return_created_at) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_set_project_id_and_user_id(self, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data() @@ -413,13 +415,10 @@ class TestPost(api_base.FunctionalTest): cc_mock.call_args[0][0]['user_id']) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_doesnt_contain_id(self, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data(image_id='my-image') @@ -483,12 +482,9 @@ class TestPost(api_base.FunctionalTest): self._create_baymodel_raises_app_error(apiserver_port='not an int') @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') - def test_create_baymodel_with_labels(self, mock_keypair_exists, - mock_image_data): + def test_create_baymodel_with_labels(self, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data(labels={'key1': 'val1', @@ -500,13 +496,10 @@ class TestPost(api_base.FunctionalTest): self.assertNotIn('id', cc_mock.call_args[0][0]) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_docker_volume_size(self, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data(docker_volume_size=99) @@ -517,21 +510,17 @@ class TestPost(api_base.FunctionalTest): self.assertNotIn('id', cc_mock.call_args[0][0]) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_generate_uuid(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None + # TODO(hongbin): Is this test correct? + pass @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def _test_create_baymodel_network_driver_attr(self, baymodel_dict, baymodel_config_dict, expect_errors, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} for k, v in baymodel_config_dict.items(): @@ -591,13 +580,10 @@ class TestPost(api_base.FunctionalTest): expect_errors_flag) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') - def test_create_baymodel_with_volume_driver(self, mock_keypair_exists, + def test_create_baymodel_with_volume_driver(self, mock_image_data): - mock_keypair_exists.return_value = None with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data(volume_driver='rexray') @@ -608,10 +594,8 @@ class TestPost(api_base.FunctionalTest): self.assertNotIn('id', cc_mock.call_args[0][0]) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') - def test_create_baymodel_with_no_volume_driver(self, mock_keypair_exists, + def test_create_baymodel_with_no_volume_driver(self, mock_image_data): - mock_keypair_exists.return_value = None with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: mock_image_data.return_value = {'name': 'mock_name', @@ -624,14 +608,11 @@ class TestPost(api_base.FunctionalTest): self.assertNotIn('id', cc_mock.call_args[0][0]) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') @mock.patch.object(magnum_policy, 'enforce') def test_create_baymodel_public_success(self, mock_policy, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_policy.return_value = True mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} @@ -645,14 +626,11 @@ class TestPost(api_base.FunctionalTest): self.assertTrue(cc_mock.call_args[0][0]['public']) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') @mock.patch.object(magnum_policy, 'enforce') def test_create_baymodel_public_fail(self, mock_policy, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel): - mock_keypair_exists.return_value = None # make policy enforcement fail mock_policy.return_value = False mock_image_data.return_value = {'name': 'mock_name', @@ -661,14 +639,11 @@ class TestPost(api_base.FunctionalTest): self.assertRaises(AppError, self.post_json, '/baymodels', bdict) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') @mock.patch.object(magnum_policy, 'enforce') def test_create_baymodel_public_not_set(self, mock_policy, - mock_keypair_exists, mock_image_data): with mock.patch.object(self.dbapi, 'create_baymodel', wraps=self.dbapi.create_baymodel) as cc_mock: - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data(public=False) @@ -681,11 +656,8 @@ class TestPost(api_base.FunctionalTest): self.assertFalse(cc_mock.call_args[0][0]['public']) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_no_os_distro_image(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.side_effect = exception.OSDistroFieldNotFound('img') bdict = apiutils.baymodel_post_data() del bdict['uuid'] @@ -693,11 +665,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(400, response.status_int) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_os_distro_image(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data() @@ -706,11 +675,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(201, response.status_int) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_image_name(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} mock_image_data.return_value = mock_image @@ -720,11 +686,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(201, response.status_int) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_no_exist_image_name(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.side_effect = exception.ResourceNotFound('test-img') bdict = apiutils.baymodel_post_data() del bdict['uuid'] @@ -732,11 +695,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(404, response.status_int) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_multi_image_name(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.side_effect = exception.Conflict('Multiple images') bdict = apiutils.baymodel_post_data() del bdict['uuid'] @@ -756,10 +716,8 @@ class TestPost(api_base.FunctionalTest): self.assertEqual(400, response.status_int) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') - def test_create_baymodel_with_dns(self, mock_keypair_exists, + def test_create_baymodel_with_dns(self, mock_image_data): - mock_keypair_exists.return_value = None mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data() @@ -769,11 +727,9 @@ class TestPost(api_base.FunctionalTest): response.json['dns_nameserver']) @mock.patch('magnum.api.attr_validator.validate_image') - @mock.patch('magnum.api.attr_validator.validate_keypair') def test_create_baymodel_with_no_exist_keypair(self, - mock_keypair_exists, mock_image_data): - mock_keypair_exists.side_effect = exception.KeyPairNotFound("Test") + self.mock_valid_os_res.side_effect = exception.KeyPairNotFound("Test") mock_image_data.return_value = {'name': 'mock_name', 'os_distro': 'fedora-atomic'} bdict = apiutils.baymodel_post_data() diff --git a/magnum/tests/unit/api/test_attr_validator.py b/magnum/tests/unit/api/test_attr_validator.py index 070938a970..f71cccaab8 100644 --- a/magnum/tests/unit/api/test_attr_validator.py +++ b/magnum/tests/unit/api/test_attr_validator.py @@ -154,12 +154,10 @@ class TestAttrValidator(base.BaseTestCase): attr_validator.validate_image, mock_os_cli, 'fedora-21-atomic-5') - @mock.patch('magnum.objects.baymodel.BayModel.get_by_uuid') @mock.patch('magnum.common.clients.OpenStackClients') def test_validate_os_resources_with_invalid_flavor(self, - mock_os_cli, - mock_baymodel_by_uuid): - mock_baymodel_by_uuid.return_value = {'flavor_id': 'test_flavor'} + mock_os_cli): + mock_baymodel = {'flavor_id': 'test_flavor'} mock_flavor = mock.MagicMock() mock_flavor.name = 'test_flavor_not_equal' mock_flavor.id = 'test_flavor_id_not_equal' @@ -170,4 +168,4 @@ class TestAttrValidator(base.BaseTestCase): mock_context = mock.MagicMock() self.assertRaises(exception.FlavorNotFound, attr_validator.validate_os_resources, - mock_context, 'baymodel_id') + mock_context, mock_baymodel)