Skip to content

Commit

Permalink
Makes snapshots work for amis. Fixes bug 873156
Browse files Browse the repository at this point in the history
(cherry picked from commit b931d51)

Change-Id: I3adee5aed8a500602fb938f27fa5096ec376cbe2
  • Loading branch information
vishvananda authored and Chuck Short committed Oct 20, 2011
1 parent 1a5e3e0 commit d46f6e0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
20 changes: 10 additions & 10 deletions nova/image/fake.py
Expand Up @@ -49,8 +49,8 @@ def __init__(self):
'deleted': False,
'status': 'active',
'is_public': False,
# 'container_format': 'ami',
# 'disk_format': 'raw',
'container_format': 'raw',
'disk_format': 'raw',
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel,
'architecture': 'x86_64'}}
Expand All @@ -63,8 +63,8 @@ def __init__(self):
'deleted': False,
'status': 'active',
'is_public': True,
# 'container_format': 'ami',
# 'disk_format': 'raw',
'container_format': 'ami',
'disk_format': 'ami',
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel}}

Expand All @@ -76,8 +76,8 @@ def __init__(self):
'deleted': False,
'status': 'active',
'is_public': True,
# 'container_format': 'ami',
# 'disk_format': 'raw',
'container_format': None,
'disk_format': None,
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel}}

Expand All @@ -89,8 +89,8 @@ def __init__(self):
'deleted': False,
'status': 'active',
'is_public': True,
# 'container_format': 'ami',
# 'disk_format': 'raw',
'container_format': 'ami',
'disk_format': 'ami',
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel}}

Expand All @@ -102,8 +102,8 @@ def __init__(self):
'deleted': False,
'status': 'active',
'is_public': True,
# 'container_format': 'ami',
# 'disk_format': 'raw',
'container_format': 'ami',
'disk_format': 'ami',
'properties': {'kernel_id': FLAGS.null_kernel,
'ramdisk_id': FLAGS.null_kernel}}

Expand Down
3 changes: 2 additions & 1 deletion nova/tests/test_image.py
Expand Up @@ -39,7 +39,8 @@ def test_detail(self):
keys = set(image.keys())
self.assertEquals(keys, set(['id', 'name', 'created_at',
'updated_at', 'deleted_at', 'deleted',
'status', 'is_public', 'properties']))
'status', 'is_public', 'properties',
'disk_format', 'container_format']))
self.assertTrue(isinstance(image['created_at'], datetime.datetime))
self.assertTrue(isinstance(image['updated_at'], datetime.datetime))

Expand Down
41 changes: 41 additions & 0 deletions nova/tests/test_libvirt.py
Expand Up @@ -340,6 +340,47 @@ def test_lxc_container_and_uri(self):
instance_data = dict(self.test_instance)
self._check_xml_and_container(instance_data)

def test_snapshot_in_ami_format(self):
if not self.lazy_load_library_exists():
return

self.flags(image_service='nova.image.fake.FakeImageService')

# Start test
image_service = utils.import_object(FLAGS.image_service)

# Assign image_ref = 3 from nova/images/fakes for testing
# ami image
test_instance = copy.deepcopy(self.test_instance)
test_instance["image_ref"] = "3"

# Assuming that base image already exists in image_service
instance_ref = db.instance_create(self.context, test_instance)
properties = {'instance_id': instance_ref['id'],
'user_id': str(self.context.user_id)}
snapshot_name = 'test-snap'
sent_meta = {'name': snapshot_name, 'is_public': False,
'status': 'creating', 'properties': properties}
# Create new image. It will be updated in snapshot method
# To work with it from snapshot, the single image_service is needed
recv_meta = image_service.create(context, sent_meta)

self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn')
connection.LibvirtConnection._conn.lookupByName = self.fake_lookup
self.mox.StubOutWithMock(connection.utils, 'execute')
connection.utils.execute = self.fake_execute

self.mox.ReplayAll()

conn = connection.LibvirtConnection(False)
conn.snapshot(self.context, instance_ref, recv_meta['id'])

snapshot = image_service.show(context, recv_meta['id'])
self.assertEquals(snapshot['properties']['image_state'], 'available')
self.assertEquals(snapshot['status'], 'active')
self.assertEquals(snapshot['disk_format'], 'ami')
self.assertEquals(snapshot['name'], snapshot_name)

def test_snapshot_in_raw_format(self):
if not self.lazy_load_library_exists():
return
Expand Down
9 changes: 8 additions & 1 deletion nova/virt/libvirt/connection.py
Expand Up @@ -420,10 +420,17 @@ def snapshot(self, context, instance, image_href):
metadata['properties']['architecture'] = arch

source_format = base.get('disk_format') or 'raw'
if source_format == 'ami':
# NOTE(vish): assume amis are raw
source_format = 'raw'
image_format = FLAGS.snapshot_image_format or source_format
if FLAGS.use_cow_images:
source_format = 'qcow2'
metadata['disk_format'] = image_format
# NOTE(vish): glance forces ami disk format to be ami
if base.get('disk_format') == 'ami':
metadata['disk_format'] = 'ami'
else:
metadata['disk_format'] = image_format

if 'container_format' in base:
metadata['container_format'] = base['container_format']
Expand Down

0 comments on commit d46f6e0

Please sign in to comment.