From c3d481085022ffffc4fb02bbf4766ca49a4c5806 Mon Sep 17 00:00:00 2001 From: Dean Troyer <dtroyer@gmail.com> Date: Thu, 19 May 2016 12:53:01 -0500 Subject: [PATCH] Fix image tests to use warlock resources We have been testing (incorrectly) Image v2 using our usual FakeResource objects, when the v2 API actually uses warlock schema modelled resources. Bring this to the tests (TestImageSet was already doing this) Change-Id: Ia6ed3a8e28a8961f770c241b49d47cce9ff328d3 --- openstackclient/tests/image/v2/fakes.py | 45 ++++++-------------- openstackclient/tests/image/v2/test_image.py | 19 +++------ 2 files changed, 21 insertions(+), 43 deletions(-) diff --git a/openstackclient/tests/image/v2/fakes.py b/openstackclient/tests/image/v2/fakes.py index f90d846d6a..a662a58524 100644 --- a/openstackclient/tests/image/v2/fakes.py +++ b/openstackclient/tests/image/v2/fakes.py @@ -18,6 +18,9 @@ import mock import random import uuid +from glanceclient.v2 import schemas +import warlock + from openstackclient.common import utils as common_utils from openstackclient.tests import fakes from openstackclient.tests import utils @@ -194,7 +197,7 @@ class FakeImage(object): # Set default attribute image_info = { - 'id': 'image-id' + uuid.uuid4().hex, + 'id': str(uuid.uuid4()), 'name': 'image-name' + uuid.uuid4().hex, 'owner': 'image-owner' + uuid.uuid4().hex, 'protected': bool(random.choice([0, 1])), @@ -205,11 +208,13 @@ class FakeImage(object): # Overwrite default attributes if there are some attributes set image_info.update(attrs) - image = fakes.FakeResource( - None, - image_info, - loaded=True) - return image + # Set up the schema + model = warlock.model_factory( + IMAGE_schema, + schemas.SchemaBasedModel, + ) + + return model(**image_info) @staticmethod def create_images(attrs=None, count=2): @@ -248,27 +253,6 @@ class FakeImage(object): return mock.MagicMock(side_effect=images) - @staticmethod - def get_image_info(image=None): - """Get the image info from a faked image object. - - :param image: - A FakeResource objects faking image - :return - A dictionary which includes the faked image info as follows: - { - 'id': image_id, - 'name': image_name, - 'owner': image_owner, - 'protected': image_protected, - 'visibility': image_visibility, - 'tags': image_tags - } - """ - if image is not None: - return image._info - return {} - @staticmethod def get_image_columns(image=None): """Get the image columns from a faked image object. @@ -280,9 +264,8 @@ class FakeImage(object): ('id', 'name', 'owner', 'protected', 'visibility', 'tags') """ if image is not None: - return tuple(k for k in sorted( - FakeImage.get_image_info(image).keys())) - return tuple([]) + return tuple(sorted(image)) + return IMAGE_columns @staticmethod def get_image_data(image=None): @@ -296,7 +279,7 @@ class FakeImage(object): """ data_list = [] if image is not None: - for x in sorted(FakeImage.get_image_info(image).keys()): + for x in sorted(image.keys()): if x == 'tags': # The 'tags' should be format_list data_list.append( diff --git a/openstackclient/tests/image/v2/test_image.py b/openstackclient/tests/image/v2/test_image.py index 33f2533105..beebdef995 100644 --- a/openstackclient/tests/image/v2/test_image.py +++ b/openstackclient/tests/image/v2/test_image.py @@ -75,7 +75,8 @@ class TestImageCreate(TestImage): # This is the return value for utils.find_resource() self.images_mock.get.return_value = copy.deepcopy( - image_fakes.FakeImage.get_image_info(self.new_image)) + self.new_image + ) self.images_mock.update.return_value = self.new_image # Get the command object to test @@ -492,7 +493,7 @@ class TestImageList(TestImage): self.api_mock = mock.Mock() self.api_mock.image_list.side_effect = [ - [image_fakes.FakeImage.get_image_info(self._image)], [], + [self._image], [], ] self.app.client_manager.image.api = self.api_mock @@ -632,10 +633,7 @@ class TestImageList(TestImage): @mock.patch('openstackclient.api.utils.simple_filter') def test_image_list_property_option(self, sf_mock): - sf_mock.return_value = [ - copy.deepcopy( - image_fakes.FakeImage.get_image_info(self._image)), - ] + sf_mock.return_value = [copy.deepcopy(self._image)] arglist = [ '--property', 'a=1', @@ -651,7 +649,7 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with() sf_mock.assert_called_with( - [image_fakes.FakeImage.get_image_info(self._image)], + [self._image], attr='a', value='1', property_field='properties', @@ -662,10 +660,7 @@ class TestImageList(TestImage): @mock.patch('openstackclient.common.utils.sort_items') def test_image_list_sort_option(self, si_mock): - si_mock.return_value = [ - copy.deepcopy( - image_fakes.FakeImage.get_image_info(self._image)) - ] + si_mock.return_value = [copy.deepcopy(self._image)] arglist = ['--sort', 'name:asc'] verifylist = [('sort', 'name:asc')] @@ -677,7 +672,7 @@ class TestImageList(TestImage): columns, data = self.cmd.take_action(parsed_args) self.api_mock.image_list.assert_called_with() si_mock.assert_called_with( - [image_fakes.FakeImage.get_image_info(self._image)], + [self._image], 'name:asc' ) self.assertEqual(self.columns, columns)