~ubuntu-branches/ubuntu/utopic/ironic/utopic

« back to all changes in this revision

Viewing changes to ironic/tests/test_images.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-01-17 09:47:33 UTC
  • Revision ID: package-import@ubuntu.com-20140117094733-nedwwtz1kkj2s3w9
Tags: upstream-0.0~git20140117
ImportĀ upstreamĀ versionĀ 0.0~git20140117

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
# coding=utf-8
 
3
 
 
4
# Copyright 2013 Hewlett-Packard Development Company, L.P.
 
5
# All Rights Reserved.
 
6
#
 
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
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
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
 
17
#    under the License.
 
18
 
 
19
import contextlib
 
20
import fixtures
 
21
 
 
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
 
26
 
 
27
 
 
28
class IronicImagesTestCase(base.TestCase):
 
29
    def test_fetch_raw_image(self):
 
30
 
 
31
        def fake_execute(*cmd, **kwargs):
 
32
            self.executes.append(cmd)
 
33
            return None, None
 
34
 
 
35
        def fake_rename(old, new):
 
36
            self.executes.append(('mv', old, new))
 
37
 
 
38
        def fake_unlink(path):
 
39
            self.executes.append(('rm', path))
 
40
 
 
41
        @contextlib.contextmanager
 
42
        def fake_rm_on_error(path):
 
43
            try:
 
44
                yield
 
45
            except Exception:
 
46
                with excutils.save_and_reraise_exception():
 
47
                    fake_del_if_exists(path)
 
48
 
 
49
        def fake_del_if_exists(path):
 
50
            self.executes.append(('rm', '-f', path))
 
51
 
 
52
        def fake_qemu_img_info(path):
 
53
            class FakeImgInfo(object):
 
54
                pass
 
55
 
 
56
            file_format = path.split('.')[-1]
 
57
            if file_format == 'part':
 
58
                file_format = path.split('.')[-2]
 
59
            elif file_format == 'converted':
 
60
                file_format = 'raw'
 
61
            if 'backing' in path:
 
62
                backing_file = 'backing'
 
63
            else:
 
64
                backing_file = None
 
65
 
 
66
            FakeImgInfo.file_format = file_format
 
67
            FakeImgInfo.backing_file = backing_file
 
68
 
 
69
            return FakeImgInfo()
 
70
 
 
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',
 
81
                fake_rm_on_error))
 
82
        self.useFixture(fixtures.MonkeyPatch(
 
83
                'ironic.openstack.common.fileutils.delete_if_exists',
 
84
                fake_del_if_exists))
 
85
 
 
86
        context = 'opaque context'
 
87
        image_id = '4'
 
88
 
 
89
        target = 't.qcow2'
 
90
        self.executes = []
 
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)
 
97
 
 
98
        target = 't.raw'
 
99
        self.executes = []
 
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)
 
103
 
 
104
        target = 'backing.qcow2'
 
105
        self.executes = []
 
106
        expected_commands = [('rm', '-f', 'backing.qcow2.part')]
 
107
        self.assertRaises(exception.ImageUnacceptable,
 
108
                          images.fetch_to_raw,
 
109
                          context, image_id, target)
 
110
        self.assertEqual(self.executes, expected_commands)
 
111
 
 
112
        del self.executes