| 1 |
from pytest import raises |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock |
| 4 |
) |
| 5 |
|
| 6 |
from kiwi.defaults import Defaults |
| 7 |
from kiwi.storage.subformat.oci import DiskFormatOci |
| 8 |
from kiwi.exceptions import KiwiContainerBuilderError |
| 9 |
from kiwi.system.uri import Uri |
| 10 |
|
| 11 |
|
| 12 |
class TestDiskFormatOci: |
| 13 |
def setup(self): |
| 14 |
Defaults.set_platform_name('x86_64') |
| 15 |
xml_data = Mock() |
| 16 |
xml_data.get_name = Mock( |
| 17 |
return_value='some-disk-image' |
| 18 |
) |
| 19 |
self.xml_state = Mock() |
| 20 |
self.uri = Uri( |
| 21 |
'docker-archive:/home/ms/alpine.tar', repo_type='container' |
| 22 |
) |
| 23 |
self.xml_state.xml_data = xml_data |
| 24 |
self.xml_state.get_derived_from_image_uri = Mock( |
| 25 |
return_value=[self.uri] |
| 26 |
) |
| 27 |
self.xml_state.get_image_version = Mock( |
| 28 |
return_value='1.2.3' |
| 29 |
) |
| 30 |
self.disk_format = DiskFormatOci( |
| 31 |
self.xml_state, 'root_dir', 'target_dir' |
| 32 |
) |
| 33 |
self.disk_format.base_format = 'raw' |
| 34 |
self.runtime_config = Mock() |
| 35 |
self.runtime_config.get_container_compression.return_value = True |
| 36 |
self.shasum = self.runtime_config.get_checksum_handler.return_value |
| 37 |
self.shasum.suffix = '.sha256' |
| 38 |
self.disk_format.runtime_config = self.runtime_config |
| 39 |
|
| 40 |
def setup_method(self, cls): |
| 41 |
self.setup() |
| 42 |
|
| 43 |
def test_post_init(self): |
| 44 |
self.disk_format.post_init({'base_format': 'raw'}) |
| 45 |
assert self.disk_format.base_container_uris == [self.uri] |
| 46 |
assert self.disk_format.base_format == 'raw' |
| 47 |
|
| 48 |
@patch('kiwi.storage.subformat.oci.Temporary.new_dir') |
| 49 |
@patch('kiwi.storage.subformat.oci.Command.run') |
| 50 |
@patch('kiwi.storage.subformat.oci.Defaults') |
| 51 |
@patch('kiwi.storage.subformat.oci.ContainerImage') |
| 52 |
@patch('kiwi.storage.subformat.oci.DiskFormat') |
| 53 |
@patch('kiwi.storage.subformat.oci.Path') |
| 54 |
@patch('kiwi.storage.subformat.oci.Result') |
| 55 |
@patch('os.chown') |
| 56 |
@patch('os.stat') |
| 57 |
def test_create_image_format_empty_root( |
| 58 |
self, |
| 59 |
mock_stat, |
| 60 |
mock_chown, |
| 61 |
mock_Result, |
| 62 |
mock_Path, |
| 63 |
mock_DiskFormat, |
| 64 |
mock_ContainerImage, |
| 65 |
mock_Defaults, |
| 66 |
mock_Command_run, |
| 67 |
mock_Temporary_new_dir |
| 68 |
): |
| 69 |
mock_Temporary_new_dir.return_value.name = 'root_dir' |
| 70 |
result = Mock() |
| 71 |
result.get_name_for_pattern.return_value = 'some_custom_name' |
| 72 |
mock_Result.return_value = result |
| 73 |
self.disk_format.bundle_format = 'some' |
| 74 |
self.disk_format.base_container_uris = [] |
| 75 |
mock_Defaults.get_imported_root_image.return_value \ |
| 76 |
= 'some-base' |
| 77 |
|
| 78 |
# test raw format |
| 79 |
self.disk_format.create_image_format() |
| 80 |
|
| 81 |
mock_Path.assert_called_once_with('root_dir/disk') |
| 82 |
mock_Command_run.assert_called_once_with( |
| 83 |
[ |
| 84 |
'cp', '-a', |
| 85 |
'target_dir/some-disk-image.x86_64-1.2.3.raw', |
| 86 |
'root_dir/disk/some_custom_name.raw' |
| 87 |
] |
| 88 |
) |
| 89 |
mock_ContainerImage.new.assert_called_once_with( |
| 90 |
'docker', 'root_dir', |
| 91 |
self.xml_state.get_container_config.return_value |
| 92 |
) |
| 93 |
mock_ContainerImage.new.return_value.create.assert_called_once_with( |
| 94 |
'target_dir/some-disk-image.x86_64-1.2.3.docker.tar', |
| 95 |
'', True, True |
| 96 |
) |
| 97 |
|
| 98 |
@patch('kiwi.storage.subformat.oci.Command.run') |
| 99 |
@patch('kiwi.storage.subformat.oci.Temporary.new_dir') |
| 100 |
@patch('kiwi.storage.subformat.oci.RootImport') |
| 101 |
@patch('kiwi.storage.subformat.oci.Defaults') |
| 102 |
@patch('kiwi.storage.subformat.oci.Checksum') |
| 103 |
@patch('kiwi.storage.subformat.oci.ContainerImage') |
| 104 |
@patch('kiwi.storage.subformat.oci.RootInit') |
| 105 |
@patch('kiwi.storage.subformat.oci.DiskFormat') |
| 106 |
@patch('kiwi.storage.subformat.oci.Path') |
| 107 |
@patch('kiwi.storage.subformat.oci.Result') |
| 108 |
@patch('os.chown') |
| 109 |
@patch('os.stat') |
| 110 |
def test_create_image_format_derived_from( |
| 111 |
self, |
| 112 |
mock_stat, |
| 113 |
mock_chown, |
| 114 |
mock_Result, |
| 115 |
mock_Path, |
| 116 |
mock_DiskFormat, |
| 117 |
mock_RootInit, |
| 118 |
mock_ContainerImage, |
| 119 |
mock_Checksum, |
| 120 |
mock_Defaults, |
| 121 |
mock_RootImport, |
| 122 |
mock_Temporary_new_dir, |
| 123 |
mock_Command_run |
| 124 |
): |
| 125 |
result = Mock() |
| 126 |
result.get_name_for_pattern.return_value = 'some_custom_name' |
| 127 |
mock_Result.return_value = result |
| 128 |
self.disk_format.bundle_format = 'some' |
| 129 |
mock_Defaults.get_imported_root_image.return_value \ |
| 130 |
= 'some-base' |
| 131 |
tmpdir = Mock() |
| 132 |
tmpdir.name = 'tmpdir' |
| 133 |
mock_Temporary_new_dir.return_value = tmpdir |
| 134 |
|
| 135 |
# test raw format |
| 136 |
self.disk_format.create_image_format() |
| 137 |
|
| 138 |
mock_RootInit.assert_called_once_with( |
| 139 |
'tmpdir', allow_existing=True |
| 140 |
) |
| 141 |
mock_RootInit.return_value.create.assert_called_once_with() |
| 142 |
mock_RootImport.new.assert_called_once_with( |
| 143 |
'tmpdir', [self.uri], 'docker' |
| 144 |
) |
| 145 |
mock_RootImport.new.return_value.sync_data.assert_called_once_with() |
| 146 |
mock_Path.assert_called_once_with('tmpdir/disk') |
| 147 |
mock_Command_run.assert_called_once_with( |
| 148 |
[ |
| 149 |
'cp', '-a', |
| 150 |
'target_dir/some-disk-image.x86_64-1.2.3.raw', |
| 151 |
'tmpdir/disk/some_custom_name.raw' |
| 152 |
] |
| 153 |
) |
| 154 |
mock_Checksum.return_value.matches.assert_called_once_with( |
| 155 |
self.shasum.digest.return_value, 'some-base.sha256' |
| 156 |
) |
| 157 |
mock_ContainerImage.new.assert_called_once_with( |
| 158 |
'docker', 'tmpdir', self.xml_state.get_container_config.return_value |
| 159 |
) |
| 160 |
mock_ContainerImage.new.return_value.create.assert_called_once_with( |
| 161 |
'target_dir/some-disk-image.x86_64-1.2.3.docker.tar', |
| 162 |
'some-base', True, True |
| 163 |
) |
| 164 |
|
| 165 |
# test qcow2 format |
| 166 |
mock_Command_run.reset_mock() |
| 167 |
self.disk_format.base_format = 'qcow2' |
| 168 |
self.disk_format.create_image_format() |
| 169 |
mock_DiskFormat.new.assert_called_once_with( |
| 170 |
'qcow2', self.xml_state, 'root_dir', 'target_dir' |
| 171 |
) |
| 172 |
disk_format = mock_DiskFormat.new.return_value.__enter__.return_value |
| 173 |
disk_format.create_image_format.assert_called_once_with() |
| 174 |
mock_Command_run.assert_called_once_with( |
| 175 |
[ |
| 176 |
'cp', '-a', |
| 177 |
disk_format.get_target_file_path_for_format.return_value, |
| 178 |
'tmpdir/disk/some_custom_name.qcow2' |
| 179 |
] |
| 180 |
) |
| 181 |
|
| 182 |
# test checksum failed for base container |
| 183 |
mock_Checksum.return_value.matches.reset_mock() |
| 184 |
mock_Checksum.return_value.matches.return_value = False |
| 185 |
with raises(KiwiContainerBuilderError): |
| 186 |
self.disk_format.create_image_format() |
| 187 |
|
| 188 |
def test_store_to_result(self): |
| 189 |
result = Mock() |
| 190 |
self.disk_format.store_to_result(result) |
| 191 |
result.add.assert_called_once_with( |
| 192 |
compress=False, |
| 193 |
filename='target_dir/some-disk-image.x86_64-1.2.3.docker.tar', |
| 194 |
key='disk_format_image', |
| 195 |
shasum=True, |
| 196 |
use_for_bundle=True |
| 197 |
) |