1
# Vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Copyright 2013 Hewlett-Packard Development Company, L.P.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
22
from ironic.common import exception
23
from ironic.common import images
24
from ironic.openstack.common import excutils
25
from ironic.tests import base
28
class IronicImagesTestCase(base.TestCase):
29
def test_fetch_raw_image(self):
31
def fake_execute(*cmd, **kwargs):
32
self.executes.append(cmd)
35
def fake_rename(old, new):
36
self.executes.append(('mv', old, new))
38
def fake_unlink(path):
39
self.executes.append(('rm', path))
41
@contextlib.contextmanager
42
def fake_rm_on_error(path):
46
with excutils.save_and_reraise_exception():
47
fake_del_if_exists(path)
49
def fake_del_if_exists(path):
50
self.executes.append(('rm', '-f', path))
52
def fake_qemu_img_info(path):
53
class FakeImgInfo(object):
56
file_format = path.split('.')[-1]
57
if file_format == 'part':
58
file_format = path.split('.')[-2]
59
elif file_format == 'converted':
62
backing_file = 'backing'
66
FakeImgInfo.file_format = file_format
67
FakeImgInfo.backing_file = backing_file
71
self.useFixture(fixtures.MonkeyPatch(
72
'ironic.common.utils.execute', fake_execute))
73
self.useFixture(fixtures.MonkeyPatch('os.rename', fake_rename))
74
self.useFixture(fixtures.MonkeyPatch('os.unlink', fake_unlink))
75
self.useFixture(fixtures.MonkeyPatch(
76
'ironic.common.images.fetch', lambda *_: None))
77
self.useFixture(fixtures.MonkeyPatch(
78
'ironic.common.images.qemu_img_info', fake_qemu_img_info))
79
self.useFixture(fixtures.MonkeyPatch(
80
'ironic.openstack.common.fileutils.remove_path_on_error',
82
self.useFixture(fixtures.MonkeyPatch(
83
'ironic.openstack.common.fileutils.delete_if_exists',
86
context = 'opaque context'
91
expected_commands = [('qemu-img', 'convert', '-O', 'raw',
92
't.qcow2.part', 't.qcow2.converted'),
93
('rm', 't.qcow2.part'),
94
('mv', 't.qcow2.converted', 't.qcow2')]
95
images.fetch_to_raw(context, image_id, target)
96
self.assertEqual(self.executes, expected_commands)
100
expected_commands = [('mv', 't.raw.part', 't.raw')]
101
images.fetch_to_raw(context, image_id, target)
102
self.assertEqual(self.executes, expected_commands)
104
target = 'backing.qcow2'
106
expected_commands = [('rm', '-f', 'backing.qcow2.part')]
107
self.assertRaises(exception.ImageUnacceptable,
109
context, image_id, target)
110
self.assertEqual(self.executes, expected_commands)