| 1 |
from unittest.mock import ( |
| 2 |
patch, Mock, call |
| 3 |
) |
| 4 |
|
| 5 |
import kiwi |
| 6 |
|
| 7 |
from kiwi.defaults import Defaults |
| 8 |
from kiwi.storage.subformat.qcow2 import DiskFormatQcow2 |
| 9 |
|
| 10 |
|
| 11 |
class TestDiskFormatQcow2: |
| 12 |
def setup(self): |
| 13 |
Defaults.set_platform_name('x86_64') |
| 14 |
xml_data = Mock() |
| 15 |
xml_data.get_name = Mock( |
| 16 |
return_value='some-disk-image' |
| 17 |
) |
| 18 |
self.xml_state = Mock() |
| 19 |
self.xml_state.xml_data = xml_data |
| 20 |
self.xml_state.get_image_version = Mock( |
| 21 |
return_value='1.2.3' |
| 22 |
) |
| 23 |
self.runtime_config = Mock() |
| 24 |
self.runtime_config.get_bundle_compression.return_value = False |
| 25 |
kiwi.storage.subformat.base.RuntimeConfig = Mock( |
| 26 |
return_value=self.runtime_config |
| 27 |
) |
| 28 |
self.disk_format = DiskFormatQcow2( |
| 29 |
self.xml_state, 'root_dir', 'target_dir' |
| 30 |
) |
| 31 |
|
| 32 |
def setup_method(self, cls): |
| 33 |
self.setup() |
| 34 |
|
| 35 |
def test_post_init(self): |
| 36 |
self.disk_format.post_init({'option': 'value'}) |
| 37 |
assert self.disk_format.options == ['-o', 'option=value'] |
| 38 |
|
| 39 |
@patch('kiwi.storage.subformat.qcow2.Command.run') |
| 40 |
@patch('kiwi.storage.subformat.qcow2.Temporary.new_file') |
| 41 |
def test_create_image_format(self, mock_Temporary_new_file, mock_command): |
| 42 |
tmpfile = Mock() |
| 43 |
tmpfile.name = 'tmpfile' |
| 44 |
mock_Temporary_new_file.return_value = tmpfile |
| 45 |
self.disk_format.create_image_format() |
| 46 |
assert mock_command.call_args_list == [ |
| 47 |
call( |
| 48 |
[ |
| 49 |
'qemu-img', 'convert', '-f', 'raw', |
| 50 |
'target_dir/some-disk-image.x86_64-1.2.3.raw', |
| 51 |
'-O', 'qcow2', |
| 52 |
'tmpfile' |
| 53 |
] |
| 54 |
), |
| 55 |
call( |
| 56 |
[ |
| 57 |
'qemu-img', 'convert', '-c', '-f', 'qcow2', |
| 58 |
'tmpfile', |
| 59 |
'-O', 'qcow2', |
| 60 |
'target_dir/some-disk-image.x86_64-1.2.3.qcow2' |
| 61 |
] |
| 62 |
) |
| 63 |
] |
| 64 |
|
| 65 |
def test_store_to_result(self): |
| 66 |
result = Mock() |
| 67 |
self.disk_format.store_to_result(result) |
| 68 |
result.add.assert_called_once_with( |
| 69 |
compress=False, |
| 70 |
filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2', |
| 71 |
key='disk_format_image', |
| 72 |
shasum=True, |
| 73 |
use_for_bundle=True |
| 74 |
) |