Merge "Refactor TestImageCreate with FakeImage class"

This commit is contained in:
Jenkins 2015-12-19 06:59:43 +00:00 committed by Gerrit Code Review
commit b611045639
2 changed files with 64 additions and 53 deletions

View File

@ -195,7 +195,7 @@ class FakeImage(object):
'owner': 'image-owner' + uuid.uuid4().hex, 'owner': 'image-owner' + uuid.uuid4().hex,
'protected': bool(random.choice([0, 1])), 'protected': bool(random.choice([0, 1])),
'visibility': random.choice(['public', 'private']), '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 # Overwrite default attributes if there are some attributes set

View File

@ -55,18 +55,12 @@ class TestImageCreate(TestImage):
def setUp(self): def setUp(self):
super(TestImageCreate, self).setUp() super(TestImageCreate, self).setUp()
self.images_mock.create.return_value = fakes.FakeResource( self.new_image = image_fakes.FakeImage.create_one_image()
None, self.images_mock.create.return_value = self.new_image
copy.deepcopy(image_fakes.IMAGE),
loaded=True,
)
# This is the return value for utils.find_resource() # This is the return value for utils.find_resource()
self.images_mock.get.return_value = copy.deepcopy(image_fakes.IMAGE) self.images_mock.get.return_value = copy.deepcopy(
self.images_mock.update.return_value = fakes.FakeResource( image_fakes.FakeImage.get_image_info(self.new_image))
None, self.images_mock.update.return_value = self.new_image
copy.deepcopy(image_fakes.IMAGE),
loaded=True,
)
# Get the command object to test # Get the command object to test
self.cmd = image.CreateImage(self.app, None) self.cmd = image.CreateImage(self.app, None)
@ -77,12 +71,12 @@ class TestImageCreate(TestImage):
} }
self.images_mock.configure_mock(**mock_exception) self.images_mock.configure_mock(**mock_exception)
arglist = [ arglist = [
image_fakes.image_name, self.new_image.name
] ]
verifylist = [ verifylist = [
('container_format', image.DEFAULT_CONTAINER_FORMAT), ('container_format', image.DEFAULT_CONTAINER_FORMAT),
('disk_format', image.DEFAULT_DISK_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) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -91,7 +85,7 @@ class TestImageCreate(TestImage):
# ImageManager.create(name=, **) # ImageManager.create(name=, **)
self.images_mock.create.assert_called_with( self.images_mock.create.assert_called_with(
name=image_fakes.image_name, name=self.new_image.name,
container_format=image.DEFAULT_CONTAINER_FORMAT, container_format=image.DEFAULT_CONTAINER_FORMAT,
disk_format=image.DEFAULT_DISK_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT,
) )
@ -103,8 +97,12 @@ class TestImageCreate(TestImage):
mock.ANY, mock.ANY, mock.ANY, mock.ANY,
) )
self.assertEqual(image_fakes.IMAGE_columns, columns) self.assertEqual(
self.assertEqual(image_fakes.IMAGE_SHOW_data, data) 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') @mock.patch('glanceclient.common.utils.get_data_file', name='Open')
def test_image_reserve_options(self, mock_open): def test_image_reserve_options(self, mock_open):
@ -120,22 +118,24 @@ class TestImageCreate(TestImage):
'--disk-format', 'fs', '--disk-format', 'fs',
'--min-disk', '10', '--min-disk', '10',
'--min-ram', '4', '--min-ram', '4',
'--owner', '123456', '--owner', self.new_image.owner,
'--protected', ('--protected'
'--private', if self.new_image.protected else '--unprotected'),
image_fakes.image_name, ('--private'
if self.new_image.visibility == 'private' else '--public'),
self.new_image.name,
] ]
verifylist = [ verifylist = [
('container_format', 'ovf'), ('container_format', 'ovf'),
('disk_format', 'fs'), ('disk_format', 'fs'),
('min_disk', 10), ('min_disk', 10),
('min_ram', 4), ('min_ram', 4),
('owner', '123456'), ('owner', self.new_image.owner),
('protected', True), ('protected', self.new_image.protected),
('unprotected', False), ('unprotected', not self.new_image.protected),
('public', False), ('public', self.new_image.visibility == 'public'),
('private', True), ('private', self.new_image.visibility == 'private'),
('name', image_fakes.image_name), ('name', self.new_image.name),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -144,14 +144,14 @@ class TestImageCreate(TestImage):
# ImageManager.create(name=, **) # ImageManager.create(name=, **)
self.images_mock.create.assert_called_with( self.images_mock.create.assert_called_with(
name=image_fakes.image_name, name=self.new_image.name,
container_format='ovf', container_format='ovf',
disk_format='fs', disk_format='fs',
min_disk=10, min_disk=10,
min_ram=4, min_ram=4,
owner='123456', owner=self.new_image.owner,
protected=True, protected=self.new_image.protected,
visibility='private', visibility=self.new_image.visibility,
) )
# Verify update() was not called, if it was show the args # Verify update() was not called, if it was show the args
@ -161,14 +161,19 @@ class TestImageCreate(TestImage):
mock.ANY, mock.ANY, mock.ANY, mock.ANY,
) )
self.assertEqual(image_fakes.IMAGE_columns, columns) self.assertEqual(
self.assertEqual(image_fakes.IMAGE_SHOW_data, data) 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') @mock.patch('glanceclient.common.utils.get_data_file', name='Open')
def test_image_create_file(self, mock_open): def test_image_create_file(self, mock_open):
mock_file = mock.MagicMock(name='File') mock_file = mock.MagicMock(name='File')
mock_open.return_value = mock_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 = { mock_exception = {
'find.side_effect': exceptions.CommandError('x'), 'find.side_effect': exceptions.CommandError('x'),
} }
@ -176,23 +181,25 @@ class TestImageCreate(TestImage):
arglist = [ arglist = [
'--file', 'filer', '--file', 'filer',
'--unprotected', ('--unprotected'
'--public', if not self.new_image.protected else '--protected'),
('--public'
if self.new_image.visibility == 'public' else '--private'),
'--property', 'Alpha=1', '--property', 'Alpha=1',
'--property', 'Beta=2', '--property', 'Beta=2',
'--tag', 'awesome', '--tag', self.new_image.tags[0],
'--tag', 'better', '--tag', self.new_image.tags[1],
image_fakes.image_name, self.new_image.name,
] ]
verifylist = [ verifylist = [
('file', 'filer'), ('file', 'filer'),
('protected', False), ('protected', self.new_image.protected),
('unprotected', True), ('unprotected', not self.new_image.protected),
('public', True), ('public', self.new_image.visibility == 'public'),
('private', False), ('private', self.new_image.visibility == 'private'),
('properties', {'Alpha': '1', 'Beta': '2'}), ('properties', {'Alpha': '1', 'Beta': '2'}),
('tags', ['awesome', 'better']), ('tags', self.new_image.tags),
('name', image_fakes.image_name), ('name', self.new_image.name),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@ -201,14 +208,14 @@ class TestImageCreate(TestImage):
# ImageManager.create(name=, **) # ImageManager.create(name=, **)
self.images_mock.create.assert_called_with( self.images_mock.create.assert_called_with(
name=image_fakes.image_name, name=self.new_image.name,
container_format=image.DEFAULT_CONTAINER_FORMAT, container_format=image.DEFAULT_CONTAINER_FORMAT,
disk_format=image.DEFAULT_DISK_FORMAT, disk_format=image.DEFAULT_DISK_FORMAT,
protected=False, protected=self.new_image.protected,
visibility='public', visibility=self.new_image.visibility,
Alpha='1', Alpha='1',
Beta='2', Beta='2',
tags=['awesome', 'better'], tags=self.new_image.tags,
) )
# Verify update() was not called, if it was show the args # Verify update() was not called, if it was show the args
@ -218,17 +225,21 @@ class TestImageCreate(TestImage):
mock.ANY, mock.ANY, mock.ANY, mock.ANY,
) )
self.assertEqual(image_fakes.IMAGE_columns, columns) self.assertEqual(
self.assertEqual(image_fakes.IMAGE_SHOW_data, data) 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): def test_image_create_dead_options(self):
arglist = [ arglist = [
'--store', 'somewhere', '--store', 'somewhere',
image_fakes.image_name, self.new_image.name,
] ]
verifylist = [ verifylist = [
('name', image_fakes.image_name), ('name', self.new_image.name),
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)