Debian Codegraph @

test/unit/storage/subformat/base_test.py

raw
1 from unittest.mock import (
2 patch, Mock
3 )
4 from pytest import raises
5
6 from kiwi.defaults import Defaults
7 from kiwi.storage.subformat.base import DiskFormatBase
8
9 import kiwi
10
11 from kiwi.exceptions import (
12 KiwiFormatSetupError,
13 KiwiResizeRawDiskError
14 )
15
16
17 class TestDiskFormatBase:
18 @patch('kiwi.storage.subformat.base.DiskFormatBase.post_init')
19 def setup(self, mock_post_init):
20 Defaults.set_platform_name('x86_64')
21 xml_data = Mock()
22 xml_data.get_name = Mock(
23 return_value='some-disk-image'
24 )
25 self.xml_state = Mock()
26 self.xml_state.xml_data = xml_data
27 self.xml_state.get_image_version = Mock(
28 return_value='1.2.3'
29 )
30 self.xml_state.get_luks_credentials = Mock(
31 return_value=None
32 )
33 self.runtime_config = Mock()
34 self.runtime_config.get_bundle_compression.return_value = True
35 kiwi.storage.subformat.base.RuntimeConfig = Mock(
36 return_value=self.runtime_config
37 )
38 DiskFormatBase(
39 self.xml_state, 'root_dir', 'target_dir', {'option': 'unhandled'}
40 )
41 mock_post_init.assert_called_once_with({'option': 'unhandled'})
42 mock_post_init.reset_mock()
43 self.disk_format = DiskFormatBase(
44 self.xml_state, 'root_dir', 'target_dir'
45 )
46 mock_post_init.assert_called_once_with({})
47
48 @patch('kiwi.storage.subformat.base.DiskFormatBase.post_init')
49 def setup_method(self, cls, mock_post_init):
50 self.setup()
51
52 def test_create_image_format(self):
53 with raises(NotImplementedError):
54 self.disk_format.create_image_format()
55
56 def test_get_target_file_path_for_format_invalid_format(self):
57 with raises(KiwiFormatSetupError):
58 self.disk_format.get_target_file_path_for_format('foo')
59
60 def test_post_init(self):
61 # in the base class this method just pass
62 self.disk_format.post_init({})
63
64 def test_get_qemu_option_list(self):
65 custom_args = {
66 'subformat=format': None,
67 'adapter_type=type': None
68 }
69 assert self.disk_format.get_qemu_option_list(custom_args) == [
70 '-o', 'adapter_type=type', '-o', 'subformat=format'
71 ]
72
73 def test_get_target_file_path_for_format(self):
74 assert self.disk_format.get_target_file_path_for_format('vhd') == \
75 'target_dir/some-disk-image.x86_64-1.2.3.vhd'
76
77 def test_store_to_result_default(self):
78 result = Mock()
79 self.disk_format.image_format = 'qcow2'
80 self.disk_format.store_to_result(result)
81 result.add.assert_called_once_with(
82 compress=True,
83 filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2',
84 key='disk_format_image',
85 shasum=True,
86 use_for_bundle=True
87 )
88
89 def test_store_to_result_with_luks(self):
90 result = Mock()
91 self.xml_state.get_luks_credentials = Mock(return_value='foo')
92 self.disk_format.image_format = 'qcow2'
93 self.disk_format.store_to_result(result)
94 result.add.assert_called_once_with(
95 compress=False,
96 filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2',
97 key='disk_format_image',
98 shasum=True,
99 use_for_bundle=True
100 )
101 self.xml_state.get_luks_credentials = Mock(return_value=None)
102
103 @patch('os.path.getsize')
104 def test_resize_raw_disk_raises_on_shrink_disk(self, mock_getsize):
105 mock_getsize.return_value = 42
106 with raises(KiwiResizeRawDiskError):
107 self.disk_format.resize_raw_disk(10)
108
109 @patch('os.path.getsize')
110 @patch('kiwi.storage.subformat.base.Command.run')
111 def test_resize_raw_disk(self, mock_command, mock_getsize):
112 mock_getsize.return_value = 42
113 assert self.disk_format.resize_raw_disk(1024) is True
114 mock_command.assert_called_once_with(
115 [
116 'qemu-img', 'resize',
117 'target_dir/some-disk-image.x86_64-1.2.3.raw', '1024'
118 ]
119 )
120
121 @patch('os.path.getsize')
122 @patch('kiwi.storage.subformat.base.Command.run')
123 def test_resize_raw_disk_append(self, mock_command, mock_getsize):
124 mock_getsize.return_value = 42
125 assert self.disk_format.resize_raw_disk(1024, append=True) is True
126 mock_command.assert_called_once_with(
127 [
128 'qemu-img', 'resize',
129 'target_dir/some-disk-image.x86_64-1.2.3.raw', '+1024'
130 ]
131 )
132
133 @patch('os.path.getsize')
134 def test_resize_raw_disk_same_size(self, mock_getsize):
135 mock_getsize.return_value = 42
136 assert self.disk_format.resize_raw_disk(42) is False
137
138 @patch('os.path.exists')
139 def test_has_raw_disk(self, mock_exists):
140 mock_exists.return_value = True
141 assert self.disk_format.has_raw_disk() is True
142 mock_exists.assert_called_once_with(
143 'target_dir/some-disk-image.x86_64-1.2.3.raw'
144 )
145
146 @patch('kiwi.storage.subformat.base.Path.wipe')
147 @patch('os.path.exists')
148 def test_context_manager_exit(self, mock_exists, mock_wipe):
149 mock_exists.return_value = True
150 with DiskFormatBase(
151 self.xml_state, 'root_dir', 'target_dir'
152 ) as disk_format:
153 disk_format.temp_image_dir = 'tmpdir'
154 mock_wipe.assert_called_once_with('tmpdir')