From 6aa82e544f37da1f6340c4452ca3473a262a08fd Mon Sep 17 00:00:00 2001 From: "shubham.git" Date: Tue, 15 Nov 2016 20:26:45 +0530 Subject: [PATCH] Rename image_create() to image_pull() at various locations image_create() is not appropriate for image pull operation. This patch renames the functions where create is used for image pull related operations. Change-Id: Id684831e07e36f15fd148832d8e84df3e81314e7 Closes-Bug: 1641034 --- etc/zun/policy.json | 2 +- zun/api/controllers/v1/images.py | 8 ++-- zun/compute/api.py | 4 +- zun/compute/manager.py | 6 +-- zun/db/api.py | 4 +- zun/db/sqlalchemy/api.py | 2 +- zun/objects/image.py | 4 +- .../unit/api/controllers/v1/test_images.py | 46 +++++++++---------- zun/tests/unit/db/test_image.py | 8 ++-- zun/tests/unit/db/utils.py | 2 +- zun/tests/unit/objects/test_image.py | 12 ++--- 11 files changed, 49 insertions(+), 49 deletions(-) diff --git a/etc/zun/policy.json b/etc/zun/policy.json index 8297a3e5e..98fe9b605 100644 --- a/etc/zun/policy.json +++ b/etc/zun/policy.json @@ -19,7 +19,7 @@ "container:execute": "rule:admin_or_user", "container:kill": "rule:admin_or_user", - "image:create": "rule:default", + "image:pull": "rule:default", "image:get_all": "rule:default", "zun-service:get_all": "rule:admin_api" diff --git a/zun/api/controllers/v1/images.py b/zun/api/controllers/v1/images.py index fda101de9..e0fc48768 100644 --- a/zun/api/controllers/v1/images.py +++ b/zun/api/controllers/v1/images.py @@ -181,8 +181,8 @@ class ImagesController(rest.RestController): :param image: an image within the request body. """ context = pecan.request.context - policy.enforce(context, "image:create", - action="image:create") + policy.enforce(context, "image:pull", + action="image:pull") image_dict = Image(**image_dict).as_dict() image_dict['project_id'] = context.project_id image_dict['user_id'] = context.user_id @@ -190,8 +190,8 @@ class ImagesController(rest.RestController): image_dict['repo'], image_dict['tag'] = utils.parse_image_name( repo_tag) new_image = objects.Image(context, **image_dict) - new_image.create() - pecan.request.rpcapi.image_create(context, new_image) + new_image.pull() + pecan.request.rpcapi.image_pull(context, new_image) # Set the HTTP Location Header pecan.response.location = link.build_url('images', new_image.uuid) pecan.response.status = 202 diff --git a/zun/compute/api.py b/zun/compute/api.py index 58d269ca6..abb45d956 100644 --- a/zun/compute/api.py +++ b/zun/compute/api.py @@ -72,5 +72,5 @@ class API(rpc_service.API): def image_show(self, context, image): return self._call('image_show', image=image) - def image_create(self, context, image): - return self._cast('image_create', image=image) + def image_pull(self, context, image): + return self._cast('image_pull', image=image) diff --git a/zun/compute/manager.py b/zun/compute/manager.py index 2950548bc..e4bd7dd0e 100644 --- a/zun/compute/manager.py +++ b/zun/compute/manager.py @@ -276,10 +276,10 @@ class Manager(object): six.text_type(e)) raise - def image_create(self, context, image): - utils.spawn_n(self._do_image_create, context, image) + def image_pull(self, context, image): + utils.spawn_n(self._do_image_pull, context, image) - def _do_image_create(self, context, image): + def _do_image_pull(self, context, image): LOG.debug('Creating image...', context=context, image=image) repo_tag = image.repo + ":" + image.tag diff --git a/zun/db/api.py b/zun/db/api.py index eeba6a673..1bef30e9c 100644 --- a/zun/db/api.py +++ b/zun/db/api.py @@ -216,7 +216,7 @@ class Connection(object): marker, sort_key, sort_dir) @classmethod - def create_image(cls, values): + def pull_image(cls, values): """Create a new image. :param values: A dict containing several items used to identify @@ -233,7 +233,7 @@ class Connection(object): :returns: An image. """ dbdriver = get_instance() - return dbdriver.create_image(values) + return dbdriver.pull_image(values) @classmethod def update_image(self, image_id, values): diff --git a/zun/db/sqlalchemy/api.py b/zun/db/sqlalchemy/api.py index 41a9e6653..264037324 100644 --- a/zun/db/sqlalchemy/api.py +++ b/zun/db/sqlalchemy/api.py @@ -293,7 +293,7 @@ class Connection(api.Connection): return _paginate_query(models.ZunService, limit, marker, sort_key, sort_dir, query) - def create_image(self, values): + def pull_image(self, values): # ensure defaults are present for new containers if not values.get('uuid'): values['uuid'] = uuidutils.generate_uuid() diff --git a/zun/objects/image.py b/zun/objects/image.py index ba1c390d2..957beb2ba 100644 --- a/zun/objects/image.py +++ b/zun/objects/image.py @@ -95,7 +95,7 @@ class Image(base.ZunPersistentObject, base.ZunObject, return Image._from_db_object_list(db_images, cls, context) @base.remotable - def create(self, context=None): + def pull(self, context=None): """Create an image record in the DB. :param context: Security context. NOTE: This should only @@ -107,7 +107,7 @@ class Image(base.ZunPersistentObject, base.ZunObject, """ values = self.obj_get_changes() - db_image = dbapi.Connection.create_image(values) + db_image = dbapi.Connection.pull_image(values) self._from_db_object(self, db_image) @base.remotable diff --git a/zun/tests/unit/api/controllers/v1/test_images.py b/zun/tests/unit/api/controllers/v1/test_images.py index 6c8965e9c..4eab89ff7 100644 --- a/zun/tests/unit/api/controllers/v1/test_images.py +++ b/zun/tests/unit/api/controllers/v1/test_images.py @@ -21,9 +21,9 @@ from zun.tests.unit.db import utils class TestImageController(api_base.FunctionalTest): - @patch('zun.compute.api.API.image_create') - def test_image_create(self, mock_image_create): - mock_image_create.side_effect = lambda x, y: y + @patch('zun.compute.api.API.image_pull') + def test_image_pull(self, mock_image_pull): + mock_image_pull.side_effect = lambda x, y: y params = ('{"repo": "hello-world"}') response = self.app.post('/v1/images/', @@ -31,7 +31,7 @@ class TestImageController(api_base.FunctionalTest): content_type='application/json') self.assertEqual(202, response.status_int) - self.assertTrue(mock_image_create.called) + self.assertTrue(mock_image_pull.called) params = ('{"repo": "hello-world:test"}') response = self.app.post('/v1/images/', @@ -39,20 +39,20 @@ class TestImageController(api_base.FunctionalTest): content_type='application/json') self.assertEqual(202, response.status_int) - self.assertTrue(mock_image_create.called) + self.assertTrue(mock_image_pull.called) - @patch('zun.compute.api.API.image_create') - def test_image_create_with_no_repo(self, mock_image_create): - mock_image_create.side_effect = lambda x, y: y + @patch('zun.compute.api.API.image_pull') + def test_image_pull_with_no_repo(self, mock_image_pull): + mock_image_pull.side_effect = lambda x, y: y self.assertRaises(AppError, self.app.post, '/v1/images/', content_type='application/json') - self.assertTrue(mock_image_create.not_called) + self.assertTrue(mock_image_pull.not_called) - @patch('zun.compute.api.API.image_create') - def test_image_create_conflict(self, mock_image_create): - mock_image_create.side_effect = lambda x, y: y + @patch('zun.compute.api.API.image_pull') + def test_image_pull_conflict(self, mock_image_pull): + mock_image_pull.side_effect = lambda x, y: y params = ('{"repo": "hello-world"}') response = self.app.post('/v1/images/', @@ -60,28 +60,28 @@ class TestImageController(api_base.FunctionalTest): content_type='application/json') self.assertEqual(202, response.status_int) - self.assertTrue(mock_image_create.called) + self.assertTrue(mock_image_pull.called) self.assertRaises(AppError, self.app.post, '/v1/images/', params=params, content_type='application/json') - self.assertTrue(mock_image_create.not_called) + self.assertTrue(mock_image_pull.not_called) - @patch('zun.compute.api.API.image_create') - def test_create_image_set_project_id_and_user_id( - self, mock_image_create): + @patch('zun.compute.api.API.image_pull') + def test_pull_image_set_project_id_and_user_id( + self, mock_image_pull): def _create_side_effect(cnxt, image): self.assertEqual(self.context.project_id, image.project_id) self.assertEqual(self.context.user_id, image.user_id) return image - mock_image_create.side_effect = _create_side_effect + mock_image_pull.side_effect = _create_side_effect params = ('{"repo": "hello-world"}') self.app.post('/v1/images/', params=params, content_type='application/json') - @patch('zun.compute.api.API.image_create') - def test_image_create_with_tag(self, mock_image_create): - mock_image_create.side_effect = lambda x, y: y + @patch('zun.compute.api.API.image_pull') + def test_image_pull_with_tag(self, mock_image_pull): + mock_image_pull.side_effect = lambda x, y: y params = ('{"repo": "hello-world:latest"}') response = self.app.post('/v1/images/', @@ -89,7 +89,7 @@ class TestImageController(api_base.FunctionalTest): content_type='application/json') self.assertEqual(202, response.status_int) - self.assertTrue(mock_image_create.called) + self.assertTrue(mock_image_pull.called) @patch('zun.compute.api.API.image_show') @patch('zun.objects.Image.list') @@ -172,7 +172,7 @@ class TestImageEnforcement(api_base.FunctionalTest): def test_policy_disallow_create(self): params = ('{"repo": "foo"}') self._common_policy_check( - 'image:create', self.app.post, '/v1/images/', + 'image:pull', self.app.post, '/v1/images/', params=params, content_type='application/json', expect_errors=True) diff --git a/zun/tests/unit/db/test_image.py b/zun/tests/unit/db/test_image.py index f24ad687f..dee368241 100644 --- a/zun/tests/unit/db/test_image.py +++ b/zun/tests/unit/db/test_image.py @@ -21,18 +21,18 @@ from zun.tests.unit.db import utils class DbImageTestCase(base.DbTestCase): - def test_create_image(self): + def test_pull_image(self): utils.create_test_image(repo="ubuntu:latest") - def test_create_image_duplicate_repo(self): + def test_pull_image_duplicate_repo(self): utils.create_test_image(repo="ubuntu:latest") utils.create_test_image(repo="ubuntu:14.04") - def test_create_image_duplicate_tag(self): + def test_pull_image_duplicate_tag(self): utils.create_test_image(repo="ubuntu:latest") utils.create_test_image(repo="centos:latest") - def test_create_image_already_exists(self): + def test_pull_image_already_exists(self): utils.create_test_image(repo="ubuntu:latest") self.assertRaises(exception.ResourceExists, utils.create_test_image, diff --git a/zun/tests/unit/db/utils.py b/zun/tests/unit/db/utils.py index 2dec79a7e..5ad0498d5 100644 --- a/zun/tests/unit/db/utils.py +++ b/zun/tests/unit/db/utils.py @@ -88,7 +88,7 @@ def create_test_image(**kw): if 'repo' not in kw: image['repo'] = _generate_repo_for_image() dbapi = db_api.get_instance() - return dbapi.create_image(image) + return dbapi.pull_image(image) def _generate_repo_for_image(): diff --git a/zun/tests/unit/objects/test_image.py b/zun/tests/unit/objects/test_image.py index 1acf1a13c..aea266284 100644 --- a/zun/tests/unit/objects/test_image.py +++ b/zun/tests/unit/objects/test_image.py @@ -72,13 +72,13 @@ class TestImageObject(base.DbTestCase): limit=None, marker=None, sort_key=None, sort_dir=None) - def test_create(self): - with mock.patch.object(self.dbapi, 'create_image', - autospec=True) as mock_create_image: - mock_create_image.return_value = self.fake_image + def test_pull(self): + with mock.patch.object(self.dbapi, 'pull_image', + autospec=True) as mock_pull_image: + mock_pull_image.return_value = self.fake_image image = objects.Image(self.context, **self.fake_image) - image.create() - mock_create_image.assert_called_once_with(self.fake_image) + image.pull() + mock_pull_image.assert_called_once_with(self.fake_image) self.assertEqual(self.context, image._context) def test_save(self):