| 1 |
import logging |
| 2 |
from pytest import fixture |
| 3 |
from unittest.mock import ( |
| 4 |
patch, mock_open, Mock |
| 5 |
) |
| 6 |
|
| 7 |
import kiwi |
| 8 |
|
| 9 |
from kiwi.container.oci import ContainerImageOCI |
| 10 |
from kiwi.version import __version__ |
| 11 |
|
| 12 |
|
| 13 |
class TestContainerImageOCI: |
| 14 |
@fixture(autouse=True) |
| 15 |
def inject_fixtures(self, caplog): |
| 16 |
self._caplog = caplog |
| 17 |
|
| 18 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 19 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 20 |
def setup(self, mock_is_buildservice_worker, mock_cmd_caps): |
| 21 |
mock_is_buildservice_worker.return_value = False |
| 22 |
mock_cmd_caps.return_value = True |
| 23 |
self.runtime_config = Mock() |
| 24 |
self.runtime_config.get_container_compression = Mock( |
| 25 |
return_value=None |
| 26 |
) |
| 27 |
kiwi.container.oci.RuntimeConfig = Mock( |
| 28 |
return_value=self.runtime_config |
| 29 |
) |
| 30 |
self.oci = ContainerImageOCI( |
| 31 |
'root_dir', 'docker-archive', { |
| 32 |
'container_name': 'foo/bar', |
| 33 |
'additional_names': [':current', 'name:foobar', 'newname'] |
| 34 |
} |
| 35 |
) |
| 36 |
|
| 37 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 38 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 39 |
def setup_method(self, cls, mock_is_buildservice_worker, mock_cmd_caps): |
| 40 |
self.setup() |
| 41 |
|
| 42 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 43 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 44 |
def test_init_custom_args(self, mock_is_buildservice_worker, mock_cmd_caps): |
| 45 |
mock_is_buildservice_worker.return_value = False |
| 46 |
mock_cmd_caps.return_value = True |
| 47 |
custom_args = { |
| 48 |
'container_name': 'foo', |
| 49 |
'container_tag': '1.0', |
| 50 |
'additional_names': ['current', 'foobar'], |
| 51 |
'entry_command': ['/bin/bash', '-x'], |
| 52 |
'entry_subcommand': ['ls', '-l'], |
| 53 |
'maintainer': 'tux', |
| 54 |
'user': 'root', |
| 55 |
'workingdir': '/root', |
| 56 |
'expose_ports': ['80', '42'], |
| 57 |
'volumes': ['/var/log', '/tmp'], |
| 58 |
'environment': {'PATH': '/bin', 'FOO': 'bar'}, |
| 59 |
'labels': {'a': 'value', 'b': 'value'} |
| 60 |
} |
| 61 |
container = ContainerImageOCI( |
| 62 |
'root_dir', 'oci-archive', custom_args |
| 63 |
) |
| 64 |
assert container.oci_config == custom_args |
| 65 |
|
| 66 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 67 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 68 |
def test_init_without_custom_args( |
| 69 |
self, mock_is_buildservice_worker, mock_cmd_caps |
| 70 |
): |
| 71 |
mock_is_buildservice_worker.return_value = False |
| 72 |
mock_cmd_caps.return_value = True |
| 73 |
container = ContainerImageOCI('root_dir', 'oci-archive') |
| 74 |
assert container.oci_config == { |
| 75 |
'container_name': 'kiwi-container', |
| 76 |
'container_tag': 'latest', |
| 77 |
'history': {'created_by': 'KIWI {0}'.format(__version__)} |
| 78 |
} |
| 79 |
|
| 80 |
@patch('kiwi.container.oci.RuntimeConfig') |
| 81 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 82 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 83 |
def test_init_in_buildservice( |
| 84 |
self, mock_buildservice, mock_cmd_caps, mock_RuntimeConfig |
| 85 |
): |
| 86 |
mock_buildservice.return_value = True |
| 87 |
mock_cmd_caps.return_value = True |
| 88 |
|
| 89 |
m_open = mock_open() |
| 90 |
with patch('builtins.open', m_open, create=True): |
| 91 |
m_open.return_value.__iter__ = lambda _: \ |
| 92 |
iter(['BUILD_DISTURL=obs://build.opensuse.org/some:project']) |
| 93 |
container = ContainerImageOCI( |
| 94 |
'root_dir', 'oci-archive' |
| 95 |
) |
| 96 |
|
| 97 |
m_open.assert_called_once_with('/.buildenv') |
| 98 |
assert container.oci_config['labels'] == { |
| 99 |
'org.openbuildservice.disturl': |
| 100 |
'obs://build.opensuse.org/some:project' |
| 101 |
} |
| 102 |
|
| 103 |
m_open = mock_open() |
| 104 |
with patch('builtins.open', m_open, create=True): |
| 105 |
m_open.return_value.__iter__ = lambda _: \ |
| 106 |
iter(['BUILD_DISTURL=obs://build.opensuse.org/some:project']) |
| 107 |
container = ContainerImageOCI( |
| 108 |
'root_dir', 'oci-archive', {'labels': {'label': 'value'}} |
| 109 |
) |
| 110 |
|
| 111 |
m_open.assert_called_once_with('/.buildenv') |
| 112 |
assert container.oci_config['labels'] == { |
| 113 |
'label': 'value', |
| 114 |
'org.openbuildservice.disturl': |
| 115 |
'obs://build.opensuse.org/some:project' |
| 116 |
} |
| 117 |
|
| 118 |
@patch('kiwi.container.oci.RuntimeConfig') |
| 119 |
@patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') |
| 120 |
@patch('kiwi.defaults.Defaults.is_buildservice_worker') |
| 121 |
def test_init_in_buildservice_without_disturl( |
| 122 |
self, mock_buildservice, mock_cmd_caps, mock_RuntimeConfig |
| 123 |
): |
| 124 |
mock_buildservice.return_value = True |
| 125 |
mock_cmd_caps.return_value = True |
| 126 |
|
| 127 |
m_open = mock_open() |
| 128 |
with patch('builtins.open', m_open, create=True): |
| 129 |
m_open.return_value.__iter__ = lambda _: iter(['line content']) |
| 130 |
with self._caplog.at_level(logging.WARNING): |
| 131 |
container = ContainerImageOCI('root_dir', 'oci-archive') |
| 132 |
|
| 133 |
m_open.assert_called_once_with('/.buildenv') |
| 134 |
assert 'labels' not in container.oci_config |
| 135 |
|
| 136 |
@patch('kiwi.container.oci.OCI') |
| 137 |
@patch('kiwi.container.oci.Defaults.get_shared_cache_location') |
| 138 |
@patch('kiwi.container.oci.Compress') |
| 139 |
def test_create_oci_archive(self, mock_Compress, mock_cache, mock_OCI): |
| 140 |
compress = Mock() |
| 141 |
mock_Compress.return_value = compress |
| 142 |
mock_cache.return_value = 'var/cache/kiwi' |
| 143 |
mock_oci = Mock() |
| 144 |
mock_OCI.new.return_value.__enter__.return_value = mock_oci |
| 145 |
|
| 146 |
self.oci.archive_transport = 'oci-archive' |
| 147 |
self.oci.create('result.tar', '', True, True) |
| 148 |
|
| 149 |
mock_oci.init_container.assert_called_once_with() |
| 150 |
mock_oci.unpack.assert_called_once_with() |
| 151 |
mock_oci.sync_rootfs.assert_called_once_with( |
| 152 |
'root_dir', [ |
| 153 |
'image', '.kconfig', 'run/*', 'tmp/*', |
| 154 |
'.buildenv', 'var/cache/kiwi', 'dev/*', 'sys/*', 'proc/*' |
| 155 |
] |
| 156 |
) |
| 157 |
mock_oci.repack.assert_called_once_with( |
| 158 |
{ |
| 159 |
'container_name': 'foo/bar', |
| 160 |
'additional_names': [':current', 'name:foobar', 'newname'], |
| 161 |
'container_tag': 'latest', |
| 162 |
'entry_subcommand': ['/bin/bash'], |
| 163 |
'history': {'created_by': 'KIWI {0}'.format(__version__)} |
| 164 |
} |
| 165 |
) |
| 166 |
mock_oci.set_config.assert_called_once_with( |
| 167 |
{ |
| 168 |
'container_name': 'foo/bar', |
| 169 |
'additional_names': [':current', 'name:foobar', 'newname'], |
| 170 |
'container_tag': 'latest', |
| 171 |
'entry_subcommand': ['/bin/bash'], |
| 172 |
'history': {'created_by': 'KIWI {0}'.format(__version__)} |
| 173 |
} |
| 174 |
) |
| 175 |
mock_oci.post_process.assert_called_once_with() |
| 176 |
mock_oci.export_container_image.assert_called_once_with( |
| 177 |
'result.tar', 'oci-archive', 'foo/bar:latest', [] |
| 178 |
) |
| 179 |
compress.xz.assert_called_once_with( |
| 180 |
self.runtime_config.get_xz_options.return_value |
| 181 |
) |
| 182 |
|
| 183 |
@patch('kiwi.container.oci.OCI') |
| 184 |
@patch('kiwi.container.oci.Defaults.get_shared_cache_location') |
| 185 |
def test_create_derived_docker_archive( |
| 186 |
self, mock_cache, mock_OCI |
| 187 |
): |
| 188 |
mock_cache.return_value = 'var/cache/kiwi' |
| 189 |
mock_oci = Mock() |
| 190 |
mock_OCI.new.return_value.__enter__.return_value = mock_oci |
| 191 |
|
| 192 |
self.runtime_config.get_container_compression.return_value = 'xz' |
| 193 |
|
| 194 |
self.oci.create('result.tar', 'root_dir/image/image_file') |
| 195 |
|
| 196 |
mock_oci.import_container_image.assert_called_once_with( |
| 197 |
'oci-archive:root_dir/image/image_file:base_layer' |
| 198 |
) |
| 199 |
mock_oci.unpack.assert_called_once_with() |
| 200 |
mock_oci.sync_rootfs.assert_called_once_with( |
| 201 |
'root_dir', [ |
| 202 |
'image', '.kconfig', 'run/*', 'tmp/*', |
| 203 |
'.buildenv', 'var/cache/kiwi', 'dev/*', 'sys/*', 'proc/*' |
| 204 |
] |
| 205 |
) |
| 206 |
mock_oci.repack.assert_called_once_with({ |
| 207 |
'container_name': 'foo/bar', |
| 208 |
'additional_names': [':current', 'name:foobar', 'newname'], |
| 209 |
'container_tag': 'latest', |
| 210 |
'history': {'created_by': 'KIWI {0}'.format(__version__)} |
| 211 |
}) |
| 212 |
mock_oci.set_config.assert_called_once_with({ |
| 213 |
'container_name': 'foo/bar', |
| 214 |
'additional_names': [':current', 'name:foobar', 'newname'], |
| 215 |
'container_tag': 'latest', |
| 216 |
'history': {'created_by': 'KIWI {0}'.format(__version__)} |
| 217 |
}) |
| 218 |
mock_oci.post_process.assert_called_once_with() |
| 219 |
mock_oci.export_container_image.assert_called_once_with( |
| 220 |
'result.tar', 'docker-archive', 'foo/bar:latest', |
| 221 |
['foo/bar:current', 'name:foobar', 'newname:latest'] |
| 222 |
) |