Debian Codegraph @

test/unit/oci_tools/umoci_test.py

raw
1 from unittest.mock import (
2 Mock, patch, call
3 )
4
5 from kiwi.oci_tools.umoci import OCIUmoci
6 from kiwi.oci_tools.base import OCIBase
7
8
9 class TestOCIUmoci:
10 @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help')
11 @patch('kiwi.oci_tools.base.datetime')
12 @patch('kiwi.oci_tools.umoci.Temporary')
13 def setup(self, mock_Temporary, mock_datetime, mock_cmd_caps):
14 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
15 mock_cmd_caps.return_value = True
16 strftime = Mock()
17 strftime.strftime = Mock(return_value='current_date')
18 mock_datetime.utcnow = Mock(
19 return_value=strftime
20 )
21 mock_datetime.fromtimestamp = Mock(
22 return_value=strftime
23 )
24 self.oci = OCIUmoci()
25
26 @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help')
27 @patch('kiwi.oci_tools.base.datetime')
28 @patch('kiwi.oci_tools.umoci.Temporary')
29 def setup_method(self, cls, mock_Temporary, mock_datetime, mock_cmd_caps):
30 self.setup()
31
32 @patch('kiwi.oci_tools.umoci.Command.run')
33 def test_init_container(self, mock_Command_run):
34 self.oci.init_container()
35 assert mock_Command_run.call_args_list == [
36 call(['umoci', 'init', '--layout', 'tmpdir/oci_layout']),
37 call(['umoci', 'new', '--image', 'tmpdir/oci_layout:base_layer'])
38 ]
39
40 @patch('kiwi.oci_tools.umoci.Temporary')
41 @patch('kiwi.oci_tools.umoci.Command.run')
42 def test_unpack(self, mock_Command_run, mock_Temporary):
43 mock_Temporary.return_value.new_dir.return_value.name = 'oci_root'
44 self.oci.unpack()
45 mock_Command_run.assert_called_once_with(
46 [
47 'umoci', 'unpack', '--image',
48 'tmpdir/oci_layout:base_layer', 'oci_root'
49 ]
50 )
51
52 @patch('kiwi.oci_tools.base.DataSync')
53 def test_sync_rootfs(self, mock_sync):
54 sync = Mock()
55 mock_sync.return_value = sync
56 self.oci.oci_root_dir = 'oci_root'
57 self.oci.sync_rootfs('root_dir', exclude_list=['/dev', '/proc'])
58 mock_sync.assert_called_once_with(
59 'root_dir/', 'oci_root/rootfs'
60 )
61 sync.sync_data.assert_called_once_with(
62 exclude=['/dev', '/proc'],
63 options=[
64 '--archive', '--hard-links', '--xattrs', '--acls',
65 '--one-file-system', '--inplace',
66 '--delete'
67 ]
68 )
69
70 @patch('kiwi.oci_tools.base.DataSync')
71 def test_import_rootfs(self, mock_sync):
72 sync = Mock()
73 mock_sync.return_value = sync
74 self.oci.oci_root_dir = 'oci_root'
75 self.oci.import_rootfs('root_dir', exclude_list=['/dev', '/proc'])
76 mock_sync.assert_called_once_with(
77 'oci_root/rootfs/', 'root_dir'
78 )
79 sync.sync_data.assert_called_once_with(
80 exclude=['/dev', '/proc'],
81 options=[
82 '--archive', '--hard-links', '--xattrs', '--acls',
83 '--one-file-system', '--inplace'
84 ]
85 )
86
87 @patch('kiwi.oci_tools.umoci.Temporary')
88 @patch('kiwi.oci_tools.umoci.Command.run')
89 def test_repack(self, mock_Command_run, mock_Temporary):
90 mock_Temporary.return_value.new_dir.return_value.name = 'oci_root'
91 oci_config = {
92 'history': {
93 'author': 'history author',
94 'comment': 'This is a comment',
95 'created_by': 'created by text'
96 }
97 }
98 self.oci.unpack()
99 self.oci.repack(oci_config)
100 assert call(
101 [
102 'umoci', 'repack',
103 '--history.comment=This is a comment',
104 '--history.created_by=created by text',
105 '--history.author=history author',
106 '--history.created', 'current_date',
107 '--image', 'tmpdir/oci_layout:base_layer', 'oci_root'
108 ]
109 ) in mock_Command_run.call_args_list
110
111 @patch('kiwi.oci_tools.umoci.Command.run')
112 def test_set_config(self, mock_Command_run):
113 oci_config = {
114 'container_tag': 'tag',
115 'entry_command': ['/bin/bash', '-x'],
116 'entry_subcommand': ['ls', '-l'],
117 'maintainer': 'tux',
118 'user': 'root',
119 'workingdir': '/root',
120 'expose_ports': ['80', '42'],
121 'volumes': ['/var/log', '/tmp'],
122 'stopsignal': 'SIGINT',
123 'environment': {'FOO': 'bar', 'PATH': '/bin'},
124 'labels': {'a': 'value', 'b': 'value'},
125 }
126 self.oci.set_config(oci_config)
127 mock_Command_run.assert_called_once_with(
128 [
129 'umoci', 'config', '--author=tux', '--config.user=root',
130 '--config.workingdir=/root', '--config.entrypoint=/bin/bash',
131 '--config.entrypoint=-x', '--config.cmd=ls', '--config.cmd=-l',
132 '--config.volume=/var/log', '--config.volume=/tmp',
133 '--config.stopsignal=SIGINT',
134 '--config.exposedports=80', '--config.exposedports=42',
135 '--config.env=FOO=bar', '--config.env=PATH=/bin',
136 '--config.label=a=value', '--config.label=b=value',
137 '--no-history', '--image', 'tmpdir/oci_layout:base_layer',
138 '--tag', 'tag', '--created', 'current_date'
139 ]
140 )
141
142 @patch('kiwi.oci_tools.umoci.Temporary')
143 @patch('kiwi.oci_tools.base.datetime')
144 @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help')
145 @patch('kiwi.oci_tools.umoci.Command.run')
146 def test_set_config_with_history(
147 self, mock_Command_run, mock_cmd_caps, mock_datetime, mock_Temporary
148 ):
149 oci_config = {
150 'container_tag': 'tag',
151 'entry_command': ['/bin/bash', '-x'],
152 'entry_subcommand': ['ls', '-l'],
153 'maintainer': 'tux',
154 'user': 'root',
155 'workingdir': '/root',
156 'expose_ports': ['80', '42'],
157 'volumes': ['/var/log', '/tmp'],
158 'environment': {'FOO': 'bar', 'PATH': '/bin'},
159 'labels': {'a': 'value', 'b': 'value'},
160 }
161 mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir'
162 mock_cmd_caps.return_value = False
163 strftime = Mock()
164 strftime.strftime = Mock(return_value='current_date')
165 mock_datetime.utcnow = Mock(
166 return_value=strftime
167 )
168 mock_datetime.fromtimestamp = Mock(
169 return_value=strftime
170 )
171 oci = OCIUmoci()
172 oci.set_config(oci_config)
173 mock_Command_run.assert_called_once_with(
174 [
175 'umoci', 'config', '--author=tux', '--config.user=root',
176 '--config.workingdir=/root', '--config.entrypoint=/bin/bash',
177 '--config.entrypoint=-x', '--config.cmd=ls', '--config.cmd=-l',
178 '--config.volume=/var/log', '--config.volume=/tmp',
179 '--config.exposedports=80', '--config.exposedports=42',
180 '--config.env=FOO=bar', '--config.env=PATH=/bin',
181 '--config.label=a=value', '--config.label=b=value',
182 '--image', 'tmpdir/oci_layout:base_layer',
183 '--tag', 'tag', '--created', 'current_date'
184 ]
185 )
186
187 @patch('kiwi.oci_tools.umoci.Command.run')
188 def test_set_config_derived_image(self, mock_Command_run):
189 self.oci.set_config({'container_tag': 'tag'})
190 mock_Command_run.assert_called_once_with([
191 'umoci', 'config', '--no-history',
192 '--image', 'tmpdir/oci_layout:base_layer', '--tag', 'tag',
193 '--created', 'current_date'
194 ])
195 self.oci.working_image == 'tmpdir/oci_layout:tag'
196
197 @patch('kiwi.oci_tools.umoci.Command.run')
198 def test_set_config_clear_inherited_commands(
199 self, mock_Command_run
200 ):
201 oci_config = {
202 'container_tag': 'tag',
203 'entry_command': [],
204 'entry_subcommand': []
205 }
206 self.oci.set_config(oci_config)
207 assert mock_Command_run.call_args_list == [
208 call([
209 'umoci', 'config', '--clear=config.entrypoint',
210 '--clear=config.cmd', '--no-history',
211 '--image', 'tmpdir/oci_layout:base_layer', '--tag', 'tag',
212 '--created', 'current_date'
213 ])
214 ]
215
216 @patch('kiwi.oci_tools.umoci.Command.run')
217 @patch.object(OCIBase, '_skopeo_provides_tmpdir_option')
218 def test_import_container_image(
219 self, mock_skopeo_provides_tmpdir_option, mock_Command_run
220 ):
221 mock_skopeo_provides_tmpdir_option.return_value = True
222 self.oci.import_container_image('oci-archive:image.tar')
223 mock_Command_run.assert_called_once_with([
224 'skopeo', 'copy', 'oci-archive:image.tar',
225 'oci:tmpdir/oci_layout:base_layer',
226 '--tmpdir', '/var/tmp'
227 ])
228
229 @patch('kiwi.oci_tools.umoci.Path.wipe')
230 @patch('kiwi.oci_tools.umoci.Command.run')
231 @patch.object(OCIBase, '_skopeo_provides_tmpdir_option')
232 def test_export_container_image(
233 self, mock_skopeo_provides_tmpdir_option, mock_Command_run, mock_wipe
234 ):
235 mock_skopeo_provides_tmpdir_option.return_value = True
236 self.oci.export_container_image(
237 'image.tar', 'oci-archive', 'myimage:tag',
238 ['myimage:tag2', 'myimage:tag3']
239 )
240 mock_Command_run.assert_called_once_with([
241 'skopeo', 'copy', 'oci:tmpdir/oci_layout:base_layer',
242 'oci-archive:image.tar:myimage:tag',
243 '--additional-tag', 'myimage:tag2',
244 '--additional-tag', 'myimage:tag3',
245 '--tmpdir', '/var/tmp',
246 ])
247 mock_wipe.assert_called_once_with('image.tar')
248
249 @patch('kiwi.oci_tools.umoci.Command.run')
250 def test_post_process(self, mock_Command_run):
251 self.oci.post_process()
252 mock_Command_run.assert_called_once_with(
253 ['umoci', 'gc', '--layout', 'tmpdir/oci_layout']
254 )
255
256 @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help')
257 def test_context_manager_exit(
258 self, mock_CommandCapabilities_has_option_in_help
259 ):
260 mock_CommandCapabilities_has_option_in_help.return_value = True
261 with OCIUmoci():
262 pass
263 # just pass