From 556397aae721b240b6bd8eb971a18e669ad50c1d Mon Sep 17 00:00:00 2001 From: xiexs Date: Wed, 16 Dec 2015 09:25:38 +0800 Subject: [PATCH] Refactor TestImageCreate with FakeImage class Change-Id: I0044df36bb4d761c7998dfc8aa9a86d21d81da83 Implements: blueprint improve-image-unittest-framework --- openstackclient/tests/image/v2/fakes.py | 2 +- openstackclient/tests/image/v2/test_image.py | 115 ++++++++++--------- 2 files changed, 64 insertions(+), 53 deletions(-) diff --git a/openstackclient/tests/image/v2/fakes.py b/openstackclient/tests/image/v2/fakes.py index 692ef104de..5441a3e2ab 100644 --- a/openstackclient/tests/image/v2/fakes.py +++ b/openstackclient/tests/image/v2/fakes.py @@ -195,7 +195,7 @@ class FakeImage(object): 'owner': 'image-owner' + uuid.uuid4().hex, 'protected': bool(random.choice([0, 1])), 'visibility': random.choice(['public', 'private']), - 'tags': [uuid.uuid4().hex for r in range(random.randint(1, 5))], + 'tags': [uuid.uuid4().hex for r in range(2)], } # Overwrite default attributes if there are some attributes set diff --git a/openstackclient/tests/image/v2/test_image.py b/openstackclient/tests/image/v2/test_image.py index 10266f0223..cb92d495ab 100644 --- a/openstackclient/tests/image/v2/test_image.py +++ b/openstackclient/tests/image/v2/test_image.py @@ -55,18 +55,12 @@ class TestImageCreate(TestImage): def setUp(self): super(TestImageCreate, self).setUp() - self.images_mock.create.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.new_image = image_fakes.FakeImage.create_one_image() + self.images_mock.create.return_value = self.new_image # This is the return value for utils.find_resource() - self.images_mock.get.return_value = copy.deepcopy(image_fakes.IMAGE) - self.images_mock.update.return_value = fakes.FakeResource( - None, - copy.deepcopy(image_fakes.IMAGE), - loaded=True, - ) + self.images_mock.get.return_value = copy.deepcopy( + image_fakes.FakeImage.get_image_info(self.new_image)) + self.images_mock.update.return_value = self.new_image # Get the command object to test self.cmd = image.CreateImage(self.app, None) @@ -77,12 +71,12 @@ class TestImageCreate(TestImage): } self.images_mock.configure_mock(**mock_exception) arglist = [ - image_fakes.image_name, + self.new_image.name ] verifylist = [ ('container_format', image.DEFAULT_CONTAINER_FORMAT), ('disk_format', image.DEFAULT_DISK_FORMAT), - ('name', image_fakes.image_name), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -91,7 +85,7 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format=image.DEFAULT_CONTAINER_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT, ) @@ -103,8 +97,12 @@ class TestImageCreate(TestImage): mock.ANY, mock.ANY, ) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_SHOW_data, data) + self.assertEqual( + image_fakes.FakeImage.get_image_columns(self.new_image), + columns) + self.assertEqual( + image_fakes.FakeImage.get_image_data(self.new_image), + data) @mock.patch('glanceclient.common.utils.get_data_file', name='Open') def test_image_reserve_options(self, mock_open): @@ -120,22 +118,24 @@ class TestImageCreate(TestImage): '--disk-format', 'fs', '--min-disk', '10', '--min-ram', '4', - '--owner', '123456', - '--protected', - '--private', - image_fakes.image_name, + '--owner', self.new_image.owner, + ('--protected' + if self.new_image.protected else '--unprotected'), + ('--private' + if self.new_image.visibility == 'private' else '--public'), + self.new_image.name, ] verifylist = [ ('container_format', 'ovf'), ('disk_format', 'fs'), ('min_disk', 10), ('min_ram', 4), - ('owner', '123456'), - ('protected', True), - ('unprotected', False), - ('public', False), - ('private', True), - ('name', image_fakes.image_name), + ('owner', self.new_image.owner), + ('protected', self.new_image.protected), + ('unprotected', not self.new_image.protected), + ('public', self.new_image.visibility == 'public'), + ('private', self.new_image.visibility == 'private'), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -144,14 +144,14 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format='ovf', disk_format='fs', min_disk=10, min_ram=4, - owner='123456', - protected=True, - visibility='private', + owner=self.new_image.owner, + protected=self.new_image.protected, + visibility=self.new_image.visibility, ) # Verify update() was not called, if it was show the args @@ -161,14 +161,19 @@ class TestImageCreate(TestImage): mock.ANY, mock.ANY, ) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_SHOW_data, data) + self.assertEqual( + image_fakes.FakeImage.get_image_columns(self.new_image), + columns) + self.assertEqual( + image_fakes.FakeImage.get_image_data(self.new_image), + data) @mock.patch('glanceclient.common.utils.get_data_file', name='Open') def test_image_create_file(self, mock_open): mock_file = mock.MagicMock(name='File') mock_open.return_value = mock_file - mock_open.read.return_value = image_fakes.IMAGE_data + mock_open.read.return_value = ( + image_fakes.FakeImage.get_image_data(self.new_image)) mock_exception = { 'find.side_effect': exceptions.CommandError('x'), } @@ -176,23 +181,25 @@ class TestImageCreate(TestImage): arglist = [ '--file', 'filer', - '--unprotected', - '--public', + ('--unprotected' + if not self.new_image.protected else '--protected'), + ('--public' + if self.new_image.visibility == 'public' else '--private'), '--property', 'Alpha=1', '--property', 'Beta=2', - '--tag', 'awesome', - '--tag', 'better', - image_fakes.image_name, + '--tag', self.new_image.tags[0], + '--tag', self.new_image.tags[1], + self.new_image.name, ] verifylist = [ ('file', 'filer'), - ('protected', False), - ('unprotected', True), - ('public', True), - ('private', False), + ('protected', self.new_image.protected), + ('unprotected', not self.new_image.protected), + ('public', self.new_image.visibility == 'public'), + ('private', self.new_image.visibility == 'private'), ('properties', {'Alpha': '1', 'Beta': '2'}), - ('tags', ['awesome', 'better']), - ('name', image_fakes.image_name), + ('tags', self.new_image.tags), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -201,14 +208,14 @@ class TestImageCreate(TestImage): # ImageManager.create(name=, **) self.images_mock.create.assert_called_with( - name=image_fakes.image_name, + name=self.new_image.name, container_format=image.DEFAULT_CONTAINER_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT, - protected=False, - visibility='public', + protected=self.new_image.protected, + visibility=self.new_image.visibility, Alpha='1', Beta='2', - tags=['awesome', 'better'], + tags=self.new_image.tags, ) # Verify update() was not called, if it was show the args @@ -218,17 +225,21 @@ class TestImageCreate(TestImage): mock.ANY, mock.ANY, ) - self.assertEqual(image_fakes.IMAGE_columns, columns) - self.assertEqual(image_fakes.IMAGE_SHOW_data, data) + self.assertEqual( + image_fakes.FakeImage.get_image_columns(self.new_image), + columns) + self.assertEqual( + image_fakes.FakeImage.get_image_data(self.new_image), + data) def test_image_create_dead_options(self): arglist = [ '--store', 'somewhere', - image_fakes.image_name, + self.new_image.name, ] verifylist = [ - ('name', image_fakes.image_name), + ('name', self.new_image.name), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist)