| 1 |
import logging |
| 2 |
from unittest.mock import ( |
| 3 |
patch, Mock |
| 4 |
) |
| 5 |
from pytest import ( |
| 6 |
raises, fixture |
| 7 |
) |
| 8 |
|
| 9 |
from kiwi.system.uri import Uri |
| 10 |
from kiwi.system.root_import.oci import RootImportOCI |
| 11 |
|
| 12 |
from kiwi.exceptions import KiwiRootImportError |
| 13 |
|
| 14 |
|
| 15 |
class TestRootImportOCI: |
| 16 |
@fixture(autouse=True) |
| 17 |
def inject_fixtures(self, caplog): |
| 18 |
self._caplog = caplog |
| 19 |
|
| 20 |
@patch('os.path.exists') |
| 21 |
def setup(self, mock_path): |
| 22 |
mock_path.return_value = True |
| 23 |
with patch.dict('os.environ', {'HOME': '../data'}): |
| 24 |
self.oci_import = RootImportOCI( |
| 25 |
'root_dir', [Uri('file:///image.tar')], |
| 26 |
{'archive_transport': 'oci-archive'} |
| 27 |
) |
| 28 |
self.runtime_config = Mock() |
| 29 |
self.shasum = self.runtime_config.get_checksum_handler.return_value |
| 30 |
self.oci_import.runtime_config = self.runtime_config |
| 31 |
assert self.oci_import.image_files == ['/image.tar'] |
| 32 |
|
| 33 |
@patch('os.path.exists') |
| 34 |
def setup_method(self, cls, mock_path): |
| 35 |
self.setup() |
| 36 |
|
| 37 |
@patch('os.path.exists') |
| 38 |
def test_failed_init(self, mock_path): |
| 39 |
mock_path.return_value = False |
| 40 |
with raises(KiwiRootImportError): |
| 41 |
RootImportOCI( |
| 42 |
'root_dir', [Uri('file:///image.tar.xz')], |
| 43 |
{'archive_transport': 'oci-archive'} |
| 44 |
) |
| 45 |
|
| 46 |
@patch('kiwi.system.root_import.oci.Compress') |
| 47 |
@patch('kiwi.system.root_import.oci.Path.create') |
| 48 |
@patch('kiwi.system.root_import.oci.OCI') |
| 49 |
def test_sync_data(self, mock_OCI, mock_path, mock_compress): |
| 50 |
oci = Mock() |
| 51 |
mock_OCI.new.return_value.__enter__.return_value = oci |
| 52 |
|
| 53 |
uncompress = Mock() |
| 54 |
uncompress.get_format = Mock(return_value=None) |
| 55 |
mock_compress.return_value = uncompress |
| 56 |
|
| 57 |
self.oci_import.sync_data() |
| 58 |
|
| 59 |
mock_OCI.new.assert_called_once_with() |
| 60 |
|
| 61 |
oci.unpack.assert_called_once_with() |
| 62 |
oci.import_rootfs.assert_called_once_with( |
| 63 |
'root_dir' |
| 64 |
) |
| 65 |
self.shasum.digest.assert_called_once_with() |
| 66 |
uncompress.get_format.assert_called_once_with() |
| 67 |
|
| 68 |
@patch('kiwi.system.root_import.oci.Compress') |
| 69 |
@patch('kiwi.system.root_import.oci.Path.create') |
| 70 |
@patch('kiwi.system.root_import.oci.pathlib.Path') |
| 71 |
@patch('kiwi.system.root_import.oci.MountManager') |
| 72 |
@patch('kiwi.system.root_import.oci.OCI') |
| 73 |
def test_overlay_data( |
| 74 |
self, mock_OCI, mock_MountManager, mock_Path, |
| 75 |
mock_path_create, mock_compress |
| 76 |
): |
| 77 |
oci = Mock() |
| 78 |
mock_OCI.new.return_value.__enter__.return_value = oci |
| 79 |
|
| 80 |
mock_pth = Mock() |
| 81 |
mock_Path.return_value = mock_pth |
| 82 |
|
| 83 |
self.oci_import.overlay_data() |
| 84 |
|
| 85 |
mock_OCI.new.assert_called_once_with() |
| 86 |
|
| 87 |
oci.import_container_image.assert_called_once_with( |
| 88 |
f'oci-archive:{mock_compress.return_value.uncompressed_filename}' |
| 89 |
) |
| 90 |
oci.unpack.assert_called_once_with() |
| 91 |
oci.import_rootfs.assert_called_once_with( |
| 92 |
'root_dir' |
| 93 |
) |
| 94 |
mock_Path.assert_called_once_with('root_dir') |
| 95 |
mock_pth.replace.assert_called_once_with('root_dir_ro') |
| 96 |
mock_path_create.assert_called_once_with('root_dir') |
| 97 |
mock_MountManager.return_value.overlay_mount.assert_called_once_with( |
| 98 |
'root_dir_ro' |
| 99 |
) |
| 100 |
|
| 101 |
@patch('kiwi.system.root_import.oci.Compress') |
| 102 |
@patch('kiwi.system.root_import.oci.Path.create') |
| 103 |
@patch('kiwi.system.root_import.oci.OCI') |
| 104 |
def test_sync_data_compressed_image( |
| 105 |
self, mock_OCI, mock_path, mock_compress |
| 106 |
): |
| 107 |
oci = Mock() |
| 108 |
mock_OCI.new.return_value.__enter__.return_value = oci |
| 109 |
|
| 110 |
uncompress = Mock() |
| 111 |
uncompress.get_format = Mock(return_value='xz') |
| 112 |
mock_compress.return_value = uncompress |
| 113 |
|
| 114 |
self.oci_import.sync_data() |
| 115 |
|
| 116 |
mock_OCI.new.assert_called_once_with() |
| 117 |
|
| 118 |
oci.unpack.assert_called_once_with() |
| 119 |
oci.import_rootfs.assert_called_once_with( |
| 120 |
'root_dir' |
| 121 |
) |
| 122 |
self.shasum.digest.assert_called_once_with() |
| 123 |
uncompress.get_format.assert_called_once_with() |
| 124 |
uncompress.uncompress.assert_called_once_with(True) |
| 125 |
|
| 126 |
@patch('os.path.exists') |
| 127 |
@patch('kiwi.system.root_import.oci.Path.create') |
| 128 |
@patch('kiwi.system.root_import.oci.OCI') |
| 129 |
def test_sync_data_unknown_uri( |
| 130 |
self, mock_OCI, mock_path, mock_exists |
| 131 |
): |
| 132 |
mock_exists.return_value = True |
| 133 |
oci = Mock() |
| 134 |
mock_OCI.new.return_value.__enter__.return_value = oci |
| 135 |
with patch.dict('os.environ', {'HOME': '../data'}): |
| 136 |
oci_import = RootImportOCI( |
| 137 |
'root_dir', [Uri('docker:image:tag')], |
| 138 |
{'archive_transport': 'docker-archive'} |
| 139 |
) |
| 140 |
oci_import.runtime_config = self.runtime_config |
| 141 |
|
| 142 |
with self._caplog.at_level(logging.WARNING): |
| 143 |
oci_import.sync_data() |
| 144 |
mock_OCI.new.assert_called_once_with() |
| 145 |
oci.import_container_image.assert_called_once_with( |
| 146 |
'docker:image:tag' |
| 147 |
) |
| 148 |
oci.unpack.assert_called_once_with() |
| 149 |
oci.import_rootfs.assert_called_once_with( |
| 150 |
'root_dir' |
| 151 |
) |
| 152 |
self.shasum.digest.assert_called_once_with() |